Last active
July 29, 2020 21:09
-
-
Save DMTSource/47e8e3e123315e956b5971070776e45f to your computer and use it in GitHub Desktop.
Ning's question(https://groups.google.com/forum/#!topic/deap-users/W8wacFPyj9Y) on eaSimpleWithElitism(https://github.com/PacktPublishing/Hands-On-Genetic-Algorithms-with-Python/blob/master/Chapter04/elitism.py)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Derek Tishler, answering a question from Deap Discussions: | |
# For Ning's question: https://groups.google.com/forum/#!topic/deap-users/W8wacFPyj9Y | |
# Based on symbreg example: https://github.com/DEAP/deap/blob/master/examples/gp/symbreg.py | |
# eaSimpleWithElitism from: https://github.com/PacktPublishing/Hands-On-Genetic-Algorithms-with-Python/blob/master/Chapter04/elitism.py | |
import operator | |
import math | |
import random | |
import numpy | |
from deap import algorithms | |
from deap import base | |
from deap import creator | |
from deap import tools | |
from deap import gp | |
# Define new functions | |
def protectedDiv(left, right): | |
try: | |
return left / right | |
except ZeroDivisionError: | |
return 1 | |
pset = gp.PrimitiveSet("MAIN", 1) | |
pset.addPrimitive(operator.add, 2) | |
pset.addPrimitive(operator.sub, 2) | |
pset.addPrimitive(operator.mul, 2) | |
pset.addPrimitive(protectedDiv, 2) | |
pset.addPrimitive(operator.neg, 1) | |
pset.addPrimitive(math.cos, 1) | |
pset.addPrimitive(math.sin, 1) | |
pset.addEphemeralConstant("rand101", lambda: random.randint(-1,1)) | |
pset.renameArguments(ARG0='x') | |
creator.create("FitnessMin", base.Fitness, weights=(-1.0,)) | |
creator.create("Individual", gp.PrimitiveTree, fitness=creator.FitnessMin) | |
toolbox = base.Toolbox() | |
toolbox.register("expr", gp.genHalfAndHalf, pset=pset, min_=1, max_=2) | |
toolbox.register("individual", tools.initIterate, creator.Individual, toolbox.expr) | |
toolbox.register("population", tools.initRepeat, list, toolbox.individual) | |
toolbox.register("compile", gp.compile, pset=pset) | |
def evalSymbReg(individual, points): | |
# Transform the tree expression in a callable function | |
func = toolbox.compile(expr=individual) | |
# Evaluate the mean squared error between the expression | |
# and the real function : x**4 + x**3 + x**2 + x | |
sqerrors = ((func(x) - x**4 - x**3 - x**2 - x)**2 for x in points) | |
return math.fsum(sqerrors) / len(points), | |
toolbox.register("evaluate", evalSymbReg, points=[x/10. for x in range(-10,10)]) | |
toolbox.register("select", tools.selTournament, tournsize=3) | |
toolbox.register("mate", gp.cxOnePoint) | |
toolbox.register("expr_mut", gp.genFull, min_=0, max_=2) | |
toolbox.register("mutate", gp.mutUniform, expr=toolbox.expr_mut, pset=pset) | |
toolbox.decorate("mate", gp.staticLimit(key=operator.attrgetter("height"), max_value=17)) | |
toolbox.decorate("mutate", gp.staticLimit(key=operator.attrgetter("height"), max_value=17)) | |
def main(): | |
random.seed(318) | |
pop = toolbox.population(n=300) | |
#hof = tools.HallOfFame(1) | |
#hof = tools.HallOfFame(10) | |
hof = tools.HallOfFame(100) | |
stats_fit = tools.Statistics(lambda ind: ind.fitness.values) | |
stats_size = tools.Statistics(len) | |
mstats = tools.MultiStatistics(fitness=stats_fit, size=stats_size) | |
mstats.register("avg", numpy.mean) | |
mstats.register("std", numpy.std) | |
mstats.register("min", numpy.min) | |
mstats.register("max", numpy.max) | |
#pop, log = algorithms.eaSimple(pop, toolbox, 0.5, 0.1, 40, stats=mstats, | |
# halloffame=hof, verbose=True) | |
pop, log = eaSimpleWithElitism(pop, toolbox, 0.5, 0.1, 20, stats=mstats, | |
halloffame=hof, verbose=True) | |
# print log | |
return pop, log, hof | |
def eaSimpleWithElitism(population, toolbox, cxpb, mutpb, ngen, stats=None, | |
halloffame=None, verbose=__debug__): | |
# SOURCE https://github.com/PacktPublishing/Hands-On-Genetic-Algorithms-with-Python/blob/master/Chapter04/elitism.py | |
"""This algorithm is similar to DEAP eaSimple() algorithm, with the modification that | |
halloffame is used to implement an elitism mechanism. The individuals contained in the | |
halloffame are directly injected into the next generation and are not subject to the | |
genetic operators of selection, crossover and mutation. | |
""" | |
logbook = tools.Logbook() | |
logbook.header = ['gen', 'nevals'] + (stats.fields if stats else []) | |
# 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 None: | |
raise ValueError("halloffame parameter must not be empty!") | |
halloffame.update(population) | |
hof_size = len(halloffame.items) if halloffame.items else 0 | |
record = stats.compile(population) if stats else {} | |
logbook.record(gen=0, nevals=len(invalid_ind), | |
popsize=len(population), hofsize=len(halloffame), n_offspring=0, **record) | |
if verbose: | |
print(logbook.stream) | |
# Begin the generational process | |
for gen in range(1, ngen + 1): | |
# Select the next generation individuals | |
offspring = toolbox.select(population, len(population) - hof_size) | |
# Vary the pool of individuals | |
offspring = algorithms.varAnd(offspring, toolbox, 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 | |
# add the best back to population: | |
offspring.extend(halloffame.items) | |
# Update the hall of fame with the generated individuals | |
halloffame.update(offspring) | |
# Replace the current population by the offspring | |
population[:] = offspring | |
# Append the current generation statistics to the logbook | |
record = stats.compile(population) if stats else {} | |
logbook.record(gen=gen, nevals=len(invalid_ind), | |
popsize=len(population), hofsize=len(halloffame), n_offspring=len(offspring), **record) | |
if verbose: | |
print(logbook.stream) | |
return population, logbook | |
if __name__ == "__main__": | |
main() | |
''' | |
NORMAL EXAMPLE | |
------------------------------------------------------- ----------------------------------------------- | |
gen nevals avg gen max min nevals std avg gen max min nevals std | |
0 300 1.78879 0 30.34 0.450825 300 2.67896 3.54667 0 7 2 300 1.49482 | |
1 166 1.43254 1 44.4437 0.183711 166 3.05668 3.60667 1 12 1 166 1.77725 | |
2 166 2.16879 2 315.736 0.165572 166 18.1873 3.55 2 9 1 166 1.62506 | |
3 163 0.98255 3 2.9829 0.165572 163 0.712666 3.42667 3 9 1 163 1.45073 | |
4 153 0.836017 4 14.538 0.165572 153 0.979399 3.77 4 11 1 153 1.64025 | |
5 158 0.944635 5 18.9739 0.165572 158 1.61614 3.77667 5 10 1 158 1.62894 | |
6 169 0.885819 6 14.2181 0.165572 169 1.00296 4 6 10 1 169 1.87617 | |
7 167 0.731332 7 3.35292 0.165572 167 0.56016 4.35 7 10 1 167 1.92722 | |
8 187 0.785916 8 19.1852 0.13008 187 1.2426 5.13667 8 14 1 187 2.21465 | |
9 176 0.672788 9 14.2339 0.123719 176 1.00764 5.71667 9 14 1 176 2.48389 | |
10 176 0.786975 10 32.4952 0.123719 176 2.56679 6.27333 10 15 1 176 2.33922 | |
11 167 0.961666 11 62.7581 0.123719 167 4.10149 6.75667 11 16 1 167 2.30162 | |
12 170 0.630202 12 18.7613 0.114276 170 1.55098 7.06333 12 16 1 170 2.55199 | |
13 173 0.792442 13 18.7749 0.114276 173 2.0836 7.07333 13 17 1 173 2.3212 | |
14 163 0.701482 14 16.5323 0.0935121 163 1.92386 7.01 14 16 1 163 2.74042 | |
15 178 0.796168 15 63.2953 0.0935121 178 3.82723 7.42333 15 17 1 178 3.11942 | |
16 154 0.446513 16 19.0443 0.0512969 154 1.18016 7.73333 16 17 1 154 3.35095 | |
17 177 0.378618 17 4.88791 0.0512969 177 0.470854 9.01 17 22 1 177 3.68464 | |
18 177 0.394816 18 2.552 0.0512969 177 0.45627 10.14 18 23 2 177 3.86097 | |
19 175 0.347051 19 2.84707 0.0147194 175 0.428075 11 19 24 1 175 4.21347 | |
20 167 0.370537 20 18.9739 0.0147194 167 1.15424 11.6 20 25 3 167 4.13441 | |
21 159 0.302206 21 2.54483 0.0147194 159 0.36307 12.78 21 33 1 159 5.04298 | |
22 161 0.329424 22 5.15351 0.014539 161 0.494736 14.1033 22 32 1 161 5.23762 | |
23 165 0.464998 23 18.9739 0.0147194 165 1.51044 14.8933 23 34 1 165 6.13259 | |
24 188 0.271713 24 3.71933 0.0147194 188 0.436801 15.0867 24 34 2 188 6.25719 | |
25 152 0.269631 25 14.5905 0.0147194 152 0.88428 15.22 25 47 1 152 6.49602 | |
26 164 0.230995 26 4.4082 0.0147194 164 0.436685 16.0633 26 41 3 164 6.52171 | |
27 159 0.185306 27 5.249 0.0144201 159 0.410217 17.2333 27 40 1 159 6.7111 | |
28 164 0.165298 28 1.98267 0.0143442 164 0.292591 18.3 28 43 1 164 7.23763 | |
29 174 0.172642 29 2.54124 0.00182347 174 0.352515 18.33 29 37 1 174 6.69037 | |
30 141 0.10727 30 2.61126 0.00182347 141 0.241954 19.3 30 37 3 141 5.483 | |
31 154 0.129872 31 2.18033 0.00182347 154 0.269947 19.7933 31 39 3 154 5.94788 | |
32 165 0.129453 32 2.54124 0.00182347 165 0.287201 20.2867 32 43 1 165 6.5175 | |
33 173 0.16656 33 2.54124 0.00182347 173 0.341339 20.5633 33 43 1 173 7.38823 | |
34 168 0.131975 34 1.52568 0.00182347 168 0.232491 20.4233 34 48 3 168 7.45637 | |
35 153 0.152313 35 2.54124 0.00182347 153 0.324276 19.82 35 43 2 153 7.13589 | |
36 178 0.237937 36 16.1106 0.00182347 178 0.990786 19.11 36 43 3 178 7.68318 | |
37 157 0.185061 37 4.76458 0.00182347 157 0.449021 18.3667 37 39 1 157 6.31128 | |
38 184 0.185972 38 4.09456 5.12297e-33 184 0.358825 17.4767 38 48 1 184 7.37899 | |
39 149 0.193025 39 2.9829 5.12297e-33 149 0.377824 16.87 39 35 1 149 5.85205 | |
40 161 0.127091 40 1.59859 5.12297e-33 161 0.216044 16.4767 40 36 1 161 6.14948 | |
HOF SIZE 1 | |
--------------------------------------------------------------------------------------- ------------------------------------------------------------------------------- | |
gen nevals avg gen hofsize max min n_offspring nevals popsize std avg gen hofsize max min n_offspringnevals popsize std | |
0 300 1.78879 0 1 30.34 0.450825 0 300 300 2.67896 3.54667 0 1 7 2 0 300 300 1.49482 | |
1 169 1.20247 1 1 18.9443 0.450825 300 169 300 1.538 3.56333 1 1 10 1 300 169 300 1.70274 | |
2 157 1.02214 2 1 18.7749 0.450825 300 157 300 1.47742 3.44667 2 1 9 1 300 157 300 1.39541 | |
3 169 1.02699 3 1 18.0015 0.165572 300 169 300 1.46278 3.58667 3 1 9 1 300 169 300 1.52397 | |
4 149 0.94393 4 1 14.538 0.165572 300 149 300 1.18104 4.00667 4 1 11 1 300 149 300 1.65528 | |
5 161 0.952269 5 1 19.2973 0.165572 300 161 300 1.29215 4.48667 5 1 12 1 300 161 300 1.95699 | |
6 156 0.880084 6 1 3.87385 0.165572 300 156 300 0.754649 4.61 6 1 13 1 300 156 300 2.21312 | |
7 169 0.816916 7 1 18.9443 0.165572 300 169 300 1.23212 4.88 7 1 14 1 300 169 300 2.10213 | |
8 173 0.939778 8 1 19.1852 0.165572 300 173 300 1.95157 5.43333 8 1 13 1 300 173 300 1.94565 | |
9 161 0.719798 9 1 18.842 0.0407679 300 161 300 1.6707 5.53333 9 1 13 1 300 161 300 1.79505 | |
10 155 0.606555 10 1 29.8179 0.0407679 300 155 300 1.7949 5.61667 10 1 17 1 300 155 300 1.80639 | |
11 160 0.450146 11 1 14.3675 0.02737 300 160 300 0.950152 5.68 11 1 13 1 300 160 300 1.902 | |
12 186 0.469557 12 1 18.7749 0.02737 300 186 300 1.18146 6.28 12 1 15 1 300 186 300 2.48494 | |
13 163 0.437544 13 1 18.7749 0.02737 300 163 300 1.16779 7.01 13 1 18 1 300 163 300 2.99052 | |
14 151 0.44656 14 1 22.3008 0.00182347 300 151 300 1.77706 8.17 14 1 20 1 300 151 300 3.7087 | |
15 158 3.87273 15 1 1083.31 0.00182347 300 158 300 62.4272 10.04 15 1 22 3 300 158 300 4.01892 | |
16 170 0.247525 16 1 2.6735 0.00182347 300 170 300 0.386785 11.3267 16 1 22 1 300 170 300 3.72558 | |
17 169 0.252829 17 1 4.88791 5.12297e-33 300 169 300 0.550269 12.0367 17 1 28 3 300 169 300 4.16117 | |
18 172 0.204311 18 1 2.54124 5.12297e-33 300 172 300 0.323202 12.7567 18 1 33 3 300 172 300 4.27365 | |
19 155 0.180033 19 1 2.9829 5.12297e-33 300 155 300 0.372006 13.8833 19 1 44 1 300 155 300 5.47142 | |
20 137 0.17307 20 1 16.5323 5.12297e-33 300 137 300 0.971675 14.3633 20 1 33 3 300 137 300 4.75654 | |
HOF SIZE 10 | |
fitness size | |
--------------------------------------------------------------------------------------- ------------------------------------------------------------------------------- | |
gen nevals avg gen hofsize max min n_offspring nevals popsize std avg gen hofsize max min n_offspringnevals popsize std | |
0 300 1.78879 0 10 30.34 0.450825 0 300 300 2.67896 3.54667 0 10 7 2 0 300 300 1.49482 | |
1 167 1.24244 1 10 27.7607 0.183711 300 167 300 1.833 3.64333 1 10 12 1 300 167 300 1.86086 | |
2 166 0.860875 2 10 4.15979 0.165572 300 166 300 0.51136 3.74 2 10 13 1 300 166 300 1.66104 | |
3 168 0.787477 3 10 2.9829 0.165572 300 168 300 0.513706 4.06 3 10 12 1 300 168 300 1.84655 | |
4 146 0.82475 4 10 18.9739 0.165572 300 146 300 1.42691 4.59 4 10 12 1 300 146 300 1.78005 | |
5 144 0.670531 5 10 3.87633 0.165572 300 144 300 0.599398 5.24333 5 10 13 1 300 144 300 1.82504 | |
6 156 0.615565 6 10 18.9739 0.165572 300 156 300 1.16363 5.72333 6 10 13 1 300 156 300 1.75313 | |
7 169 0.48206 7 10 13.309 0.13008 300 169 300 0.860344 5.82667 7 10 13 1 300 169 300 1.75972 | |
8 148 0.442843 8 10 18.7749 0.13008 300 148 300 1.14995 5.95667 8 10 15 1 300 148 300 1.92045 | |
9 168 0.491538 9 10 14.5099 0.13008 300 168 300 1.27003 5.83 9 10 13 1 300 168 300 1.85861 | |
10 143 0.434913 10 10 15.0932 0.13008 300 143 300 1.00501 5.95667 10 10 14 1 300 143 300 1.80595 | |
11 145 0.405018 11 10 18.9739 0.117731 300 145 300 1.15208 6.01 11 10 13 3 300 145 300 1.82845 | |
12 180 0.381484 12 10 4.10055 0.117731 300 180 300 0.427833 6.21667 12 10 16 1 300 180 300 2.12518 | |
13 176 0.357948 13 10 2.9829 0.117731 300 176 300 0.38987 6.61667 13 10 14 1 300 176 300 2.37691 | |
14 168 0.390484 14 10 14.2339 0.0253803 300 168 300 0.925435 7.34 14 10 17 2 300 168 300 2.6729 | |
15 143 0.352963 15 10 12.9229 0.0253803 300 143 300 0.850212 8.05667 15 10 24 1 300 143 300 2.80834 | |
16 158 0.283881 16 10 2.61126 0.0253803 300 158 300 0.370229 8.40667 16 10 17 1 300 158 300 2.49291 | |
17 150 0.272669 17 10 4.19956 0.0115213 300 150 300 0.383183 8.88333 17 10 18 1 300 150 300 2.69377 | |
18 144 0.262802 18 10 2.71688 5.12297e-33 300 144 300 0.368678 9.65 18 10 25 1 300 144 300 3.4815 | |
19 145 0.182264 19 10 2.3179 5.12297e-33 300 145 300 0.264833 10.3167 19 10 25 1 300 145 300 3.27969 | |
20 176 0.232079 20 10 5.32509 5.12297e-33 300 176 300 0.461161 11.3333 20 10 26 1 300 176 300 3.61601 | |
HOF SIZE 100 | |
fitness size | |
--------------------------------------------------------------------------------------- ------------------------------------------------------------------------------- | |
gen nevals avg gen hofsize max min n_offspring nevals popsize std avg gen hofsize max min n_offspringnevals popsize std | |
0 300 1.78879 0 100 30.34 0.450825 0 300 300 2.67896 3.54667 0 100 7 2 0 300 300 1.49482 | |
1 108 1.04978 1 100 7.40348 0.450825 300 108 300 0.62363 3.62 1 100 9 1 300 108 300 1.52827 | |
2 106 1.01148 2 100 14.2073 0.450825 300 106 300 0.935493 4.01 2 100 11 1 300 106 300 1.71753 | |
3 99 0.851539 3 100 4.19956 0.356827 300 99 300 0.45038 4.08 3 100 11 1 300 99 300 1.56427 | |
4 117 0.814281 4 100 2.9829 0.356827 300 117 300 0.458513 4.15333 4 100 11 1 300 117 300 1.5989 | |
5 108 0.796651 5 100 6.85956 0.190351 300 108 300 0.623488 4.63667 5 100 11 1 300 108 300 1.79202 | |
6 115 0.763765 6 100 12.7314 0.190351 300 115 300 0.835516 5.00667 6 100 12 1 300 115 300 1.98493 | |
7 102 0.69439 7 100 2.9829 0.190351 300 102 300 0.46888 5.46667 7 100 11 1 300 102 300 1.97034 | |
8 111 0.781223 8 100 19.1384 0.190351 300 111 300 1.5142 5.71333 8 100 13 1 300 111 300 2.1995 | |
9 117 0.729383 9 100 8.82422 0.190351 300 117 300 0.714219 6.13 9 100 16 1 300 117 300 2.39995 | |
10 103 0.601175 10 100 6.1116 0.165572 300 103 300 0.503877 6.48667 10 100 14 1 300 103 300 2.45693 | |
11 105 0.638545 11 100 16.318 0.142761 300 105 300 1.16021 7.11 11 100 16 1 300 105 300 2.72603 | |
12 103 0.538118 12 100 4.12093 0.142761 300 103 300 0.42669 7.91333 12 100 24 2 300 103 300 3.15053 | |
13 115 0.573982 13 100 6.85956 0.122294 300 115 300 0.590912 8.35 13 100 20 2 300 115 300 3.33879 | |
14 124 0.481701 14 100 4.39829 0.122294 300 124 300 0.445114 8.91 14 100 19 1 300 124 300 3.29877 | |
15 100 0.506224 15 100 5.3602 0.122294 300 100 300 0.667878 9.58667 15 100 26 1 300 100 300 3.69628 | |
16 96 0.485995 16 100 15.5032 0.120998 300 96 300 0.99194 10.1067 16 100 27 1 300 96 300 4.06226 | |
17 118 0.382279 17 100 2.97321 0.0976781 300 118 300 0.411649 10.6133 17 100 28 1 300 118 300 4.57061 | |
18 106 0.312505 18 100 5.09794 0.0976781 300 106 300 0.353323 11.4833 18 100 29 1 300 106 300 4.72049 | |
19 113 0.303823 19 100 6.1543 0.0556352 300 113 300 0.430592 12.5333 19 100 29 1 300 113 300 5.04138 | |
20 106 0.241489 20 100 2.552 0.0261781 300 106 300 0.263734 13.5167 20 100 33 2 300 106 300 5.00896 | |
''' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment