Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
random.seed(64)
NISLES = 5
islands = [toolbox.population(n=300) for i in range(NISLES)]
# Unregister unpicklable methods before sending the toolbox.
toolbox.unregister("attr_bool")
toolbox.unregister("individual")
toolbox.unregister("population")
NGEN, FREQ = 40, 5
toolbox.register("algorithm", algorithms.eaSimple, toolbox=toolbox,
cxpb=0.5, mutpb=0.2, ngen=FREQ, verbose=False)
for i in range(0, NGEN, FREQ):
results = toolbox.map(toolbox.algorithm, islands)
islands = [pop for pop, logbook in results]
tools.migRing(islands, 15, tools.selBest)
return islands
for gen in range(20):
pop = algorithm.evolve(pop)
pop = update_fitness(pop)
records = stats.compile(pop)
logbook.record(gen=gen, **records)
front = deap.tools.ParetoFront()
front.update(pop)
app = Mock()
app.gp_runner = Mock()
app.gp_runner.pareto_front = front
app.logbook = logbook
observer(app)
best = deap.tools.selBest(pop, 1)[0]
if best.fitness.values[0] <= 1e-3:
break
# Gather all the fitnesses in one list and print the stats
fits = [ind.fitness.values[0] for ind in pop]
length = len(pop)
mean = sum(fits) / length
sum2 = sum(x*x for x in fits)
std = abs(sum2 / length - mean**2)**0.5
print(" Min %s" % min(fits))
print(" Max %s" % max(fits))
print(" Avg %s" % mean)
print(" Std %s" % std)
print("-- End of (successful) evolution --")
best_ind = tools.selBest(pop, 1)[0]
print("Best individual is %s, %s" % (best_ind, best_ind.fitness.values))
def main():
# Generate the population
pop = toolBox.toolbox.population(n=POPULATION_SIZE)
hof = tools.HallOfFame(1)
stats = tools.Statistics(lambda ind: ind.fitness.values)
stats.register("avg", numpy.mean)
stats.register("std", numpy.std)
stats.register("min", numpy.min)
stats.register("max", numpy.max)
pop, log = algorithms.eaSimple(pop, toolBox.toolbox, cxpb=CROSS_OVER_PROB,
mutpb=MUTATION_PROB, ngen=NO_OF_GENERATION, stats=stats,
halloffame=hof, verbose=True)
# The Best Individual found
best_ind = tools.selBest(pop, 1)[0]
individual = sorted(best_ind, key=itemgetter(3))
individual = sorted(individual, key=itemgetter(0))
#print("Best individual is %s, %s" % (individual, best_ind.fitness.values))
timetable = fitnessClass.genTimetable(individual)
databaseClass.insertBusTrip(timetable)
stats = tools.Statistics(lambda ind: ind.fitness.values)
stats.register("avg", np.mean)
stats.register("std", np.std)
stats.register("min", np.min)
stats.register("max", np.max)
# Get elapsed time
import time
tic = time.clock()
def get_elapsedtime(data):
return time.clock() - tic
stats.register("elapsed time",get_elapsedtime)
pop_last, logbook = algorithms.eaSimple(pop, toolbox, cxpb=0.5, mutpb=0.2, ngen=ngen, stats=stats, halloffame=hof)
best_ind = tools.selBest(pop_last, 1)[0]
return best_ind, logbook
# Gather all the fitnesses in one list and print the stats
fits = [ind.fitness.values[0] for ind in pop]
length = len(pop)
mean = sum(fits) / length
sum2 = sum(x*x for x in fits)
std = abs(sum2 / length - mean**2)**0.5
print " Min %s" % min(fits)
print " Max %s" % max(fits)
print " Avg %s" % mean
print " Std %s" % std
print "-- End of (successful) evolution --"
best_ind = tools.selBest(pop, 1)[0]
print "Best individual is %s, %s" % (best_ind, best_ind.fitness.values)
del child2.fitness.values
for mutant in offspring:
if random.random() < MUTPB:
toolbox.mutate(mutant)
del mutant.fitness.values
# The population is entirely replaced by the offspring
pop[:] = offspring
# Stop the multiprocessing pool
pool.close()
pool.join()
print("-- End of (successful) evolution --")
best_ind = tools.selBest(pop, 1)[0]
print("Best individual is %s, %s" % (best_ind, best_ind.fitness.values))
traj.f_store() # And store all the rest of the data
mate = deap.gp.cxOnePoint
expr_mut = partial(deap.gp.genFull, min_=0, max_=2)
mutate = partial(deap.gp.mutUniform, expr=expr_mut, pset=Individual.pset)
simplify = gp.individual.simplify_constant
algorithm = gp.algorithms.AgeFitness(mate, mutate, deap.tools.selNSGA2, Individual.create_population)
pop = update_fitness(Individual.create_population(pop_size))
for gen in range(20):
pop = algorithm.evolve(pop)
pop = [Individual(simplify(ind)) for ind in pop]
pop = update_fitness(pop)
best = deap.tools.selBest(pop, 1)[0]
print(gp.individual.simplify_this(best), best.fitness.values)
if best.fitness.values[0] <= 1e-3:
break
toolbox = base.Toolbox()
toolbox.register("binary", bernoulli.rvs, 0.5)
toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.binary, n=L)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
toolbox.register("mate", tools.cxOrdered)
toolbox.register("mutate", tools.mutShuffleIndexes, indpb=0.8)
toolbox.register("select", tools.selRoulette)
toolbox.register("evaluate", evaluateModel)
popl = toolbox.population(n=population_size)
result = algorithms.eaSimple(popl, toolbox, cxpb=0.4, mutpb=0.05, ngen=num_generations, verbose=True)
print(result)
# print top-3 optimal solutions
best_individuals = tools.selBest(popl, k=3)
for bi in best_individuals:
print(bi)