def main(): random.seed(1024) ind = toolbox.individual() pop = toolbox.population(n=100) 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) column_names = ["gen", "evals"] column_names.extend(stats.functions.keys()) logger = tools.EvolutionLogger(column_names) logger.logHeader() CXPB, MUTPB, NGEN = 0.5, 0.2, 40 # Evaluate the entire population for ind in pop: ind.fitness.values = toolbox.evaluate(ind) hof.update(pop) stats.update(pop) logger.logGeneration(gen=0, evals=len(pop), stats=stats) for g in range(1, NGEN): # Select the offspring offspring = toolbox.select(pop, len(pop)) # Clone the offspring offspring = [toolbox.clone(ind) for ind in offspring] # Apply crossover and mutation for ind1, ind2 in zip(offspring[::2], offspring[1::2]): for tree1, tree2 in zip(ind1, ind2): if random.random() < CXPB: toolbox.mate(tree1, tree2) del ind1.fitness.values del ind2.fitness.values for ind in offspring: for tree in ind: if random.random() < MUTPB: toolbox.mutate(tree) del ind.fitness.values # Evaluate the individuals with an invalid fitness invalids = [ind for ind in offspring if not ind.fitness.valid] for ind in invalids: ind.fitness.values = toolbox.evaluate(ind) # Replacement of the population by the offspring pop = offspring hof.update(pop) stats.update(pop) logger.logGeneration(gen=g, evals=len(invalids), stats=stats) print('Best individual : ', gp.stringify(hof[0][0]), hof[0].fitness) return pop, stats, hof
def main(): pop_ga = toolbox_ga.population(n=200) pop_gp = tools_gp.population(n=200) stats_ga = tools.Statistics(lambda ind: ind.fitness.values) stats_ga.register("avg", tools.mean) stats_ga.register("std", tools.std) stats_ga.register("min", min) stats_ga.register("max", max) stats_gp = tools.Statistics(lambda ind: ind.fitness.values) stats_gp.register("avg", tools.mean) stats_gp.register("std", tools.std) stats_gp.register("min", min) stats_gp.register("max", max) column_names = ["gen", "evals"] column_names.extend(stats_ga.functions.keys()) logger = tools.EvolutionLogger(column_names) logger.logHeader() best_ga = tools.selRandom(pop_ga, 1)[0] best_gp = tools.selRandom(pop_gp, 1)[0] for ind in pop_gp: ind.fitness.values = evalSymbReg(ind, best_ga) for ind in pop_ga: ind.fitness.values = evalSymbReg(best_gp, ind) stats_ga.update(pop_ga) stats_gp.update(pop_gp) logger.logGeneration(gen="0 (ga)", evals=len(pop_ga), stats=stats_ga) logger.logGeneration(gen="0 (gp)", evals=len(pop_gp), stats=stats_gp) CXPB, MUTPB, NGEN = 0.5, 0.2, 50 # Begin the evolution for g in range(1, NGEN): # Select and clone the offspring off_ga = toolbox_ga.select(pop_ga, len(pop_ga)) off_gp = tools_gp.select(pop_gp, len(pop_gp)) off_ga = [toolbox_ga.clone(ind) for ind in off_ga] off_gp = [tools_gp.clone(ind) for ind in off_gp] # Apply crossover and mutation for ind1, ind2 in zip(off_ga[::2], off_ga[1::2]): if random.random() < CXPB: toolbox_ga.mate(ind1, ind2) del ind1.fitness.values del ind2.fitness.values for ind1, ind2 in zip(off_gp[::2], off_gp[1::2]): if random.random() < CXPB: tools_gp.mate(ind1, ind2) del ind1.fitness.values del ind2.fitness.values for ind in off_ga: if random.random() < MUTPB: toolbox_ga.mutate(ind) del ind.fitness.values for ind in off_gp: if random.random() < MUTPB: tools_gp.mutate(ind) del ind.fitness.values # Evaluate the individuals with an invalid fitness for ind in off_ga: ind.fitness.values = evalSymbReg(best_gp, ind) for ind in off_gp: ind.fitness.values = evalSymbReg(ind, best_ga) # Replace the old population by the offspring pop_ga = off_ga pop_gp = off_gp stats_ga.update(pop_ga) stats_gp.update(pop_gp) best_ga = tools.selBest(pop_ga, 1)[0] best_gp = tools.selBest(pop_gp, 1)[0] logger.logGeneration(gen="%d (ga)" % g, evals=len(off_ga), stats=stats_ga) logger.logGeneration(gen="%d (gp)" % g, evals=len(off_gp), stats=stats_gp) print("Best individual GA is %s, %s" % (best_ga, best_ga.fitness.values)) print("Best individual GP is %s, %s" % (gp.stringify(best_gp), best_gp.fitness.values)) return pop_ga, pop_gp, stats_ga, stats_gp, best_ga, best_gp
def generateWebService(self,individual): """ Generates a file called 'clonedwebservice.py' that contains a webservice with the specified individual""" generateService(self.configuration.imports, getSignature(self.configuration.testArguments), self.lambdify(gp.stringify(individual),self.configuration.pset), self.configuration.configClass,)
def generateSource(self,individual): """Takes all the information from the configuration file and represents the source of the individual """ source = self.configuration.configClass.getSource(self.configuration.imports, self.lambdify(gp.stringify(individual),self.configuration.pset), getSignature(self.configuration.testArguments)) return source
def outputIndividuals(self): inds = [] for ind in self.hof: inds.append((gp.stringify(ind), ind.fitness)) return inds