Created
May 22, 2016 01:34
-
-
Save fmder/bb535ce2ff0cdc5e09aa22f7c9c1a587 to your computer and use it in GitHub Desktop.
ParetoFront test
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
# This file is part of DEAP. | |
# | |
# DEAP is free software: you can redistribute it and/or modify | |
# it under the terms of the GNU Lesser General Public License as | |
# published by the Free Software Foundation, either version 3 of | |
# the License, or (at your option) any later version. | |
# | |
# DEAP is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU Lesser General Public License for more details. | |
# | |
# You should have received a copy of the GNU Lesser General Public | |
# License along with DEAP. If not, see <http://www.gnu.org/licenses/>. | |
import array | |
import logging | |
import random | |
import numpy | |
from deap import algorithms | |
from deap import base | |
from deap import benchmarks | |
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() | |
# Attribute generator | |
toolbox.register("attr_float", random.uniform, -5, 5) | |
# Structure initializers | |
toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_float, 3) | |
toolbox.register("population", tools.initRepeat, list, toolbox.individual) | |
def checkBounds(min, max): | |
def decorator(func): | |
def wrappper(*args, **kargs): | |
offspring = func(*args, **kargs) | |
for child in offspring: | |
for i in range(len(child)): | |
if child[i] > max: | |
child[i] = max | |
elif child[i] < min: | |
child[i] = min | |
return offspring | |
return wrappper | |
return decorator | |
toolbox.register("evaluate", benchmarks.kursawe) | |
toolbox.register("mate", lambda ind1, ind2: (ind1, ind2)) | |
toolbox.register("mutate", lambda ind: (ind,)) | |
toolbox.register("select", tools.selNSGA2) | |
toolbox.decorate("mate", checkBounds(-5, 5)) | |
toolbox.decorate("mutate", checkBounds(-5, 5)) | |
def main(): | |
random.seed(64) | |
MU, LAMBDA = 50, 100 | |
pop = toolbox.population(n=MU) | |
hof = tools.ParetoFront() | |
stats = tools.Statistics(lambda ind: ind.fitness.values) | |
stats.register("avg", numpy.mean, axis=0) | |
stats.register("std", numpy.std, axis=0) | |
stats.register("min", numpy.min, axis=0) | |
stats.register("max", numpy.max, axis=0) | |
algorithms.eaMuPlusLambda(pop, toolbox, mu=MU, lambda_=LAMBDA, | |
cxpb=0.5, mutpb=0.2, ngen=150, | |
stats=stats, halloffame=hof) | |
return pop, stats, hof | |
if __name__ == "__main__": | |
pop, stats, hof = main() | |
# import matplotlib.pyplot as plt | |
# import numpy | |
# | |
# front = numpy.array([ind.fitness.values for ind in pop]) | |
# plt.scatter(front[:,0], front[:,1], c="b") | |
# plt.axis("tight") | |
# plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment