More tuning
This commit is contained in:
@@ -13,6 +13,13 @@ from deap import tools as deap_tools
|
|||||||
|
|
||||||
from collections import OrderedDict as odict
|
from collections import OrderedDict as odict
|
||||||
|
|
||||||
|
def hamming_distance(ind1, ind2):
|
||||||
|
res = 0
|
||||||
|
for x,y in enumerate(ind1, ind2):
|
||||||
|
if x==y:
|
||||||
|
res = res + 1
|
||||||
|
return res
|
||||||
|
|
||||||
def closest_divisor(N, x):
|
def closest_divisor(N, x):
|
||||||
x_low=x_high=max(1,min(round(x),N))
|
x_low=x_high=max(1,min(round(x),N))
|
||||||
while N % x_low > 0 and x_low>0:
|
while N % x_low > 0 and x_low>0:
|
||||||
@@ -131,8 +138,7 @@ class GeneticOperators(object):
|
|||||||
maxtime = maxtime.tm_min*60 + maxtime.tm_sec
|
maxtime = maxtime.tm_min*60 + maxtime.tm_sec
|
||||||
start_time = time.time()
|
start_time = time.time()
|
||||||
|
|
||||||
mu = 70
|
mu = 30
|
||||||
_lambda = 100
|
|
||||||
cxpb = 0.2
|
cxpb = 0.2
|
||||||
mutpb = 0.7
|
mutpb = 0.7
|
||||||
|
|
||||||
@@ -146,7 +152,7 @@ class GeneticOperators(object):
|
|||||||
while time.time() - start_time < maxtime:
|
while time.time() - start_time < maxtime:
|
||||||
# Vary the population
|
# Vary the population
|
||||||
offspring = []
|
offspring = []
|
||||||
for _ in xrange(_lambda):
|
for _ in xrange(mu):
|
||||||
op_choice = random.random()
|
op_choice = random.random()
|
||||||
if op_choice < cxpb: # Apply crossover
|
if op_choice < cxpb: # Apply crossover
|
||||||
ind1, ind2 = map(self.toolbox.clone, random.sample(population, 2))
|
ind1, ind2 = map(self.toolbox.clone, random.sample(population, 2))
|
||||||
@@ -160,6 +166,7 @@ class GeneticOperators(object):
|
|||||||
offspring.append(ind)
|
offspring.append(ind)
|
||||||
else: # Apply reproduction
|
else: # Apply reproduction
|
||||||
offspring.append(random.choice(population))
|
offspring.append(random.choice(population))
|
||||||
|
|
||||||
# Evaluate the individuals with an invalid fitness
|
# Evaluate the individuals with an invalid fitness
|
||||||
invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
|
invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
|
||||||
fitnesses = self.toolbox.map(self.evaluate, invalid_ind)
|
fitnesses = self.toolbox.map(self.evaluate, invalid_ind)
|
||||||
@@ -169,6 +176,7 @@ class GeneticOperators(object):
|
|||||||
hof.update(offspring)
|
hof.update(offspring)
|
||||||
# Select the next generation population
|
# Select the next generation population
|
||||||
population[:] = self.toolbox.select(population + offspring, mu)
|
population[:] = self.toolbox.select(population + offspring, mu)
|
||||||
|
#Update
|
||||||
gen = gen + 1
|
gen = gen + 1
|
||||||
best_profile = '(%s)'%','.join(map(str,GeneticOperators.decode(hof[0])));
|
best_profile = '(%s)'%','.join(map(str,GeneticOperators.decode(hof[0])));
|
||||||
best_performance = compute_perf(hof[0].fitness.values[0])
|
best_performance = compute_perf(hof[0].fitness.values[0])
|
||||||
|
Reference in New Issue
Block a user