forked from anyoptimization/pymoo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark_nsga2.py
145 lines (120 loc) · 4.02 KB
/
benchmark_nsga2.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
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
"""
This is the experiment for nsga2.
"""
import os
import pickle
from pymoo.algorithms.nsga2 import NSGA2
from pymoo.factory import get_sampling, get_crossover, get_mutation, get_problem
from pymoo.operators.crossover.simulated_binary_crossover import SimulatedBinaryCrossover
from pymoo.operators.mutation.polynomial_mutation import PolynomialMutation
setup = {
'zdt1': {
'pop_size': 100,
'termination': ('n_gen', 200),
'problem': get_problem("zdt1", n_var=30),
'crossover': SimulatedBinaryCrossover(15),
'mutation': PolynomialMutation(20),
},
'zdt2': {
'pop_size': 100,
'termination': ('n_gen', 200),
'problem': get_problem("zdt2", n_var=30),
'crossover': SimulatedBinaryCrossover(15),
'mutation': PolynomialMutation(20)
},
'zdt3': {
'pop_size': 100,
'termination': ('n_gen', 200),
'problem': get_problem("zdt3", n_var=30),
'crossover': SimulatedBinaryCrossover(15),
'mutation': PolynomialMutation(20)
},
'zdt4': {
'pop_size': 100,
'termination': ('n_gen', 200),
'problem': get_problem("zdt4", n_var=10),
'crossover': SimulatedBinaryCrossover(15),
'mutation': PolynomialMutation(20)
},
'zdt5': {
'pop_size': 100,
'termination': ('n_gen', 400),
'problem': get_problem("zdt5", normalize=False),
'sampling': get_sampling("bin_random"),
'crossover': get_crossover("bin_two_point"),
'mutation': get_mutation("bin_bitflip")
},
'zdt6': {
'pop_size': 100,
'termination': ('n_gen', 400),
'problem': get_problem("zdt6", n_var=10),
'crossover': SimulatedBinaryCrossover(15),
'mutation': PolynomialMutation(20)
},
'kursawe': {
'pop_size': 100,
'termination': ('n_gen', 100),
'problem': get_problem("kursawe"),
'crossover': SimulatedBinaryCrossover(10),
'mutation': PolynomialMutation(10)
},
'bnh': {
'pop_size': 100,
'termination': ('n_gen', 150),
'problem': get_problem("bnh"),
'crossover': SimulatedBinaryCrossover(10),
'mutation': PolynomialMutation(20)
}
,
'osy': {
'pop_size': 200,
'termination': ('n_gen', 250),
'problem': get_problem("osy"),
'crossover': SimulatedBinaryCrossover(5),
'mutation': PolynomialMutation(5)
}
}
if __name__ == '__main__':
# all the files to be run in a list
run_files = []
# prefix of the folder to save the files
prefix = "runs"
# name of the experiment
name = "pynsga2-0.4.2"
# number of runs to execute
n_runs = 100
# single to be investigated
#problems = ['zdt5']
problems = setup.keys()
# path were the files for this experiment are saved
path = os.path.join(prefix, name)
for _problem in problems:
s = setup[_problem]
problem = s['problem']
method = NSGA2(
pop_size=s['pop_size'],
crossover=s['crossover'],
mutation=s['mutation'],
eliminate_duplicates=True
)
termination = s['termination']
for run in range(1, n_runs+1):
fname = "%s_%s.run" % (_problem, run)
_in = os.path.join(path, fname)
_out = "results/%s/%s/%s_%s.out" % (name, _problem.replace("_", "/"), _problem, run)
data = {
'args': [problem, method, termination],
'kwargs': {
'seed': run,
},
'in': _in,
'out': _out,
}
os.makedirs(os.path.join(os.path.dirname(_in)), exist_ok=True)
with open(_in, 'wb') as f:
pickle.dump(data, f)
run_files.append(data)
# create the final run.txt file
with open(os.path.join(prefix, name, "run.bat"), 'w') as f:
for run_file in run_files:
f.write("python execute.py %s %s\n" % (run_file['in'], run_file['out']))