Python deap.tools.Logbook() Examples
The following are 22
code examples of deap.tools.Logbook().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
deap.tools
, or try the search function
.
Example #1
Source File: test_pickle.py From deap with GNU Lesser General Public License v3.0 | 5 votes |
def test_pickle_logbook(self): stats = tools.Statistics() logbook = tools.Logbook() stats.register("mean", numpy.mean) record = stats.compile([1,2,3,4,5,6,8,9,10]) logbook.record(**record) logbook_s = pickle.dumps(logbook) logbook_r = pickle.loads(logbook_s) self.assertEqual(logbook, logbook_r, "Unpickled logbook != pickled logbook")
Example #2
Source File: test_logbook.py From deap with GNU Lesser General Public License v3.0 | 5 votes |
def setUp(self): self.logbook = tools.Logbook() print
Example #3
Source File: onefifth.py From deap with GNU Lesser General Public License v3.0 | 5 votes |
def main(): """Implements the One-Fifth rule algorithm as expressed in : Kern, S., S.D. Muller, N. Hansen, D. Buche, J. Ocenasek and P. Koumoutsakos (2004). Learning Probability Distributions in Continuous Evolutionary Algorithms - A Comparative Review. Natural Computing, 3(1), pp. 77-112. However instead of parent and offspring the algorithm is expressed in terms of best and worst. Best is equivalent to the parent, and worst to the offspring. Instead of producing a new individual each time, we have defined a function which updates the worst individual using the best one as the mean of the gaussian and the sigma computed as the standard deviation. """ random.seed(64) logbook = tools.Logbook() logbook.header = "gen", "fitness" interval = (-3,7) mu = (random.uniform(interval[0], interval[1]) for _ in range(IND_SIZE)) sigma = (interval[1] - interval[0])/2.0 alpha = 2.0**(1.0/IND_SIZE) best = creator.Individual(mu) best.fitness.values = toolbox.evaluate(best) worst = creator.Individual((0.0,)*IND_SIZE) NGEN = 1500 for g in range(NGEN): toolbox.update(worst, best, sigma) worst.fitness.values = toolbox.evaluate(worst) if best.fitness <= worst.fitness: sigma = sigma * alpha best, worst = worst, best else: sigma = sigma * alpha**(-0.25) logbook.record(gen=g, fitness=best.fitness.values) print(logbook.stream) return best
Example #4
Source File: basic.py From deap with GNU Lesser General Public License v3.0 | 5 votes |
def main(): pop = toolbox.population(n=5) 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", "evals"] + stats.fields GEN = 1000 best = None for g in range(GEN): for part in pop: part.fitness.values = toolbox.evaluate(part) if not part.best or part.best.fitness < part.fitness: part.best = creator.Particle(part) part.best.fitness.values = part.fitness.values if not best or best.fitness < part.fitness: best = creator.Particle(part) best.fitness.values = part.fitness.values for part in pop: toolbox.update(part, best) # Gather all the fitnesses in one list and print the stats logbook.record(gen=g, evals=len(pop), **stats.compile(pop)) print(logbook.stream) return pop, logbook, best
Example #5
Source File: basic_numpy.py From deap with GNU Lesser General Public License v3.0 | 5 votes |
def main(): pop = toolbox.population(n=5) 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", "evals"] + stats.fields GEN = 1000 best = None for g in range(GEN): for part in pop: part.fitness.values = toolbox.evaluate(part) if part.best is None or part.best.fitness < part.fitness: part.best = creator.Particle(part) part.best.fitness.values = part.fitness.values if best is None or best.fitness < part.fitness: best = creator.Particle(part) best.fitness.values = part.fitness.values for part in pop: toolbox.update(part, best) # Gather all the fitnesses in one list and print the stats logbook.record(gen=g, evals=len(pop), **stats.compile(pop)) print(logbook.stream) return pop, logbook, best
Example #6
Source File: basic.py From deap with GNU Lesser General Public License v3.0 | 4 votes |
def main(): # Differential evolution parameters CR = 0.25 F = 1 MU = 300 NGEN = 200 pop = 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", "evals", "std", "min", "avg", "max" # Evaluate the individuals fitnesses = toolbox.map(toolbox.evaluate, pop) for ind, fit in zip(pop, fitnesses): ind.fitness.values = fit record = stats.compile(pop) logbook.record(gen=0, evals=len(pop), **record) print(logbook.stream) for g in range(1, NGEN): for k, agent in enumerate(pop): a,b,c = toolbox.select(pop) y = toolbox.clone(agent) index = random.randrange(NDIM) for i, value in enumerate(agent): if i == index or random.random() < CR: y[i] = a[i] + F*(b[i]-c[i]) y.fitness.values = toolbox.evaluate(y) if y.fitness > agent.fitness: pop[k] = y hof.update(pop) record = stats.compile(pop) logbook.record(gen=g, evals=len(pop), **record) print(logbook.stream) print("Best individual is ", hof[0], hof[0].fitness.values[0])
Example #7
Source File: hillis.py From deap with GNU Lesser General Public License v3.0 | 4 votes |
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
Example #8
Source File: coop_niche.py From deap with GNU Lesser General Public License v3.0 | 4 votes |
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))
Example #9
Source File: onemax_multidemic.py From deap with GNU Lesser General Public License v3.0 | 4 votes |
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
Example #10
Source File: nsga2.py From deap with GNU Lesser General Public License v3.0 | 4 votes |
def main(seed=None): random.seed(seed) NGEN = 250 MU = 100 CXPB = 0.9 stats = tools.Statistics(lambda ind: ind.fitness.values) # stats.register("avg", numpy.mean, axis=0) # stats.register("std", numpy.std, axis=0) stats.register("min", numpy.min, axis=0) stats.register("max", numpy.max, axis=0) logbook = tools.Logbook() logbook.header = "gen", "evals", "std", "min", "avg", "max" pop = toolbox.population(n=MU) # Evaluate the individuals with an invalid fitness invalid_ind = [ind for ind in pop if not ind.fitness.valid] fitnesses = toolbox.map(toolbox.evaluate, invalid_ind) for ind, fit in zip(invalid_ind, fitnesses): ind.fitness.values = fit # This is just to assign the crowding distance to the individuals # no actual selection is done pop = toolbox.select(pop, len(pop)) record = stats.compile(pop) logbook.record(gen=0, evals=len(invalid_ind), **record) print(logbook.stream) # Begin the generational process for gen in range(1, NGEN): # Vary the population offspring = tools.selTournamentDCD(pop, len(pop)) offspring = [toolbox.clone(ind) for ind in offspring] for ind1, ind2 in zip(offspring[::2], offspring[1::2]): if random.random() <= CXPB: toolbox.mate(ind1, ind2) toolbox.mutate(ind1) toolbox.mutate(ind2) del ind1.fitness.values, ind2.fitness.values # 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 pop = toolbox.select(pop + offspring, MU) record = stats.compile(pop) logbook.record(gen=gen, evals=len(invalid_ind), **record) print(logbook.stream) print("Final population hypervolume is %f" % hypervolume(pop, [11.0, 11.0])) return pop, logbook
Example #11
Source File: nsga3.py From deap with GNU Lesser General Public License v3.0 | 4 votes |
def main(seed=None): random.seed(seed) # Initialize statistics object stats = tools.Statistics(lambda ind: ind.fitness.values) stats.register("avg", numpy.mean, axis=0) stats.register("std", numpy.std, axis=0) stats.register("min", numpy.min, axis=0) stats.register("max", numpy.max, axis=0) logbook = tools.Logbook() logbook.header = "gen", "evals", "std", "min", "avg", "max" pop = toolbox.population(n=MU) # Evaluate the individuals with an invalid fitness invalid_ind = [ind for ind in pop if not ind.fitness.valid] fitnesses = toolbox.map(toolbox.evaluate, invalid_ind) for ind, fit in zip(invalid_ind, fitnesses): ind.fitness.values = fit # Compile statistics about the population record = stats.compile(pop) logbook.record(gen=0, evals=len(invalid_ind), **record) print(logbook.stream) # Begin the generational process for gen in range(1, NGEN): offspring = algorithms.varAnd(pop, 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 # Select the next generation population from parents and offspring pop = toolbox.select(pop + offspring, MU) # Compile statistics about the new population record = stats.compile(pop) logbook.record(gen=gen, evals=len(invalid_ind), **record) print(logbook.stream) return pop, logbook
Example #12
Source File: gp_deap.py From tpot with GNU Lesser General Public License v3.0 | 4 votes |
def varOr(population, toolbox, lambda_, cxpb, mutpb): """Part of an evolutionary algorithm applying only the variation part (crossover, mutation **or** reproduction). The modified individuals have their fitness invalidated. The individuals are cloned so returned population is independent of the input population. :param population: A list of individuals to vary. :param toolbox: A :class:`~deap.base.Toolbox` that contains the evolution operators. :param lambda\_: The number of children to produce :param cxpb: The probability of mating two individuals. :param mutpb: The probability of mutating an individual. :returns: The final population :returns: A class:`~deap.tools.Logbook` with the statistics of the evolution The variation goes as follow. On each of the *lambda_* iteration, it selects one of the three operations; crossover, mutation or reproduction. In the case of a crossover, two individuals are selected at random from the parental population :math:`P_\mathrm{p}`, those individuals are cloned using the :meth:`toolbox.clone` method and then mated using the :meth:`toolbox.mate` method. Only the first child is appended to the offspring population :math:`P_\mathrm{o}`, the second child is discarded. In the case of a mutation, one individual is selected at random from :math:`P_\mathrm{p}`, it is cloned and then mutated using using the :meth:`toolbox.mutate` method. The resulting mutant is appended to :math:`P_\mathrm{o}`. In the case of a reproduction, one individual is selected at random from :math:`P_\mathrm{p}`, cloned and appended to :math:`P_\mathrm{o}`. This variation is named *Or* beceause an offspring will never result from both operations crossover and mutation. The sum of both probabilities shall be in :math:`[0, 1]`, the reproduction probability is 1 - *cxpb* - *mutpb*. """ offspring = [] for _ in range(lambda_): op_choice = np.random.random() if op_choice < cxpb: # Apply crossover ind1, ind2 = pick_two_individuals_eligible_for_crossover(population) if ind1 is not None: ind1, _ = toolbox.mate(ind1, ind2) del ind1.fitness.values else: # If there is no pair eligible for crossover, we still want to # create diversity in the population, and do so by mutation instead. ind1 = mutate_random_individual(population, toolbox) offspring.append(ind1) elif op_choice < cxpb + mutpb: # Apply mutation ind = mutate_random_individual(population, toolbox) offspring.append(ind) else: # Apply reproduction idx = np.random.randint(0, len(population)) offspring.append(toolbox.clone(population[idx])) return offspring
Example #13
Source File: sphere.py From deap with GNU Lesser General Public License v3.0 | 4 votes |
def main(): # Differential evolution parameters MU = NDIM * 10 NGEN = 200 pop = 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", "evals", "std", "min", "avg", "max" # Evaluate the individuals fitnesses = toolbox.map(toolbox.evaluate, pop) for ind, fit in zip(pop, fitnesses): ind.fitness.values = fit record = stats.compile(pop) logbook.record(gen=0, evals=len(pop), **record) print(logbook.stream) for g in range(1, NGEN): children = [] for agent in pop: # We must clone everything to ensure independance a, b, c = [toolbox.clone(ind) for ind in toolbox.select(pop)] x = toolbox.clone(agent) y = toolbox.clone(agent) y = toolbox.mutate(y, a, b, c) z = toolbox.mate(x, y) del z.fitness.values children.append(z) fitnesses = toolbox.map(toolbox.evaluate, children) for (i, ind), fit in zip(enumerate(children), fitnesses): ind.fitness.values = fit if ind.fitness > pop[i].fitness: pop[i] = ind hof.update(pop) record = stats.compile(pop) logbook.record(gen=g, evals=len(pop), **record) print(logbook.stream) print("Best individual is ", hof[0]) print("with fitness", hof[0].fitness.values[0]) return logbook
Example #14
Source File: train_cnn_ga.py From neuroevolution with MIT License | 4 votes |
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
Example #15
Source File: elitism.py From Hands-On-Genetic-Algorithms-with-Python with MIT License | 4 votes |
def eaSimpleWithElitism(population, toolbox, cxpb, mutpb, ngen, stats=None, halloffame=None, verbose=__debug__): """This algorithm is similar to DEAP eaSimple() algorithm, with the modification that halloffame is used to implement an elitism mechanism. The individuals contained in the halloffame are directly injected into the next generation and are not subject to the genetic operators of selection, crossover and mutation. """ 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 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) # Begin the generational process for gen in range(1, ngen + 1): # Select the next generation individuals offspring = toolbox.select(population, len(population) - hof_size) # 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 # add the best back to population: offspring.extend(halloffame.items) # Update the hall of fame with the generated individuals 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
Example #16
Source File: elitism.py From Hands-On-Genetic-Algorithms-with-Python with MIT License | 4 votes |
def eaSimpleWithElitism(population, toolbox, cxpb, mutpb, ngen, stats=None, halloffame=None, verbose=__debug__): """This algorithm is similar to DEAP eaSimple() algorithm, with the modification that halloffame is used to implement an elitism mechanism. The individuals contained in the halloffame are directly injected into the next generation and are not subject to the genetic operators of selection, crossover and mutation. """ 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 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) # Begin the generational process for gen in range(1, ngen + 1): # Select the next generation individuals offspring = toolbox.select(population, len(population) - hof_size) # 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 # add the best back to population: offspring.extend(halloffame.items) # Update the hall of fame with the generated individuals 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
Example #17
Source File: elitism.py From Hands-On-Genetic-Algorithms-with-Python with MIT License | 4 votes |
def eaSimpleWithElitism(population, toolbox, cxpb, mutpb, ngen, stats=None, halloffame=None, verbose=__debug__): """This algorithm is similar to DEAP eaSimple() algorithm, with the modification that halloffame is used to implement an elitism mechanism. The individuals contained in the halloffame are directly injected into the next generation and are not subject to the genetic operators of selection, crossover and mutation. """ 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 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) # Begin the generational process for gen in range(1, ngen + 1): # Select the next generation individuals offspring = toolbox.select(population, len(population) - hof_size) # 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 # add the best back to population: offspring.extend(halloffame.items) # Update the hall of fame with the generated individuals 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
Example #18
Source File: elitism.py From Hands-On-Genetic-Algorithms-with-Python with MIT License | 4 votes |
def eaSimpleWithElitism(population, toolbox, cxpb, mutpb, ngen, stats=None, halloffame=None, verbose=__debug__): """This algorithm is similar to DEAP eaSimple() algorithm, with the modification that halloffame is used to implement an elitism mechanism. The individuals contained in the halloffame are directly injected into the next generation and are not subject to the genetic operators of selection, crossover and mutation. """ 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 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) # Begin the generational process for gen in range(1, ngen + 1): # Select the next generation individuals offspring = toolbox.select(population, len(population) - hof_size) # 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 # add the best back to population: offspring.extend(halloffame.items) # Update the hall of fame with the generated individuals 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
Example #19
Source File: 03-pso-himmelblau.py From Hands-On-Genetic-Algorithms-with-Python with MIT License | 4 votes |
def main(): # create the population of particle population: population = toolbox.populationCreator(n=POPULATION_SIZE) # prepare the statistics object: stats = tools.Statistics(lambda ind: ind.fitness.values) stats.register("min", np.min) stats.register("avg", np.mean) logbook = tools.Logbook() logbook.header = ["gen", "evals"] + stats.fields best = None for generation in range(MAX_GENERATIONS): # evaluate all particles in polulation: for particle in population: # find the fitness of the particle: particle.fitness.values = toolbox.evaluate(particle) # particle best needs to be updated: if particle.best is None or particle.best.size == 0 or particle.best.fitness < particle.fitness: particle.best = creator.Particle(particle) particle.best.fitness.values = particle.fitness.values # global best needs to be updated: if best is None or best.size == 0 or best.fitness < particle.fitness: best = creator.Particle(particle) best.fitness.values = particle.fitness.values # update each particle's speed and position: for particle in population: toolbox.update(particle, best) # record the statistics for the current generation and print it: logbook.record(gen=generation, evals=len(population), **stats.compile(population)) print(logbook.stream) # print info for best solution found: print("-- Best Particle = ", best) print("-- Best Fitness = ", best.fitness.values[0])
Example #20
Source File: elitism.py From Hands-On-Genetic-Algorithms-with-Python with MIT License | 4 votes |
def eaSimpleWithElitism(population, toolbox, cxpb, mutpb, ngen, stats=None, halloffame=None, verbose=__debug__): """This algorithm is similar to DEAP eaSimple() algorithm, with the modification that halloffame is used to implement an elitism mechanism. The individuals contained in the halloffame are directly injected into the next generation and are not subject to the genetic operators of selection, crossover and mutation. """ 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 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) # Begin the generational process for gen in range(1, ngen + 1): # Select the next generation individuals offspring = toolbox.select(population, len(population) - hof_size) # 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 # add the best back to population: offspring.extend(halloffame.items) # Update the hall of fame with the generated individuals 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
Example #21
Source File: elitism.py From Hands-On-Genetic-Algorithms-with-Python with MIT License | 4 votes |
def eaSimpleWithElitism(population, toolbox, cxpb, mutpb, ngen, stats=None, halloffame=None, verbose=__debug__): """This algorithm is similar to DEAP eaSimple() algorithm, with the modification that halloffame is used to implement an elitism mechanism. The individuals contained in the halloffame are directly injected into the next generation and are not subject to the genetic operators of selection, crossover and mutation. """ 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 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) # Begin the generational process for gen in range(1, ngen + 1): # Select the next generation individuals offspring = toolbox.select(population, len(population) - hof_size) # 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 # add the best back to population: offspring.extend(halloffame.items) # Update the hall of fame with the generated individuals 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
Example #22
Source File: elitism.py From Hands-On-Genetic-Algorithms-with-Python with MIT License | 4 votes |
def eaSimpleWithElitism(population, toolbox, cxpb, mutpb, ngen, stats=None, halloffame=None, verbose=__debug__): """This algorithm is similar to DEAP eaSimple() algorithm, with the modification that halloffame is used to implement an elitism mechanism. The individuals contained in the halloffame are directly injected into the next generation and are not subject to the genetic operators of selection, crossover and mutation. """ 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 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) # Begin the generational process for gen in range(1, ngen + 1): # Select the next generation individuals offspring = toolbox.select(population, len(population) - hof_size) # 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 # add the best back to population: offspring.extend(halloffame.items) # Update the hall of fame with the generated individuals 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