Created
April 17, 2017 13:32
-
-
Save darden1/7d42a7741c0d409c95a13c2508efe10a to your computer and use it in GitHub Desktop.
DEAP's ga tutorial dealing with one max problem using numpy ndarray for a container of an individual.
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
| import random | |
| import numpy | |
| from deap import algorithms | |
| from deap import base | |
| from deap import creator | |
| from deap import tools | |
| creator.create("FitnessMax", base.Fitness, weights=(1.0,)) | |
| creator.create("Individual", numpy.ndarray, fitness=creator.FitnessMax) | |
| toolbox = base.Toolbox() | |
| toolbox.register("attr_bool", random.randint, 0, 1) | |
| toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_bool, n=100) | |
| toolbox.register("population", tools.initRepeat, list, toolbox.individual) | |
| def evalOneMax(individual): | |
| return sum(individual), | |
| def cxTwoPointCopy(ind1, ind2): | |
| size = len(ind1) | |
| cxpoint1 = random.randint(1, size) | |
| cxpoint2 = random.randint(1, size - 1) | |
| if cxpoint2 >= cxpoint1: | |
| cxpoint2 += 1 | |
| else: # Swap the two cx points | |
| cxpoint1, cxpoint2 = cxpoint2, cxpoint1 | |
| ind1[cxpoint1:cxpoint2], ind2[cxpoint1:cxpoint2] = ind2[cxpoint1:cxpoint2].copy(), ind1[cxpoint1:cxpoint2].copy() | |
| return ind1, ind2 | |
| toolbox.register("evaluate", evalOneMax) | |
| toolbox.register("mate", cxTwoPointCopy) | |
| toolbox.register("mutate", tools.mutFlipBit, indpb=0.05) | |
| toolbox.register("select", tools.selTournament, tournsize=3) | |
| def main(): | |
| random.seed(64) | |
| pop = toolbox.population(n=300) | |
| hof = tools.HallOfFame(1, similar=numpy.array_equal) | |
| stats = tools.Statistics(lambda ind: ind.fitness.values) | |
| stats.register("avg", numpy.mean) | |
| stats.register("std", numpy.std) | |
| stats.register("min", numpy.min) | |
| stats.register("max", numpy.max) | |
| algorithms.eaSimple(pop, toolbox, cxpb=0.5, mutpb=0.2, ngen=40, stats=stats,halloffame=hof) | |
| return pop, stats, hof | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment