-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgeneticOptimizer.pyx
334 lines (295 loc) · 14.3 KB
/
geneticOptimizer.pyx
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
from os import error
import random
from random import randint
from captureAgents import GenesAgent, RandomAgent
from capture import CaptureRules
from genes import Genes
from baselineTeam import OffensiveReflexAgent, DefensiveReflexAgent
import math as m
import multiprocessing as mp
import json
import time
import concurrent.futures
class GeneticOptimizer:
def __init__(self, population, fitnessCalculator, maxGenerations, fitnessThreshold=9999999999999):
""" Initialize the optimizer
:param population - set of starting genes, all of same initial species
:param fitnessCalculator - capable of scoring a population of individuals
"""
self.population = [{
"id": 0,
"individuals": population,
"fitness": None,
"stagnation": 0
}]
self.speciesCount = 1
self.fitnessCalculator = fitnessCalculator
self.generationCount = 0
self.populationSize = len(population)
self.maxGenerations = maxGenerations
self.fitnessThreshold = fitnessThreshold
self.best = population[randint(0, len(population) - 1)]
self.stagnated = False
self.selector = Tournament()
self.startTime = None
self.saveInterval = 25 # Interval at which to save population
def initialize(self):
""" Prepare for evolution. """
self._calculateFitness(self.population, self.best)
self.population[0]["fitness"] = max(list(map(lambda ind: ind.getFitness(), self.population[0]["individuals"])))
self.startTime = time.time()
def evolve(self):
""" Run optimization until a termination condition is met. """
while not self.isTerminated():
self._evolveSingle()
self.generationCount += 1
self._endOfEpoch()
def _evolveSingle(self):
""" Execute a single evolution of genetic optimization. """
representatives = list(map(
lambda species: (species["individuals"][randint(0, len(species["individuals"]) - 1)], species["id"]), self.population))
populationFitnessSum = sum(list(map(lambda species: sum(
list(map(lambda ind: ind.getFitness(), species["individuals"]))) / len(species["individuals"]), self.population)))
nextGenPopulation = []
for species in self.population:
nextGenPopulation.append({
"id": species["id"],
"individuals": [],
"fitness": species["fitness"],
"stagnation": species["stagnation"]
})
all_inds = []
for species in self.population:
for individual in species["individuals"]:
all_inds.append(individual)
allOffspring = []
allOffspring.append(self.best.clone())
for species in self.population:
speciesOffspring = []
individuals = species["individuals"]
speciesFitnessSum = sum(
list(map(lambda ind: ind.getFitness(), individuals))) / len(individuals)
# Constant number of offspring proportional to species fitness within larger population
speciesNumOffspring = 0
if populationFitnessSum == 0:
speciesNumOffspring = len(individuals)
else:
speciesNumOffspring = m.ceil(
speciesFitnessSum / populationFitnessSum * self.populationSize)
individuals.sort(key=lambda ind: ind.getFitness())
# Eliminate worst individual
# TODO: eliminate worst individual from population, not from each species
# candidates = individuals[(len(individuals) / 4):]
candidates = individuals
# candidates = individuals
# Autocopy best individual for large species
if len(individuals) > 5:
speciesOffspring.append(individuals[-1].clone())
if len(individuals) > 2:
candidates = individuals[1:]
# Only non-empty, non-stagnating species may evolve
if len(candidates) >= 1 and species["stagnation"] < 15:
while len(speciesOffspring) < speciesNumOffspring:
selected = self.selector.select(candidates, 3, 1)[0]
# selected = candidates[randint(0, len(candidates) - 1)]
crossRand = random.uniform(0, 1)
connMutateRand = random.uniform(0, 1)
if crossRand < .75:
if crossRand < .001:
randSpecies = self.population[randint(0, len(self.population) - 1)]
randMate = randSpecies["individuals"][randint(0, len(randSpecies["individuals"]) - 1)]
child = selected.breed(randMate, (selected.getFitness() > randMate.getFitness()))
else:
mate = candidates[randint(0, len(candidates) - 1)]
child = selected.breed(mate, (selected.getFitness() > mate.getFitness()))
else:
child = selected.clone()
# if connMutateRand < .80:
child = child.mutate()
speciesOffspring.append(child)
allOffspring.extend(speciesOffspring)
# If a species stagnates, it won't reproduce. Fill its space with random sample across all species.
while len(allOffspring) < self.populationSize:
rando = random.uniform(0, 1)
if rando < .3:
randSpecies = self.population[randint(0, len(self.population) - 1)]
allOffspring.append(randSpecies["individuals"][randint(0, len(randSpecies["individuals"]) - 1)])
else:
allOffspring.append(self.selector.select(all_inds, 2, 1)[0])
for offspring in allOffspring:
compatible = False
for rep in representatives:
if compatible == False and offspring.distance(rep[0]) < 2:
compatible = True
for species in nextGenPopulation:
if species["id"] == rep[1]:
species["individuals"].append(offspring)
if compatible == False:
newSpecies = {
"id": self.speciesCount,
"individuals": [offspring],
"stagnation": 0,
"fitness": None
}
self.speciesCount += 1
nextGenPopulation.append(newSpecies)
newRep = (offspring, newSpecies["id"])
representatives.append(newRep)
# Filter out empty species
nextGenPopulation = list(filter(lambda species: len(
species["individuals"]), nextGenPopulation))
# Calculate fitness in each new species
self._calculateFitness(nextGenPopulation, self.best)
# Update fitness and stagnation values
for species in nextGenPopulation:
maxFitness = max(
list(map(lambda ind: ind.getFitness(), species["individuals"])))
if species["fitness"] is not None and maxFitness <= species["fitness"]:
species["stagnation"] += 1
else:
species["fitness"] = maxFitness
species["stagnation"] = 0
if sum(list(map(lambda species: len(species["individuals"]), nextGenPopulation))) >= self.populationSize:
self.population = nextGenPopulation
self.best = self.getBestIndividual()
else:
self.stagnated = True
def _calculateFitness(self, population, bestInd):
""" Calculate and cache the fitness of each individual in the population. """
self.fitnessCalculator.calculateFitness(
population, bestInd)
def _endOfEpoch(self):
print("BEST_FITNESS: ", self.best.getFitness(), " GEN_COUNT: ", self.generationCount, " SPECIES_SIZE: ",
[len(species["individuals"]) for species in self.population], " POP_SIZE: ",
sum(list(map(lambda species: len(species["individuals"]), self.population))),
" GPS: ", self.generationCount / (time.time() - self.startTime))
self.best._metaparameters.reset_tracking()
if self.generationCount % self.saveInterval == 0:
Runner.save(self)
def getPopulation(self):
""" Access all individuals by species. """
return self.population
def getBestIndividual(self):
""" Access best individual by fitness across entire population. """
bestInd = None
for species in self.population:
for ind in species["individuals"]:
if bestInd is None or ind.getFitness() > bestInd.getFitness():
bestInd = ind
return bestInd
def isTerminated(self):
""" Access whether optimization has reached any termination condition. """
return self.generationCount >= self.maxGenerations or self.stagnated or self.getBestIndividual().getFitness() >= self.fitnessThreshold
class FitnessCalculator:
def __init__(self, layout, gameDisplay, length, muteAgents, catchExceptions):
self.layout = layout
self.gameDisplay = gameDisplay
self.length = length
self.muteAgents = muteAgents
self.catchExceptions = catchExceptions
self.rules = CaptureRules()
self.prevBest = None
self.isRunParallel = True
self.useChamp = False
self.pool = mp.Pool(int(mp.cpu_count() / 2))
def calculateFitness(self, population, prevBest):
""" Calculate and cache fitness of each individual in the population.
Run competitive simulation to determine fitness scores.
Utilize fitness sharing within each species.
"""
# Run a game for each member of the population against the previous best member of the population
# Cache score as fitness on individual
self.prevBest = prevBest
if self.useChamp:
self.prevBest = Genes.load(open("sample_gene.json", "r"), self.prevBest._metaparameters)
all_inds = []
for species in population:
for individual in species["individuals"]:
all_inds.append(individual)
battler = Battler(self.prevBest, self.rules, self.layout, self.gameDisplay, self.length, self.muteAgents, self.catchExceptions)
if self.isRunParallel:
res = self.pool.map(battler.battle, all_inds)
i = 0
while i < len(res):
all_inds[i].setFitness(res[i])
i += 1
else:
for ind in all_inds:
ind.setFitness(battler.battle(ind))
# Needed a class without pool as member
class Battler:
def __init__(self, prevBest, rules, layout, gameDisplay, length, muteAgents, catchExceptions):
self.prevBest = prevBest
self.layout = layout
self.gameDisplay = gameDisplay
self.length = length
self.muteAgents = muteAgents
self.catchExceptions = catchExceptions
self.rules = rules
def battle(self, individual):
agents = [GenesAgent(0, individual), DefensiveReflexAgent(1), DefensiveReflexAgent(2), DefensiveReflexAgent(3)]
g = self.rules.newGame(self.layout, agents, self.gameDisplay,
self.length, self.muteAgents, self.catchExceptions)
g.run()
score = g.state.getScore()
score = score + 40 + min(agents[0].maxPathDist, 40) / 40
return score
class Tournament:
def select(self, individuals, k, n):
i = 0
selected = []
while len(selected) < n:
warriors = []
while len(warriors) < k:
warriors.append(individuals[randint(0, len(individuals) - 1)])
warriors.sort(key=lambda ind: ind.getFitness())
selected.append(warriors[len(warriors) - 1])
return selected
class Runner:
def defaultGeneration(populationSize):
mapNodes = 16 * 32
totalNodes = mapNodes + 8 + 2
baseUnit = Genes(16 * 32 + 8 + 2, 5, Genes.Metaparameters(new_node_chance=0.3))
for in_index in range(mapNodes, totalNodes):
for out_index in range(5):
baseUnit.add_connection(baseUnit.input_node_index(in_index), baseUnit.output_node_index(out_index))
return [baseUnit.clone().perturb() for _ in range(populationSize)]
def __init__(self, layout, gameDisplay, length, muteAgents, catchExceptions):
maxGen = 1000
populationSize = 150
self.load = True
self.save = True
self.fitnessCalculator = FitnessCalculator(
layout, gameDisplay, length, muteAgents, True)
base = []
try:
if self.load:
metaparams = Genes.Metaparameters.load(open("metaparameters.json", "r"))
f = open("sample_population.json", "r")
asJson = json.load(f)
for ind in asJson:
if len(base) < populationSize:
base.append(Genes.load_from_json(ind, metaparams))
else:
base = Runner.defaultGeneration(populationSize)
except:
print("Failed to load, defaulting to regeneration!")
base = Runner.defaultGeneration(populationSize)
self.optimizer = GeneticOptimizer(base, self.fitnessCalculator, maxGen)
def run(self):
self.optimizer.initialize()
self.optimizer.evolve()
if self.save:
Runner.save(self.optimizer)
self.fitnessCalculator.pool.close()
exit(0)
def save(optimizer):
all_inds = []
for species in optimizer.getPopulation():
for individual in species["individuals"]:
all_inds.append(individual)
f = open("sample_population.json", "w")
f.write(json.dumps(list(map(lambda ind: ind.as_json(), all_inds))))
f.flush()
optimizer.getBestIndividual().save(open("sample_gene.json", "w"))
optimizer.getBestIndividual()._metaparameters.save(open("metaparameters.json", "w"))