Last active
July 3, 2022 06:05
-
-
Save tk42/80b65cb4c2e11a5ef8f3be0582be6178 to your computer and use it in GitHub Desktop.
Python DEAP with Multiprocessing Example
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
#!/usr/bin/env python3 | |
import time | |
import array | |
import multiprocessing | |
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", array.array, typecode="b", fitness=creator.FitnessMax) | |
toolbox = base.Toolbox() | |
# Attribute generator | |
toolbox.register("attr_bool", random.randint, 0, 1) | |
# Structure initializers | |
toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_bool, 100) | |
toolbox.register("population", tools.initRepeat, list, toolbox.individual) | |
def eval_heavy_individual(individual): | |
time.sleep(0.1) # create CPU overhead intentionally | |
return (sum(individual),) | |
toolbox.register("evaluate", eval_heavy_individual) | |
toolbox.register("mate", tools.cxTwoPoint) | |
toolbox.register("mutate", tools.mutFlipBit, indpb=0.05) | |
toolbox.register("select", tools.selTournament, tournsize=3) | |
if __name__ == "__main__": | |
random.seed(64) | |
# Process Pool | |
cpu_count = multiprocessing.cpu_count() | |
print(f"CPU count: {cpu_count}") | |
pool = multiprocessing.Pool(cpu_count) | |
toolbox.register("map", pool.map) | |
pop = toolbox.population(n=10) | |
hof = tools.HallOfFame(1) | |
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) | |
pool.close() |
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
#!/usr/bin/env python3 | |
import array | |
import time | |
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", array.array, typecode="b", fitness=creator.FitnessMax) | |
toolbox = base.Toolbox() | |
# Attribute generator | |
toolbox.register("attr_bool", random.randint, 0, 1) | |
# Structure initializers | |
toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_bool, 100) | |
toolbox.register("population", tools.initRepeat, list, toolbox.individual) | |
def eval_heavy_individual(individual): | |
time.sleep(0.1) # create CPU overhead intentionally | |
return (sum(individual),) | |
toolbox.register("evaluate", eval_heavy_individual) | |
toolbox.register("mate", tools.cxTwoPoint) | |
toolbox.register("mutate", tools.mutFlipBit, indpb=0.05) | |
toolbox.register("select", tools.selTournament, tournsize=3) | |
if __name__ == "__main__": | |
random.seed(64) | |
# Process Pool | |
# cpu_count = multiprocessing.cpu_count() | |
# pool = multiprocessing.Pool(cpu_count) | |
# toolbox.register("map", pool.map) | |
pop = toolbox.population(n=10) | |
hof = tools.HallOfFame(1) | |
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) | |
# pool.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment