-
Notifications
You must be signed in to change notification settings - Fork 0
/
jmoo_algorithms.py
executable file
·398 lines (304 loc) · 12.5 KB
/
jmoo_algorithms.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
"""
##########################################################
### @Author Joe Krall ###############################
### @copyright see below ###############################
This file is part of JMOO,
Copyright Joe Krall, 2014.
JMOO is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
JMOO is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with JMOO. If not, see <http://www.gnu.org/licenses/>.
### ###############################
##########################################################
"""
"Brief notes"
"Algorithms for evolution"
from Algorithms.DEAP import base
from Algorithms.DEAP import creator
from Algorithms.DEAP import tools
from Algorithms.DEAP.tools.emo import *
import os, sys, inspect
def do_nothing_initializer(problem, population):
return population, 0
from Algorithms.GALE.gale_components import *
from Algorithms.DE.de_components import *
from Algorithms.MOEA_D.moead_components import *
from Algorithms.NSGAIII.nsgaiii_components import *
from Algorithms.STORM.storm_components import *
from Algorithms.NSGAIII.nsgaiii_components import *
from Algorithms.GALE0.gale_components import *
from Algorithms.GALE_no_mutation.gale_components import *
from Algorithms.GALE2.gale_components import *
from jmoo_individual import *
from jmoo_properties import *
from utility import *
import jmoo_stats_box
import array,random,numpy
#############################################################
### MOO Algorithms
#############################################################
class jmoo_NSGAII:
def __init__(self, color="Blue"):
self.name = "NSGAII"
self.initializer = None
self.selector = selTournamentDCD
self.adjustor = crossoverAndMutation
self.recombiner = selNSGA2
self.color = color
self.type = '^'
class jmoo_SPEA2:
def __init__(self, color="Green"):
self.name = "SPEA2"
self.initializer = None
self.selector = selTournament
self.adjustor = crossoverAndMutation
self.recombiner = selSPEA2
self.color = color
self.type = 'h'
class jmoo_GALE:
def __init__(self, color="Red"):
self.name = "GALE"
self.initializer = None
self.selector = galeWHERE
self.adjustor = galeMutate
self.recombiner = galeRegen
self.color = color
self.type = '*'
class jmoo_GALE0:
def __init__(self, color="Blue"):
self.name = "GALE0"
self.initializer = None
self.selector = gale0WHERE
self.adjustor = gale0Mutate
self.recombiner = gale0Regen
self.color = color
self.type = '*'
class jmoo_GALE_no_mutation:
def __init__(self, color="Green"):
self.name = "GALE_no_mutation"
self.initializer = None
self.selector = gale_nm_WHERE
self.adjustor = gale_nm_Mutate
self.recombiner = gale_nm_Regen
self.color = color
self.type = '*'
class jmoo_GALE2:
def __init__(self, color="Black"):
self.name = "GALE2"
self.initializer = None
self.selector = gale2WHERE
self.adjustor = gale2Mutate
self.recombiner = gale2Regen
self.color = color
self.type = '*'
class jmoo_DE:
def __init__(self, color="magenta"):
self.name = "DE"
self.initializer = None
self.selector = de_selector
self.adjustor = de_mutate
self.recombiner = de_recombine # stub
self.color = color
self.type = '*'
class jmoo_MOEAD_TCH:
def __init__(self, color="#6F3662"):
self.name = "MOEAD"
self.initializer = initialize_moead
self.selector = moead_selector_tch
self.adjustor = moead_mutate
self.recombiner = moead_recombine
self.color = color
self.type = '*'
class jmoo_MOEAD_PBI:
def __init__(self, color="Blue"):
self.name = "MOEAD_PBI"
self.initializer = initialize_moead
self.selector = moead_selector_pbi
self.adjustor = moead_mutate
self.recombiner = moead_recombine
self.color = color
self.type = '*'
class jmoo_NSGAIII:
def __init__(self, color="green"):
self.name = "NSGA3"
self.initializer = None
self.selector = nsgaiii_selector2
self.adjustor = nsgaiii_regenerate2
self.recombiner = nsgaiii_recombine2
self.color = color
self.type = '*'
class jmoo_ANYWHERE:
def __init__(self, color="Yellow"):
self.name = "ANYWHERE"
self.initializer = None
self.selector = anywhere_selector
self.adjustor = anywhere_mutate
self.recombiner = anywhere_recombine
self.color = color
self.type = '*'
class jmoo_STORM:
def __init__(self, color="Green"):
self.name = "STORM2"
self.initializer = None
self.selector = anywhere3_selector
self.adjustor = anywhere_mutate
self.recombiner = anywhere_recombine
self.color = color
self.type = 'p'
class Bin:
def __init__(self):
self.low = 0
self.up = 0
self.mid = 0
def binner(problem, mu):
numBins = 10
Bins = [Bin() for x in problem.decisions]
for dec in problem.decisions:
for bin in range(numBins):
Bins[bin].low = dec.low + (bin) * ((dec.up - dec.low) / numBins)
Bins[bin].up = dec.low + (bin + 1) * ((dec.up - dec.low) / numBins)
Bins[bin].mid = (Bins[bin].up - Bins[bin].low) / 2
# random initial sample - pick bins for each decision
initialBins = []
for dec in problem.decisions:
initialBins.append(random.randint(0, numBins - 1))
population = []
population.append(initialBins)
# build population sample
for i in range(mu - 1):
furthest = 0
# glob
#############################################################
### MOO Algorithm Selectors
#############################################################
def selTournament(problem, individuals, configuration, value_to_be_passed):
# Format a population Data structure usable by DEAP's package
dIndividuals = deap_format(problem, individuals)
# Select elites
from Algorithms.DEAP.tools.selection import deap_selTournament
selectees = deap_selTournament(dIndividuals, len(individuals), 4)
# Update beginning population Data structure
selectedIndices = [i for i, sel in enumerate(selectees)]
return [individuals[s] for s in selectedIndices], len(individuals)
def selTournamentDCD(problem, individuals, configuration, value_to_be_passed):
# Evaluate any new guys
for individual in individuals:
if not individual.valid:
individual.evaluate()
# Format a population data structure usable by DEAP's package
dIndividuals = deap_format(problem, individuals)
# Assign crowding distance
from Algorithms.DEAP.tools.emo import assignCrowdingDist
assignCrowdingDist(dIndividuals)
# Select elites
from Algorithms.DEAP.tools.emo import selTournamentDCD
selectees = selTournamentDCD(dIndividuals, len(individuals))
# Update beginning population data structure
selectedIndices = [i for i,sel in enumerate(selectees)]
return [individuals[s] for s in selectedIndices], len(individuals)
#############################################################
### MOO Algorithm Adjustors
#############################################################
def crossoverAndMutation(problem, individuals, configuration, gen, actual_population):
# Format a population data structure usable by DEAP's package
dIndividuals = deap_format(problem, individuals)
# Crossover
for ind1, ind2 in zip(dIndividuals[::2], dIndividuals[1::2]):
if random.random() <= 0.9: #crossover rate
tools.cxUniform(ind1, ind2, indpb=1.0/len(problem.decisions))
# Mutation
for ind in dIndividuals:
tools.mutPolynomialBounded(ind, eta = 1.0, low=[dec.low for dec in problem.decisions], up=[dec.up for dec in problem.decisions], indpb=0.1 )
del ind.fitness.values
# Update beginning population data structure
for individual,dIndividual in zip(individuals, dIndividuals):
for i in range(len(individual.decisionValues)):
individual.decisionValues[i] = dIndividual[i]
individual.fitness = None
return individuals,0
def variator(problem, selectees):
return selectees, 0
" jiggle everyone by ~ 1% "
# Variation
d = 0.0 #0.03
for r_index,row in enumerate(selectees):
for i in range(len(problem.decisions)):
selectees[r_index].decisionValues[i] = max(problem.decisions[i].low, min(problem.decisions[i].up, row.decisionValues[i] + (random.uniform(0.0, d)-d/2) * (problem.decisions[i].up - problem.decisions[i].low)))
#############################################################
### MOO Algorithm Recombiners
#############################################################
def selSPEA2(problem, population, selectees, configurations, gen):
k = configurations["Universal"]["Population_Size"]
# Evaluate any new guys
for individual in population + selectees:
if not individual.valid:
individual.evaluate()
# Format a population Data structure usable by DEAP's package
dIndividuals = deap_format(problem, population + selectees)
# Combine
from Algorithms.DEAP.tools.emo import selSPEA2
dIndividuals = selSPEA2(dIndividuals, k)
# Copy from DEAP structure to JMOO structure
population = []
for i, dIndividual in enumerate(dIndividuals):
cells = []
for j in range(len(dIndividual)):
cells.append(dIndividual[j])
population.append(jmoo_individual(problem, cells, gen, dIndividual.fitness.values))
return population, k
def selNSGA2(problem, population, selectees, configurations, gen):
k = configurations["Universal"]["Population_Size"]
# Evaluate any new guys
for individual in population + selectees:
if not individual.valid:
individual.evaluate()
# Format a population Data structure usable by DEAP's package
dIndividuals = deap_format(problem, population + selectees)
# Combine
from Algorithms.DEAP.tools.emo import deap_selNSGA2
dIndividuals = deap_selNSGA2(dIndividuals, k)
# Copy from DEAP structure to JMOO structure
population = []
for i, dIndividual in enumerate(dIndividuals):
cells = []
for j in range(len(dIndividual)):
cells.append(dIndividual[j])
population.append(jmoo_individual(problem, cells, gen, dIndividual.fitness.values))
return population, k
#############################################################
### MOO Algorithm Convergence Stops
#############################################################
def default_toStop(statBox):
return False
def bstop(statBox):
stop = True
for o, obj in enumerate(statBox.problem.objectives):
if statBox.box[-1].changes[o] <= statBox.bests[o]: stop = False
if stop == True:
statBox.lives += -1
print "#" * 20
return stop and statBox.lives == 0
#############################################################
### MOO Algorithm Utility
#############################################################
def deap_format(problem, individuals):
from Algorithms.DEAP import base, creator
import array
"copy a jmoo-style list of individuals into a DEAP-style list of individuals"
toolbox = base.Toolbox()
creator.create("FitnessMin", base.Fitness, weights=[-1.0 if obj.lismore else 1.0 for obj in problem.objectives])
creator.create("Individual", array.array, typecode='d', fitness=creator.FitnessMin)
dIndividuals = []
for i, individual in enumerate(individuals):
dIndividuals.append(creator.Individual([dv for dv in individual.decisionValues]))
dIndividuals[-1].fitness.decisionValues = [dv for dv in individual.decisionValues]
if individual.valid: dIndividuals[i].fitness.values = individual.fitness.fitness
dIndividuals[-1].fitness.feasible = not problem.evalConstraints([dv for dv in individual.decisionValues])
dIndividuals[-1].fitness.problem = problem
return dIndividuals