def main(self, checkpoint=None): state = GenState(checkpoint=checkpoint, sheep=self.s_toolbox.population(n=self.SHEEP_COUNT), wolfs=self.w_toolbox.population(n=self.WOLFS_COUNT)) self.evaluate_population(state.sheep, state.wolfs) state.log_population(0, self.v) state.checkpoint(0) for g in range(1, self.MAX_GEN): state.sheep = self.s_toolbox.select(state.sheep, len(state.sheep)) state.wolfs = self.w_toolbox.select(state.wolfs, len(state.wolfs)) state.sheep = algorithms.varAnd( state.sheep, self.common_tbx, self.S_CXPB, 0 if self.SHEEP_LAMARCK else self.S_MUTPB) state.wolfs = algorithms.varAnd( state.wolfs, self.common_tbx, self.W_CXPB, 0 if self.WOLFS_LAMARCK else self.W_MUTPB) self.evaluate_population(state.sheep, state.wolfs) state.log_population(g, self.v) state.checkpoint(g) if self.v: print(state.s_hof) print(state.w_hof) return state
def gen_opt(): creator.create("FitnessMax", base.Fitness, weights=(1.0, )) creator.create("Individual", list, fitness=creator.FitnessMax) toolbox = base.Toolbox() toolbox.register("population_guess", initPopulation, list, creator.Individual) # toolbox.register("evaluate", async_score) toolbox.register("mate", tools.cxTwoPoint) toolbox.register("mutate", tools.mutGaussian, mu=0, sigma=0.05, indpb=0.1) toolbox.decorate("mate", checkBounds(0, 1)) toolbox.decorate("mutate", checkBounds(0, 1)) # toolbox.register("select", tools.selTournament, tournsize=3) population = toolbox.population_guess() update_id("experiment") for gen in range(NGEN): update_id("session") print("New Generation: {}/{}".format(gen, NGEN)) offspring1 = algorithms.varAnd(random.sample(population, k=MU), toolbox, cxpb=CXPB, mutpb=MUTPB) offspring2 = algorithms.varAnd(population, toolbox, cxpb=CXPB, mutpb=MUTPB) population += offspring1 + offspring2 # 1st Turn best = tournament(population, T1_POOL, T1_WIN) # 2nd Turn population = tournament(best, T2_POOL, T2_WIN, boss=True)
def main(): random.seed(64) hosts = htoolbox.population(n=300) parasites = ptoolbox.population(n=300) hof = tools.HallOfFame(1) hstats = tools.Statistics(lambda ind: ind.fitness.values) hstats.register("avg", tools.mean) hstats.register("std", tools.std) hstats.register("min", min) hstats.register("max", max) column_names = ["gen", "evals"] column_names.extend(hstats.functions.keys()) logger = tools.EvolutionLogger(column_names) logger.logHeader() MAXGEN = 50 H_CXPB, H_MUTPB = 0.5, 0.3 P_CXPB, P_MUTPB = 0.5, 0.3 fits = htoolbox.map(htoolbox.evaluate, hosts, parasites) for host, parasite, fit in zip(hosts, parasites, fits): host.fitness.values = parasite.fitness.values = fit hof.update(hosts) hstats.update(hosts) logger.logGeneration(gen=0, evals=len(hosts), stats=hstats) for g in range(1, MAXGEN): hosts = htoolbox.select(hosts, len(hosts)) parasites = ptoolbox.select(parasites, len(parasites)) hosts = algorithms.varAnd(hosts, htoolbox, H_CXPB, H_MUTPB) parasites = algorithms.varAnd(parasites, ptoolbox, P_CXPB, P_MUTPB) fits = htoolbox.map(htoolbox.evaluate, hosts, parasites) for host, parasite, fit in zip(hosts, parasites, fits): host.fitness.values = parasite.fitness.values = fit hof.update(hosts) hstats.update(hosts) logger.logGeneration(gen=g, evals=len(hosts), stats=hstats) best_network = sn.SortingNetwork(INPUTS, hof[0]) print(best_network) print(best_network.draw()) print("%i errors" % best_network.assess()) return hosts, hstats, hof
def main(): random.seed(64) hosts = htoolbox.population(n=300) parasites = ptoolbox.population(n=300) hof = tools.HallOfFame(1) hstats = tools.Statistics(lambda ind: ind.fitness.values) hstats.register("avg", numpy.mean) hstats.register("std", numpy.std) hstats.register("min", numpy.min) hstats.register("max", numpy.max) logbook = tools.Logbook() logbook.header = "gen", "evals", "std", "min", "avg", "max" MAXGEN = 50 H_CXPB, H_MUTPB = 0.5, 0.3 P_CXPB, P_MUTPB = 0.5, 0.3 fits = htoolbox.map(htoolbox.evaluate, hosts, parasites) for host, parasite, fit in zip(hosts, parasites, fits): host.fitness.values = parasite.fitness.values = fit hof.update(hosts) record = hstats.compile(hosts) logbook.record(gen=0, evals=len(hosts), **record) print(logbook.stream) for g in range(1, MAXGEN): hosts = htoolbox.select(hosts, len(hosts)) parasites = ptoolbox.select(parasites, len(parasites)) hosts = algorithms.varAnd(hosts, htoolbox, H_CXPB, H_MUTPB) parasites = algorithms.varAnd(parasites, ptoolbox, P_CXPB, P_MUTPB) fits = htoolbox.map(htoolbox.evaluate, hosts, parasites) for host, parasite, fit in zip(hosts, parasites, fits): host.fitness.values = parasite.fitness.values = fit hof.update(hosts) record = hstats.compile(hosts) logbook.record(gen=g, evals=len(hosts), **record) print(logbook.stream) best_network = sn.SortingNetwork(INPUTS, hof[0]) print(best_network) print(best_network.draw()) print("%i errors" % best_network.assess()) return hosts, logbook, hof
def run_algorithm(pop, cxpb, mutpb, ngen, stats): for g in range(ngen): state.generation = g settings.output_statistics.write('\nGeneration {}\n\n'.format(g)) offspring = map(toolbox.clone, toolbox.select(pop, len(pop))) offspring = algorithms.varAnd(offspring, toolbox, cxpb, mutpb) invalid_ind = [ind for ind in offspring if not ind.fitness.valid] fitnesses = [] for i, ind in enumerate(invalid_ind): state.individual = i fitness = toolbox.evaluate(ind) fitnesses.append(fitness) settings.output_statistics.write( 'Individiaul {} Fitness {} Price {} Edge {} Offset {}\n'.format( i, fitness, ind[0], state.edges_list[int(ind[1]) % len(state.edges)], ind[2])) settings.output_statistics.flush() for ind, fit in zip(invalid_ind, fitnesses): ind.fitness.values = fit pop[:] = offspring nevals = len(fitness) record = stats.compile(pop) logbook.record(gen=g, evals=nevals, **record) settings.output_statistics.write('\n\n' + str(logbook) + '\n\n') settings.output_statistics.flush()
def modifiedGA(population, toolbox, hallOfFame, n_neurons, trialAverage, trialBest, cxpb, mutpb, ngen, game): # Begin the generational process for gen in range(1, ngen + 1): # Select the next generation individuals offspring = toolbox.select(population, len(population) - 1) offspring.append(hallOfFame[0]) # Vary the pool of individuals offspring = algorithms.varAnd(offspring, toolbox, cxpb, mutpb) # Replace the current population by the offspring population[:] = offspring # MODIFIED BLOCK: Adds new agents based on the new offspring and goes through a game loop to set fitness values game.reset() for i in range(len(population)): game.add_agent(Brain(population[i], n_neurons)) game.game_loop(display=False) totalFitness = 0 largest = 0 for i in range(c.game['n_agents']): totalFitness += g.agents[i].fitness if g.agents[i].fitness > largest: largest = g.agents[i].fitness averageFitness = totalFitness / c.game['n_agents'] trialAverage.append(averageFitness) trialBest.append(largest) game.generation += 1 return population
def propagate(gens, ind, toolbox, pool, pop_size, cxpb, mutpb): working_gen = gens[ind.gen] asp_idx = [i for i, (a1, a2) in enumerate(working_gen.tournament_aspirants) if a1 == ind.pop_idx or a2 == ind.pop_idx] for a in asp_idx: a1, a2 = working_gen.tournament_aspirants[a] if working_gen.pop[a1] and working_gen.pop[a2] and working_gen.pop[a1].fitness.valid and working_gen.pop[a2].fitness.valid: working_gen.selected[a] = max(working_gen.pop[a1], working_gen.pop[a2], key=attrgetter('fitness')) if a % 2 == 0: start, stop = a, a + 2 else: start, stop = a - 1, a + 1 if working_gen.selected[start] is not None and working_gen.selected[stop - 1] is not None and working_gen.offspring[start] is None and working_gen.offspring[stop - 1] is None: working_gen.offspring[start:stop] = map(toolbox.clone, working_gen.selected[start:stop]) working_gen.offspring[start:stop] = algorithms.varAnd(working_gen.offspring[start:stop], toolbox, cxpb, mutpb) if len(gens) <= ind.gen + 1: gens.append(Generation(pop_size, tournament_aspirants=utils.generate_aspirant_pairs(pop_size), pop=[None] * pop_size)) gens[ind.gen + 1].pop[start:stop] = working_gen.offspring[start:stop] # comma selection for idx, i in enumerate(gens[ind.gen + 1].pop[start:stop]): i.pop_idx = start + idx i.gen = ind.gen + 1 if not i.fitness.valid and i.pop_idx in gens[i.gen].aspirants: pool.submit(i, order=(i.gen, gens[i.gen].aspirant_order[i.pop_idx])) gens[i.gen].waiting.add(i.pop_idx) else: propagate(gens, i, toolbox, pool, pop_size, cxpb, mutpb)
def GA(toolbox, cxpb=0.5, mutpb=0.2, ngen=40, mu=2000, *args, **kwargs): ''' ''' stats, logbook, record, pop, hof = initMisc(toolbox, npop=mu, nhof=100, similar=np.array_equal) # Begin the generational process for gen in range(1, ngen + 1): # Select the next generation individuals offspring = toolbox.select(pop, len(pop)) # Vary the pool of individuals offspring = varAnd(offspring, toolbox, cxpb, mutpb) # 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 # Update the hall of fame with the generated individuals hof.update(offspring) if kwargs.has_key('callback'): kwargs['callback'](1, p=hof[0]) # Replace the current population by the offspring pop[:] = offspring # Append the current generation statistics to the logbook record = stats.compile(pop) if stats else {} logbook.record(gen=gen, nevals=len(invalid_ind), **record) print logbook.stream print "Best individual is ", hof[0].fitness.values[0] if len(hof[0]) < 20: print hof[0] return {'x':hof[0], 'population':pop, 'stats':stats, 'hof':hof, }
def eaSimplePlusElitism(population, toolbox, cxpb, mutpb, eprop, ngen, stats=None, halloffame=None, verbose=__debug__): """ Variante de `deap.algorithms.eaSimple` con una proporción de elitismo. """ logbook = tools.Logbook() logbook.header = ['gen', 'nevals'] + (stats.fields if stats else []) # Evalua los individuos con aptitud inválida. 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 if halloffame is not None: halloffame.update(population) record = stats.compile(population) if stats else {} logbook.record(gen=0, nevals=len(invalid_ind), **record) if verbose: print(logbook.stream) # Comienza el proceso evolutivo. for gen in range(1, ngen + 1): # Seleccina la próxima generación de individuos. offspring = toolbox.select(population, len(population)) # Varia el pool de individuos, aplicando cruza y mutación. offspring = algorithms.varAnd(offspring, toolbox, cxpb, mutpb) # Evalua los individuos con aptitud inválida. 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 # Actualiza el grupo de mejores individuos. if halloffame is not None: halloffame.update(offspring) # Reemplaza la población actual con los mejores del conjunto # compuesta por su descendencia y la elite. elite_count = int(len(population) * eprop) elite = tools.selBest(population, elite_count) population[:] = tools.selBest(offspring + elite, len(population)) # Toma nota de las estadísticas de la generación actual. record = stats.compile(population) if stats else {} logbook.record(gen=gen, nevals=len(invalid_ind), **record) if verbose: print(logbook.stream) return population, logbook
def DoSingleIterGA_SHGMaximize (self, remeasure=True) : """ Perform a single iteration of GA to find phase of laser pulse that coherently suppresses single sample fluorescence """ wx.Yield() # abort, if requested if self.need_abort : return if remeasure : # Record spectrum self.optimization_pop = self.MeasureSpectra(self.optimization_pop) wx.Yield() # abort, if requested if self.need_abort : return # Calculate the fitness the population for ind in self.optimization_pop : ind.fitness.values = (ind.spectra.sum(),) self.optimization_pop[:] = self.optimization_toolbox.select( self.optimization_pop, self.population_size ) else : # Generate offspring offspring = algorithms.varAnd(self.optimization_pop, self.optimization_toolbox, self.cxpb, self.mutpb) # Measure spectra for offspring only offspring = self.MeasureSpectra(offspring) wx.Yield() # abort, if requested if self.need_abort : return # Calculate the fitness of offspring for ind in offspring : ind.fitness.values = (ind.spectra.sum(),) self.optimization_pop[:] = self.optimization_toolbox.select( self.optimization_pop + offspring, int(1.2*self.population_size) ) wx.Yield() # abort, if requested if self.need_abort : return # Saving statistics for plotting purposes for key, value in self.optimization_stats.compile(self.optimization_pop).items() : try : self.optimization_log[key].append(value) except KeyError : self.optimization_log[key] = [value] self.SaveOptimization() self.DisplayOptimization() # Going to the next iteration wx.CallAfter (self.DoSingleIterGA_SHGMaximize, remeasure=(not remeasure))
def generate_word(word, letters, population_size): creator.create("Fitness", base.Fitness, weights=(-1.0,)) creator.create("Individual", list, fitness=creator.Fitness) toolbox = base.Toolbox() toolbox.register("attr_bool", generate_letter, letters=letters) toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_bool, n=len(word)) toolbox.register("population", tools.initRepeat, list, toolbox.individual) toolbox.register("evaluate", evaluate_alphabet_distance, word=word, alphabet=letters) toolbox.register("mate", tools.cxTwoPoint) toolbox.register("mutate", mutInAlphabet, letters=letters, max_step=3, indpb=0.05) toolbox.register("select", tools.selTournament, tournsize=3) population = toolbox.population(n=population_size) generation = 0 while have_not_found_yet(population, generation, word): offspring = algorithms.varAnd(population, toolbox, cxpb=0.5, mutpb=0.1) fits = toolbox.map(toolbox.evaluate, offspring) for fit, ind in zip(fits, offspring): ind.fitness.values = fit population = toolbox.select(offspring, k=len(population)) generation += 1 if generation % 100 == 0: print(generation) print(generation) print(tools.selBest(population, k=1)[0])
def _do_generation(self, pop, gen_num, logbook): """Implement a generation of the search process. :param pop: The population :type pop: Any iterable type :param gen_num: The generation number :type gen_num: :py:class:`int` :param logbook: Logbook for the stats :type logbook: :py:class:`~deap.tools.Logbook` """ # Select the next generation individuals offspring = self._toolbox.select(pop, self.pop_size) # Vary the pool of individuals offspring = varAnd(offspring, self._toolbox, self.xover_pb, self.mut_pb) # Evaluate the individuals with an invalid fitness num_evals = self._eval(offspring) # Replace the current population by the offspring pop[:] = offspring # Append the current generation statistics to the logbook self._do_stats(pop, gen_num, num_evals, logbook)
def _execute(self) -> list: """ Запуск алгоритма. Потомок offspring - с вероятностью cxpb 2 подряд идущих в списке индивида скрещиваются и их дети заменяют родителей, а с вероятностью mutpb после процесса скрещивания каждый индивид мутирует. Далее рассчитывается ценность каждого потомка и рассчитанные ценности заполняют поля ценности (values) в соответствующих индивидах. Затем отбор нового поколения. В конце - выбор лучшего :return: """ population = self._toolbox.population(n=self.START_POPULATION_SIZE) for gen in range(self.GENERATION_COUNT): offspring = algorithms.varAnd(population, self._toolbox, cxpb=self.CROSSOVER_CHANCE, mutpb=self.MUTATE_CHANCE) fits = self._toolbox.map(self._toolbox.evaluate, offspring) for fit, ind in zip(fits, offspring): ind.fitness.values = fit population = self._toolbox.select(offspring, k=len(population)) top1 = tools.selBest(population, k=1) return top1[0]
def _run_evolution(self): creator.create("FitnessMax", base.Fitness, weights=(1.0, )) creator.create("Individual", list, fitness=creator.FitnessMax) toolbox = base.Toolbox() toolbox.register("attr_bool", random.randint, 0, 1) toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_bool, n=100) toolbox.register("population", tools.initRepeat, list, toolbox.individual) toolbox.register("evaluate", self._eval_evolution_by_loss) toolbox.register("mate", tools.cxTwoPoint) toolbox.register("mutate", tools.mutFlipBit, indpb=0.05) toolbox.register("select", tools.selTournament, tournsize=3) population = toolbox.population(n=300) NGEN = 40 for gen in range(NGEN): offspring = algorithms.varAnd(population, toolbox, cxpb=0.5, mutpb=0.1) fits = toolbox.map(toolbox.evaluate, offspring) for fit, ind in zip(fits, offspring): ind.fitness.values = fit population = toolbox.select(offspring, k=len(population)) top10 = tools.selBest(population, k=10) return top10
def approximate(func, base_range, bounds, population_size, generations): def evaluation_func(individual): score = 0.0 for idx, x in enumerate(base_range): score += abs(func(x) - individual[idx]) return score, creator.create("Fitness", base.Fitness, weights=(-1.0,)) creator.create("Individual", list, fitness=creator.Fitness) toolbox = base.Toolbox() toolbox.register("attr_bool", float_between, *bounds) toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_bool, n=len(base_range)) toolbox.register("population", tools.initRepeat, list, toolbox.individual) toolbox.register("evaluate", evaluation_func) toolbox.register("mate", tools.cxTwoPoint) toolbox.register("mutate", tools.mutFlipBit, indpb=0.05) toolbox.register("select", tools.selTournament, tournsize=3) population = toolbox.population(n=population_size) for gen in range(generations): offspring = algorithms.varAnd(population, toolbox, cxpb=0.5, mutpb=0.1) fits = toolbox.map(toolbox.evaluate, offspring) for fit, ind in zip(fits, offspring): ind.fitness.values = fit population = toolbox.select(offspring, k=len(population)) show_best_individual(base_range, func, population)
def run_evolution(generations=10): toolbox.register("evaluate", eval_diff_fitness) toolbox.register("mate", tools.cxTwoPoint) toolbox.register("mutate", tools.mutFlipBit, indpb=0.05) toolbox.register("select", tools.selTournament, tournsize=3) population = toolbox.population(n=20) fit_history = [] for gen in range(generations): print(f'gen # {gen}') offspring = algorithms.varAnd(population, toolbox, cxpb=0.5, mutpb=0.1) fits = toolbox.map(toolbox.evaluate, offspring) for fit, ind in zip(fits, offspring): ind.fitness.values = fit population = toolbox.select(offspring, k=len(population)) fit_values = [] for ind in population: fitness = ind.fitness.values fit_values.append(fitness) fit_history.append(np.average(fit_values)) top10 = tools.selBest(population, k=10) return top10, fit_history
def learn(self): """ Runs genetic algorithms. By using DEAP, generates an initial population and runs for n generations. Returns: A dict with the features weight and others params. """ solution = {} # Generate Population population = self.deap_toolbox.population(n=self.population) # Evolve for gen in range(self.generations): offspring = algorithms.varAnd(population, self.deap_toolbox, cxpb=0.5, mutpb=0.1) fits = self.deap_toolbox.map(self.deap_toolbox.evaluate, offspring) for fit, ind in zip(fits, offspring): ind.fitness.values = fit population = self.deap_toolbox.select(offspring, k=len(population)) # Select best best = tools.selBest(population, k=1)[0] best_fitness, = self.fitness_wrapper(best) revisions_weight, fixes_weight, authors_weight = self.decode_individual(best) solution["revisions"] = revisions_weight solution["fixes"] = fixes_weight solution["authors"] = authors_weight solution["fitness"] = best_fitness solution["bits"] = self.bits solution["generations"] = self.generations return solution
def run(self): breakEarly = False for gen in range(1, self.NGEN + 1): try: if breakEarly: print("Stopped by user!") break offsprings = algorithms.varAnd(self.population, self.toolbox, cxpb=MathFunGA.CXPB, mutpb=MathFunGA.MUTPB) fitnesses = self.toolbox.map(self.toolbox.evaluate, offsprings) for fit, ind in zip(fitnesses, offsprings): ind.fitness.values = fit self.population = self.toolbox.select(offsprings, k=len(self.population)) theBestThisPop = tools.selBest(self.population, k=1)[0] theBestFitness = theBestThisPop.fitness.getValues()[0] print((str(gen) + ":").ljust(5) + str(theBestFitness)) if theBestFitness < MathFunGA.FITNESS_TARGET: print("Satisfying fitness reached!") break except KeyboardInterrupt: breakEarly = True else: print("Maximum number of iterations reached!") self.showResults()
def seqSelectionRound(sess, population, toolbox, args): offspring = algorithms.varAnd(population, toolbox, cxpb=args['cspb'], mutpb=args['mutpb']) fits = evaluateThese(sess, offspring,args); for fit, ind in zip(fits, offspring): ind.fitness.values = fit population = toolbox.select(offspring, k=len(population)) return (population);
def run_algorithm(pop, cxpb, mutpb, ngen, stats): for g in range(ngen): state.generation = g settings.output_statistics.write('\nGeneration {}\n\n'.format(g)) offspring = map(toolbox.clone, toolbox.select(pop, len(pop))) offspring = algorithms.varAnd(offspring, toolbox, cxpb, mutpb) invalid_ind = [ind for ind in offspring if not ind.fitness.valid] fitnesses = [] for i, ind in enumerate(invalid_ind): state.individual = i fitness = toolbox.evaluate(ind) fitnesses.append(fitness) settings.output_statistics.write( 'Individiaul {} Fitness {} Price {} Edge {} Offset {}\n'. format(i, fitness, ind[0], state.edges_list[int(ind[1]) % len(state.edges)], ind[2])) settings.output_statistics.flush() for ind, fit in zip(invalid_ind, fitnesses): ind.fitness.values = fit pop[:] = offspring nevals = len(fitness) record = stats.compile(pop) logbook.record(gen=g, evals=nevals, **record) settings.output_statistics.write('\n\n' + str(logbook) + '\n\n') settings.output_statistics.flush()
def update(self, *args): self.canvas.clear() self.epoch += 1 top10 = tools.selBest(self.population, k=1) elite = top10[0] offspring = algorithms.varAnd(self.population, toolbox, cxpb=1.0, mutpb=0.7) fits = list(toolbox.map(toolbox.evaluator, offspring)) for fit, ind in zip(fits, offspring): ind.fitness.values = [fit] self.population = toolbox.select(offspring, k=self.pops) self.population[0] = elite top10 = tools.selBest(self.population, k=1) top_v, top = eval_f(top10[0], True) with self.canvas: Color(rgba=(1, 0.8, 0.1, 0.7)) for x, y in zip(top[0], top[1]): Ellipse(pos=(x, y), size=(5, 5)) self.label = Label(text="{:.2f} : {}".format( float(top_v), self.epoch), halign="right")
def eaSimpleConverge(population, toolbox, cxpb, mutpb, ngen, stats=None, halloffame=None, verbose=True): """This algorithm reproduce the simplest evolutionary algorithm as presented in chapter 7 of [Back2000]_. Modified to allow checking if there is no change for ngen, as a simple rule for convergence. Interface is similar to eaSimple(). However, in eaSimple, ngen is total number of iterations; in eaSimpleConverge, we terminate only when the best is NOT updated for ngen iterations. """ # 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 if halloffame is not None: halloffame.update(population) record = stats.compile(population) if stats else {} # Begin the generational process gen = 1 best = 0 while True: # Select the next generation individuals offspring = toolbox.select(population, len(population)) # Vary the pool of individuals offspring = varAnd(offspring, toolbox, cxpb, mutpb) # 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 # Update the hall of fame with the generated individuals if halloffame is not None: halloffame.update(offspring) # Replace the current population by the offspring population[:] = offspring # Append the current generation statistics to the logbook record = stats.compile(population) if stats else {} current_best = record['max'] if gen % 20 == 0 and verbose: print >> sys.stderr, "Current iteration {0}: max_score={1}".\ format(gen, current_best) if current_best != best: best = current_best updated = gen gen += 1 if gen - updated > ngen: break return population
def test_pheromone_mode(self): toolbox = self.toolbox toolbox.register("select", tigerMosquitoSelection, tournsize=3, useFitness=False) population = toolbox.population(n=3) # population size = 3 offspring = algorithms.varAnd(population, toolbox, cxpb=0., mutpb=0.) # set pheromone each individual pheromones = [50, 100, 75] # evaluation fits = toolbox.map(toolbox.evaluate, offspring) max_p = -1 for i, fit_ind in enumerate(zip(fits, offspring)): fit, ind = fit_ind ind.fitness.values = fit ind.pheromone.values = (pheromones[i], ) if max_p <= pheromones[i]: max_p = pheromones[i] max_sample = ind random.seed(20180719) selected_ind = toolbox.select(offspring, k=1) self.assertEqual([[1, 1, 0]], selected_ind)
def compose(toolbox, window, tonic): global chords_in_vector population = toolbox.population(n=pop_size) NGEN = num_gens for gen in range(NGEN): offspring = algorithms.varAnd(population, toolbox, cxpb=cx_pb, mutpb=mut_pb) fits = toolbox.map(toolbox.evaluate, offspring) total_fitness = 0 for fit, ind in zip(fits, offspring): ind.fitness.values = fit total_fitness += fit population = toolbox.select(offspring, k=len(population)) #print("Total fitness in gen %s : %s" % (gen, total_fitness)) #print("Best : %s" % evaluate(tools.selBest(population, k=1)[0])) top = tools.selBest(population, k=1)[0] np.set_printoptions(threshold=np.nan) #pprint("%s, %s" % (window, top)) #pprint(evaluate(top)) return top, evaluate(top)
def eaSimpleWithElitism(population, toolbox, cxpb, mutpb, ngen, stats=None, halloffame=None, verbose=__debug__): #подготовка исходных данных для генерации logbook = tools.Logbook() logbook.header = ['gen', 'nevals'] + (stats.fields if stats else []) # сравнение единиц с не валидным фитнес значением 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 if halloffame is None: raise ValueError("halloffame parameter must not be empty!") halloffame.update(population) hof_size = len(halloffame.items) if halloffame.items else 0 record = stats.compile(population) if stats else {} logbook.record(gen=0, nevals=len(invalid_ind), **record) if verbose: print(logbook.stream) # Начало процесса генерации for gen in range(1, ngen + 1): # выборка следующего поколения единиц offspring = toolbox.select(population, len(population) - hof_size) #отклонение поколения offspring = algorithms.varAnd(offspring, toolbox, cxpb, mutpb) # сравнение единиц с не валидным фитнес значением 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 # возвращение лучшего значения популяции offspring.extend(halloffame.items) # дополненией результата новым поколением halloffame.update(offspring) #замещение старой популяции новой population[:] = offspring # обновление и вывод статистики record = stats.compile(population) if stats else {} logbook.record(gen=gen, nevals=len(invalid_ind), **record) if verbose: print(logbook.stream) return population, logbook
def nsga(population, start_gen, toolbox, cxpb, mutpb, ngen, stats, halloffame, logbook, verbose, exp_id=None): eval_invalid_inds = ( eval_invalid_inds_single_gpu if Config.GPU else eval_invalid_inds_cpu_parallel ) popsize = len(population) total_time = datetime.timedelta(seconds=0) eval_invalid_inds(population, toolbox) for gen in range(start_gen, ngen): start_time = datetime.datetime.now() offspring = algorithms.varAnd(population, toolbox, cxpb=cxpb, mutpb=mutpb) evals = eval_invalid_inds(offspring, toolbox) population = toolbox.select(population+offspring, k=popsize) halloffame.update(offspring) # updates pareto front # update statics record = stats.compile(population) logbook.record(gen=gen, evals=evals, **record) if verbose: print(logbook.stream, flush=True) # save checkpoint if gen % 1 == 0: # Fill the dictionary using the dict(key=value[, ...]) constructor cp = dict( population=population, generation=gen, halloffame=halloffame, logbook=logbook, rndstate=random.getstate(), config=Config, ) if exp_id is None: cp_name = "checkpoint_nsga.pkl" else: cp_name = "checkpoint_nsga_{}.pkl".format(exp_id) pickle.dump(cp, open(cp_name, "wb")) # check hard time limit gen_time = datetime.datetime.now() - start_time total_time = total_time + gen_time print("Time ", total_time) # hard time limit was necessary at metacentrum # # if total_time > datetime.timedelta(hours=4*24): # print("Time limit exceeded.") # break return population, logbook
def runExample(): args = {'sequence_length' : 110 , 'nucleotide_frequency' : [0.25,0.25,0.25,0.25], 'mutation_frequency' : 0.05 } np.random.seed(1239571243); randomizer=np.random creator.create("FitnessMax", base.Fitness, weights=(1.0,)) creator.create("Individual", list , fitness=creator.FitnessMax) toolbox = base.Toolbox() toolbox.register("base", getRandomBase, randomizer, args) toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.base, n=args['sequence_length']) toolbox.register("population", tools.initRepeat, list, toolbox.individual) toolbox.register("evaluate", fitness, "ATGC") #toolbox.register("evaluate", fitness) toolbox.register("mate", tools.cxTwoPoint) toolbox.register("mutate", mutateSequence, randomizer, args) #toolbox.register("mutate", mutateSequence, args['mutation_frequency']) toolbox.register("select", tools.selTournament, tournsize=3) population = toolbox.population(n=300) NGEN=1000 for gen in range(NGEN): offspring = algorithms.varAnd(population, toolbox, cxpb=0.1, mutpb=0.1) #offspring = algorithms.varAnd(population, toolbox, cxpb=0.1) fits = toolbox.map(toolbox.evaluate, offspring) print(type(fits)) for fit, ind in zip(fits, offspring): ind.fitness.values = fit population = toolbox.select(offspring, k=len(population)) top10 = tools.selBest(population, k=10) return(top10);
def findNominalValues(self): nominalVals = [] for evalMode in self.model.modes: nominalValsMode = [] self.toolbox.register("evaluate", evalMode) #initialize new random population self.popu = self.toolbox.population(self.populationSize) for gen in range(self.NGEN): print(gen) #generate offspprings with crossover and mutations offspring = algorithms.varAnd(self.popu, self.toolbox, cxpb=0.5, mutpb=0.75) #evaluate individuals fits = self.toolbox.map(self.toolbox.evaluate, offspring) for fit, ind in zip(fits, offspring): if self.model.isViable(ind) and ind not in nominalValsMode: nominalValsMode.append(ind) ind.fitness.values = fit #roulete wheel selection self.popu = self.toolbox.select(offspring, k=len(self.popu)) print("Number of viable points: " + str(len(nominalValsMode))) nominalVals.extend(nominalValsMode) return nominalVals
def geneticAlgorithmWithEvalFunctional(base_func, eval_func, search_region, population_size, generations, **args): creator.create("Fitness", base.Fitness, weights=(-1.0,)) creator.create("Individual", list, fitness=creator.Fitness) toolbox = base.Toolbox() toolbox.register("attr_bool", float_between, *search_region) toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_bool, n=1) toolbox.register("population", tools.initRepeat, list, toolbox.individual) toolbox.register("evaluate", eval_func, **args) toolbox.register("mate", skippingCrosover) (a, b) = search_region toolbox.register("mutate", mutInRegion, a=a, b=b, max_step=0.1, indpb=0.05) toolbox.register("select", tools.selTournament, tournsize=3) population = toolbox.population(n=population_size) for gen in range(generations): offspring = algorithms.varAnd(population, toolbox, cxpb=0.5, mutpb=0.1) fits = toolbox.map(toolbox.evaluate, offspring) for fit, ind in zip(fits, offspring): ind.fitness.values = fit population = toolbox.select(offspring, k=len(population)) print(tools.selBest(population, k=1)) base_range = float_range(*search_region) plot(base_range, [base_func(x) for x in base_range], 'r') show()
def GetBetters(self): self.toolbox.register("evaluate", self.EvalOneMax) self.toolbox.register("mate", tools.cxTwoPoint) # crossover self.toolbox.register("mutate", tools.mutFlipBit, indpb=0.05) # mutação self.toolbox.register("select", tools.selTournament, tournsize=3) population = self.toolbox.population(n=300) NGEN = 40 for gen in range(NGEN): offspring = algorithms.varAnd(population, self.toolbox, cxpb=0.5, mutpb=0.1) fits = self.toolbox.map(self.toolbox.evaluate, offspring) for fit, ind in zip(fits, offspring): if fit['weight'] > self.limit: ind.fitness.values = [0] else: ind.fitness.values = fit['value'] population = self.toolbox.select(offspring, k=len(population)) betters = tools.selBest(population, k=10)[0] result = [] for key, value in enumerate(betters): if value == 1: result.append(self.products[key]) return {"products": result, "state": self.EvalOneMax(betters)}
def startalgrithm(topk): liststring = input("pls input the list, saperate with , :") for item in liststring.split(','): listinput.append(int(item)) creator.create("FitnessMax", base.Fitness, weights=(1,)) #weights creator.create("Individual", list, fitness=creator.FitnessMax) toolbox = base.Toolbox() toolbox.register("attr_bool", random.randint, -1, 1) # value range toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_bool, n=len(listinput)) #number of parameters toolbox.register("population", tools.initRepeat, list, toolbox.individual) toolbox.register("evaluate", evalOneMax) toolbox.register("mate", tools.cxTwoPoint) toolbox.register("mutate", tools.mutFlipBit, indpb=0.05) toolbox.register("select", tools.selTournament, tournsize=3) population = toolbox.population(n=300) NGEN=40 for gen in range(NGEN): offspring = algorithms.varAnd(population, toolbox, cxpb=0.5, mutpb=0.1) fits = toolbox.map(toolbox.evaluate, offspring) for fit, ind in zip(fits, offspring): ind.fitness.values = fit population = toolbox.select(offspring, k=len(population)) topk = input("pls input the number of result:") top2 = tools.selBest(population, k=int(topk)) #top1 return top2
def example_nevzpominam(): # Creating appropriate type creator.create("FitnessMax", base.Fitness, weights=(1.0,)) creator.create("Individual", list, fitness=creator.FitnessMax) # Initialization IND_SIZE = 100 toolbox = base.Toolbox() toolbox.register("attr_bool", random.randint, 0, 1) toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_bool, n=IND_SIZE) toolbox.register("population", tools.initRepeat, list, toolbox.individual) toolbox.register("evaluate", evalOneMax) toolbox.register("mate", tools.cxTwoPoint) toolbox.register("mutate", tools.mutFlipBit, indpb=0.05) toolbox.register("select", tools.selTournament, tournsize=3) population = toolbox.population(n=300) NGEN=40 for gen in range(NGEN): offspring = algorithms.varAnd(population, toolbox, cxpb=0.5, mutpb=0.1) fits = toolbox.map(toolbox.evaluate, offspring) for fit, ind in zip(fits, offspring): ind.fitness.values = fit population = toolbox.select(offspring, k=len(population)) top10 = tools.selBest(population, k=10)
def genetic(self, n_epochs=40): creator.create("FitnessMax", base.Fitness, weights=(-1.0, )) creator.create("Individual", np.ndarray, fitness=creator.FitnessMax) toolbox = base.Toolbox() toolbox.register("attr_float", np.random.random) toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_float, n=self.M) toolbox.register("population", tools.initRepeat, list, toolbox.individual) def eval_def(individual): return self.rmse_loss(self.X_train @ individual), toolbox.register("evaluate", eval_def) toolbox.register("mate", tools.cxTwoPoint) toolbox.register("mutate", tools.mutFlipBit, indpb=0.05) toolbox.register("select", tools.selTournament, tournsize=3) population = toolbox.population(n=300) for gen in range(n_epochs): offspring = algorithms.varAnd(population, toolbox, cxpb=0.5, mutpb=0.1) fits = toolbox.map(toolbox.evaluate, offspring) for fit, ind in zip(fits, offspring): ind.fitness.values = fit population = toolbox.select(offspring, k=len(population)) self.w = tools.selBest(population, k=1)[0]
def main(procid, pipein, pipeout, sync, run_num, config,vseed=None): #random.seed(seed) toolbox.register("migrate", migPipe, k=5, pipein=pipein, pipeout=pipeout, selection=tools.selBest, replacement=random.sample) MU = config["POPULATION_SIZE"] NGEN = 500 CXPB = 0.5 MUTPB = 0.2 MIG_RATE = 5 deme = toolbox.population(n=MU) 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) logbook = tools.Logbook() logbook.header = "gen", "deme", "evals", "std", "min", "avg", "max" for ind in deme: ind.fitness.values = toolbox.evaluate(ind) record = stats.compile(deme) logbook.record(gen=0, deme=procid, evals=len(deme), **record) hof.update(deme) # if procid == 0: # # Synchronization needed to log header on top and only once # print run_num, logbook.stream # sync.set() # else: # logbook.log_header = False # Never output the header # print run_num, logbook.stream # sync.wait() for gen in range(1, NGEN): deme = toolbox.select(deme, len(deme)) deme = algorithms.varAnd(deme, toolbox, cxpb=CXPB, mutpb=MUTPB) invalid_ind = [ind for ind in deme if not ind.fitness.valid] for ind in invalid_ind: ind.fitness.values = toolbox.evaluate(ind) hof.update(deme) record = stats.compile(deme) logbook.record(gen=gen, deme=procid, evals=len(deme), **record) r = "%d,%d,%d,%d,%d" % (run_num,procid, gen, len(deme), record['max']) print r if record['max']>=128: #print run_num, "FOUND" return if gen % MIG_RATE == 0 and gen > 0: toolbox.migrate(deme)
def evolve_stage1(self, cxpb, mutpb, n_gen=50, k_elites=1, w_ssv=None): """ Perform stage 1 evolution, where only the local fitness (number of false states) matters. In this stage, each gene's regulation function is evolved separately and not cooperation is needed. :param cxpb: cross-over probability :param mutpb: mutation probability :param n_gen: number of generations :param k_elites: number of elites :param w_ssv: penalty weight of stable state violations. If None, then no penalty. :return: """ self.stage = 1 self.n_gen_stage1 = n_gen if n_gen == 0: return # normal single-objective GP self.toolbox.register('select', tools.selTournament, tournsize=3) self.toolbox.register('evaluate', self._evaluate_stage1, w_ssv=w_ssv) FitnessMin.weights = (-1.0, ) for gen in range(n_gen + 1): self.gen = gen # individuals that have been changed due to mutation or crossover invalid_pop = [ ind for ind in self.population if not ind.fitness.valid ] self._compile(invalid_pop) if self.stable_states is not None: self._count_stable_state_violations(invalid_pop) self._count_num_regulators(invalid_pop) # Evaluate the individuals with an invalid fitness fitnesses = self.toolbox.map(self.toolbox.evaluate, invalid_pop) for ind, fit in zip(invalid_pop, fitnesses): ind.fitness.values = fit self.hof_stage1.update(self.population) self.best_local_fitness = max( self.population, key=lambda ind: ind.fitness).fitness.values[0] # log if hasattr(self, 'logbook'): record = self.mstats.compile(self.population) self.logbook.record(index=self.index, stage=self.stage, gen=self.gen, **record) self._log_to_file() if gen == n_gen: break # selection and variation to produce the next generation elites = tools.selBest(self.population, k_elites) parents = self.toolbox.select(self.population, len(self.population) - k_elites) offspring = algorithms.varAnd(parents, toolbox=self.toolbox, cxpb=cxpb, mutpb=mutpb) self.population = elites + offspring
def main(): if os.path.isfile(CP): # A file name has been given, then load the data from the file cp = read_checkpoint() population = cp["population"] start_gen = cp["generation"] + 1 halloffame = cp["halloffame"] logbook = cp["logbook"] random.setstate(cp["rndstate"]) else: # Start a new evolution random.seed() population = toolbox.population(n=300) start_gen = 0 halloffame = tools.HallOfFame(maxsize=1) logbook = tools.Logbook() stats_fit = tools.Statistics(lambda ind: ind.fitness.values) stats_size = tools.Statistics(len) mstats = tools.MultiStatistics(fitness=stats_fit, size=stats_size) mstats.register("avg", numpy.mean) mstats.register("std", numpy.std) mstats.register("min", numpy.min) mstats.register("max", numpy.max) #algorithms.eaSimple(pop, toolbox, 0.5, 0.2, NGEN, mstats, halloffame=hof) #print(hof[0]) begin = start_gen end = begin + NGEN print("begin: {}, end: {}".format(begin, end)) for gen in range(begin, end): population = algorithms.varAnd(population, toolbox, cxpb=toolbox.mate, mutpb=toolbox.mutate) # 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 halloffame.update(population) record = mstats.compile(population) logbook.record(gen=gen, evals=len(invalid_ind), **record) population = toolbox.select(population, k=len(population)) if (gen + 1) % FREQ == 0 or gen == end - 1: # Fill the dictionary using the dict(key=value[, ...]) constructor cp = dict(population=population, generation=gen, halloffame=halloffame, logbook=logbook, rndstate=random.getstate()) with open(CP, "wb") as cp_file: pickle.dump(cp, cp_file, -1)
def compose(params): global window, chords_in_vector start = time() pop_size = 35 # int(params[0]) num_gens = 65 # int(params[1]) tourn_size = int(params[0]) ind_pb = params[1] cx_pb = params[2] mut_pb = params[3] creator.create("FitnessMulti", base.Fitness, weights=(-1.0, 1.0)) creator.create("Individual", list, fitness=creator.FitnessMulti) toolbox = base.Toolbox() IND_SIZE = chords_in_vector - len(window) toolbox.register("attr_chord", cg.generate_chord) toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_chord, n=IND_SIZE) toolbox.register("population", tools.initRepeat, list, toolbox.individual) toolbox.register("evaluate", predict) toolbox.register("mate", tools.cxTwoPoint) toolbox.register("mutate", mutate_individual, indpb=ind_pb) toolbox.register("select", tools.selTournament, tournsize=tourn_size) population = toolbox.population(n=pop_size) NGEN = num_gens for gen in range(NGEN): offspring = algorithms.varAnd(population, toolbox, cxpb=cx_pb, mutpb=mut_pb) fits = toolbox.map(toolbox.evaluate, offspring) for fit, ind in zip(fits, offspring): ind.fitness.values = fit population = toolbox.select(offspring, k=len(population)) top = tools.selBest(population, k=1)[0] np.set_printoptions(threshold=np.nan, suppress=True) #pprint("%s, %s" % (window, top)) #pprint(predict(top)) score = predict(top) end = time() run_time = end - start #time_dist = power(absolute(20 - run_time), 2) + 1 #f = -log(score[1] / time_dist) f = -log(score[1]) * 100000000 print(params) print("{:2.10f}".format(f)) results.append([list(params), f]) return f
def eaSimpleCustom(population, toolbox, cxpb, mutpb, sgen=1, ngen=1, stats=None, halloffame=None, verbose=__debug__): logbook = tools.Logbook() logbook.header = ['gen', 'nevals'] + (stats.fields if stats else []) # Evaluate the individuals with an invalid fitness invalid_ind = [ind for ind in population if not ind.fitness.valid] fitnesses = eval_exec_async(toolbox.evaluate, invalid_ind) for ind, fit in zip(invalid_ind, fitnesses): ind.fitness.values = fit if halloffame is not None: halloffame.update(population) log_hof(sgen, halloffame) record = stats.compile(population) if stats else {} logbook.record(gen=sgen, nevals=len(invalid_ind), **record) if verbose: log_stats(logbook.stream) # Begin the generational process for gen in range(sgen, ngen + 1): # Select the next generation individuals offspring = toolbox.select(population, len(population)) # Vary the pool of individuals offspring = algorithms.varAnd(offspring, toolbox, cxpb, mutpb) # Evaluate the individuals with an invalid fitness invalid_ind = [ind for ind in offspring if not ind.fitness.valid] fitnesses = eval_exec_async(toolbox.evaluate, invalid_ind) for ind, fit in zip(invalid_ind, fitnesses): ind.fitness.values = fit # Update the hall of fame with the generated individuals if halloffame is not None: halloffame.update(offspring) log_hof(gen, halloffame) # Replace the current population by the offspring population[:] = offspring population_persist(population, gen) # Append the current generation statistics to the logbook record = stats.compile(population) if stats else {} logbook.record(gen=gen, nevals=len(invalid_ind), **record) if verbose: log_stats(logbook.stream) return population, logbook
def evolve(population, toolbox, cxpb, mutpb, ngen, stats=None, halloffame=None, verbose=__debug__): logbook = tools.Logbook() logbook.header = ['gen', 'nevals'] + (stats.fields if stats else []) # 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 if halloffame is not None: halloffame.update(population) record = stats.compile(population) if stats else {} logbook.record(gen=0, nevals=len(invalid_ind), **record) if verbose: print logbook.stream # Begin the generational process for gen in range(1, ngen + 1): # Select the next generation individuals offspring = toolbox.select(population, len(population)) # Vary the pool of individuals offspring = varAnd(offspring, toolbox, cxpb, mutpb) # 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 # Update the hall of fame with the generated individuals if halloffame is not None: halloffame.update(offspring) # Replace the current population by the offspring population[:] = offspring # elitism t = random.randint(0, len(population) - 1) # t = 0 population[t] = halloffame[0] # Append the current generation statistics to the logbook record = stats.compile(population) if stats else {} logbook.record(gen=gen, nevals=len(invalid_ind), **record) if verbose: print logbook.stream return population, logbook
def run_ea(fitness_function, time_function, max_evals, max_gen, n_cpus=50, pop_size=100, cxpb = 0.8, mutpb= 0.1): toolbox = create_toolbox(fitness_function) initial_pop = toolbox.population(n=pop_size) aspirant_pairs = utils.generate_aspirant_pairs(pop_size) pool = ParallelSimulator(n_cpus=n_cpus, eval_func=toolbox.evaluate, time_func=time_function) start = datetime.datetime.now() last = start gens = [Generation(pop_size, tournament_aspirants=aspirant_pairs, pop=initial_pop)] t = 0 best_ind = None while pool.evals < max_evals: # submit individuals for current generation for idx, ind in enumerate(gens[t].pop): if not ind.fitness.valid: ind.pop_idx = idx pool.submit(ind) while not pool.all_evaluations_finished(): result = pool.next_finished() ind = result.args[0] gens[t].pop[ind.pop_idx].fitness.values = result.value if not best_ind or ind.fitness > best_ind.fitness: best_ind = ind pop = gens[t].pop # finish tournament selection gens[t].selected = [max(pop[asp1], pop[asp2], key=attrgetter('fitness')) for asp1, asp2 in gens[t].tournament_aspirants] # run operators gens[t].offspring = map(toolbox.clone, gens[t].selected) gens[t].offspring = algorithms.varAnd(gens[t].offspring, toolbox, cxpb, mutpb) # create next generation gens.append(Generation(pop_size, tournament_aspirants=utils.generate_aspirant_pairs(pop_size), pop=gens[t].offspring[:])) t += 1 # print logging information every second now = datetime.datetime.now() if now - last > datetime.timedelta(seconds=1): runtime = (now - start).total_seconds() print('TIME:', runtime, 'SPEED:', pool.evals / runtime) last = now end = datetime.datetime.now() print(pool.evals / (end - start).total_seconds()) print(pool.evals) print(pool.current_time) pool.print_stats() return best_ind, pool.log
def eaSimpleModified(population, toolbox, cxpb, mutpb, ngen, stats=None, halloffame=None, verbose=__debug__): logbook = tools.Logbook() logbook.header = ['gen', 'nevals'] + (stats.fields if stats else []) # 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 if halloffame is not None: halloffame.update(population) record = stats.compile(population) if stats else {} logbook.record(gen=0, nevals=len(invalid_ind), **record) if verbose: print(logbook.stream) best = [] best_ind = max(population, key=attrgetter("fitness")) best.append(best_ind) # Begin the generational process for gen in range(1, ngen + 1): # Select the next generation individuals offspring = toolbox.select(population, len(population)) # Vary the pool of individuals offspring = algorithms.varAnd(offspring, toolbox, cxpb, mutpb) # 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 # Save the best individual from the generation best_ind = max(offspring, key=attrgetter("fitness")) best.append(best_ind) # Update the hall of fame with the generated individuals if halloffame is not None: halloffame.update(offspring) # Replace the current population by the offspring population[:] = offspring # Append the current generation statistics to the logbook record = stats.compile(population) if stats else {} logbook.record(gen=gen, nevals=len(invalid_ind), **record) if verbose: print(logbook.stream) return population, logbook, best
def eaSimpleWarp(population, toolbox, filename, cxpb, mutpb, ngen, stats=None, halloffame=None, verbose=__debug__, labels=None): logbook = tools.Logbook() logbook.header = ['gen', 'nevals'] + (stats.fields if stats else []) # Evaluate the individuals with an invalid fitness # TODO: Change fitness evaluation to operate on the population not on individuals # TODO because all fitnesses are returned from WARP we need to pass all to the batch script simulatneously # TODO and then wait. # generate_fitnesses(population, toolbox) # Find which individuals have no fitness assigned (probably all of them here) fitnesses = return_efficiency(population, 'generation_0') print fitnesses # Assign new fitness values to the individuals for ind, fit in zip(population, fitnesses): ind.fitness.values = (fit,) # Update HoF if halloffame is not None: halloffame.update(population) save_generation(filename, population, generation=0, labels=None) # Begin the generational process for gen in range(1, ngen + 1): print "Starting Generation: {}".format(gen) # Select the next generation individuals offspring = toolbox.select(population, len(population)) # Vary the pool of individuals offspring = varAnd(offspring, toolbox, cxpb, mutpb) # ######################## Start server setup routine ################################################## # ######################## End server setup routine #################################################### fitnesses = return_efficiency(offspring, 'generation_{}'.format(gen)) for ind, fit in zip(offspring, fitnesses): ind.fitness.values = (fit,) # Update the hall of fame with the generated individuals if halloffame is not None: halloffame.update(offspring) # Replace the current population by the offspring population[:] = offspring # Append the current generation statistics to the logbook and save data file save_generation(filename, population, generation=gen, labels=None) # record = stats.compile(population) if stats else {} # logbook.record(gen=gen, nevals=len(invalid_ind), **record) # if verbose: # print logbook.stream print "Completed Generation: {}".format(gen) # TODO switch return back to population return offspring, logbook
def main(extended=True, verbose=True): target_set = [] species = [] stats = tools.Statistics(lambda ind: ind.fitness.values, n=TARGET_TYPE) stats.register("avg", tools.mean) stats.register("std", tools.std) stats.register("min", min) stats.register("max", max) if verbose: column_names = ["gen", "species", "evals"] column_names.extend(stats.functions.keys()) logger = tools.EvolutionLogger(column_names) logger.logHeader() ngen = 200 g = 0 schematas = nicheSchematas(TARGET_TYPE, IND_SIZE) for i in range(TARGET_TYPE): size = int(TARGET_SIZE/TARGET_TYPE) target_set.extend(toolbox.target_set(schematas[i], size)) species.append(toolbox.species()) # Init with a random representative for each species representatives = [random.choice(s) for s in species] while g < ngen: # Initialize a container for the next generation representatives next_repr = [None] * len(species) for i, s in enumerate(species): # Variate the species individuals s = algorithms.varAnd(s, toolbox, 0.6, 1.0) # Get the representatives excluding the current species r = representatives[:i] + representatives[i+1:] for ind in s: ind.fitness.values = toolbox.evaluate([ind] + r, target_set) stats.update(s, index=i) if verbose: logger.logGeneration(gen=g, species=i, evals=len(s), stats=stats, index=i) # Select the individuals species[i] = toolbox.select(s, len(s)) # Tournament selection next_repr[i] = toolbox.get_best(s)[0] # Best selection g += 1 representatives = next_repr if extended: for r in representatives: print("".join(str(x) for x in r))
def runCoEA(toolbox, stats, logbook, numGen=100, rnd=False, cxp=0.6, mutp=1.0, **kwargs): """Initializes and runs the coevolutionary algorithm. :returns: the best individual of the run.""" # RUN IT log.info("CxP: %f; MutP: %f" % (cxp, mutp)) log.info("Creating the population...") NUMSPECIES = 2 species_index = list(range(NUMSPECIES)) species = [toolbox.species() for _ in range(NUMSPECIES)] representatives = [random.choice(species[i]) for i in range(NUMSPECIES)] representatives[0].fitness.values = (toolbox.evaluate(representatives, curspecies=0),) representatives[1].fitness.values = (toolbox.evaluate(representatives[::-1], curspecies=1),) # pop = toolbox.population(n=4) bestsofar = [random.choice(species[i]) for i in range(NUMSPECIES)] bestsofar[0].fitness.values = (toolbox.evaluate(bestsofar, curspecies=0),) bestsofar[1].fitness.values = (toolbox.evaluate(bestsofar[::-1], curspecies=1),) log.info("Running the algorithm...") for g in xrange(numGen): # Initialize a container for the next generation representatives next_repr = [None] * len(species) for (i, s), j in zip(enumerate(species), species_index): # Vary the species individuals s = algorithms.varAnd(s, toolbox, cxp, mutp) # Get the representatives excluding the current species r = representatives[:i] + representatives[i + 1 :] for ind in s: log.debug("Evaluating species %d" % (j,)) log.debug(r) # Evaluate and set the individual fitness ind.fitness.values = (toolbox.evaluate([ind] + r, curspecies=j),) # Select the individuals species[i] = toolbox.select(s, len(s)) # Tournament selection if not rnd: next_repr[i] = toolbox.get_best(species[i])[0] # Best selection else: next_repr[i] = random.choice(species[i]) # Update stats record = stats.compile(s) logbook.record(gen=g, species=j, evals=len(s), **record) log.info(logbook.stream) for i in species_index: if next_repr[i].fitness.values <= representatives[i].fitness.values: representatives[i] = next_repr[i] # for index in range(len(representatives)): if representatives[i].fitness.values < bestsofar[i].fitness.values: bestsofar[i] = representatives[i] log.info("Done!\n") log.info(bestsofar[0]) log.info(bestsofar[1]) return bestsofar
def main(): random.seed(64) NBR_DEMES = 3 MU = 300 NGEN = 40 CXPB = 0.5 MUTPB = 0.2 MIG_RATE = 5 demes = [toolbox.population(n=MU) for _ in range(NBR_DEMES)] hof = tools.HallOfFame(1) stats = tools.Statistics(lambda ind: ind.fitness.values, 4) stats.register("avg", tools.mean) stats.register("std", tools.std) stats.register("min", min) stats.register("max", max) column_names = ["gen", "deme", "evals"] column_names.extend(stats.functions.keys()) logger = tools.EvolutionLogger(column_names) logger.logHeader() for idx, deme in enumerate(demes): for ind in deme: ind.fitness.values = toolbox.evaluate(ind) stats.update(deme, idx) hof.update(deme) logger.logGeneration(gen="0", deme=idx, evals=len(deme), stats=stats, index=idx) stats.update(demes[0]+demes[1]+demes[2], 3) logger.logGeneration(gen=0, deme=idx, evals="-", stats=stats, index=3) gen = 1 while gen <= NGEN and stats.max[3][-1][0] < 100.0: for idx, deme in enumerate(demes): deme[:] = toolbox.select(deme, len(deme)) deme[:] = algorithms.varAnd(deme, toolbox, cxpb=CXPB, mutpb=MUTPB) invalid_ind = [ind for ind in deme if not ind.fitness.valid] for ind in invalid_ind: ind.fitness.values = toolbox.evaluate(ind) stats.update(deme, idx) hof.update(deme) logger.logGeneration(gen="%d" %gen, deme=idx, evals=len(invalid_ind), stats=stats, index=idx) if gen % MIG_RATE == 0: toolbox.migrate(demes) stats.update(demes[0]+demes[1]+demes[2], 3) logger.logGeneration(gen="%d" % gen, deme="-", evals="-", stats=stats, index=3) gen += 1 return demes, stats, hof
def gen_new_offspring(): valid_offspring = True offspring = None while valid_offspring: # select two individuals and clone them offspring = map(toolbox.clone, toolbox.select(population, 2)) # apply crossover and mutation, take the first offspring offspring = algorithms.varAnd(offspring, toolbox, cxpb, mutpb)[0] valid_offspring = offspring.fitness.valid # if the offspring is valid it is already in the population return offspring
def _evolve(self, measure, population, toolbox, ngen, cxpb, mutpb, verbose=True): ''' Main evolution algorithm. A tweaked version of the eaSimple algorithm included in the DEAP package that adds per-generation logging of the best individual's properties and drops all the Statistics/HallOfFame stuff (since we're handling that ourselves). See DEAP documentation of algorithms.eaSimple() for all arguments. ''' # if verbose: # column_names = ["gen", "evals"] # if stats is not None: # column_names += stats.functions.keys() # logger = tools.Logbook(column_names) # logger.logHeader() # logger.logGeneration(evals=len(population), gen=0, stats=stats) # Begin the generational process for gen in range(0, ngen): # Select the next generation individuals offspring = toolbox.select(population, k=len(population)) # Variate the pool of individuals offspring = algorithms.varAnd(offspring, toolbox, cxpb, mutpb) # 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 # Replace the current population by the offspring offspring = sorted( offspring, key=lambda x: x.fitness, reverse=True) population[:] = offspring # Save best individual as an AbbreviatedMeasure self.best_individuals.append(population[0]) best_abb = AbbreviatedMeasure( self.measure, population[0], abbreviator=self.abbreviator, evaluator=self.evaluator, stats=True) self.best_measures.append(best_abb) r_squared = np.round(best_abb.r_squared.mean(), 2) n_items = np.sum(population[0]) # Update the statistics with the new population if self.stats is not None: record = self.stats.compile(population) self.logbook.record( gen=gen, r_squared=r_squared, n_items=n_items, **record) # Save last population in case we want to resume self.pop = population
def evolve_machines(POP): population = POP offspring = algorithms.varAnd(population, toolbox, cxpb=0.5, mutpb=0.1) fits = toolbox.map(toolbox.evaluate, population) for fit, ind in zip(fits, population): ind.fitness = fit population = toolbox.select(population, k=len(population)) top10 = tools.selBest(population, k=10) return population
def main(extended=True, verbose=True): target_set = [] species = [] 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) logbook = tools.Logbook() logbook.header = "gen", "species", "evals", "std", "min", "avg", "max" ngen = 200 g = 0 schematas = nicheSchematas(TARGET_TYPE, IND_SIZE) for i in range(TARGET_TYPE): size = int(TARGET_SIZE/TARGET_TYPE) target_set.extend(toolbox.target_set(schematas[i], size)) species.append(toolbox.species()) # Init with a random representative for each species representatives = [random.choice(s) for s in species] while g < ngen: # Initialize a container for the next generation representatives next_repr = [None] * len(species) for i, s in enumerate(species): # Vary the species individuals s = algorithms.varAnd(s, toolbox, 0.6, 1.0) # Get the representatives excluding the current species r = representatives[:i] + representatives[i+1:] for ind in s: ind.fitness.values = toolbox.evaluate([ind] + r, target_set) record = stats.compile(s) logbook.record(gen=g, species=i, evals=len(s), **record) if verbose: print(logbook.stream) # Select the individuals species[i] = toolbox.select(s, len(s)) # Tournament selection next_repr[i] = toolbox.get_best(s)[0] # Best selection g += 1 representatives = next_repr if extended: for r in representatives: print("".join(str(x) for x in r))
def main(procid, pipein, pipeout, sync, seed=None): random.seed(seed) toolbox.register("migrate", migPipe, k=5, pipein=pipein, pipeout=pipeout, selection=tools.selBest, replacement=random.sample) MU = 300 NGEN = 40 CXPB = 0.5 MUTPB = 0.2 MIG_RATE = 5 deme = toolbox.population(n=MU) 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) logbook = tools.Logbook() logbook.header = "gen", "deme", "evals", "std", "min", "avg", "max" for ind in deme: ind.fitness.values = toolbox.evaluate(ind) record = stats.compile(deme) logbook.record(gen=0, deme=procid, evals=len(deme), **record) hof.update(deme) if procid == 0: # Synchronization needed to log header on top and only once print(logbook.stream) sync.set() else: logbook.log_header = False # Never output the header sync.wait() print(logbook.stream) for gen in range(1, NGEN): deme = toolbox.select(deme, len(deme)) deme = algorithms.varAnd(deme, toolbox, cxpb=CXPB, mutpb=MUTPB) invalid_ind = [ind for ind in deme if not ind.fitness.valid] for ind in invalid_ind: ind.fitness.values = toolbox.evaluate(ind) hof.update(deme) record = stats.compile(deme) logbook.record(gen=gen, deme=procid, evals=len(deme), **record) print(logbook.stream) if gen % MIG_RATE == 0 and gen > 0: toolbox.migrate(deme)
def train(self, pop = 20, gen = 10): from deap import algorithms from deap import base from deap import creator from deap import tools from deap.tools import Statistics # import random from scipy.stats import rv_discrete # creator.create("FitnessMulti", base.Fitness, weights=(1.0, -1.0)) # creator.create("Individual", list, fitness=creator.FitnessMulti) creator.create("FitnessMin", base.Fitness, weights=(-1.0,)) creator.create("Individual", list, fitness=creator.FitnessMin) toolbox = base.Toolbox() # Attribute generator custm = rv_discrete(name='custm', values=(self.a_w.index, self.a_w.values)) toolbox.register("attr_int", custm.rvs) # Structure initializers toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_int, n=len(self.s)) toolbox.register("population", tools.initRepeat, list, toolbox.individual, n=pop) # Operator registering toolbox.register("evaluate", self.eval_classifer) toolbox.register("mate", tools.cxUniform, indpb=0.5) toolbox.register("mutate", tools.mutUniformInt, low=min(self.a.index), up=max(self.a.index), indpb=0.1) toolbox.register("select", tools.selNSGA2) MU, LAMBDA = pop, pop population = toolbox.population(n=MU) hof = tools.ParetoFront() s = Statistics(key=lambda ind: ind.fitness.values) s.register("mean", np.mean) s.register("min", min) # pop, logbook = algorithms.eaMuPlusLambda(pop, toolbox, mu=MU, lambda_=LAMBDA, cxpb=0.7, mutpb=0.3, ngen=gen, stats=s, halloffame=hof) for i in range(gen): offspring = algorithms.varAnd(population, toolbox, cxpb=0.95, mutpb=0.1) fits = toolbox.map(toolbox.evaluate, offspring) for fit, ind in zip(fits, offspring): ind.fitness.values = fit population = tools.selBest(offspring, int(0.05*len(offspring))) + tools.selTournament(offspring, len(offspring)-int(0.05*len(offspring)), tournsize=3) # population = toolbox.select(offspring, k=len(population)) print s.compile(population) top10 = tools.selBest(population, k=10) return top10
def main(procid, pipein, pipeout, sync, seed=None): random.seed(seed) toolbox.register( "migrate", migPipe, k=5, pipein=pipein, pipeout=pipeout, selection=tools.selBest, replacement=random.sample ) MU = 300 NGEN = 100 CXPB = 0.8 MUTPB = 1 MIG_RATE = 5 deme = toolbox.population(n=MU) hof = tools.HallOfFame(1) stats = tools.Statistics(lambda ind: ind.fitness.values) stats.register("avg", tools.mean) stats.register("std", tools.std) stats.register("min", min) stats.register("max", max) logger = tools.EvolutionLogger(["gen", "deme", "evals"] + stats.functions.keys()) if procid == 0: # Synchronization needed to log header on top and only once logger.logHeader() sync.set() else: sync.wait() for ind in deme: ind.fitness.values = toolbox.evaluate(ind) stats.update(deme) hof.update(deme) logger.logGeneration(gen="0", deme=procid, evals=len(deme), stats=stats) for gen in range(1, NGEN): deme = toolbox.select(deme, len(deme)) deme = algorithms.varAnd(deme, toolbox, cxpb=CXPB, mutpb=MUTPB) invalid_ind = [ind for ind in deme if not ind.fitness.valid] for ind in invalid_ind: ind.fitness.values = toolbox.evaluate(ind) stats.update(deme) hof.update(deme) logger.logGeneration(gen="%d" % gen, deme=procid, evals=len(invalid_ind), stats=stats) if gen % MIG_RATE == 0 and gen > 0: toolbox.migrate(deme) print "Best individual: " + str(convertArrToFloat(hof[-1])) print "Rastrigin value: " + str(rastrigin_arg0(hof[-1])[0]) + "\n\n\n"
def train(self, pop = 20, gen = 10): from deap import algorithms from deap import base from deap import creator from deap import tools import random import numpy as np from deap.tools import Statistics # creator.create("FitnessMulti", base.Fitness, weights=(1.0, -1.0)) # creator.create("Individual", list, fitness=creator.FitnessMulti) creator.create("FitnessMax", base.Fitness, weights=(1.0,)) creator.create("Individual", list, fitness=creator.FitnessMax) toolbox = base.Toolbox() # Attribute generator toolbox.register("attr_bool", random.randint, 0, 1) # Structure initializers toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_bool, n=len(self.X.columns)) toolbox.register("population", tools.initRepeat, list, toolbox.individual, n=pop) # Operator registering toolbox.register("evaluate", self.eval_classifer) toolbox.register("mate", tools.cxUniform, indpb=0.1) toolbox.register("mutate", tools.mutFlipBit, indpb=0.05) # toolbox.register("select", tools.selNSGA2) MU, LAMBDA = pop, pop population = toolbox.population(n=MU) # hof = tools.ParetoFront() s = Statistics(key=lambda ind: ind.fitness.values) s.register("mean", np.mean) s.register("max", max) # pop, logbook = algorithms.eaMuPlusLambda(pop, toolbox, mu=MU, lambda_=LAMBDA, cxpb=0.7, mutpb=0.3, ngen=gen, stats=s, halloffame=hof) for i in range(gen): offspring = algorithms.varAnd(population, toolbox, cxpb=0.95, mutpb=0.1) fits = toolbox.map(toolbox.evaluate, offspring) for fit, ind in zip(fits, offspring): ind.fitness.values = fit population = tools.selBest(offspring, int(0.05*len(offspring))) + tools.selTournament(offspring, len(offspring)-int(0.05*len(offspring)), tournsize=3) # population = toolbox.select(offspring, k=len(population)) print s.compile(population) top10 = tools.selBest(population, k=10) print top10 return top10[0]
def eaSimple(population, toolbox, cxpb, mutpb, ngen, stats=None, halloffame=None, verbose=__debug__): logbook = tools.Logbook() logbook.header = ['gen', 'nevals'] + (stats.fields if stats else []) # 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 if halloffame is not None: halloffame.update(population) record = stats.compile(population) if stats else {} logbook.record(gen=0, nevals=len(invalid_ind), **record) if verbose: print logbook.stream # Begin the generational process gen = 1 while evalNumThreaten(tools.selBest(population, k=1)[0])[0] != 0: gen += 1 # Select the next generation individuals offspring = toolbox.select(population, len(population)) # Vary the pool of individuals offspring = algorithms.varAnd(offspring, toolbox, cxpb, mutpb) # 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 # Update the hall of fame with the generated individuals if halloffame is not None: halloffame.update(offspring) # Replace the current population by the offspring population[:] = offspring # Append the current generation statistics to the logbook record = stats.compile(population) if stats else {} logbook.record(gen=gen, nevals=len(invalid_ind), **record) if verbose: print logbook.stream print 'total generations is',gen return population, logbook
def main(): random.seed(64) NBR_DEMES = 3 MU = 300 NGEN = 40 CXPB = 0.5 MUTPB = 0.2 MIG_RATE = 5 demes = [toolbox.population(n=MU) for _ in range(NBR_DEMES)] 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) logbook = tools.Logbook() logbook.header = "gen", "deme", "evals", "std", "min", "avg", "max" for idx, deme in enumerate(demes): for ind in deme: ind.fitness.values = toolbox.evaluate(ind) logbook.record(gen=0, deme=idx, evals=len(deme), **stats.compile(deme)) hof.update(deme) print(logbook.stream) gen = 1 while gen <= NGEN and logbook[-1]["max"] < 100.0: for idx, deme in enumerate(demes): deme[:] = toolbox.select(deme, len(deme)) deme[:] = algorithms.varAnd(deme, toolbox, cxpb=CXPB, mutpb=MUTPB) invalid_ind = [ind for ind in deme if not ind.fitness.valid] for ind in invalid_ind: ind.fitness.values = toolbox.evaluate(ind) logbook.record(gen=gen, deme=idx, evals=len( deme), **stats.compile(deme)) hof.update(deme) print(logbook.stream) if gen % MIG_RATE == 0: toolbox.migrate(demes) gen += 1 return demes, logbook, hof
def main(seed=None): random.seed(seed) node = Node() toolbox.register("migrate", migRingMPI, k=5, node=node, selection=tools.selBest, replacement=random.sample) MU = 300 NGEN = 200 CXPB = 0.5 MUTPB = 0.2 MIG_RATE = 5 deme = toolbox.population(n=MU) hof = tools.HallOfFame(1) stats = tools.Statistics(lambda ind: ind.fitness.values) stats.register("avg", tools.mean) stats.register("std", tools.std) stats.register("min", min) stats.register("max", max) logger = tools.EvolutionLogger(["gen", "deme", "evals"] + stats.functions.keys()) if rank == 0: # Synchronization needed to log header on top and only once logger.logHeader() comm.Barrier() for ind in deme: ind.fitness.values = toolbox.evaluate(ind) stats.update(deme) hof.update(deme) logger.logGeneration(gen="0", deme=rank, evals=len(deme), stats=stats) for gen in range(1, NGEN): deme = toolbox.select(deme, len(deme)) deme = algorithms.varAnd(deme, toolbox, cxpb=CXPB, mutpb=MUTPB) invalid_ind = [ind for ind in deme if not ind.fitness.valid] for ind in invalid_ind: ind.fitness.values = toolbox.evaluate(ind) stats.update(deme) hof.update(deme) logger.logGeneration(gen="%d" % gen, deme=rank, evals=len(invalid_ind), stats=stats) if gen % MIG_RATE == 0 and gen > 0: toolbox.migrate(deme)
def run(self): meta_spec = self.experiment.spec['meta'] ray.init(**meta_spec.get('resources', {})) max_generation = meta_spec['max_generation'] pop_size = meta_spec['max_trial'] or calc_population_size(self.experiment) logger.info(f'EvolutionarySearch max_generation: {max_generation}, population size: {pop_size}') trial_data_dict = {} config_hash = {} # config hash_str to trial_index toolbox = self.init_deap() population = toolbox.population(n=pop_size) for gen in range(1, max_generation + 1): logger.info(f'Running generation: {gen}/{max_generation}') ray_id_to_config = {} pending_ids = [] for individual in population: config = dict(individual.items()) hash_str = util.to_json(config, indent=0) if hash_str not in config_hash: trial_index = self.experiment.info_space.tick('trial')['trial'] config_hash[hash_str] = config['trial_index'] = trial_index ray_id = run_trial.remote(self.experiment, config) ray_id_to_config[ray_id] = config pending_ids.append(ray_id) individual['trial_index'] = config_hash[hash_str] trial_data_dict.update(get_ray_results(pending_ids, ray_id_to_config)) for individual in population: trial_index = individual.pop('trial_index') trial_data = trial_data_dict.get(trial_index, {'fitness': 0}) # if trial errored individual.fitness.values = trial_data['fitness'], preview = 'Fittest of population preview:' for individual in tools.selBest(population, k=min(10, pop_size)): preview += f'\nfitness: {individual.fitness.values[0]}, {individual}' logger.info(preview) # prepare offspring for next generation if gen < max_generation: population = toolbox.select(population, len(population)) # Vary the pool of individuals population = algorithms.varAnd(population, toolbox, cxpb=0.5, mutpb=0.5) ray.worker.cleanup() return trial_data_dict
def genetic_keyboard(pop_num, gen_num, input_pop=None): #What creator.create does is that it creates a new class. Its name is the first argument of the function. # Here on the first line we create a class named FitnessMax().base.Fitness tells you that this class is derived from the class base.Fitness().weight() is explained in the tutorial. creator.create("FitnessMin", base.Fitness, weights=(-1.0,)) # On the second line we create a class named Individual() derived from a list. We add the class creator.Fitness as one of its additionnal attribute. creator.create("Individual", list, fitness=creator.FitnessMin) toolbox = base.Toolbox() toolbox.register("indices", random.sample, range(len(CHARACTERS)), len(CHARACTERS)) # container – The type to put in the data from func. # generator – A function returning an iterable (list, tuple, ...), the content of this iterable will fill the container. toolbox.register("individual", tools.initIterate, creator.Individual, toolbox.indices) toolbox.register("population", tools.initRepeat, list, toolbox.individual) toolbox.register("evaluate", frequencyFitness) #ordered crossover toolbox.register("mate", tools.cxOrdered) #mutation we will swap elements from two points of the individual. toolbox.register("mutate", tools.mutShuffleIndexes, indpb=0.05) toolbox.register("select", tools.selTournament, tournsize=3) # result, log = algorithms.eaSimple(population, toolbox, # cxpb=0.8, mutpb=0.2, # gen_num=400, verbose=False) # best_individual = tools.selBest(result, k=1)[0] # pop_num = 100 # gen_num=100 population = toolbox.population(n=pop_num) with open('deap_results', 'w') as f: for gen in range(gen_num): offspring = algorithms.varAnd(population, toolbox, cxpb=0.5, mutpb=0.1) fits = toolbox.map(toolbox.evaluate, offspring) for fit, ind in zip(fits, offspring): ind.fitness.values = fit population = toolbox.select(offspring, k=len(population)) # best_individual = tools.selBest(population, k=1)[0] # f.write(str(gen) + ' ' + str(frequencyFitness(best_individual)) + # ' ' + str(indices_to_keyboard(best_individual))) # f.write('\n') return population