Last active
August 29, 2015 14:17
-
-
Save akiross/06d6b832516a86c2b4f7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env python3 | |
from deap import gp, base, creator, tools, algorithms | |
import itertools | |
import operator | |
import random | |
import math | |
def converter(value): | |
return value / 100 | |
pset = gp.PrimitiveSetTyped('main', [int], float) | |
pset.addPrimitive(converter, [int], float) | |
creator.create('FitnessMin', base.Fitness, weights=(-1,)) | |
creator.create('Individual', gp.PrimitiveTree, fitness=creator.FitnessMin) | |
toolbox = base.Toolbox() | |
toolbox.register('expr', gp.genHalfAndHalf, min_=0, max_=1, pset=pset, type_=float) | |
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 evalTree(individual): | |
func = toolbox.compile(expr=individual) | |
return (sum(abs(func(i) - i / 100) for i in range(20)), ) | |
toolbox.register('evaluate', evalTree) | |
toolbox.register('select', tools.selTournament, tournsize=3) | |
toolbox.register('mate', gp.cxOnePoint) | |
toolbox.register('expr_mut', gp.genFull, min_=0, max_=1, type_=float) | |
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)) | |
pop = toolbox.population(n=30) | |
hof = tools.HallOfFame(1) | |
num_gen = 10 | |
pop, log = algorithms.eaSimple(pop, toolbox, 0.5, 0.1, num_gen, halloffame=hof, verbose=True) | |
for i, indiv in enumerate(pop): | |
print(indiv) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment