-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevolution.py
More file actions
356 lines (279 loc) · 12.4 KB
/
evolution.py
File metadata and controls
356 lines (279 loc) · 12.4 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
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
import copy
from selectionFunctions import *
from insertionFunctions import *
from onemaxFunctions import *
from reinforcmentLearning import *
import os
import csv
import json
from statistics import pstdev, mean
#
# Author: Maxence Marot
#
# last update 25/01/22
#
class GeneticAlgorithm:
def __init__(self, json_file=None):
if json_file is not None:
file = open(json_file)
parameters = json.load(file)
self.ind_generation = globals()[parameters["ind_generation"]]
self.selection_method = globals()[parameters["selection_method"]]
self.crossover_method = globals()[parameters["crossover_method"]]
self.crossover_rate = parameters["crossover_rate"]
self.mutation_method = globals()[parameters["mutation_method"]]
self.mutation_rate = parameters["mutation_rate"]
self.insertion_method = globals()[parameters["insertion_method"]]
self.endCondition = (parameters["endCondition"][0], parameters["endCondition"][1])
self.fitness_function = globals()[parameters["fitness_function"]]
self.seed = parameters["seed"]
file.close()
else:
self.ind_generation = None
self.selection_method = None
self.crossover_method = None
self.crossover_rate = None
self.mutation_method = None
self.mutation_rate = None
self.insertion_method = None
self.endCondition = None
self.fitness_function = None
self.seed = None
# Setters
def setIndividualBase(self, ind_generation):
self.ind_generation = ind_generation
def setSelection(self, method):
self.selection_method = method
def setCrossover(self, method, prob=None):
self.crossover_method = method
if prob is not None:
self.crossover_rate = prob
def setMutation(self, method, prob=None):
self.mutation_method = method
if prob is not None:
self.mutation_rate = prob
def setInsertion(self, method):
self.insertion_method = method
def setFitness(self, method):
self.fitness_function = method
def setEndCond(self, endCond):
self.endCondition = endCond
def setSeed(self, seed):
self.seed = seed
random.seed(seed)
def _generatePop(self, n, individual):
"""
Generate the population
:param n: The size of the population
:param individual: A function representing an individual
:rtype: list
:return: A list of individuals
"""
# We randomize the list in case we use age to remove individuals, as it won't be always the same
# to be removed (if we use the same list)
population = []
for i in range(n):
population += [individual()]
random.shuffle(population)
return population
# Fitness
def _fitPop(self, individuals, f):
"""
Function calculating the fitness of an individual by applying a fitness function for each
:param individuals: A list of individuals
:param f: The function used to calculate the fitness
:rtype: dict
:return: A dict with a list of the individuals plus their fitness, and the highest fit of the population
"""
if type(individuals[0]) == tuple:
population_fitness = [(individual[0], f(individual[0])) for individual in individuals]
else:
population_fitness = [(individual, f(individual)) for individual in individuals]
return population_fitness
# Selection
def _select(self, selectionProcess, ind): # 2 bests, tournament, 2 randoms
"""
Function which select the parents of the next generation
:param selectionProcess: How the parents are selected (tournament, best or random as a string)
:param ind: A list of individuals with their fitness
:rtype: list
:return: The choosen ones
"""
return selectionProcess(ind)
# Crossover
def _crossoverPop(self, method, parents):
"""
Apply crossover between parents to create new children
:param method: Crossover function to apply
:param parents: The newlywed happy couple having a bunch of children
:rtype: list
:return: A list of children or an empty list
"""
return method([parents[0][0], parents[1][0]])
# Mutation
def _mutatePop(self, method, ind):
"""
Apply a mutation to a population
:param method: Mutation function to apply
:param ind: Individual population on which the mutation happens
:rtype: list
:return: A list of mutated individuals or an empty list
"""
return [method(individual) for individual in ind]
# Insertion
def _insertion(self, population, children, method):
"""
Insert newly born children to the population
:param children: Children to add to the population
:param population: Population in which we add children as a list
:param method: Method of removal
:rtype: list
:return: the population
"""
return method(population) + children
def _createDataLog(self):
"""
Create a folder in which we create a json file for the caracteristics of the ga and a csv with the data
:rtype: str
:return: The name of the created folder (might remove and make it an attribute)
"""
mutName = ""
if type(self.mutation_method) == list:
mutName += "roulette_["
for name in self.mutation_method:
mutName += name.__name__ + "_"
mutName = mutName[:-1] + "]"
else:
mutName = self.mutation_method.__name__
characteristics = self.selection_method.__name__ + "_" + self.crossover_method.__name__ + "_" + \
mutName + self.fitness_function.__name__
root_data_files = './data/' + str(self.endCondition) + "/"
data_folder = root_data_files + characteristics + "/" + "seed_" + str(self.seed) + "/"
if not os.path.exists(data_folder):
os.makedirs(data_folder)
json_file = open(data_folder + 'parameters.json', "w")
parameters = {
"selection_method": self.selection_method.__name__,
"crossover_method": self.crossover_method.__name__,
"crossover_rate": self.crossover_rate,
"mutation_method": mutName,
"mutation_rate": self.mutation_rate,
"endCondition": self.endCondition,
"fitness_function": self.fitness_function.__name__,
"seed": self.seed
}
json_file.write(json.dumps(parameters))
json_file.close()
csv_file = open(data_folder + "/data.csv", "w", encoding="UTF8", newline="")
writer = csv.writer(csv_file)
# Write header
writer.writerow(["generation", "min_fitness", "max_fitness", "mean", "standard_deviation",
"methodsHistory", "probOpe", "improvement"])
csv_file.close()
return data_folder
def _writeToDataLog(self, population, dataFolder, generation, opHistory=None, probaList=None, imp=None):
"""
Write data to log for stats & graphs
:param population: The current population of the AG
:param dataFolder: The folder in which we write
:param generation: Current generation
:param opHistory: Number of time each method was called
:param probaList: Evolution of the probability of use of each method
"""
sum_fitness = 0
for element in population:
sum_fitness += element[1]
# Calculate a bunch of sophisticated (or not) stats
meanFit = mean([fitness[1] for fitness in population])
std_dev = pstdev([fitness[1] for fitness in population], mu=meanFit)
minFit = min(population, key=lambda fitness: fitness[1])[1]
maxFit = max(population, key=lambda fitness: fitness[1])[1]
# Write to csv
csv_file = open(dataFolder + "/data.csv", "a", encoding="UTF8", newline="")
writer = csv.writer(csv_file)
writer.writerow([str(generation), str(minFit), str(maxFit), str(meanFit), str(std_dev),
str(opHistory), str(probaList), str(imp)])
csv_file.close()
# Main method of ga, Darwin would be proud
def evolution(self, popSize=200, reinforcement=None, historySize=10, pmin=0.05, keepImproved=True):
"""
Main function, make the population evolve
Generate a csv file with data about the population and
a json file with the parameters of the model in a "data" folder
:param popSize: Size of the population
:param reinforcement: Method of reinforcement to use, None if there is none
:param historySize: Size of history used for the reinforcement
:param pmin: Min probability of a method for reinforcement wheel
:param keepImproved: True if we keep only the improved mutants, False otherwise
:rtype: list
:return: The bestest individual
"""
random.seed(self.seed)
population = self._generatePop(popSize, self.ind_generation)
generation = 0
# Create a folder to put the data
data_folder = self._createDataLog()
# Stats
taille = 0
if type(self.mutation_method) == list and reinforcement is not None:
taille = len(self.mutation_method)
rewardList = [0 for i in range(taille)]
rewardHistory = [[0] for i in range(taille)]
probaList = [1 / taille for i in range(taille)]
opHistory = [0 for i in range(taille)]
nbrMut = []
# fit
population = self._fitPop(population, self.fitness_function)
maxFitness = max(population, key=lambda fitness: fitness[1])[1]
while (self.endCondition[0] is not None and self.endCondition[0] > maxFitness) \
and (generation < self.endCondition[1]):
generation += 1
# Reinforcement
currentOpe = -1
if reinforcement is not None:
currentOpe = select_op(probaList)
if currentOpe != -1:
nbrMut.append(self.mutation_method[currentOpe])
# select
parents = self._select(self.selection_method, population)
children = None
# cross
if random.random() < self.crossover_rate:
children = self._crossoverPop(self.crossover_method, parents)
else:
children = (copy.deepcopy(parents[0][0]), copy.deepcopy(parents[1][0]))
# Fitness for utility
initialFitnessP1 = parents[0][1]
initialFitnessP2 = parents[1][1]
newFitnessC1 = 0
newFitnessC2 = 0
# mutate
if random.random() < self.mutation_rate:
mutation_m = self.mutation_method
if reinforcement is not None:
mutation_m = self.mutation_method[currentOpe]
children = self._mutatePop(mutation_m, children)
# Fitness calculus
children = self._fitPop(children, self.fitness_function)
newFitnessC1 = children[0][1]
newFitnessC2 = children[1][1]
# MAJ des utilités
# For the improvement, we use the mean of the fitness
imp = improvement((initialFitnessP1 + initialFitnessP2) / 2, (newFitnessC1 + newFitnessC2) / 2)
if reinforcement is not None:
update_reward_sliding(rewardList, rewardHistory, historySize, currentOpe, imp)
# MAJ roulette
update_roulette_wheel(rewardList, probaList, pmin)
# insert
if keepImproved:
if imp > 0:
population = self._insertion(population, children, self.insertion_method)
else:
population = self._insertion(population, children, self.insertion_method)
maxFitness = max(population, key=lambda fitness: fitness[1])[1]
if reinforcement is not None:
for o in range(len(self.mutation_method)):
if o == currentOpe:
opHistory[o] += 1
self._writeToDataLog(population, data_folder, generation, opHistory, probaList, imp)
return bestSelection(population, 1)