-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgaoptimizer.py
305 lines (257 loc) · 10.5 KB
/
gaoptimizer.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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import os
import pickle
import random
import warnings
import numpy as np
from deap import base
from deap import creator
from deap import tools
from scoop import futures
from tqdm import tqdm
HISTFNAME = 'ghist'
class OptimizerError(Exception):
""" Optimizer error class.
"""
def __init__(self, value):
self.value = value
def __str__(self):
return self.value
class OptimizerWarning(UserWarning):
""" Optimizer warning class.
"""
pass
class NSGAII:
""" NSGA-II optimizer.
"""
def __init__(self, evaluate=None):
""" Initialize the optimizer.
Keyword arguments:
evaluate -- [None] the evaluate function.
"""
self.NDIM = 30
self.OBJ = (-1.0, -1.0)
self.ETAC = 10
self.ETAM = 10
self.CXPB = 0.9
self.MPB = 0.3
self.MIDPB = 0.5
self.evaluate = evaluate
self.toolbox = None
self.pop = None
self.log = None
self.setup()
def setup(self):
""" Call this method when any member variable changes to update
the NSGA-II arguments.
"""
try:
del creator.Quality
except AttributeError:
pass
try:
del creator.Individual
except AttributeError:
pass
creator.create("Quality", base.Fitness, weights=self.OBJ)
creator.create("Individual", list, fitness=creator.Quality)
toolbox = base.Toolbox()
toolbox.register("norm_var", random.random)
toolbox.register("individual", tools.initRepeat, creator.Individual,
toolbox.norm_var, self.NDIM)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
# toolbox.register("evaluate", benchmarks.zdt2)
if not self.evaluate:
warnings.warn("evaluate function not defined!", OptimizerWarning)
toolbox.register("evaluate", self.evaluate)
toolbox.register("mate", tools.cxSimulatedBinaryBounded,
low=0, up=1, eta=self.ETAC)
toolbox.register("mutate", tools.mutPolynomialBounded,
low=0, up=1, eta=self.ETAM, indpb=self.MIDPB)
toolbox.register("select", tools.selNSGA2)
toolbox.register("map", futures.map)
self.toolbox = toolbox
def evolve(self, npop, ngen, seed=None, pre=''):
""" Generate and evolve the population based on NSGA-II.
Keyword arguments:
npop -- population size or list of existed populations.
ngen -- number of generations.
seed -- [None] the random seed.
pre -- [''] path of the root of the simulation folders.
"""
init = 1 # flag show that if it's a new run
if isinstance(npop, int):
if npop % 4:
raise OptimizerError('population size has to be a multiple of 4!')
random.seed(seed)
else:
init = 0
ipop = npop
npop = len(npop)
toolbox = self.toolbox
def ind_fitness(ind):
return ind.fitness.values
stats = tools.Statistics(ind_fitness)
stats.register("avg", np.mean, axis=0)
stats.register("std", np.std, axis=0)
stats.register("min", np.min, axis=0)
stats.register("max", np.max, axis=0)
logbook = tools.Logbook()
logbook.header = "gen", "evals", "std", "min", "avg", "max"
# If generation history files exist find a new ghist filename
fcount = 1
while os.path.exists(HISTFNAME + (' {:d}'.format(fcount) if fcount > 1 else '')):
fcount += 1
ghist = HISTFNAME + (' {:d}'.format(fcount) if fcount > 1 else '')
# Begin the generational process
for gen in tqdm(range(ngen), desc='Generation', ascii=True):
if not gen:
pop = []
offspring = toolbox.population(n=npop) if init else ipop
else:
# Vary the population
offspring = tools.selTournamentDCD(pop, npop)
offspring = [toolbox.clone(ind) for ind in offspring]
for ind1, ind2 in zip(offspring[::2], offspring[1::2]):
if random.random() <= self.CXPB: toolbox.mate(ind1, ind2)
if random.random() <= self.MPB: toolbox.mutate(ind1)
if random.random() <= self.MPB: toolbox.mutate(ind2)
del ind1.fitness.values, ind2.fitness.values
# Evaluate the individuals with an invalid fitness
invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
sims = [os.path.join(pre, '{0:03d}'.format(i + 1)) for i in range(len(invalid_ind))]
fitnesses = toolbox.map(toolbox.evaluate, invalid_ind, sims)
with tqdm(total=len(invalid_ind), desc='Population', ascii=True) as pbar:
for ind, fit in zip(invalid_ind, fitnesses):
pbar.update(1)
ind.fitness.values = fit
# Select the next generation population
pop = toolbox.select(pop + offspring, npop)
record = stats.compile(pop)
logbook.record(gen=gen, evals=len(invalid_ind), **record)
# print(logbook.stream)
# Save the populations of the latest generation
with open(ghist, 'ab') as f:
pickle.dump(pop, f)
# print("Done!")
self.pop = pop
self.log = logbook
class SPEA2:
""" SPEA2 optimizer.
"""
def __init__(self, evaluate=None):
""" Initialize the optimizer.
Keyword arguments:
evaluate -- [None] the evaluate function.
"""
self.NDIM = 30
self.OBJ = (-1.0, -1.0)
self.ETAC = 1.0
self.ETAM = 1.0
self.CXPB = 0.9
self.MPB = 0.3
self.MIDPB = 0.5
self.evaluate = evaluate
self.toolbox = None
self.pop = None
self.log = None
self.setup()
def setup(self):
""" Call this method when any member variable changes to update
the SPEA2 arguments.
"""
try:
del creator.Quality
except AttributeError:
pass
try:
del creator.Individual
except AttributeError:
pass
creator.create("Quality", base.Fitness, weights=self.OBJ)
creator.create("Individual", list, fitness=creator.Quality)
toolbox = base.Toolbox()
toolbox.register("norm_var", random.random)
toolbox.register("individual", tools.initRepeat, creator.Individual,
toolbox.norm_var, self.NDIM)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
# toolbox.register("evaluate", benchmarks.zdt2)
if not self.evaluate:
warnings.warn("evaluate function not defined!", OptimizerWarning)
toolbox.register("evaluate", self.evaluate)
toolbox.register("mate", tools.cxSimulatedBinaryBounded,
low=0, up=1, eta=self.ETAC)
toolbox.register("mutate", tools.mutPolynomialBounded,
low=0, up=1, eta=self.ETAM, indpb=self.MIDPB)
toolbox.register("select", tools.selSPEA2)
toolbox.register("selectTournament", tools.selTournament, tournsize=2)
toolbox.register("map", futures.map)
self.toolbox = toolbox
def evolve(self, npop, narc, ngen, seed=None, pre=''):
""" Generate and evolve the population based on SPEA2.
Keyword arguments:
npop -- population size or list of existed populations.
ngen -- number of generations.
narc -- capacity of archive.
seed -- [None] the random seed.
pre -- [''] path of the root of the simulation folders.
"""
init = 1 # flag show that if it's a new run with no population given
arch = 0 # flag show that if it's a new run with archive given
if isinstance(npop, int):
random.seed(seed)
else:
init = 0
ipop = npop
npop = len(npop)
if not isinstance(narc, int):
arch = 1
iarc = narc
narc = len(narc)
toolbox = self.toolbox
def ind_fitness(ind):
return ind.fitness.values
stats = tools.Statistics(ind_fitness)
stats.register("avg", np.mean, axis=0)
stats.register("std", np.std, axis=0)
stats.register("min", np.min, axis=0)
stats.register("max", np.max, axis=0)
logbook = tools.Logbook()
logbook.header = "gen", "evals", "std", "min", "avg", "max"
# If generation history files exist find a new ghist filename
fcount = 1
while os.path.exists(HISTFNAME + (' {:d}'.format(fcount) if fcount > 1 else '')):
fcount += 1
ghist = HISTFNAME + (' {:d}'.format(fcount) if fcount > 1 else '')
# Step 1 Initialization
pop = toolbox.population(n=npop) if init else ipop
archive = iarc if arch else []
for gen in tqdm(range(ngen), leave=True, ascii=True):
# Step 2 Fitness assignment
invalid_ind = [ind for ind in pop if not ind.fitness.valid]
sims = [os.path.join(pre, '{0:03d}'.format(i + 1)) for i in range(len(invalid_ind))]
fitnesses = toolbox.map(toolbox.evaluate, invalid_ind, sims)
with tqdm(total=len(invalid_ind), desc='Population', ascii=True) as pbar:
for ind, fit in zip(invalid_ind, fitnesses):
pbar.update(1)
ind.fitness.values = fit
# Step 3 Environmental selection
archive = toolbox.select(pop + archive, k=narc)
# Step 5 Mating selection
offspring = toolbox.selectTournament(archive, k=npop)
offspring = [toolbox.clone(ind) for ind in offspring]
# Step 6 Variation
for ind1, ind2 in zip(offspring[::2], offspring[1::2]):
if random.random() <= self.CXPB: toolbox.mate(ind1, ind2)
if random.random() <= self.MPB: toolbox.mutate(ind1)
if random.random() <= self.MPB: toolbox.mutate(ind2)
del ind1.fitness.values, ind2.fitness.values
pop = offspring
record = stats.compile(archive)
logbook.record(gen=gen, evals=len(invalid_ind), **record)
# print(logbook.stream)
# Save the populations of the latest generation
with open(ghist, 'ab') as f:
pickle.dump([pop, archive], f)
# print("Done!")
self.pop = archive
self.log = logbook