-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun.py
More file actions
213 lines (170 loc) · 6.81 KB
/
run.py
File metadata and controls
213 lines (170 loc) · 6.81 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import json
from src.MOEAs.NSGAII import *
from src.MOEAs.MRGA import *
from src.MOEAs.mutations.PolynomialMutation import PolynomialMutation
from src.MOEAs.crossovers.SBXCrossover import SBXCrossover
from src.MOEAs.sparsities.CrowdingDistance import CrowdingDistance
from src.BinaryTournament import BinaryTournament
from src.frameworks.M1 import M1
from src.frameworks.M3 import M3
from src.frameworks.M6 import M6
from src.frameworks.SMO import SMO
from src.frameworks.SMB import SMB
from src.problems.ZDT import *
from src.problems.DTLZ import *
from pymoo.problems.multi.zdt import ZDT1 as ZDT1_pymoo
from pymoo.problems.multi.zdt import ZDT3 as ZDT3_pymoo
from pymoo.problems.multi.zdt import ZDT6 as ZDT6_pymoo
from pymoo.problems.many.dtlz import DTLZ1 as DTLZ1_pymoo
from pymoo.problems.many.dtlz import DTLZ2 as DTLZ2_pymoo
from pymoo.problems.many.dtlz import DTLZ3 as DTLZ3_pymoo
from pymoo.problems.many.dtlz import DTLZ4 as DTLZ4_pymoo
from pymoo.problems.many.dtlz import DTLZ5 as DTLZ5_pymoo
from pymoo.problems.many.dtlz import DTLZ6 as DTLZ6_pymoo
from pymoo.problems.many.dtlz import DTLZ7 as DTLZ7_pymoo
from pymoo.factory import get_performance_indicator
from pymoo.performance_indicator.kktpm import KKTPM
from pathlib import Path
def get_sparsity(sparsity):
if sparsity == "CrowdingDistance":
return CrowdingDistance()
print("unknown crowding")
return None
def get_selection(selection):
if selection == "Binary":
return BinaryTournament()
print("unknown selection")
return None
def get_mutation(mutation, args_file):
args = None
with open(args_file) as fargs:
args = json.load(fargs)
if mutation == "Polynomial":
return PolynomialMutation(mutationProbability=args["probability"], distributionIndex=args["distribution"])
print("unknown mutation")
return None
def get_crossover(crossover, args_file):
args = None
with open(args_file) as fargs:
args = json.load(fargs)
if crossover == "SBX":
return SBXCrossover(distributionIndex=args["distribution"], crossoverProbability=args["probability"])
print("unknown crossover")
return None
def get_problem_pymoo(problem, args_file):
args = None
with open(args_file) as fargs:
args = json.load(fargs)
if problem == "ZDT1":
return ZDT1_pymoo(args["n"])
if problem == "ZDT3":
return ZDT3_pymoo(args["n"])
if problem == "ZDT6":
return ZDT6_pymoo(args["n"])
args["n"] = args["m"] + args["k"] - 1
if problem == "DTLZ1":
return DTLZ1_pymoo(args["n"], args["m"])
if problem == "DTLZ2":
return DTLZ2_pymoo(args["n"], args["m"])
if problem == "DTLZ3":
return DTLZ3_pymoo(args["n"], args["m"])
if problem == "DTLZ4":
return DTLZ4_pymoo(args["n"], args["m"])
if problem == "DTLZ5":
return DTLZ5_pymoo(args["n"], args["m"])
if problem == "DTLZ6":
return DTLZ6_pymoo(args["n"], args["m"])
if problem == "DTLZ7":
return DTLZ7_pymoo(args["n"], args["m"])
print("unknown problem")
def get_framework(framework, args_file):
args = None
with open(args_file) as fargs:
args = json.load(fargs)
if framework == "M1":
return M1(None, None, args["sample_size"], args["tau"], args["SEmax"])
if framework == "M3":
return M3(None, None, args["sample_size"], args["SEmax"], args["k"], args["alfa"], args["R"])
if framework == "M6":
return M6(None, None, None, args["sample_size"], args["SEmax"], args["R"], KKTPM())
if framework == "SMO":
return SMO(None, None, args["sample_size"], args["tmax"], args["tparada"])
if framework == "SMB":
return SMB(None, None, args["sample_size"], args["tmax"], args["tparada"])
print("unknown framework")
def get_MOEA(MOEA, args_file):
args = None
with open(args_file) as fargs:
args = json.load(fargs)
if MOEA == "NSGAII":
return NSGAII(None, args["maxEvaluations"], args["populationSize"], args["offspringSize"], None, None, None, None)
if MOEA == "MRGA":
return MRGA(None, args["maxEvaluations"], args["populationSize"], args["offspringSize"], None, None, None, None, R=None)
print("unknown problem")
return None
def get_problem(problem, args_file):
args = None
with open(args_file) as fargs:
args = json.load(fargs)
# ZDT problems
if problem == "ZDT1":
return ZDT1(args["n"])
if problem == "ZDT3":
return ZDT3(args["n"])
if problem == "ZDT6":
return ZDT6(args["n"])
if problem == "DTLZ1":
return DTLZ1(args["m"], args["k"])
if problem == "DTLZ2":
return DTLZ2(args["m"], args["k"])
if problem == "DTLZ3":
return DTLZ3(args["m"], args["k"])
if problem == "DTLZ4":
return DTLZ4(args["m"], args["k"])
if problem == "DTLZ5":
return DTLZ5(args["m"], args["k"])
if problem == "DTLZ6":
return DTLZ6(args["m"], args["k"])
if problem == "DTLZ7":
return DTLZ7(args["m"], args["k"])
print("unknown problem")
return None
def save(name, i, P, F, igd):
Path("results/" + name).mkdir(parents=True, exist_ok=True)
with open("results/" + name + "/P" + i, 'w+') as P_file:
P_file.write(json.dumps(P))
with open("results/" + name + "/F" + i, 'w+') as F_file:
F_file.write(json.dumps(F))
with open("results/" + name + "/igd" + i, 'w+') as igd_file:
igd_file.write(json.dumps(igd))
def run(framework, problem, moea, crossover, mutation, selection, sparsity, n, framework_args_file, problem_args_file, moea_args_file, pareto_front_file, mutation_args_file, crossover_args_file):
Problem = get_problem(problem, problem_args_file)
MOEA = get_MOEA(moea, moea_args_file)
MOEA.problem = Problem
MOEA.mutation = get_mutation(mutation, mutation_args_file)
MOEA.crossover = get_crossover(crossover, crossover_args_file)
MOEA.selection = get_selection(selection)
MOEA.sparsity = get_sparsity(sparsity)
Framework = get_framework(framework, framework_args_file)
Framework.problem = Problem
if framework == "M6":
Framework.problem_np = get_problem_pymoo(problem, problem_args_file)
MOEA.R = Framework.R
Framework.EMO = MOEA
pareto_front = []
with open(pareto_front_file, 'r') as front_file:
for line in front_file.readlines():
coords = []
for coord in line.split():
coords.append(float(coord))
pareto_front.append(coords)
for i in range(n):
P = Framework.run()
X = []
F = []
for p in P:
X.append(p.decisionVariables)
F.append(p.objectives)
figd = get_performance_indicator("igd", np.array(pareto_front))
igd = figd.calc(np.array(F))
save(framework + "_" + problem + "_" + moea, str(i), X, F, igd)