Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting the Pareto Front for every generation #735

Open
diazguerra1103 opened this issue Dec 9, 2023 · 0 comments
Open

Getting the Pareto Front for every generation #735

diazguerra1103 opened this issue Dec 9, 2023 · 0 comments

Comments

@diazguerra1103
Copy link

I am beginner using DEAP to run a multi objective optimization using eaMuPlusLambda. The following code returns the ParetoFront after the last generation. Is there any way to get a set of ParetoFront for each generation? I would like to see the evolution of the fronts with every generation.


import random
import numpy
import matplotlib.pyplot as plt
from deap import base, creator, tools, algorithms

''' Objective Function 1 '''
def f1(individual):
x, y = individual
return x1 + y0

''' Objective Function 2 '''
def f2(individual):
x, y = individual
return (1+y)/x

def feasible(individual):
if 0.1 < individual[0] < 1:
if 0 < individual[1] < 5:
return True
return False

creator.create("FitnessMulti", base.Fitness, weights=(-1.0, -1.0))
creator.create("Individual", list, fitness=creator.FitnessMulti)

toolbox = base.Toolbox()
toolbox.register("attr_float", random.random)
toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_float, n=2)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
toolbox.register("mate", tools.cxBlend, alpha=0.5)
toolbox.register("mutate", tools.mutGaussian, mu=0, sigma=1, indpb=0.2)
toolbox.register("evaluate", lambda ind: (f1(ind), f2(ind)))
toolbox.decorate("evaluate", tools.DeltaPenalty(feasible, 1000))
toolbox.register("select", tools.selNSGA2)

population = toolbox.population(n=100)

stats = tools.Statistics(lambda ind: ind.fitness.values)
stats.register('mean', numpy.mean, axis=0)
stats.register("min", numpy.min, axis=0)
stats.register("max", numpy.max, axis=0)

result, log = algorithms.eaMuPlusLambda(population, toolbox, mu=100, lambda_=200, cxpb=0.7, mutpb=0.2, ngen=200,
stats=stats,verbose=True)

pareto_front = tools.sortNondominated(population, len(population), first_front_only=True)[0]

O1 = []
O2 = []

for ind in pareto_front:
O1 = O1 + [ind.fitness.values[0]]
O2 = O2 + [ind.fitness.values[1]]

plt.scatter(O1, O2, label='Pareto Front’,s=10, marker=".", color='darkblue')
plt.show()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant