Modifications of the mutation
This commit is contained in:
@@ -2,6 +2,7 @@ import random
|
|||||||
import time
|
import time
|
||||||
import tools
|
import tools
|
||||||
import pyviennacl as vcl
|
import pyviennacl as vcl
|
||||||
|
import numpy
|
||||||
|
|
||||||
from collections import OrderedDict as odict
|
from collections import OrderedDict as odict
|
||||||
|
|
||||||
@@ -108,45 +109,48 @@ class GeneticOperators(object):
|
|||||||
return offspring
|
return offspring
|
||||||
return wrappper
|
return wrappper
|
||||||
|
|
||||||
def mutate(self, individual):
|
def mutate(self, individual, indpb = 0.15):
|
||||||
p = random.random()
|
for i in individual:
|
||||||
coef = random.choice([2, 4])
|
if random.random() < indpb:
|
||||||
multiply_or_divide = random.choice([lambda x:x/coef, lambda x:x*coef])
|
coef = 2**(1 + numpy.random.poisson())
|
||||||
if p < 0.1 :
|
funs = [lambda x:x/coef, lambda x:x*coef]
|
||||||
individual[0] = random.choice(self.parameters[0])
|
F = random.choice(funs)
|
||||||
elif p < 0.2:
|
nF = funs[1] if F==funs[0] else funs[0]
|
||||||
idx = random.choice([1, 4])
|
#swapping-based mutations
|
||||||
nidx = 4 if idx==1 else 1
|
def m0():
|
||||||
individual[idx] = individual[idx]*coef
|
individual[1], individual[3] = individual[3], individual[1]
|
||||||
individual[nidx] = individual[nidx]/coef
|
def m1():
|
||||||
elif p < 0.3:
|
individual[4], individual[6] = individual[6], individual[4]
|
||||||
idx = random.choice([1, 4])
|
def m2():
|
||||||
individual[idx] = multiply_or_divide(individual[idx])
|
individual[9], individual[10] = individual[10], individual[9]
|
||||||
if idx==1:
|
#value modification mutations
|
||||||
individual[9] = multiply_or_divide(individual[9])
|
def m3():
|
||||||
elif p < 0.4:
|
individual[0] = random.choice(self.parameters[0])
|
||||||
idx = random.choice([3, 6])
|
def m4():
|
||||||
nidx = 6 if idx==3 else 3
|
individual[1] = F(individual[1])
|
||||||
individual[idx] = individual[idx]*coef
|
individual[9] = F(individual[9])
|
||||||
individual[nidx] = individual[nidx]/coef
|
def m5():
|
||||||
elif p < 0.5:
|
individual[2] = F(individual[2])
|
||||||
idx = random.choice([3, 6])
|
def m6():
|
||||||
individual[idx] = multiply_or_divide(individual[idx])
|
individual[3] = F(individual[3])
|
||||||
if idx==3:
|
individual[10] = F(individual[10])
|
||||||
individual[10] = multiply_or_divide(individual[10])
|
def m7():
|
||||||
elif p < 0.6:
|
individual[4] = F(individual[4])
|
||||||
individual[2] = multiply_or_divide(individual[2])
|
def m8():
|
||||||
elif p < 0.7:
|
individual[5] = F(individual[5])
|
||||||
individual[4] = multiply_or_divide(individual[4])
|
def m9():
|
||||||
elif p < 0.8:
|
individual[6] = F(individual[6])
|
||||||
individual[7] = random.choice([x for x in self.parameters[7] if x!=individual[7]])
|
def m10():
|
||||||
elif p < 0.9:
|
individual[7] = random.choice([x for x in self.parameters[7] if x!=individual[7]])
|
||||||
individual[8] = random.choice([x for x in self.parameters[8] if x!=individual[8]])
|
def m11():
|
||||||
elif p < 1:
|
individual[8] = random.choice([x for x in self.parameters[8] if x!=individual[8]])
|
||||||
idx = random.choice([9, 10])
|
def m12():
|
||||||
nidx = 10 if idx==9 else 9
|
individual[9] = F(individual[9])
|
||||||
individual[idx] = individual[idx]*coef
|
individual[10] = nF(individual[10])
|
||||||
individual[nidx] = individual[nidx]/coef
|
def m13():
|
||||||
|
individual[10] = F(individual[10])
|
||||||
|
individual[9] = nF(individual[9])
|
||||||
|
random.choice([m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12, m13])()
|
||||||
return individual,
|
return individual,
|
||||||
|
|
||||||
def evaluate(self, individual):
|
def evaluate(self, individual):
|
||||||
|
@@ -65,19 +65,16 @@ def genetic(statement, context, TemplateType, build_template, parameter_names, a
|
|||||||
toolbox.decorate("mate", gen.repair)
|
toolbox.decorate("mate", gen.repair)
|
||||||
toolbox.register("mutate", gen.mutate)
|
toolbox.register("mutate", gen.mutate)
|
||||||
toolbox.decorate("mutate", gen.repair)
|
toolbox.decorate("mutate", gen.repair)
|
||||||
toolbox.register("select", tools.selNSGA2)
|
toolbox.register("select", tools.selBest)
|
||||||
|
|
||||||
pop = toolbox.population(n=30)
|
pop = toolbox.population(n=30)
|
||||||
hof = tools.HallOfFame(1)
|
hof = tools.HallOfFame(1)
|
||||||
|
|
||||||
best_performer = lambda x: max([compute_perf(hof[0].fitness.values[0]) for t in x])
|
best_performer = lambda x: max([compute_perf(hof[0].fitness.values[0]) for t in x])
|
||||||
best_profile = lambda x: '(%s)'%','.join(map(str,hof[0]))
|
best_profile = lambda x: '(%s)'%','.join(map(str,hof[0]))
|
||||||
|
|
||||||
cxpb = 0.5
|
|
||||||
mutpb = 0.2
|
|
||||||
|
|
||||||
stats = tools.Statistics(lambda ind: ind.fitness.values)
|
stats = tools.Statistics(lambda ind: ind.fitness.values)
|
||||||
stats.register("max (" + perf_metric + ")", lambda x: max([compute_perf(hof[0].fitness.values[0]) for t in x]))
|
stats.register("max (" + perf_metric + ")", lambda x: max([compute_perf(hof[0].fitness.values[0]) for t in x]))
|
||||||
stats.register("profile ", lambda x: '(%s)'%','.join(map(str,hof[0])))
|
stats.register("profile ", lambda x: '(%s)'%','.join(map(str,hof[0])))
|
||||||
|
|
||||||
pop = eaMuPlusLambda(pop, toolbox, 30, 50, cxpb=0.2, mutpb=0.3, maxtime='5m0s', maxgen=200, halloffame=hof, compute_perf=compute_perf, perf_metric=perf_metric)
|
pop = eaMuPlusLambda(pop, toolbox, 30, 50, cxpb=0.2, mutpb=0.3, maxtime='3m0s', maxgen=200, halloffame=hof, compute_perf=compute_perf, perf_metric=perf_metric)
|
||||||
|
Reference in New Issue
Block a user