-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathalg.py
120 lines (91 loc) · 4.35 KB
/
alg.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
import random
import pickle
import time
import datetime
from deap import algorithms
def myEASimple(population, start_gen, toolbox, cxpb, mutpb, ngen,
stats, halloffame, logbook, verbose, id=None):
total_time = datetime.timedelta(seconds=0)
for gen in range(start_gen, ngen):
start_time = datetime.datetime.now()
population = algorithms.varAnd(population, toolbox, cxpb=cxpb, mutpb=mutpb)
# Evaluate the individuals with an invalid fitness
invalid_ind = [ind for ind in population if not ind.fitness.valid]
fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
for ind, fit in zip(invalid_ind, fitnesses):
ind.fitness.values = fit
halloffame.update(population)
record = stats.compile(population)
logbook.record(gen=gen, evals=len(invalid_ind), **record)
if verbose:
print(logbook.stream)
population = toolbox.select(population, k=len(population))
if gen % 1 == 0:
# Fill the dictionary using the dict(key=value[, ...]) constructor
cp = dict(population=population, generation=gen, halloffame=halloffame,
logbook=logbook, rndstate=random.getstate())
if id is None:
cp_name = "checkpoint_ea.pkl"
else:
cp_name = "checkpoint_ea_{}.pkl".format(id)
pickle.dump(cp, open(cp_name, "wb"))
gen_time = datetime.datetime.now() - start_time
total_time = total_time + gen_time
#print("Time ", total_time)
if total_time > datetime.timedelta(hours=4*24):
print("Time limit exceeded.")
break
return population, logbook
def myEAMuCommaLambda(population, startgen, toolbox, mu, lambda_, cxpb, mutpb, ngen,
stats=None, halloffame=None, logbook=None, verbose=False, id=None):
assert lambda_ >= mu, "lambda must be greater or equal to mu."
# Evaluate the individuals with an invalid fitness
invalid_ind = [ind for ind in population if not ind.fitness.valid]
fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
for ind, fit in zip(invalid_ind, fitnesses):
ind.fitness.values = fit
if halloffame is not None:
halloffame.update(population)
if logbook is None:
logbook = tools.Logbook()
logbook.header = ['gen', 'nevals'] + (stats.fields if stats else [])
record = stats.compile(population) if stats is not None else {}
logbook.record(gen=startgen, nevals=len(invalid_ind), **record)
if verbose:
print(logbook.stream)
# Begin the generational process
total_time = datetime.timedelta(seconds=0)
for gen in range(startgen+1, ngen):
start_time = datetime.datetime.now()
# Vary the population
offspring = algorithms.varOr(population, toolbox, lambda_, cxpb, mutpb)
# Evaluate the individuals with an invalid fitness
invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
for ind, fit in zip(invalid_ind, fitnesses):
ind.fitness.values = fit
# Update the hall of fame with the generated individuals
if halloffame is not None:
halloffame.update(offspring)
# Select the next generation population
population[:] = toolbox.select(offspring, mu)
# Update the statistics with the new population
record = stats.compile(population) if stats is not None else {}
logbook.record(gen=gen, nevals=len(invalid_ind), **record)
if verbose:
print(logbook.stream)
if gen % 1 == 0:
# Fill the dictionary using the dict(key=value[, ...]) constructor
cp = dict(population=population, generation=gen, halloffame=halloffame,
logbook=logbook, rndstate=random.getstate())
if id is None:
cp_name = "checkpoint_es.pkl"
else:
cp_name = "checkpoint_es_{}.pkl".format(id)
pickle.dump(cp, open(cp_name, "wb"))
gen_time = datetime.datetime.now() - start_time
total_time = total_time + gen_time
if total_time > datetime.timedelta(hours=4*24):
print("Time limit exceeded.")
break
return population, logbook