Skip to content

Instantly share code, notes, and snippets.

@fmder
Last active October 7, 2015 16:58
Show Gist options
  • Save fmder/7fd2e0c97ff714c19e7b to your computer and use it in GitHub Desktop.
Save fmder/7fd2e0c97ff714c19e7b to your computer and use it in GitHub Desktop.
End of evolution after 80 generations
MU = 10, LAMBDA = 100
Population of the generation number 79 :
(array('d', [0.3384816385414553, 0.2913862946297661, 0.6391631079035605, 99.78019922362375, 0.9999999999999991]), (100.78019922362375, 102.04923026469854))
(array('d', [0.12725983751030595, 0.06852642494740502, 0.2944790143036873, -80.78193202686674, 0.9999999999999988]), (-79.78193202686674, -79.29166675010535))
(array('d', [0.32497537960481293, 0.30232682417375684, 0.6611237261340428, 34.76077835597124, 0.999999999999999]), (35.76077835597124, 37.04920428588385))
(array('d', [0.12518823446474206, 0.05982068209343572, 0.2846256754372451, -24.98563318764257, 0.9999999999999986]), (-23.98563318764257, -23.515998595647147))
(array('d', [0.16447172340744912, -0.37862460917923624, 0.2425460722676009, -0.8978315694776402, 0.9999999999999983]), (0.10216843052235813, 0.13056161701817193))
(array('d', [0.17269510499090013, -0.3647635822434079, 0.27057986499062364, 8.828981876870646, 0.9999999999999986]), (9.828981876870644, 9.90749326460876))
(array('d', [0.12764146521017944, 0.07156554973724466, 0.2334363196615799, -68.24937696457144, 0.9999999999999988]), (-67.24937696457144, -66.81673362996244))
(array('d', [0.3294069995418949, 0.32964611410507216, 0.6428384312024694, 47.698431373475145, 0.999999999999999]), (48.698431373475145, 50.000322918324585))
(array('d', [0.2963294027448626, 0.3110002369345953, 0.7118619083217919, 59.825259052973365, 0.9999999999999987]), (60.825259052973365, 62.144450600974615))
(array('d', [0.11186811605848641, 0.05265496942442649, 0.2384143931077872, -47.06346204979162, 0.9999999999999987]), (-46.06346204979162, -45.66052457120092))
Best individual is array('d', [0.32711402660097877, 0.2834190205990133, 0.63746623032697, 116.03511086018636, 0.9999999999999991]), (117.03511086018636, 118.28311013771332)
Execution time : 0.561530828476 seconds ---
[Finished in 0.6s]
# -*- coding: utf-8 -*-
from __future__ import division
import time
start_time = time.time()
import random
import array
from deap import base
from deap import creator
from deap import tools
# Creation of fitness and Individual classes
creator.create("Fitness", base.Fitness, weights=(1.0, -1.0))
creator.create("Individual", array.array, typecode='d', fitness=creator.Fitness)
toolbox = base.Toolbox()
# Attribute definition
L_fen_MIN, L_fen_MAX = 0.1, 1.0
H_fen_MIN, H_fen_MAX = 0.1, 1.0
Zall_MIN, Zall_MAX = 0.1, 1.0
L_cas_MIN, L_cas_MAX = 0.1, 1.0
H_cas_MIN, H_cas_MAX = 1.0, 1.0
toolbox.register("largeur_fenetre", random.uniform, L_fen_MIN, L_fen_MAX)
toolbox.register("hauteur_fenetre", random.uniform, H_fen_MIN, H_fen_MAX)
toolbox.register("ZallegeFen", random.uniform, Zall_MIN, Zall_MAX)
toolbox.register("Longueur_casquette", random.uniform, L_cas_MIN, L_cas_MAX)
toolbox.register("Hauteur_casquette", random.uniform, H_cas_MIN, H_cas_MAX)
attr_list = []
attr_list.append(toolbox.largeur_fenetre)
attr_list.append(toolbox.hauteur_fenetre)
attr_list.append(toolbox.ZallegeFen)
attr_list.append(toolbox.Longueur_casquette)
attr_list.append(toolbox.Hauteur_casquette)
# Individuals generator
def generator (ind_class, attr_list) :
ind = ind_class(attribute() for attribute in attr_list)
return ind
toolbox.register("individual", generator, creator.Individual, attr_list)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
# definition of functions to minimize/maximize
def funcs_obj (individual):
sum1 = sum(individual)
sum2 = sum((individual[3], individual[4]))
return sum2, sum1
# Operator registering
toolbox.register("evaluate", funcs_obj)
toolbox.register("mate", tools.cxSimulatedBinary, eta=10)
toolbox.register("mutate", tools.mutGaussian, mu=0, sigma=0.1, indpb=0.05)
toolbox.register("select", tools.selNSGA2)
def main():
MU, LAMBDA = 10, 100
population = toolbox.population(n=MU)
CXPB, MUTPB, NGEN = 0.5, 0.02, 80
# Evaluate the individuals with an invalid fitness
invalid_ind = [ind for ind in population if not ind.fitness.valid]
fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
for ind, fit in zip(invalid_ind, fitnesses):
ind.fitness.values = fit
all_pop = []
# Begin the generational process
for gen in range(1, NGEN + 1):
all_pop.append(map(toolbox.clone, population))
# Vary the population
assert (CXPB + MUTPB) <= 1.0, ("The sum of the crossover and mutation "
"probabilities must be smaller or equal to 1.0.")
offspring = []
for _ in xrange(LAMBDA):
op_choice = random.random()
if op_choice < CXPB : # Apply crossover
ind1, ind2 = map(toolbox.clone, random.sample(population, 2))
ind1, ind2 = toolbox.mate(ind1, ind2)
del ind1.fitness.values
offspring.append(ind1)
elif op_choice < CXPB + MUTPB : # Apply mutation
indiv = toolbox.clone(random.choice(population))
indiv, = toolbox.mutate(indiv)
del indiv.fitness.values
offspring.append(indiv)
else: # Apply reproduction
offspring.append(random.choice(population))
# Evaluate the individuals with an invalid fitness
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
# Select the next generation population
population[:] = toolbox.select(offspring + population, MU)
print("End of evolution after %s generations" % NGEN)
print(" MU = %s, LAMBDA = %s" %(MU, LAMBDA))
for num_pop in range(NGEN-1, NGEN) :
print("Population of the generation number %s :" % num_pop)
for indiv in all_pop[num_pop] :
print(indiv, indiv.fitness.values)
best_ind = tools.selBest(population, 1)[0]
print("Best individual is %s, %s" % (best_ind, best_ind.fitness.values))
print("Execution time : %s seconds ---" % (time.time() - start_time))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment