-
Notifications
You must be signed in to change notification settings - Fork 2
/
IGD.py
94 lines (67 loc) · 4.05 KB
/
IGD.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import oapackage
import numpy as np
from search import evaluate_arch
from utils import *
from pymoo.indicators.igd import IGD
def archive_check(ind_obj, archive_obj, archive_var, ind_var) -> list:
def is_dominated(ind_obj, ind_archive) -> bool:
condition_forall = True
condition_exists = False
for measure in range(len(ind_obj)):
if ind_obj[measure] > ind_archive[measure]:
condition_forall = False
break
for measure in range(len(ind_obj)):
if ind_obj[measure] < ind_archive[measure]:
condition_exists = True
break
return condition_forall and condition_exists
idx_indobj_dominated_by_archive = []
for idx_archive in range(len(archive_obj)):
if is_dominated(ind_obj, archive_obj[idx_archive]):
idx_indobj_dominated_by_archive.append(idx_archive)
if is_dominated(archive_obj[idx_archive], ind_obj):
return archive_obj, archive_var
archive_obj_result = [ind_obj]
archive_var_result = [ind_var]
for idx in range(len(archive_obj)):
if idx not in idx_indobj_dominated_by_archive:
archive_obj_result.append(archive_obj[idx])
archive_var_result.append(archive_var[idx])
return archive_obj_result, archive_var_result
def remove_ind_dominated(archive_eval):
pareto=oapackage.ParetoDoubleLong()
archive_eval = np.array(archive_eval).T
archive_eval[0] = -archive_eval[0]
for ii in range(0, archive_eval.shape[1]):
w=oapackage.doubleVector((archive_eval[0,ii], archive_eval[1,ii]))
pareto.addvalue(w, ii)
lst=pareto.allindices()
archive_eval = archive_eval[:,lst]
archive_eval[0] = -archive_eval[0]
archive_eval = archive_eval.T
return archive_eval
def calc_IGD(self, x, objs):
archive_eval, archive_eval_norm = {}, {}
igd_dict, igd_norm_dict = {}, {}
for dataset in self.datasets:
archive_eval[dataset], archive_eval_norm[dataset] = {}, {}
igd_dict[dataset], igd_norm_dict[dataset] = {}, {}
for j in range(1, len(self.objectives_list)):
archive_eval[dataset][f'testacc_{self.objectives_list[j]}'] = []
archive_eval_norm[dataset][f'testacc_{self.objectives_list[j]}'] = []
for ind in self.archive_var:
accuracy = evaluate_arch(self, ind=ind, dataset=dataset, measure='test-accuracy')
complexity = evaluate_arch(self, ind=ind, dataset=dataset, measure=self.objectives_list[j])
archive_eval[dataset][f'testacc_{self.objectives_list[j]}'].append((complexity, accuracy))
archive_eval[dataset][f'testacc_{self.objectives_list[j]}'] = remove_ind_dominated(archive_eval[dataset][f'testacc_{self.objectives_list[j]}'])
archive_eval_norm[dataset][f'testacc_{self.objectives_list[j]}'] = archive_eval[dataset][f'testacc_{self.objectives_list[j]}'].copy()
archive_eval_norm[dataset][f'testacc_{self.objectives_list[j]}'][:, 0] = (archive_eval_norm[dataset][f'testacc_{self.objectives_list[j]}'][:, 0] - self.max_min_measures[dataset][f'{self.objectives_list[j]}_min']) / (self.max_min_measures[dataset][f'{self.objectives_list[j]}_max'] - self.max_min_measures[dataset][f'{self.objectives_list[j]}_min'])
archive_eval_norm[dataset][f'testacc_{self.objectives_list[j]}'][:, 1] = archive_eval_norm[dataset][f'testacc_{self.objectives_list[j]}'][:, 1] / 100
get_igd = IGD(self.pf[dataset][f'testacc_{self.objectives_list[j]}'])
get_igd_norm = IGD(self.pf_norm[dataset][f'testacc_{self.objectives_list[j]}'])
igd = get_igd(archive_eval[self.dataset][f'testacc_{self.objectives_list[j]}'])
igd_norm = get_igd_norm(archive_eval_norm[self.dataset][f'testacc_{self.objectives_list[j]}'])
igd_dict[dataset][f'testacc_{self.objectives_list[j]}'] = igd
igd_norm_dict[dataset][f'testacc_{self.objectives_list[j]}'] = igd_norm
return igd_dict, igd_norm_dict