Last active
June 21, 2023 10:59
-
-
Save darden1/fa8f96185a46796ed9516993bfe24862 to your computer and use it in GitHub Desktop.
DEAP's nsga2 tutorial.
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
# -*- coding: utf-8 -*- | |
import array | |
import random | |
import json | |
import numpy as np | |
import matplotlib.pyplot as plt | |
from math import sqrt | |
from deap import algorithms | |
from deap import base | |
from deap import benchmarks | |
from deap.benchmarks.tools import diversity, convergence, hypervolume | |
from deap import creator | |
from deap import tools | |
creator.create("FitnessMin", base.Fitness, weights=(-1.0, -1.0)) | |
creator.create("Individual", array.array, typecode='d', fitness=creator.FitnessMin) | |
toolbox = base.Toolbox() | |
BOUND_LOW, BOUND_UP = 0.0, 1.0 | |
NDIM = 30 | |
def uniform(low, up, size=None): | |
try: | |
return [random.uniform(a, b) for a, b in zip(low, up)] | |
except TypeError: | |
return [random.uniform(a, b) for a, b in zip([low] * size, [up] * size)] | |
toolbox.register("attr_float", uniform, BOUND_LOW, BOUND_UP, NDIM) | |
toolbox.register("individual", tools.initIterate, creator.Individual, toolbox.attr_float) | |
toolbox.register("population", tools.initRepeat, list, toolbox.individual) | |
toolbox.register("evaluate", benchmarks.zdt1) | |
toolbox.register("mate", tools.cxSimulatedBinaryBounded, low=BOUND_LOW, up=BOUND_UP, eta=20.0) | |
toolbox.register("mutate", tools.mutPolynomialBounded, low=BOUND_LOW, up=BOUND_UP, eta=20.0, indpb=1.0/NDIM) | |
toolbox.register("select", tools.selNSGA2) | |
def main(seed=None): | |
random.seed(seed) | |
NGEN = 250 | |
MU = 100 | |
CXPB = 0.9 | |
stats = tools.Statistics(lambda ind: ind.fitness.values) | |
stats.register("min", np.min, axis=0) | |
stats.register("max", np.max, axis=0) | |
logbook = tools.Logbook() | |
logbook.header = "gen", "evals", "min", "max" | |
pop = toolbox.population(n=MU) | |
pop_ini = pop[:] | |
invalid_ind = [ind for ind in pop if not ind.fitness.valid] | |
fitnesses = toolbox.map(toolbox.evaluate, invalid_ind) | |
for ind, fit in zip(invalid_ind, fitnesses): | |
ind.fitness.values = fit | |
pop = toolbox.select(pop, len(pop)) | |
record = stats.compile(pop) | |
logbook.record(gen=0, evals=len(invalid_ind), **record) | |
print(logbook.stream) | |
for gen in range(1, NGEN): | |
offspring = tools.selTournamentDCD(pop, len(pop)) | |
offspring = [toolbox.clone(ind) for ind in offspring] | |
for ind1, ind2 in zip(offspring[::2], offspring[1::2]): | |
if random.random() <= CXPB: | |
toolbox.mate(ind1, ind2) | |
toolbox.mutate(ind1) | |
toolbox.mutate(ind2) | |
del ind1.fitness.values, ind2.fitness.values | |
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 | |
pop = toolbox.select(pop + offspring, MU) | |
record = stats.compile(pop) | |
logbook.record(gen=gen, evals=len(invalid_ind), **record) | |
print(logbook.stream) | |
print("Final population hypervolume is %f" % hypervolume(pop, [11.0, 11.0])) | |
return pop, pop_ini, logbook | |
if __name__ == "__main__": | |
pop, pop_ini, stats = main(64) | |
fitnesses_ini = np.array([list(pop_ini[i].fitness.values) for i in range(len(pop_ini))]) | |
fitnesses = np.array([list(pop[i].fitness.values) for i in range(len(pop))]) | |
plt.plot(fitnesses_ini[:,0], fitnesses_ini[:,1], "b.", label="Initial") | |
plt.plot(fitnesses[:,0], fitnesses[:,1], "r.", label="Optimized" ) | |
plt.legend(loc="upper right") | |
plt.title("fitnesses") | |
plt.xlabel("f1") | |
plt.ylabel("f2") | |
plt.grid(True) | |
plt.savefig("fitnesses.png", dpi=300) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment