Python deap.tools.Statistics() Examples
The following are 30
code examples of deap.tools.Statistics().
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: tsp.py From deap with GNU Lesser General Public License v3.0 | 6 votes |
def main(): random.seed(169) pop = toolbox.population(n=300) 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) algorithms.eaSimple(pop, toolbox, 0.7, 0.2, 40, stats=stats, halloffame=hof) return pop, stats, hof
Example #2
Source File: onemax_numpy.py From deap with GNU Lesser General Public License v3.0 | 6 votes |
def main(): random.seed(64) pop = toolbox.population(n=300) # Numpy equality function (operators.eq) between two arrays returns the # equality element wise, which raises an exception in the if similar() # check of the hall of fame. Using a different equality function like # numpy.array_equal or numpy.allclose solve this issue. hof = tools.HallOfFame(1, similar=numpy.array_equal) 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) algorithms.eaSimple(pop, toolbox, cxpb=0.5, mutpb=0.2, ngen=40, stats=stats, halloffame=hof) return pop, stats, hof
Example #3
Source File: pbil.py From deap with GNU Lesser General Public License v3.0 | 6 votes |
def main(seed): random.seed(seed) NGEN = 50 #Initialize the PBIL EDA pbil = PBIL(ndim=50, learning_rate=0.3, mut_prob=0.1, mut_shift=0.05, lambda_=20) toolbox.register("generate", pbil.generate, creator.Individual) toolbox.register("update", pbil.update) # Statistics computation stats = tools.Statistics(lambda ind: ind.fitness.values) stats.register("avg", numpy.mean) stats.register("std", numpy.std) stats.register("min", numpy.min) stats.register("max", numpy.max) pop, logbook = algorithms.eaGenerateUpdate(toolbox, NGEN, stats=stats, verbose=True)
Example #4
Source File: emna.py From deap with GNU Lesser General Public License v3.0 | 6 votes |
def main(): N, LAMBDA = 30, 1000 MU = int(LAMBDA/4) strategy = EMNA(centroid=[5.0]*N, sigma=5.0, mu=MU, lambda_=LAMBDA) toolbox = base.Toolbox() toolbox.register("evaluate", benchmarks.sphere) toolbox.register("generate", strategy.generate, creator.Individual) toolbox.register("update", strategy.update) # Numpy equality function (operators.eq) between two arrays returns the # equality element wise, which raises an exception in the if similar() # check of the hall of fame. Using a different equality function like # numpy.array_equal or numpy.allclose solve this issue. hof = tools.HallOfFame(1, similar=numpy.array_equal) 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) algorithms.eaGenerateUpdate(toolbox, ngen=150, stats=stats, halloffame=hof) return hof[0].fitness.values[0]
Example #5
Source File: symbreg.py From deap with GNU Lesser General Public License v3.0 | 6 votes |
def main(): random.seed(318) pop = toolbox.population(n=300) hof = tools.HallOfFame(1) 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) pop, log = algorithms.eaSimple(pop, toolbox, 0.5, 0.1, 40, stats=mstats, halloffame=hof, verbose=True) # print log return pop, log, hof
Example #6
Source File: kursawefct.py From deap with GNU Lesser General Public License v3.0 | 6 votes |
def main(): random.seed(64) MU, LAMBDA = 50, 100 pop = toolbox.population(n=MU) hof = tools.ParetoFront() 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) algorithms.eaMuPlusLambda(pop, toolbox, mu=MU, lambda_=LAMBDA, cxpb=0.5, mutpb=0.2, ngen=150, stats=stats, halloffame=hof) return pop, stats, hof
Example #7
Source File: cma_1+l_minfct.py From deap with GNU Lesser General Public License v3.0 | 6 votes |
def main(): numpy.random.seed() # The CMA-ES One Plus Lambda algorithm takes a initialized parent as argument parent = creator.Individual((numpy.random.rand() * 5) - 1 for _ in range(N)) parent.fitness.values = toolbox.evaluate(parent) strategy = cma.StrategyOnePlusLambda(parent, sigma=5.0, lambda_=10) toolbox.register("generate", strategy.generate, ind_init=creator.Individual) toolbox.register("update", strategy.update) 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) algorithms.eaGenerateUpdate(toolbox, ngen=200, halloffame=hof, stats=stats)
Example #8
Source File: symbreg_epsilon_lexicase.py From deap with GNU Lesser General Public License v3.0 | 6 votes |
def main(): #random.seed(318) pop = toolbox.population(n=300) hof = tools.HallOfFame(1) 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) pop, log = algorithms.eaSimple(pop, toolbox, 0.5, 0.1, 40, stats=mstats, halloffame=hof, verbose=True) # print log return pop, log, hof
Example #9
Source File: ant.py From deap with GNU Lesser General Public License v3.0 | 6 votes |
def main(): random.seed(69) with open("ant/santafe_trail.txt") as trail_file: ant.parse_matrix(trail_file) pop = toolbox.population(n=300) 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) algorithms.eaSimple(pop, toolbox, 0.5, 0.2, 40, stats, halloffame=hof) return pop, hof, stats
Example #10
Source File: evoknn.py From deap with GNU Lesser General Public License v3.0 | 6 votes |
def main(): # random.seed(64) MU, LAMBDA = 100, 200 pop = toolbox.population(n=MU) hof = tools.ParetoFront() 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) pop, logbook = algorithms.eaMuPlusLambda(pop, toolbox, mu=MU, lambda_=LAMBDA, cxpb=0.7, mutpb=0.3, ngen=40, stats=stats, halloffame=hof) return pop, logbook, hof
Example #11
Source File: knapsack.py From deap with GNU Lesser General Public License v3.0 | 6 votes |
def main(): random.seed(64) NGEN = 50 MU = 50 LAMBDA = 100 CXPB = 0.7 MUTPB = 0.2 pop = toolbox.population(n=MU) hof = tools.ParetoFront() 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) algorithms.eaMuPlusLambda(pop, toolbox, MU, LAMBDA, CXPB, MUTPB, NGEN, stats, halloffame=hof) return pop, stats, hof
Example #12
Source File: cma_minfct.py From deap with GNU Lesser General Public License v3.0 | 6 votes |
def main(): # The cma module uses the numpy random number generator numpy.random.seed(128) # The CMA-ES algorithm takes a population of one individual as argument # The centroid is set to a vector of 5.0 see http://www.lri.fr/~hansen/cmaes_inmatlab.html # for more details about the rastrigin and other tests for CMA-ES strategy = cma.Strategy(centroid=[5.0]*N, sigma=5.0, lambda_=20*N) toolbox.register("generate", strategy.generate, creator.Individual) toolbox.register("update", strategy.update) 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) #logger = tools.EvolutionLogger(stats.functions.keys()) # The CMA-ES algorithm converge with good probability with those settings algorithms.eaGenerateUpdate(toolbox, ngen=250, stats=stats, halloffame=hof) # print "Best individual is %s, %s" % (hof[0], hof[0].fitness.values) return hof[0].fitness.values[0]
Example #13
Source File: xkcd.py From deap with GNU Lesser General Public License v3.0 | 6 votes |
def main(): NGEN = 40 MU = 100 LAMBDA = 200 CXPB = 0.3 MUTPB = 0.6 pop = toolbox.population(n=MU) hof = tools.ParetoFront() price_stats = tools.Statistics(key=lambda ind: ind.fitness.values[0]) time_stats = tools.Statistics(key=lambda ind: ind.fitness.values[1]) stats = tools.MultiStatistics(price=price_stats, time=time_stats) stats.register("avg", numpy.mean, axis=0) stats.register("std", numpy.std, axis=0) stats.register("min", numpy.min, axis=0) algorithms.eaMuPlusLambda(pop, toolbox, MU, LAMBDA, CXPB, MUTPB, NGEN, stats, halloffame=hof) return pop, stats, hof
Example #14
Source File: nqueens.py From deap with GNU Lesser General Public License v3.0 | 5 votes |
def main(seed=0): random.seed(seed) pop = toolbox.population(n=300) 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) algorithms.eaSimple(pop, toolbox, cxpb=0.5, mutpb=0.2, ngen=100, stats=stats, halloffame=hof, verbose=True) return pop, stats, hof
Example #15
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 #16
Source File: statistics.py From japonicus with MIT License | 5 votes |
def getStatisticsMeter(): stats = tools.Statistics(lambda ind: ind.fitness.values[0]) stats.register("avg", np.mean) stats.register("std", np.std) stats.register("min", np.min) stats.register("max", np.max) return stats
Example #17
Source File: gaFeatureSelection.py From GeneticAlgorithmForFeatureSelection with MIT License | 5 votes |
def geneticAlgorithm(X, y, n_population, n_generation): """ Deap global variables Initialize variables to use eaSimple """ # create individual creator.create("FitnessMax", base.Fitness, weights=(1.0,)) creator.create("Individual", list, fitness=creator.FitnessMax) # create toolbox toolbox = base.Toolbox() toolbox.register("attr_bool", random.randint, 0, 1) toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_bool, len(X.columns)) toolbox.register("population", tools.initRepeat, list, toolbox.individual) toolbox.register("evaluate", getFitness, X=X, y=y) toolbox.register("mate", tools.cxOnePoint) toolbox.register("mutate", tools.mutFlipBit, indpb=0.05) toolbox.register("select", tools.selTournament, tournsize=3) # initialize parameters pop = toolbox.population(n=n_population) hof = tools.HallOfFame(n_population * n_generation) stats = tools.Statistics(lambda ind: ind.fitness.values) stats.register("avg", np.mean) stats.register("min", np.min) stats.register("max", np.max) # genetic algorithm pop, log = algorithms.eaSimple(pop, toolbox, cxpb=0.5, mutpb=0.2, ngen=n_generation, stats=stats, halloffame=hof, verbose=True) # return hall of fame return hof
Example #18
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 #19
Source File: fctmin.py From deap with GNU Lesser General Public License v3.0 | 5 votes |
def main(): random.seed() MU, LAMBDA = 10, 100 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) pop, logbook = algorithms.eaMuCommaLambda(pop, toolbox, mu=MU, lambda_=LAMBDA, cxpb=0.6, mutpb=0.3, ngen=500, stats=stats, halloffame=hof) return pop, logbook, hof
Example #20
Source File: parity.py From deap with GNU Lesser General Public License v3.0 | 5 votes |
def main(): random.seed(21) pop = toolbox.population(n=300) 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) algorithms.eaSimple(pop, toolbox, 0.5, 0.2, 40, stats, halloffame=hof) return pop, stats, hof
Example #21
Source File: symbreg_numpy.py From deap with GNU Lesser General Public License v3.0 | 5 votes |
def main(): random.seed(318) pop = toolbox.population(n=300) 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) algorithms.eaSimple(pop, toolbox, 0.5, 0.1, 40, stats, halloffame=hof) return pop, stats, hof
Example #22
Source File: multiplexer.py From deap with GNU Lesser General Public License v3.0 | 5 votes |
def main(): # random.seed(10) pop = toolbox.population(n=40) 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) algorithms.eaSimple(pop, toolbox, 0.8, 0.1, 40, stats, halloffame=hof) return pop, stats, hof
Example #23
Source File: spambase.py From deap with GNU Lesser General Public License v3.0 | 5 votes |
def main(): random.seed(10) pop = toolbox.population(n=100) 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) algorithms.eaSimple(pop, toolbox, 0.5, 0.2, 40, stats, halloffame=hof) return pop, stats, hof
Example #24
Source File: test_statistics.py From deap with GNU Lesser General Public License v3.0 | 5 votes |
def test_statistics_compile(self): s = tools.Statistics() s.register("mean", numpy.mean) s.register("max", max) res = s.compile([1, 2, 3, 4]) self.assertDictEqual(res, {'max': 4, 'mean': 2.5}) res = s.compile([5, 6, 7, 8]) self.assertDictEqual(res, {'mean': 6.5, 'max': 8})
Example #25
Source File: test_statistics.py From deap with GNU Lesser General Public License v3.0 | 5 votes |
def test_multi_statistics_compile(self): len_stats = tools.Statistics(key=len) itm0_stats = tools.Statistics(key=itemgetter(0)) mstats = tools.MultiStatistics(length=len_stats, item=itm0_stats) mstats.register("mean", numpy.mean, axis=0) mstats.register("max", numpy.max, axis=0) res = mstats.compile([[0.0, 1.0, 1.0, 5.0], [2.0, 5.0]]) self.assertDictEqual(res, {'length': {'mean': 3.0, 'max': 4}, 'item': {'mean': 1.0, 'max': 2.0}})
Example #26
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 #27
Source File: 02-solve-cart-pole.py From Hands-On-Genetic-Algorithms-with-Python with MIT License | 5 votes |
def main(): # create initial population (generation 0): population = toolbox.populationCreator(n=POPULATION_SIZE) # prepare the statistics object: stats = tools.Statistics(lambda ind: ind.fitness.values) stats.register("max", numpy.max) stats.register("avg", numpy.mean) # define the hall-of-fame object: hof = tools.HallOfFame(HALL_OF_FAME_SIZE) # perform the Genetic Algorithm flow with hof feature added: population, logbook = elitism.eaSimpleWithElitism(population, toolbox, cxpb=P_CROSSOVER, mutpb=P_MUTATION, ngen=MAX_GENERATIONS, stats=stats, halloffame=hof, verbose=True) # print best solution found: best = hof.items[0] print() print("Best Solution = ", best) print("Best Score = ", best.fitness.values[0]) print() # save best solution for a replay: cartPole.saveParams(best) # find average score of 100 episodes using the best solution found: print("Running 100 episodes using the best solution...") scores = [] for test in range(100): scores.append(cart_pole.CartPole().getScore(best)) print("scores = ", scores) print("Avg. score = ", sum(scores) / len(scores))
Example #28
Source File: 01-solve-knapsack.py From Hands-On-Genetic-Algorithms-with-Python with MIT License | 5 votes |
def main(): # create initial population (generation 0): population = toolbox.populationCreator(n=POPULATION_SIZE) # prepare the statistics object: stats = tools.Statistics(lambda ind: ind.fitness.values) stats.register("max", numpy.max) stats.register("avg", numpy.mean) # define the hall-of-fame object: hof = tools.HallOfFame(HALL_OF_FAME_SIZE) # perform the Genetic Algorithm flow with hof feature added: population, logbook = algorithms.eaSimple(population, toolbox, cxpb=P_CROSSOVER, mutpb=P_MUTATION, ngen=MAX_GENERATIONS, stats=stats, halloffame=hof, verbose=True) # print best solution found: best = hof.items[0] print("-- Best Ever Individual = ", best) print("-- Best Ever Fitness = ", best.fitness.values[0]) print("-- Knapsack Items = ") knapsack.printItems(best) # extract statistics: maxFitnessValues, meanFitnessValues = logbook.select("max", "avg") # plot statistics: sns.set_style("whitegrid") plt.plot(maxFitnessValues, color='red') plt.plot(meanFitnessValues, color='green') plt.xlabel('Generation') plt.ylabel('Max / Average Fitness') plt.title('Max and Average fitness over Generations') plt.show()
Example #29
Source File: 01-optimize-mlp-layers.py From Hands-On-Genetic-Algorithms-with-Python with MIT License | 5 votes |
def main(): # create initial population (generation 0): population = toolbox.populationCreator(n=POPULATION_SIZE) # prepare the statistics object: stats = tools.Statistics(lambda ind: ind.fitness.values) stats.register("max", numpy.max) stats.register("avg", numpy.mean) # define the hall-of-fame object: hof = tools.HallOfFame(HALL_OF_FAME_SIZE) # perform the Genetic Algorithm flow with hof feature added: population, logbook = elitism.eaSimpleWithElitism(population, toolbox, cxpb=P_CROSSOVER, mutpb=P_MUTATION, ngen=MAX_GENERATIONS, stats=stats, halloffame=hof, verbose=True) # print best solution found: print("- Best solution is: ", test.formatParams(hof.items[0]), ", accuracy = ", hof.items[0].fitness.values[0])
Example #30
Source File: 02-optimize-mlp-hyperparameters.py From Hands-On-Genetic-Algorithms-with-Python with MIT License | 5 votes |
def main(): # create initial population (generation 0): population = toolbox.populationCreator(n=POPULATION_SIZE) # prepare the statistics object: stats = tools.Statistics(lambda ind: ind.fitness.values) stats.register("max", numpy.max) stats.register("avg", numpy.mean) # define the hall-of-fame object: hof = tools.HallOfFame(HALL_OF_FAME_SIZE) # perform the Genetic Algorithm flow with hof feature added: population, logbook = elitism.eaSimpleWithElitism(population, toolbox, cxpb=P_CROSSOVER, mutpb=P_MUTATION, ngen=MAX_GENERATIONS, stats=stats, halloffame=hof, verbose=True) # print best solution found: print("- Best solution is: \n", test.formatParams(hof.items[0]), "\n => accuracy = ", hof.items[0].fitness.values[0])