Python deap.base.Fitness() Examples
The following are 30
code examples of deap.base.Fitness().
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.base
, or try the search function
.
Example #1
Source File: base.py From tpot with GNU Lesser General Public License v3.0 | 6 votes |
def _setup_toolbox(self): with warnings.catch_warnings(): warnings.simplefilter('ignore') creator.create('FitnessMulti', base.Fitness, weights=(-1.0, 1.0)) creator.create('Individual', gp.PrimitiveTree, fitness=creator.FitnessMulti, statistics=dict) self._toolbox = base.Toolbox() self._toolbox.register('expr', self._gen_grow_safe, pset=self._pset, min_=self._min, max_=self._max) self._toolbox.register('individual', tools.initIterate, creator.Individual, self._toolbox.expr) self._toolbox.register('population', tools.initRepeat, list, self._toolbox.individual) self._toolbox.register('compile', self._compile_to_sklearn) self._toolbox.register('select', tools.selNSGA2) self._toolbox.register('mate', self._mate_operator) if self.tree_structure: self._toolbox.register('expr_mut', self._gen_grow_safe, min_=self._min, max_=self._max + 1) else: self._toolbox.register('expr_mut', self._gen_grow_safe, min_=self._min, max_=self._max) self._toolbox.register('mutate', self._random_mutation_operator)
Example #2
Source File: evolutionHooks.py From japonicus with MIT License | 6 votes |
def getGlobalToolbox(representationModule): # GLOBAL FUNCTION TO GET GLOBAL TBX UNDER DEVELOPMENT; toolbox = base.Toolbox() creator.create("FitnessMax", base.Fitness, weights=(1.0,)) creator.create( "Individual", list, fitness=creator.FitnessMax, PromoterMap=None, Strategy=genconf.Strategy, ) toolbox.register("mate", representationModule.crossover) toolbox.register("mutate", representationModule.mutate) PromoterMap = initPromoterMap(Attributes) toolbox.register("newind", initInd, creator.Individual, PromoterMap) toolbox.register("population", tools.initRepeat, list, toolbox.newind) toolbox.register("constructPhenotype", representationModule.constructPhenotype) return toolbox
Example #3
Source File: oldschool.py From japonicus with MIT License | 5 votes |
def getToolbox(Strategy, genconf, Attributes): toolbox = base.Toolbox() creator = Creator.init(base.Fitness, {'Strategy': Strategy}) toolbox.register("newind", initInd, creator.Individual, Attributes) toolbox.register("population", tools.initRepeat, list, toolbox.newind) toolbox.register("mate", tools.cxTwoPoint) toolbox.register("mutate", tools.mutUniformInt, low=10, up=10, indpb=0.2) toolbox.register("constructPhenotype", constructPhenotype, Attributes) return toolbox
Example #4
Source File: 01-solve-mountain-car.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("min", numpy.min) 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: best = hof.items[0] print() print("Best Solution = ", best) print("Best Fitness = ", best.fitness.values[0]) # save best solution for a replay: car.saveActions(best)
Example #5
Source File: 03-OneMax-short-hof.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 Hall of Fame info: print("Hall of Fame Individuals = ", *hof.items, sep="\n") print("Best Ever Individual = ", hof.items[0]) # 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 #6
Source File: 01-solve-friedman.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("min", numpy.min) 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("-- Best Ever Individual = ", best) print("-- Best Ever Fitness = ", best.fitness.values[0]) # extract statistics: minFitnessValues, meanFitnessValues = logbook.select("min", "avg") # plot statistics: sns.set_style("whitegrid") plt.plot(minFitnessValues, color='red') plt.plot(meanFitnessValues, color='green') plt.xlabel('Generation') plt.ylabel('Min / Average Fitness') plt.title('Min and Average fitness over Generations') plt.show()
Example #7
Source File: 02-solve-zoo.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 solutions are:") for i in range(HALL_OF_FAME_SIZE): print(i, ": ", hof.items[i], ", fitness = ", hof.items[i].fitness.values[0], ", accuracy = ", zoo.getMeanAccuracy(hof.items[i]), ", features = ", sum(hof.items[i])) # 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 #8
Source File: bit_counter.py From Artificial-Intelligence-with-Python with MIT License | 5 votes |
def create_toolbox(num_bits): creator.create("FitnessMax", base.Fitness, weights=(1.0,)) creator.create("Individual", list, fitness=creator.FitnessMax) # Initialize the toolbox toolbox = base.Toolbox() # Generate attributes toolbox.register("attr_bool", random.randint, 0, 1) # Initialize structures toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_bool, num_bits) # Define the population to be a list of individuals toolbox.register("population", tools.initRepeat, list, toolbox.individual) # Register the evaluation operator toolbox.register("evaluate", eval_func) # Register the crossover operator toolbox.register("mate", tools.cxTwoPoint) # Register a mutation operator toolbox.register("mutate", tools.mutFlipBit, indpb=0.05) # Operator for selecting individuals for breeding toolbox.register("select", tools.selTournament, tournsize=3) return toolbox
Example #9
Source File: symbol_regression.py From Artificial-Intelligence-with-Python with MIT License | 5 votes |
def create_toolbox(): pset = gp.PrimitiveSet("MAIN", 1) pset.addPrimitive(operator.add, 2) pset.addPrimitive(operator.sub, 2) pset.addPrimitive(operator.mul, 2) pset.addPrimitive(division_operator, 2) pset.addPrimitive(operator.neg, 1) pset.addPrimitive(math.cos, 1) pset.addPrimitive(math.sin, 1) pset.addEphemeralConstant("rand101", lambda: random.randint(-1,1)) pset.renameArguments(ARG0='x') creator.create("FitnessMin", base.Fitness, weights=(-1.0,)) creator.create("Individual", gp.PrimitiveTree, fitness=creator.FitnessMin) toolbox = base.Toolbox() toolbox.register("expr", gp.genHalfAndHalf, pset=pset, min_=1, max_=2) toolbox.register("individual", tools.initIterate, creator.Individual, toolbox.expr) toolbox.register("population", tools.initRepeat, list, toolbox.individual) toolbox.register("compile", gp.compile, pset=pset) toolbox.register("evaluate", eval_func, points=[x/10. for x in range(-10,10)]) toolbox.register("select", tools.selTournament, tournsize=3) toolbox.register("mate", gp.cxOnePoint) toolbox.register("expr_mut", gp.genFull, min_=0, max_=2) toolbox.register("mutate", gp.mutUniform, expr=toolbox.expr_mut, pset=pset) toolbox.decorate("mate", gp.staticLimit(key=operator.attrgetter("height"), max_value=17)) toolbox.decorate("mutate", gp.staticLimit(key=operator.attrgetter("height"), max_value=17)) return toolbox
Example #10
Source File: robot.py From Artificial-Intelligence-with-Python with MIT License | 5 votes |
def create_toolbox(): global robot, pset pset = gp.PrimitiveSet("MAIN", 0) pset.addPrimitive(robot.if_target_ahead, 2) pset.addPrimitive(Prog().prog2, 2) pset.addPrimitive(Prog().prog3, 3) pset.addTerminal(robot.move_forward) pset.addTerminal(robot.turn_left) pset.addTerminal(robot.turn_right) creator.create("FitnessMax", base.Fitness, weights=(1.0,)) creator.create("Individual", gp.PrimitiveTree, fitness=creator.FitnessMax) toolbox = base.Toolbox() # Attribute generator toolbox.register("expr_init", gp.genFull, pset=pset, min_=1, max_=2) # Structure initializers toolbox.register("individual", tools.initIterate, creator.Individual, toolbox.expr_init) toolbox.register("population", tools.initRepeat, list, toolbox.individual) toolbox.register("evaluate", eval_func) toolbox.register("select", tools.selTournament, tournsize=7) toolbox.register("mate", gp.cxOnePoint) toolbox.register("expr_mut", gp.genFull, min_=0, max_=2) toolbox.register("mutate", gp.mutUniform, expr=toolbox.expr_mut, pset=pset) return toolbox
Example #11
Source File: train_cnn_ga.py From neuroevolution with MIT License | 5 votes |
def plot_results(filename, gen, fitness_maxs, fitness_avgs): fig, ax1 = plt.subplots() line1 = ax1.plot(gen, fitness_maxs, "r-", label="Maximum Fitness") line2 = ax1.plot(gen, fitness_avgs, "b-", label="Average Fitness") lines = line1 + line2 labs = [line.get_label() for line in lines] ax1.legend(lines, labs, loc="lower right") ax1.set_xlabel('Generation') ax1.set_ylabel('Fitness') plt.savefig('{}'.format(filename))
Example #12
Source File: 01-solve-n-queens.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("min", np.min) stats.register("avg", np.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 hall of fame members info: print("- Best solutions are:") for i in range(HALL_OF_FAME_SIZE): print(i, ": ", hof.items[i].fitness.values[0], " -> ", hof.items[i]) # plot statistics: minFitnessValues, meanFitnessValues = logbook.select("min", "avg") plt.figure(1) sns.set_style("whitegrid") plt.plot(minFitnessValues, color='red') plt.plot(meanFitnessValues, color='green') plt.xlabel('Generation') plt.ylabel('Min / Average Fitness') plt.title('Min and Average fitness over Generations') # plot best solution: sns.set_style("whitegrid", {'axes.grid' : False}) nQueens.plotBoard(hof.items[0]) # show both plots: plt.show()
Example #13
Source File: chromosome.py From japonicus with MIT License | 5 votes |
def getToolbox(Strategy, genconf, Attributes): toolbox = base.Toolbox() creator = Creator.init(base.Fitness, {'promoterMap': None, 'Strategy': Strategy}) # creator.create("FitnessMax", base.Fitness, weights=(1.0, 3)) toolbox.register("mate", pachytene) toolbox.register("mutate", mutate) PromoterMap = initPromoterMap(Attributes) toolbox.register( "newind", initInd, creator.Individual, PromoterMap, genconf.chromosome ) toolbox.register("population", tools.initRepeat, list, toolbox.newind) toolbox.register( "constructPhenotype", constructPhenotype, Attributes, genconf.chromosome ) return toolbox
Example #14
Source File: bbob.py From deap with GNU Lesser General Public License v3.0 | 5 votes |
def main(func, dim, maxfuncevals, ftarget=None): toolbox = base.Toolbox() toolbox.register("update", update) toolbox.register("evaluate", func) toolbox.decorate("evaluate", tupleize) # Create the desired optimal function value as a Fitness object # for later comparison opt = creator.FitnessMin((ftarget,)) # Interval in which to initialize the optimizer interval = -5, 5 sigma = (interval[1] - interval[0])/2.0 alpha = 2.0**(1.0/dim) # Initialize best randomly and worst as a place holder best = creator.Individual(random.uniform(interval[0], interval[1]) for _ in range(dim)) worst = creator.Individual([0.0] * dim) # Evaluate the first individual best.fitness.values = toolbox.evaluate(best) # Evolve until ftarget is reached or the number of evaluation # is exausted (maxfuncevals) for g in range(1, maxfuncevals): toolbox.update(worst, best, sigma) worst.fitness.values = toolbox.evaluate(worst) if best.fitness <= worst.fitness: # Incease mutation strength and swap the individual sigma = sigma * alpha best, worst = worst, best else: # Decrease mutation strength sigma = sigma * alpha**(-0.25) # Test if we reached the optimum of the function # Remember that ">" for fitness means better (not greater) if best.fitness > opt: return best return best
Example #15
Source File: test_algorithms.py From deap with GNU Lesser General Public License v3.0 | 5 votes |
def setup_func_single_obj(): creator.create(FITCLSNAME, base.Fitness, weights=(-1.0,)) creator.create(INDCLSNAME, list, fitness=creator.__dict__[FITCLSNAME])
Example #16
Source File: test_algorithms.py From deap with GNU Lesser General Public License v3.0 | 5 votes |
def setup_func_multi_obj(): creator.create(FITCLSNAME, base.Fitness, weights=(-1.0, -1.0)) creator.create(INDCLSNAME, list, fitness=creator.__dict__[FITCLSNAME])
Example #17
Source File: test_algorithms.py From deap with GNU Lesser General Public License v3.0 | 5 votes |
def setup_func_multi_obj_numpy(): creator.create(FITCLSNAME, base.Fitness, weights=(-1.0, -1.0)) creator.create(INDCLSNAME, numpy.ndarray, fitness=creator.__dict__[FITCLSNAME])
Example #18
Source File: test_benchmarks.py From deap with GNU Lesser General Public License v3.0 | 5 votes |
def setUp(self): @binary.bin2float(0, 1023, 10) def evaluate(individual): """Simplest evaluation function.""" return individual creator.create("FitnessMin", base.Fitness, weights=(-1.0,)) creator.create("Individual", list, fitness=creator.FitnessMin) self.toolbox = base.Toolbox() self.toolbox.register("evaluate", evaluate)
Example #19
Source File: constraint.py From deap with GNU Lesser General Public License v3.0 | 5 votes |
def __call__(self, func): @wraps(func) def wrapper(individual, *args, **kwargs): if self.fbty_fct(individual): return func(individual, *args, **kwargs) f_ind = self.fbl_fct(individual) # print("individual", f_ind) f_fbl = func(f_ind, *args, **kwargs) # print("feasible", f_fbl) weights = tuple(1.0 if w >= 0 else -1.0 for w in individual.fitness.weights) if len(weights) != len(f_fbl): raise IndexError("Fitness weights and computed fitness are of different size.") dists = tuple(0 for w in individual.fitness.weights) if self.dist_fct is not None: dists = self.dist_fct(f_ind, individual) if not isinstance(dists, Sequence): dists = repeat(dists) # print("penalty ", tuple( - w * self.alpha * d for f, w, d in zip(f_fbl, weights, dists))) # print("returned", tuple(f - w * self.alpha * d for f, w, d in zip(f_fbl, weights, dists))) return tuple(f - w * self.alpha * d for f, w, d in zip(f_fbl, weights, dists)) return wrapper
Example #20
Source File: create_toolbox.py From pyleecan with Apache License 2.0 | 5 votes |
def create_toolbox(self): """OptiGenAlgNsga2Deap method to create DEAP toolbox Parameters ---------- self : OptiGenAlgNsga2Deap Returns ------- self : OptiGenAlgNsga2Deap OptiGenAlgNsga2Deap with toolbox created """ # Create toolbox self.toolbox = base.Toolbox() # Create Fitness and individual creator.create( "FitnessMin", base.Fitness, weights=[-1 for _ in self.problem.design_var] ) creator.create("Individual", list, typecode="d", fitness=creator.FitnessMin) self.toolbox.register("creator", creator.Individual) # Register individual and population self.toolbox.register( "individual", create_indiv, self.toolbox.creator, self.problem.output, self.problem.design_var, ) self.toolbox.register("population", tools.initRepeat, list, self.toolbox.individual)
Example #21
Source File: 02-solve-nurses.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("min", numpy.min) 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("-- Best Individual = ", best) print("-- Best Fitness = ", best.fitness.values[0]) print() print("-- Schedule = ") nsp.printScheduleInfo(best) # extract statistics: minFitnessValues, meanFitnessValues = logbook.select("min", "avg") # plot statistics: sns.set_style("whitegrid") plt.plot(minFitnessValues, color='red') plt.plot(meanFitnessValues, color='green') plt.xlabel('Generation') plt.ylabel('Min / Average Fitness') plt.title('Min and Average fitness over Generations') plt.show()
Example #22
Source File: 02-hyperparameter-tuning-genetic.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: ") print("params = ", test.formatParams(hof.items[0])) print("Accuracy = %1.5f" % hof.items[0].fitness.values[0]) # 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 #23
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 #24
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 #25
Source File: 02-solve-tsp-first-attempt.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("min", np.min) stats.register("avg", np.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 individual info: best = hof.items[0] print("-- Best Ever Individual = ", best) print("-- Best Ever Fitness = ", best.fitness.values[0]) # plot best solution: plt.figure(1) tsp.plotData(best) # plot statistics: minFitnessValues, meanFitnessValues = logbook.select("min", "avg") plt.figure(2) sns.set_style("whitegrid") plt.plot(minFitnessValues, color='red') plt.plot(meanFitnessValues, color='green') plt.xlabel('Generation') plt.ylabel('Min / Average Fitness') plt.title('Min and Average fitness over Generations') # show both plots: plt.show()
Example #26
Source File: 03-solve-tsp.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("min", np.min) stats.register("avg", np.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 individual info: best = hof.items[0] print("-- Best Ever Individual = ", best) print("-- Best Ever Fitness = ", best.fitness.values[0]) # plot best solution: plt.figure(1) tsp.plotData(best) # plot statistics: minFitnessValues, meanFitnessValues = logbook.select("min", "avg") plt.figure(2) sns.set_style("whitegrid") plt.plot(minFitnessValues, color='red') plt.plot(meanFitnessValues, color='green') plt.xlabel('Generation') plt.ylabel('Min / Average Fitness') plt.title('Min and Average fitness over Generations') # show both plots: plt.show()
Example #27
Source File: moga.py From ocelot with GNU General Public License v3.0 | 5 votes |
def init_deap_functions(self): creator.create("Fitness", base.Fitness, weights=self.weights) creator.create("Individual", list, fitness=creator.Fitness) self.toolbox = base.Toolbox() self.toolbox.register("individual", tools.initIterate, creator.Individual, self.generate_ind) self.toolbox.register("population", tools.initRepeat, list, self.toolbox.individual) self.toolbox.register("evaluate", self.fit_func) if self.penalty != None: self.toolbox.decorate("evaluate", tools.DeltaPenality(self.feasible, self.inf_val))
Example #28
Source File: 01-optimize-eggholder.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("min", np.min) stats.register("avg", np.mean) # define the hall-of-fame object: hof = tools.HallOfFame(HALL_OF_FAME_SIZE) # perform the Genetic Algorithm flow with elitism: population, logbook = elitism.eaSimpleWithElitism(population, toolbox, cxpb=P_CROSSOVER, mutpb=P_MUTATION, ngen=MAX_GENERATIONS, stats=stats, halloffame=hof, verbose=True) # print info for best solution found: best = hof.items[0] print("-- Best Individual = ", best) print("-- Best Fitness = ", best.fitness.values[0]) # extract statistics: minFitnessValues, meanFitnessValues = logbook.select("min", "avg") # plot statistics: sns.set_style("whitegrid") plt.plot(minFitnessValues, color='red') plt.plot(meanFitnessValues, color='green') plt.xlabel('Generation') plt.ylabel('Min / Average Fitness') plt.title('Min and Average fitness over Generations') plt.show()
Example #29
Source File: 05-optimize-simionescu-second.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("min", np.min) stats.register("avg", np.mean) # define the hall-of-fame object: hof = tools.HallOfFame(HALL_OF_FAME_SIZE) # perform the Genetic Algorithm flow with elitism: population, logbook = elitism.eaSimpleWithElitism(population, toolbox, cxpb=P_CROSSOVER, mutpb=P_MUTATION, ngen=MAX_GENERATIONS, stats=stats, halloffame=hof, verbose=True) # print info for best solution found: best = hof.items[0] print("-- Best Individual = ", best) print("-- Best Fitness = ", best.fitness.values[0]) # extract statistics: minFitnessValues, meanFitnessValues = logbook.select("min", "avg") # plot statistics: sns.set_style("whitegrid") plt.plot(minFitnessValues, color='red') plt.plot(meanFitnessValues, color='green') plt.xlabel('Generation') plt.ylabel('Min / Average Fitness') plt.title('Min and Average fitness over Generations') plt.show()
Example #30
Source File: 04-optimize-simionescu.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("min", np.min) stats.register("avg", np.mean) # define the hall-of-fame object: hof = tools.HallOfFame(HALL_OF_FAME_SIZE) # perform the Genetic Algorithm flow with elitism: population, logbook = elitism.eaSimpleWithElitism(population, toolbox, cxpb=P_CROSSOVER, mutpb=P_MUTATION, ngen=MAX_GENERATIONS, stats=stats, halloffame=hof, verbose=True) # print info for best solution found: best = hof.items[0] print("-- Best Individual = ", best) print("-- Best Fitness = ", best.fitness.values[0]) # extract statistics: minFitnessValues, meanFitnessValues = logbook.select("min", "avg") # plot statistics: sns.set_style("whitegrid") plt.plot(minFitnessValues, color='red') plt.plot(meanFitnessValues, color='green') plt.xlabel('Generation') plt.ylabel('Min / Average Fitness') plt.title('Min and Average fitness over Generations') plt.show()