def main(): """ the function performs a genetic simulation trying to find a solution to the knapsack problem """ # in order to test main with different parameters, change the value of this constantes NUM_GEN = 50 NUM_SELECT = 50 # number of individuals to select for the next generation NUM_CHILDREN = 100 # number of children to produce at each generation CXPB = 0.7 MUTPB = 0.2 constructItens() toolbox = defineBasic() pop = toolbox.population(n = NUM_SELECT) hall_fame = tools.ParetoFront() # display statistics 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) # use algorithm available from deap to solve problem algorithms.eaMuPlusLambda(pop, toolbox, NUM_SELECT, NUM_CHILDREN, CXPB, MUTPB, NUM_GEN, stats, halloffame = hall_fame) return (pop, stats, hall_fame)
def main(self): ''' Fonction principale de l'optimisation enregistre les fonctions et types définis précédemment dans la toolbox et lance les calculs Returns: le hall of fame des meilleurs individus ''' creator.create("Fitness", base.Fitness, weights=(-1,)) creator.create("Individual", dict, fitness=creator.Fitness) toolbox = base.Toolbox() toolbox.register("individual", self.createInd, creator.Individual) toolbox.register("population", tools.initRepeat, list, toolbox.individual) toolbox.register("evaluate", self.evaluate) toolbox.register("mutate", self.mutate) toolbox.register("select", self.selCustom) toolbox.register("clone", copy.deepcopy) toolbox.register("mate", self.crossMultiPoint) stats = tools.Statistics() stats.register("max", self.max) stats.register("min", self.min) stats.register("nbCurrentGen", self.nbGen) hof = tools.HallOfFame(10, similar=self.equals) pop = toolbox.population(n=self.mu) algorithms.eaMuPlusLambda(pop, toolbox, mu=self.mu, lambda_=self.lambda_, cxpb=self.cxpb, mutpb=self.mutpb, ngen=self.ngen, halloffame=hof, stats=stats) #print "fini" return hof
def main(self): pop = self.toolbox.population(self.population) pop = self.inject_initial_individuals(pop) CXPB, MUTPB, NGEN = self.crossover_probability, self.mutation_probability, self.ngen hof = tools.HallOfFame(1) stats = tools.Statistics() def fun(x): np.savetxt(self.savep, np.array(hof), delimiter=",") if self.print_to_stdout: print "fitness", self.evaluate(hof[0]) stats.register("hofe", fun) algorithms.eaMuPlusLambda( pop, self.toolbox, lambda_=self.population, mu=self.population, cxpb=CXPB, mutpb=MUTPB, ngen=NGEN, stats=stats, halloffame=hof, verbose=True, )
def computePlus(): random.seed(47) pop = toolbox.population(n=MU) hof = tools.HallOfFame(1) algorithms.eaMuPlusLambda(toolbox, pop, MU, LAMBDA, 0.3 , 0.5, N_GEN, hof) return sorted(list(hof[-1]))
def run(self): """ Start genetic programming to summ up t """ NGEN = self.kwargs.get("NGEN", 10) LAMBDA = self.kwargs.get("LAMBDA", 100) CXPB = self.kwargs.get("CXPB", 0.5) MUTPB = self.kwargs.get("MUTPB", 0.3) algorithms.eaMuPlusLambda( self.pop, self.toolbox, self.MU, LAMBDA, CXPB, MUTPB, NGEN, self.stats, halloffame=self.hof ) return self
def get_halloffame(self): pop = self.toolbox.population(n = self.params['pop_size']) hof = tools.HallOfFame(self.params['hof_size']) algorithms.eaMuPlusLambda( pop, self.toolbox, mu = self.params['mu'], lambda_ = self.params['lambda_'], cxpb = self.params['cxpb'], mutpb = self.params['mutpb'], ngen = self.params['num_generations'], stats = self.stats, halloffame = hof, verbose = self.params['verbose'] ) return hof
def main(): # random.seed(10) pop = toolbox.population(n=2000) hof = tools.ParetoFront() 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.eaMuPlusLambda(pop, toolbox, mu=1000, lambda_=2000, cxpb=0.65, mutpb=0.35, ngen=1000, stats=stats, halloffame=hof) """printOnlySolutions(hof)""" getBestSolution(hof)
def main(): random.seed(64) NGEN = 50 MU = 50 LAMBDA = 100 CXPB = 0.7 MUTPB = 0.2 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 = toolbox.population(n=MU) jobs = toolbox.map(toolbox.evaluate, pop) fitnesses = jobs.get() for ind, fit in zip(pop,fitnesses): ind.fitness.values = fit for g in range(NGEN): hof = tools.ParetoFront() pop, logbook = algorithms.eaMuPlusLambda( pop, toolbox, MU, LAMBDA, CXPB, MUTPB, NGEN, stats, halloffame=hof) return pop, stats, hof
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", tools.mean) stats.register("std", tools.std) stats.register("min", min) stats.register("max", max) algorithms.eaMuPlusLambda(pop, toolbox, mu=MU, lambda_=LAMBDA, cxpb=0.7, mutpb=0.3, ngen=40, stats=stats, halloffame=hof) return pop, stats, hof
def main(): NGEN = 10 MU = 25 LAMBDA = 25 CXPB = 0.7 MUTPB = 0.2 random.seed(60) print("Beginning Initial Learning\n") apriori.learn(MIN_SUPPORT_THRESHOLD, MIN_CONF_THRESHOLD, DEFAULT_COVERAGE_THRESHOLD, verbose = True) print("\n\nInitial Learning complete") 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) result = algorithms.eaMuPlusLambda(pop, toolbox, MU, LAMBDA, CXPB, MUTPB, NGEN, stats, halloffame=hof) best_ind = tools.selBest(pop, 1)[0] logbook = result[1] print("Best individual found using GA is %s, %s" % (best_ind, best_ind.fitness.values)) return logbook.select("gen", "avg", "min", "max")
def solve(self, graph, output_stats = False, output_plot = False, filename = None): self.graph = graph self.toolbox.register("individual", tools.initRepeat, creator.Individual, self.toolbox.attr_bool, graph.indSize) self.toolbox.register("population", tools.initRepeat, list, self.toolbox.individual) pop = self.toolbox.population(n=self.paramSetting.popSize) hof = tools.HallOfFame(self.paramSetting.hofSize) #print('initial_population:', pop) #pop, logbook = algorithms.eaSimple(pop, toolbox, cxpb=paramSetting.mateProb, mutpb=paramSetting.mutateProb, ngen=paramSetting.numGens, stats=stats, halloffame=hof, verbose=True) pop, logbook = algorithms.eaMuPlusLambda(pop, self.toolbox, lambda_=self.paramSetting.popSize, mu=self.paramSetting.mu, cxpb=self.paramSetting.mateProb, mutpb=self.paramSetting.mutateProb, ngen=self.paramSetting.numGens, stats=self.stats, halloffame=hof, verbose = self.paramSetting.verbose) if output_plot: gen, avg, min_, max_ = logbook.select("gen", "avg", "min", "max") plt.plot(gen, avg, label="average") plt.plot(gen, min_, label="minimum") plt.plot(gen, max_, label="maximum") plt.xlabel("Generation") plt.ylabel("Fitness") plt.legend(loc="lower right") plt.savefig(filename) plt.close() if output_stats: return pop, hof return pop
def main(self): random.seed() MU = 50 LAMBDA = 100 CXPB = 0.7 MUTPB = 0.2 pop = self.toolbox.population(n=MU) hof = tools.HallOfFame(100) stats = tools.Statistics(lambda ind: ind.fitness.values) stats.register("avg", numpy.mean, axis=0) stats.register("std", numpy.std, axis=0) stats.register("max", numpy.max, axis=0) algorithms.eaMuPlusLambda(pop, self.toolbox, MU, LAMBDA, CXPB, MUTPB, NGEN, stats, halloffame=hof) return pop, stats, hof
def evolve_string(text): """Use evolutionary algorithm (EA) to evolve 'text' string""" # Set random number generator initial seed so that results are repeatable. # See: https://docs.python.org/2/library/random.html#random.seed # and http://xkcd.com/221 random.seed(4) # Get configured toolbox and create a population of random Messages toolbox = get_toolbox(text) pop = toolbox.population(n=300) # Collect statistics as the EA runs 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) # Run simple EA # (See: http://deap.gel.ulaval.ca/doc/dev/api/algo.html for details) pop, log = algorithms.eaMuPlusLambda(pop, toolbox, mu = 5, lambda_ = 5, cxpb=0.5, # Prob. of crossover (mating) mutpb=0.2, # Probability of mutation ngen=500, # Num. of generations to run stats=stats) return pop, log
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
def main(conf): pop, toolbox, hof, stats = bootstrap(conf) prog = Progress(conf) pop, logbook = algorithms.eaMuPlusLambda(pop, toolbox, mu=conf.MU, lambda_=conf.LAMBDA, cxpb=conf.CXPB, mutpb=conf.MUTPB, ngen=0, stats=stats, halloffame=hof) prog.update(0,pop,hof) for gen in range(conf.NGEN/5): pop, logbook = algorithms.eaMuPlusLambda(pop, toolbox, mu=conf.MU, lambda_=conf.LAMBDA, cxpb=conf.CXPB, mutpb=conf.MUTPB, ngen=5, stats=stats, halloffame=hof) prog.update((gen+1)*5,pop,hof) #prog.hold() return pop, stats, hof
def main(): toolbox.register("evaluate", eval_usage_cost) toolbox.register("mate", mate_individuals) toolbox.register("mutate", mutate_individual) toolbox.register("select", tools.selNSGA2) toolbox.register("map", futures.map) pop = toolbox.population(n=MU) hof = tools.ParetoFront() stats = tools.Statistics(lambda ind: ind.fitness.values) stats.register("avg", np.mean, axis=0) stats.register("std", np.std, axis=0) stats.register("min", np.min, axis=0) stats.register("max", np.max, axis=0) algorithms.eaMuPlusLambda(pop, toolbox, MU, LAMBDA, CXPB, MUTPB, NGEN, stats, halloffame=hof) return pop, stats, hof
def main(): population_size = 50 lambda_size = 50 pop, toolbox, hof, stats = bootstrap(population_size) prog = Progress() pop, logbook = algorithms.eaMuPlusLambda(pop, toolbox, mu=population_size, lambda_=lambda_size, cxpb=0.7, mutpb=0.05, ngen=0, stats=stats, halloffame=hof) prog.update(0,pop,hof) for gen in range(1000): pop, logbook = algorithms.eaMuPlusLambda(pop, toolbox, mu=population_size, lambda_=lambda_size, cxpb=0.7, mutpb=0.05, ngen=1, stats=stats, halloffame=hof) prog.update((gen+1),pop,hof) return pop, stats, hof
def main(MU, LAMBDA, CXPB, MUTPB): NGEN = 50 # Número de gerações. Não entendi o motivo, mas o algorítmo apresenta erro para valores != 50. # MU = 50 # Número de indivíduos para selecionar para próxima geração. # LAMBDA = 100 # Número de filhos para serem produzidos a cada geração. # CXPB = 0.7 # Probapilidade de crossover. # MUTPB = 0.2 # Probabilidade de Mutação. pop = toolbox.population(n = MU) # População. hof = tools.ParetoFront() # Melhores individuos. 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
def main(toolbox): NGEN = 2000 MU = 200 LAMBDA = 300 CXPB = 0.7 MUTPB = 0.2 pop = toolbox.population(n=MU) hof = tools.ParetoFront() stats = tools.Statistics(lambda ind: ind.fitness.values) stats_len = tools.Statistics(key=len) mstats = tools.MultiStatistics(xfitness=stats, length=stats_len) mstats.register("avg", numpy.mean, axis=0) mstats.register("std", numpy.std, axis=0) algorithms.eaMuPlusLambda(pop, toolbox, MU, LAMBDA, CXPB, MUTPB, NGEN, mstats, halloffame=hof) return toolbox.unmap(pop), stats, toolbox.unmap(hof)
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", tools.mean) stats.register("std", tools.std) stats.register("min", min) stats.register("max", max) algorithms.eaMuPlusLambda(pop, toolbox, MU, LAMBDA, CXPB, MUTPB, NGEN, stats, halloffame=hof) return pop, stats, hof
def main(): start_time = time.time() random.seed(64) NGEN = 2000 MU = 200 LAMBDA = 100 CXPB = 0.5 MUTPB = 0.5 pop = toolbox.population(n=MU) hall = tools.ParetoFront() algorithms.eaMuPlusLambda(pop, toolbox, MU, LAMBDA, CXPB, MUTPB, NGEN, halloffame=hall) print "\nExecution stopped after : " + str(time.time() - start_time) # print the hall of fame hall_string = "" inds_string = ";" for i in hall: hall_string += "(d : %f e: %f n: %d)" % ( calculate_dist(problem, i)[0], i.fitness.values[0], i.fitness.values[1], ) inds_string += str(i) + ";" if ("-s" in sys.argv) or ("--save" in sys.argv): dump_file = open(instance + ".solution", "wb") pareto_list = [] for i in hall: # also check if valid solution pareto_list.append([j for j in i]) pickle.dump(pareto_list, dump_file) for i in pop: if i.fitness.values[0] < 100000.0: print calculate_dist(problem, i)[0], i.fitness.values[0], i.fitness.values[1] print str(i) + ";" return hall_string + inds_string
def main(): #print "Inside Main" random.seed(time.clock()) NGEN = 100 MU = 50 #Number of populations LAMBDA = 100 #Number of individuals in the population 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", tools.mean) stats.register("std", tools.std) stats.register("min", min) stats.register("max", max) algorithms.eaMuPlusLambda(pop, toolbox, MU, LAMBDA, CXPB, MUTPB, NGEN, stats, halloffame=hof) return pop, stats, hof
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
def runGA(pop): '''run GA with early stopping if not improving''' hof = tools.HallOfFame(10) stats = tools.Statistics(lambda ind: ind.fitness.values) stats.register("avg", np.mean) stats.register("std", np.std) stats.register("min", np.min) stats.register("max", np.max) best = 0 pop = toolbox.clone(pop) for i in xrange(40): pop, log = algorithms.eaMuPlusLambda(pop, toolbox, mu=300, lambda_=300, cxpb=0.5, mutpb=0.2, ngen=25, stats=stats, halloffame=hof, verbose=True) newmax = log[-1]['max'] if best == newmax: break best = newmax return pop
def main(): import numpy pop = toolbox.population(n=5) hof = tools.HallOfFame(3) stats = tools.Statistics(lambda ind: ind.fitness.values) stats.register("avg", numpy.mean) stats.register("min", numpy.min) stats.register("max", numpy.max) # Simple EA #pop, logbook = algorithms.eaSimple(pop, toolbox, cxpb=0.5, mutpb=0.2, ngen=5, stats=stats, halloffame=hof, verbose=True) # Mu+lambda NGEN = 5 MU = 50 LAMBDA = 100 CXPB = 0.7 MUTPB = 0.2 pop, logbook = algorithms.eaMuPlusLambda(pop, toolbox, MU, LAMBDA, CXPB, MUTPB, NGEN, stats, halloffame=hof, verbose=True) return pop, logbook, hof
def run_ga_optimization(evaluate_func: EVALUATE_FUNC, optimization_setting: OptimizationSetting, key_func: KEY_FUNC, max_workers: int = None, population_size: int = 100, ngen_size: int = 30, output: OUTPUT_FUNC = print) -> List[Tuple]: """Run genetic algorithm optimization""" # Define functions for generate parameter randomly buf: List[Dict] = optimization_setting.generate_settings() settings: List[Tuple] = [list(d.items()) for d in buf] def generate_parameter() -> list: """""" return choice(settings) def mutate_individual(individual: list, indpb: float) -> tuple: """""" size = len(individual) paramlist = generate_parameter() for i in range(size): if random() < indpb: individual[i] = paramlist[i] return individual, # Set up multiprocessing Pool and Manager with Manager() as manager, Pool(max_workers) as pool: # Create shared dict for result cache cache: Dict[Tuple, Tuple] = manager.dict() # Set up toolbox toolbox = base.Toolbox() toolbox.register("individual", tools.initIterate, creator.Individual, generate_parameter) toolbox.register("population", tools.initRepeat, list, toolbox.individual) toolbox.register("mate", tools.cxTwoPoint) toolbox.register("mutate", mutate_individual, indpb=1) toolbox.register("select", tools.selNSGA2) toolbox.register("map", pool.map) toolbox.register("evaluate", ga_evaluate, cache, evaluate_func, key_func) total_size: int = len(settings) pop_size: int = population_size # number of individuals in each generation lambda_: int = pop_size # number of children to produce at each generation mu: int = int( pop_size * 0.8) # number of individuals to select for the next generation cxpb: float = 0.95 # probability that an offspring is produced by crossover mutpb: float = 1 - cxpb # probability that an offspring is produced by mutation ngen: int = ngen_size # number of generation pop: list = toolbox.population(pop_size) # Run ga optimization output(f"开始执行遗传算法优化") output(f"参数优化空间:{total_size}") output(f"每代族群总数:{pop_size}") output(f"优良筛选个数:{mu}") output(f"迭代次数:{ngen}") output(f"交叉概率:{cxpb:.0%}") output(f"突变概率:{mutpb:.0%}") start: int = perf_counter() algorithms.eaMuPlusLambda(pop, toolbox, mu, lambda_, cxpb, mutpb, ngen, verbose=False) end: int = perf_counter() cost: int = int((end - start)) output(f"遗传算法优化完成,耗时{cost}秒") results: list = list(cache.values()) results.sort(reverse=True, key=key_func) return results
individual.add(random.randrange(NBR_ITEMS)) return individual, toolbox.register("evaluate", evalKnapsack) toolbox.register("mate", cxSet) toolbox.register("mutate", mutSet) toolbox.register("select", tools.selNSGA2) 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) print("resultado") #print (pop) for i in hof[len(hof)-1]: print (items[i]) #return pop, stats, hof
nx.draw_networkx_nodes(g, pos) nx.draw_networkx_edges(g, pos) nx.draw_networkx_labels(g, pos, labels) nx.draw(g, pos) plt.show() # In[285]: pop = toolbox.population(n=200) hof = tools.HallOfFame(1) stats = tools.Statistics(lambda ind: ind.fitness.values) stats.register('avg', np.mean) stats.register('min', np.min) stats.register('max', np.max) pop, log = algorithms.eaMuPlusLambda(pop, toolbox, 160, 160, 0.6, 0.1, 50, stats=stats, halloffame=hof) # get the info of best solution print("Best solution found...") print(hof[0]) plot(hof[0]) f = toolbox.compile(hof[0])
def main(): data = arff.load(open('Data/miyazaki94.arff')) # variables = data training_data, test_data = split_data(data['data'], 0.75) pset = get_primitive_set_miyazaki() print() creator.create("FitnessMin", base.Fitness, weights=(-1.0, )) # -1.0 as its a minimise function 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) fit_func = get_fitness_function_miyazaki(toolbox) toolbox.register("evaluate", fit_func, training_data) 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=5)) toolbox.decorate( "mutate", gp.staticLimit(key=operator.attrgetter("height"), max_value=5)) 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", np.mean) mstats.register("std", np.std) mstats.register("min", np.min) mstats.register("max", np.max) no_generations = 100 no_runs = 1 eaSimple_data = np.zeros((no_generations, no_runs)) #for j in range(no_runs): # for i in range(no_generations): # pop, log = algorithms.eaSimple(pop, toolbox, 0.5, 0.1, 1, stats=mstats, # halloffame=hof, verbose=True) # x = fit_func(test_data, hof.items[0])[0] # eaSimple_data[i, j] = x # print(j) random.seed(318) pop = toolbox.population(n=300) aMuPlusLambda_data = np.zeros((no_generations, no_runs)) for j in range(no_runs): for i in range(no_generations): pop, log = algorithms.eaMuPlusLambda(pop, toolbox, 100, 300, 0.25, 0.75, 1, stats=mstats, \ halloffame=hof, verbose=False) eaSimple_data[i, j] = hof.items[0].fitness.values[0] aMuPlusLambda_data[i, j] = fit_func(test_data, hof.items[0])[0] print(j) plt.plot(range(no_generations), np.mean(aMuPlusLambda_data, axis=1), color='b') #test plt.plot(range(no_generations), np.mean(eaSimple_data, axis=1), color='r') # train plt.show() # print log #print(str(hof.items[0]) + " " + str(hof.items[0].fitness)) #print(fit_func(test_data, hof.items[0])) return pop, log, hof
def run_ga_optimization(self, optimization_setting: OptimizationSetting, population_size=100, ngen_size=30, output=True): """""" # Get optimization setting and target settings = optimization_setting.generate_setting_ga() target_name = optimization_setting.target_name if not settings: self.output("优化参数组合为空,请检查") return if not target_name: self.output("优化目标未设置,请检查") return # Define parameter generation function def generate_parameter(): """""" return random.choice(settings) def mutate_individual(individual, indpb): """""" size = len(individual) paramlist = generate_parameter() for i in range(size): if random.random() < indpb: individual[i] = paramlist[i] return individual, # Create ga object function global ga_target_name global ga_strategy_class global ga_setting global ga_vt_symbol global ga_interval global ga_start global ga_rate global ga_slippage global ga_size global ga_pricetick global ga_capital global ga_end global ga_mode global ga_inverse ga_target_name = target_name ga_strategy_class = self.strategy_class ga_setting = settings[0] ga_vt_symbol = self.vt_symbol ga_interval = self.interval ga_start = self.start ga_rate = self.rate ga_slippage = self.slippage ga_size = self.size ga_pricetick = self.pricetick ga_capital = self.capital ga_end = self.end ga_mode = self.mode ga_inverse = self.inverse # Set up genetic algorithem toolbox = base.Toolbox() toolbox.register("individual", tools.initIterate, creator.Individual, generate_parameter) toolbox.register("population", tools.initRepeat, list, toolbox.individual) toolbox.register("mate", tools.cxTwoPoint) toolbox.register("mutate", mutate_individual, indpb=1) toolbox.register("evaluate", ga_optimize) toolbox.register("select", tools.selNSGA2) total_size = len(settings) pop_size = population_size # number of individuals in each generation lambda_ = pop_size # number of children to produce at each generation mu = int( pop_size * 0.8) # number of individuals to select for the next generation cxpb = 0.95 # probability that an offspring is produced by crossover mutpb = 1 - cxpb # probability that an offspring is produced by mutation ngen = ngen_size # number of generation pop = toolbox.population(pop_size) hof = tools.ParetoFront() # end result of pareto front stats = tools.Statistics(lambda ind: ind.fitness.values) np.set_printoptions(suppress=True) stats.register("mean", np.mean, axis=0) stats.register("std", np.std, axis=0) stats.register("min", np.min, axis=0) stats.register("max", np.max, axis=0) # Multiprocessing is not supported yet. # pool = multiprocessing.Pool(multiprocessing.cpu_count()) # toolbox.register("map", pool.map) # Run ga optimization self.output(f"参数优化空间:{total_size}") self.output(f"每代族群总数:{pop_size}") self.output(f"优良筛选个数:{mu}") self.output(f"迭代次数:{ngen}") self.output(f"交叉概率:{cxpb:.0%}") self.output(f"突变概率:{mutpb:.0%}") start = time() algorithms.eaMuPlusLambda(pop, toolbox, mu, lambda_, cxpb, mutpb, ngen, stats, halloffame=hof) end = time() cost = int((end - start)) self.output(f"遗传算法优化完成,耗时{cost}秒") # Return result list results = [] for parameter_values in hof: setting = dict(parameter_values) target_value = ga_optimize(parameter_values)[0] results.append((setting, target_value, {})) return results
def deap_solver(design_var_dict, obj_func_list, obj_func_names=None, norm_facts=None, POPSIZE=1000, GENS=50, MUTPB=None, CXPB=None, SURVIVORS=100, CHILDREN=1000, constraints=None): """ This is an optimization solver based on the Distributed Evolutionary Algorithms in Python (DEAP) package. It uses binary representations of design variables in each individual. Given an objective function (or list of objective functions) and constraints, the solver evolves each population of individuals based on the NSGA-II genetic algorithm. :param design_var_dict: dictionary of design variables :param obj_func_list: list of anonymous functions that represents the objective(s) :param obj_func_names: optional, specifies function names :param norm_facts: specify normalization factors for objective function outputs. If problem has multiple objective functions with outputs expected to be on different orders of magnitude, this is very important to include. Choose normalization factors approximately equal to maximum expected output from its associated function :param POPSIZE: size of initial population :param GENS: number of generations to be evaluated :param MUTPB: probability of mutation :param CXPB: probability of crossover :param SURVIVORS: number of individuals selected after initial population is evaluated. This governs the size of all future populations :param CHILDREN: number of offspring to be produced from the current population at each generation :param constraints: list constraints to be applied in evaluating the validity of each individual :return: the solver returns a table of solution points that contains the variable and objective function values for all optimized solutions. A print(results) statement will display the table. For problems with two objective functions, the solver also returns a plot of the Pareto Frontier EXAMPLE SETUP FOR SOLVER: design_var_dict = {'r': {'interval': [0,10],'bits': 10},'h':{'interval':[0,20],'bits':10}} S = lambda r,h: math.pi*r*math.sqrt(r**2+h**2) T = lambda r,h: math.pi*r**2+math.pi*r*math.sqrt(r**2+h**2) obj_func_list = [S,T] obj_func_names = ['Lateral Surface Area','Total Area'] norm_facts = [155,225] constraints = [[V, '>', 200]] --> [[func(r,h),'operator',val],[func(r,h),'operator',val],etc.] gens = 50 popSize = 1000 mutPB = 0.1 cxPB = 0.9 survivors = 100 children = 1000 results = deapSolver(design_vars, func_list, obj_func_names=func_names, norm_facts=norm_facts, POPSIZE=popSize, GENS=gens, MUTPB=mutPB, CXPB=cxPB, SURVIVORS=survivors, CHILDREN=children, constraints=constraint_list) """ # Assign values optional keyword arguments whose length depends on the number of objective functions specified if norm_facts is None: norm_facts = [1 for _ in obj_func_list] if MUTPB is None: maxbits = 0 for var in design_var_dict: if design_var_dict[var]['bits'] > maxbits: maxbits = design_var_dict[var]['bits'] MUTPB = 1 / maxbits if CXPB is None: CXPB = 1 - MUTPB if obj_func_names is None: obj_func_names = [ 'f{}'.format(i + 1) for i in range(len(obj_func_list)) ] def bi2de_ind( individual ): # convert binary individuals to list of decimal variable values deciInd = [] counter = 0 for var in design_var_dict: xL = design_var_dict[var]['interval'][0] xU = design_var_dict[var]['interval'][1] diff = xU - xL bits = design_var_dict[var]['bits'] step = diff / (2**bits - 1) binary = ''.join(map(str, individual[counter:(counter + bits)])) deciVal = int(binary, 2) deciInd.append(xL + deciVal * step) counter += bits return deciInd def func_eval( individual ): # evaluate individual fitness by plugging variables into objective function(s) deciInd = bi2de_ind(individual) outputs = [ func(*deciInd) / norm_fact for func, norm_fact in zip(obj_func_list, norm_facts) ] return tuple(outputs) ops = { '>': operator.gt, '<': operator.lt, '>=': operator.ge, '<=': operator.le, '=': operator.eq } #register operator dictionary for constraint evaluation def feasibility( individual ): # determine if an individual's variables violate any constraints deciInd = bi2de_ind(individual) for constraint in constraints: if ops[constraint[1]](constraint[0](*deciInd), constraint[2]) is False: return False return True def uniform( ): # fill each individual in the initial population with total bits in design_var_dict bits = 0 for var in design_var_dict: bits += design_var_dict[var]['bits'] individual = [random.randint(0, 1) for _ in range(bits)] return individual def cx_list(ind1, ind2): # define crossover strategy for lists cxPt = random.randint(0, len(ind1) - 1) child1 = toolbox.individual() child2 = toolbox.individual() child1[::] = ind1[0:cxPt] + ind2[cxPt::] child2[::] = ind2[0:cxPt] + ind1[cxPt::] return child1, child2 def mut_list(individual): # define mutation strategy for lists mutPoint = random.randint(0, len(individual) - 1) if individual[mutPoint] == 1: individual[mutPoint] = 0 else: individual[mutPoint] = 1 return individual, weights = tuple([-1.0 for _ in obj_func_list ]) # weight for all objectives are defaulted to -1 creator.create( "Fitness", base.Fitness, weights=weights ) # create fitness class inherited from 'base.Fitness' class creator.create("Individual", list, fitness=creator.Fitness ) # create Individual class inherited from 'list' class toolbox = base.Toolbox( ) # initialize toolbox class from 'base', contains all evolutionary operators toolbox.register( "initializer", uniform) # register function 'uniform' to initialize individuals toolbox.register( "individual", tools.initIterate, creator.Individual, toolbox.initializer) # register 'individual' creator in toolbox toolbox.register( "population", tools.initRepeat, list, toolbox.individual) # register 'population' creator in toolbox toolbox.register( "mate", cx_list) # register crossover strategy as function 'cx_list' toolbox.register( "mutate", mut_list) # register mutation strategy as function 'mut_list' toolbox.register( "evaluate", func_eval) # register evaluation strategy as function 'func_eval' if constraints: toolbox.decorate( "evaluate", tools.DeltaPenality(feasibility, 10000) ) # decorate evaluation strategy with constraints if they exist, penalize invalid individuals toolbox.register( "select", tools.selNSGA2 ) # register selection strategy for sorting evaluated individuals as NSGA-II stats = tools.Statistics( lambda ind: ind.fitness.values) # log statistics during evolution stats.register("avg", np.mean, axis=0) stats.register("std", np.std, axis=0) stats.register("min", np.min, axis=0) stats.register("max", np.max, axis=0) pop = toolbox.population(n=POPSIZE) # generate initial population hof = tools.ParetoFront( ) # register criteria for selecting hall of fame individuals (Pareto dominant solutions) output = algorithms.eaMuPlusLambda( pop, toolbox, SURVIVORS, CHILDREN, CXPB, MUTPB, GENS, stats, halloffame=hof) # conduct evolutionary optimization process headerList = ['Solution Point'] for var in design_var_dict: headerList.append(var) for name in obj_func_names: headerList.append(name) results = PrettyTable( headerList) # generate table to store and display results solution = 1 for individual in output[0]: # put optimal solutions into 'results' table solutionList = [solution] deciInd = bi2de_ind(individual) for val in deciInd: solutionList.append(val) for val, norm_fact in zip(individual.fitness.values, norm_facts): solutionList.append(val * norm_fact) results.add_row(solutionList) solution += 1 if len(obj_func_list ) == 2: # if two objective functions provided, plot Pareto Frontier non_dom = tools.sortNondominated(output[0], k=len(output[0]), first_front_only=True)[0] for ind in non_dom: fitvals = [ ind.fitness.value * norm_fact for ind.fitness.value, norm_fact in zip( ind.fitness.values, norm_facts) ] plt.plot(*fitvals, 'bo') plt.title('Pareto Front') plt.xlabel('{}'.format(obj_func_names[0])) plt.ylabel('{}'.format(obj_func_names[1])) plt.show() return results else: return results
def deap_solver(design_var_dict, obj_func_names=None, norm_facts=None, POPSIZE=1000, GENS=50, MUTPB=None, CXPB=None, SURVIVORS=100, CHILDREN=1000, constraints=None): combination_dict = {'initial_val': None} def func_eval(individual): # evaluate individual fitness by plugging variables into objective function(s) print(individual) combination = '{}, {:.1f}, {:.0f}'.format(*individual) #USE THIS LINE FOR ZIG-ZAG MULTI-VARIABLE # combination = '{}, {:.1f}'.format(*individual) #USE THIS LINE FOR GRID if combination not in combination_dict.keys(): #update dictionary with current solution string if not in dictionary yet combination_dict.update({combination: (1,1)}) #assign default value to solution in solution dictionary (gets overwritten at the end of func_eval infill_type = individual[0] with open('inputs.txt','w+') as writefile: for value in individual: writefile.write('{}\n'.format(value)) os.system('abaqus cae noGUI={}_thermo.py'.format(infill_type)) while os.path.isfile('temperature_results.txt') is False: time.sleep(5) os.system('abaqus cae noGUI={}_stress.py'.format(infill_type)) while os.path.isfile('stress_results.txt') is False: time.sleep(5) outputs = extractor() results = [result/norm_fact for result, norm_fact in zip(outputs,norm_facts)] combination_dict[combination] = tuple(results) #update solution dictionary to check against future individuals os.remove('stress_results.txt') os.remove('temperature_results.txt') os.remove('inputs.txt') print(results) return tuple(results) else: return combination_dict[combination] def uniform(): # fill each individual in the initial population with total bits in design_var_dict individual = [] for var in design_var_dict: if design_var_dict[var]['type'] == 'discrete': individual.append(random.choice(design_var_dict[var]['options'])) else: individual.append(random.uniform(design_var_dict[var]['interval'][0],design_var_dict[var]['interval'][1])) return individual def cx_list(ind1, ind2): # define crossover strategy for lists cxPt = random.randint(0,len(ind1)-1) child1 = toolbox.individual() child2 = toolbox.individual() child1[::] = ind1[0:cxPt]+ind2[cxPt::] child2[::] = ind2[0:cxPt]+ind1[cxPt::] return child1,child2 def mut_list(individual): # define mutation strategy for lists mutPoint = random.randint(0,len(individual)-1) if mutPoint == 0: if len(individual) == 2: mutPoint = 1 elif len(individual) == 3: mutPoint = random.randint(1,2) if mutPoint == 1: individual[mutPoint] = random.uniform(design_var_dict['thickness']['interval'][0],design_var_dict['thickness']['interval'][1]) elif mutPoint ==2: individual[mutPoint] = random.uniform(design_var_dict['angle']['interval'][0],design_var_dict['angle']['interval'][1]) return individual, creator.create("Fitness", base.Fitness, weights=(-1.0,-1.0)) # create fitness class inherited from 'base.Fitness' class creator.create("Individual", list, fitness=creator.Fitness) # create Individual class inherited from 'list' class toolbox = base.Toolbox() # initialize toolbox class from 'base', contains all evolutionary operators toolbox.register("initializer", uniform) # register function 'uniform' to initialize individuals toolbox.register("individual", tools.initIterate, creator.Individual, toolbox.initializer) # register 'individual' creator in toolbox toolbox.register("population", tools.initRepeat, list, toolbox.individual) # register 'population' creator in toolbox toolbox.register("mate", cx_list) # register crossover strategy as function 'cx_list' toolbox.register("mutate",mut_list) # register mutation strategy as function 'mut_list' toolbox.register("evaluate", func_eval) # register evaluation strategy as function 'func_eval' # if constraints: # toolbox.decorate("evaluate", tools.DeltaPenality(feasibility,10000)) # decorate evaluation strategy with constraints if they exist toolbox.register("select", tools.selNSGA2) # register selection strategy for sorting evaluated individuals as NSGA-II stats = tools.Statistics(lambda ind: ind.fitness.values) # log statistics during evolution stats.register("avg", np.mean, axis=0) stats.register("std", np.std, axis=0) stats.register("min", np.min, axis=0) stats.register("max", np.max, axis=0) pop = toolbox.population(n=POPSIZE) # generate initial population hof = tools.ParetoFront() # register criteria for selecting hall of fame individuals (Pareto dominant solutions) output = algorithms.eaMuPlusLambda(pop, toolbox, SURVIVORS, CHILDREN, CXPB, MUTPB, GENS, stats, halloffame=hof) # conduct evolutionary optimization process combination_dict.pop('initial_val') with open('meta_data_{}'.format(design_vars['infill']['options'][0]), 'w') as f: json.dump(combination_dict, f) headerList = ['Solution Point'] for var in design_var_dict: headerList.append(var) for name in obj_func_names: headerList.append(name) results = PrettyTable(headerList) # generate table to store and display results solution = 1 for individual in output[0]: # put optimal solutions into 'results' table solutionList = [solution] for value in individual: solutionList.append(value) for val,norm_fact in zip(individual.fitness.values,norm_facts): solutionList.append(val*norm_fact) results.add_row(solutionList) solution+=1 non_dom = tools.sortNondominated(output[0], k=len(output[0]), first_front_only=True)[0] for ind in non_dom: fitvals = [ind.fitness.value for ind.fitness.value in ind.fitness.values] plt.plot(*fitvals,'bo') plt.title('Pareto Front') plt.xlabel('{}'.format(obj_func_names[0])) plt.ylabel('{}'.format(obj_func_names[1])) plt.show() return results
def _run_deap(self, pop): MU = self.pop_size // 2 LAMBDA = self.pop_size // 2 algorithms.eaMuPlusLambda(pop, self.toolbox, MU, LAMBDA, self.CXPB, self.MUTPB, self.NGEN, verbose=self.verbose) return pop
up=UPPER, indpb=0.1) # Register the variate operator toolbox.register("variate", deap.algorithms.varAnd) # Register the selector (picks parents from population) toolbox.register("select", tools.selNSGA2) # Generate the population object pop = toolbox.population(n=MU) # Register the statistics we want to record during the optimisation # In this case only the minimal value first_stats = tools.Statistics(key=lambda ind: ind.fitness.values[0]) second_stats = tools.Statistics(key=lambda ind: ind.fitness.values[1]) stats = tools.MultiStatistics(obj1=first_stats, obj2=second_stats) stats.register("min", numpy.min, axis=0) # Run the actual algorithm with all the object/parameters we set up above if __name__ == '__main__': pop, logbook = algorithms.eaMuPlusLambda(pop, toolbox, MU, LAMBDA, CXPB, MUTPB, NGEN, stats, halloffame=None)
stats.register('avg', np.mean) stats.register('std', np.std) hallOfFame = tools.HallOfFame(maxsize=1) # 遗传算法参数 toolbox.ngen = 400 toolbox.cxpb = 0.8 toolbox.mutpb = 0.1 # 遗传算法主程序 # 遗传算法主程序 pop, logbook = algorithms.eaMuPlusLambda(pop, toolbox, mu=toolbox.popSize, lambda_=toolbox.popSize, cxpb=toolbox.cxpb, mutpb=toolbox.mutpb, ngen=toolbox.ngen, stats=stats, halloffame=hallOfFame, verbose=True) def calLoad(routes): loads = [] for eachRoute in routes: routeLoad = np.sum([dataDict['Demand'][i] for i in eachRoute]) loads.append(routeLoad) return loads bestInd = hallOfFame.items[0]
items = {} for i in range(NBR_ITEMS): strings = lines[i].split() items[i] = (int(strings[0]), float(strings[1]), int(strings[2])) NGEN = 500 MU = 200 LAMBDA = 40 CXPB = 0.8 MUTPB = 0.2 creator.create("Fitness", base.Fitness, weights=(-1.0, -1.0, 1.0)) creator.create("Individual", set, fitness=creator.Fitness) toolbox = base.Toolbox() toolbox.register("attr_item", randrange, NBR_ITEMS) toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_item, NBR_ITEMS) toolbox.register("population", tools.initRepeat, list, toolbox.individual) toolbox.register("evaluate", evalKnapsack) toolbox.register("mate", cxSet) toolbox.register("mutate", mutSet) toolbox.register("select", tools.selNSGA2) pop = toolbox.population(n=MU) stats = tools.Statistics(lambda ind: ind.fitness.values) stats.register("avg", numpy.mean, axis=0) stats.register("max", numpy.max, axis=0) algorithms.eaMuPlusLambda(pop, toolbox, MU, LAMBDA, CXPB, MUTPB, NGEN, stats)
MU = 2000 LAMBDA = 2000 CXPB = 0.2 MUTPB = 0.8 pop = toolbox.population(n=MU) hof = tools.HallOfFame(1, similar=np.array_equal) stats = tools.Statistics(lambda ind: ind.fitness.values) stats.register("avg", np.mean, axis=0) stats.register("std", np.std, axis=0) stats.register("min", np.min, axis=0) stats.register("max", np.max, axis=0) algorithms.eaMuPlusLambda(pop, toolbox, MU, LAMBDA, CXPB, MUTPB, NGEN, stats, halloffame=hof) solution = hof[0] for solution in hof[::-1]: print_solution((solution)) plt.pause(1) save_solution(solution)
def search_genetic(basic_fragments, basic_connections, gen_alg='varOr', seed_individ=None, multi_process=False, show_stats=False, verbose=False, **kwargs): frags_indices = list(range(len(basic_fragments))) # register initialization methods toolbox = base.Toolbox() toolbox.register("attribute", rand.randint, 0, 1) toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attribute, n=len(basic_connections)) toolbox.register("population", tools.initRepeat, list, toolbox.individual) # register genetic operators toolbox.register("mate", tools.cxUniform, indpb=0.5) toolbox.register("mutate", tools.mutFlipBit, indpb=0.1) toolbox.register("select", tools.selTournament, tournsize=3) # register evaluation function, to measure the fitness toolbox.register("evaluate", evaluate, basic_fragments=basic_fragments, fragments_indices=frags_indices, fragment_connections=basic_connections, **kwargs) pool = None if multi_process: pool = multiprocessing.Pool() toolbox.register("map", pool.map) if verbose: print("\nUsing a multiprocessing pool") pop_size = min(500, 100 + len(basic_connections) * 2) if verbose: print("Population Size = %d" % pop_size) if seed_individ: rand_pop_size = pop_size - int(pop_size / 2) pop = toolbox.population(n=rand_pop_size) seed_pop = get_population_from_seed(toolbox, seed_individ, (pop_size - rand_pop_size - 1), indpb=0.1) pop.extend(seed_pop) the_seed = toolbox.individual() for vix in range(len(seed_individ)): the_seed[vix] = seed_individ[vix] pop.append(the_seed) if verbose: seed_fitness = evaluate(the_seed, basic_fragments, frags_indices, basic_connections, **kwargs) print("The seed fitness is: %s" % seed_fitness[0]) else: pop = toolbox.population(n=pop_size) # create hall of fame list hof = tools.HallOfFame(1) if show_stats: # specify which statistics to collect fit_stats = tools.Statistics(lambda ind: ind.fitness.values) fit_stats.register("mean", np.mean, axis=0) fit_stats.register("std", np.std, axis=0) fit_stats.register("min", np.min, axis=0) fit_stats.register("max", np.max, axis=0) else: fit_stats = None # specify the parameter values cxpb, mutpb, ngen = 0.5, 0.5, 100 if gen_alg == 'varOr': # execute genetic varOr search mu_es, lampda_es = pop_size, pop_size if verbose: print("execute varOr search") print("VarOr: cxpb=%s, mutpb=%s, ngen=%s, mu_es=%s, lampda_es=%s" % (cxpb, mutpb, ngen, mu_es, lampda_es)) result, log = algorithms.eaMuPlusLambda(pop, toolbox, mu=mu_es, lambda_=lampda_es, cxpb=cxpb, mutpb=mutpb, ngen=ngen, halloffame=hof, verbose=False, stats=fit_stats) best_ind = hof[0] elif gen_alg == 'varAnd': if verbose: print("execute varAnd search") print("VarAnd: cxpb=%s, mutpb=%s, ngen=%s" % (cxpb, mutpb, ngen)) # execute simple genetic varAnd search result, log = algorithms.eaSimple(pop, toolbox, cxpb=cxpb, mutpb=mutpb, ngen=ngen, halloffame=hof, verbose=False, stats=fit_stats) best_ind = hof[0] else: raise ValueError( 'Value specified for genetic algorithm is not recognized! gen_alg=%s' % gen_alg) if multi_process and pool: pool.close() pool.join() if verbose: print("\nClosed the multiprocessing pool") if show_stats: plt.figure(figsize=(11, 4)) plots = plt.plot(log.select('min'), 'c-', log.select('std'), 'y-', log.select('mean'), 'r-') plt.legend(plots, ('Minimum fitness', 'Std fitness', 'Mean fitness'), frameon=True) plt.ylabel('Fitness') plt.xlabel('Iterations') plt.show() selected_connections = list() for j in range(len(best_ind)): if best_ind[j]: selected_connections.append(basic_connections[j]) best_ind_parts = get_partitions(basic_fragments, frags_indices, np.array(selected_connections)) return best_ind_parts, best_ind.fitness.values[0]
stats = tools.Statistics(lambda ind: ind.fitness.values) stats.register("avg", np.mean) stats.register("std", np.std) stats.register("min", np.min) stats.register("max", np.max) hof = tools.HallOfFame(1, similar=np.array_equal) pop = toolbox.population(n=10000) pop, logbook = algorithms.eaMuPlusLambda(pop, toolbox, mu=10000, lambda_=5000, cxpb=0.1, mutpb=0.90, ngen=30, stats=stats, halloffame=hof, verbose=True) best_sol = BLIMP1(*hof[0]) best_sol.plot(dataset=dataset, fitting="Fitted Params") print('mu_p and sigma_p of our solution: {}, {}'.format( best_sol.mu_p, best_sol.sigma_p)) print('Fitness of our best solution: ', fitness(hof[0])[0]) params_mart = [10e-6, 9]
m, l = population//2 , population cxpb ,mutpb = 0.2, 0.8 diff = r_upper - r_lower ### Specify the mutations. GA will be applied for every $N: $len(gauss_sigma) times. Every time: $population evolves $ngen #gauss_sigma=[diff/10,diff/100,diff/500,diff/1000,diff/5000,diff/10000] gauss_sigma=[diff/100,diff/400,diff/800,diff/1600] pop = toolbox.population(population) for i in range(len(gauss_sigma)): toolbox.register("mutate", tools.mutGaussian, mu = 0 ,sigma = gauss_sigma[i],indpb=0.99) hof = tools.HallOfFame(1) stats = tools.Statistics(lambda ind: ind.fitness.values) stats.register("min", np.min) pop,logbook = algorithms.eaMuPlusLambda(pop, toolbox,m,l, cxpb=cxpb, mutpb=mutpb, ngen=ngen, stats=stats, halloffame=hof,verbose=True) ###append data### log_arr.append(logbook) ngen_arr.append(ngen*(i+1)) time_arr.append(time.time()-start) hof_arr.append(hof) print("hof is ", hof) logs.append(log_arr) min_val_arr.append(FindMinInLogbook(logbook)) first_best_arr.append(FindFirstGoodinLogbook(logbook,goal,error)) #Minimum_value = FindMinInLogbook(logbook) #First_best = FindFirstGoodinLogbook(logbook,goal,error)
toolbox = base.Toolbox() toolbox.register("expr", gp.genHalfAndHalf, pset=pset, min_=2, max_=5) toolbox.register("select", tools.selTournament, tournsize=3) toolbox.register("mate", gp.cxOnePoint) toolbox.register("mutate", gp.mutNodeReplacement, pset=pset) toolbox.register("compile", gp.compile, pset=pset) toolbox.register("individual", tools.initIterate, creator.Individual, toolbox.expr) toolbox.register("population", tools.initRepeat, list, toolbox.individual) toolbox.register("evaluate", evaluate, inputs=inputs, outputs=outputs) stats = tools.Statistics(key=lambda ind: ind.fitness.values) stats.register('min', np.min) stats.register('max', np.max) stats.register('avg', np.mean) stats.register('std', np.std) hof = tools.HallOfFame(5) if __name__ == '__main__': pool = multiprocessing.Pool() toolbox.register("map", pool.map) pop = toolbox.population(n=100) pop2, log = algorithms.eaMuPlusLambda(population=pop, mu=len(pop), lambda_=len(pop), toolbox=toolbox, halloffame=hof, cxpb=0.5, mutpb=0.1, ngen=100, stats=stats) for ind in hof: print(ind)
def train(self, tmp_dir): """Train a model.""" self.print_options() # Sync output of previous training run from cloud. # This will either be local or S3. This allows restarting the job if it has been shut down. train_uri = self.backend_opts.train_uri train_dir = get_local_path(train_uri, tmp_dir) make_dir(train_dir) sync_from_dir(train_uri, train_dir) # Get zip file for each group, and unzip them into chip_dir. self.chip_dir = join(tmp_dir, 'chips') make_dir(self.chip_dir) train_chip_dir = self.chip_dir + '/train-img' train_truth_dir = self.chip_dir + '/train-labels' fitness_func = partial(fitness, train_chip_dir, train_truth_dir, self._toolbox.compile) self._toolbox.register("evaluate", fitness_func) # This is the key part -- this is how it knows where to get the chips from. # backend_opts comes from RV, and train_opts is where you can define backend-specific stuff. for zip_uri in list_paths(self.backend_opts.chip_uri, 'zip'): zip_path = download_if_needed(zip_uri, tmp_dir) with zipfile.ZipFile(zip_path, 'r') as zipf: zipf.extractall(self.chip_dir) # Setup data loader. def get_label_path(im_path): return Path(str(im_path.parent)[:-4] + '-labels') / im_path.name class_map = self.task_config.class_map classes = class_map.get_class_names() if 0 not in class_map.get_keys(): classes = ['nodata'] + classes # Evolve # Set up hall of fame to track the best individual hof = tools.HallOfFame(1) # Set up debugging mstats = None if self.train_opts.debug: 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("averageaverage", np.mean) mstats.register("stdeviation", np.std) mstats.register("minimumstat", np.min) mstats.register("maximumstat", np.max) pop = self._toolbox.population(n=self.train_opts.pop_size) pop, log = algorithms.eaMuPlusLambda( pop, self._toolbox, self.train_opts.num_individuals, self.train_opts.num_offspring, self.train_opts.crossover_rate, self.train_opts.mutation_rate, self.train_opts.num_generations, stats=mstats, halloffame=hof, verbose=self.train_opts.debug ) # ? What should my model output be given that the output is just a string? Should I output a # text file? # RV uses file-presence based caching to figure out whether a stage has completed (kinda # like Makefiles). So since this outputs a file every epoch, it needs to use something else # to trigger done-ness. # Since model is exported every epoch, we need some other way to # show that training is finished. if self.train_opts.debug: print(str(hof[0])) str_to_file(str(hof[0]), self.backend_opts.train_done_uri) str_to_file(str(hof[0]), self.backend_opts.model_uri) # Sync output to cloud. sync_to_dir(train_dir, self.backend_opts.train_uri)
else: individual.add(random.choice(items.keys())) toolbox.register("evaluate", evalKnapsack) toolbox.register("mate", cxSet) toolbox.register("mutate", mutSet) toolbox.register("select", tools.selSPEA2) logging.info("Items to choose for knapsack are: %s\n", items) for _ in range(4): logging.info("Number of steps in evolution: %s\n\n", NGEN) for _ in range(3): pop = toolbox.population(n=30) hof = tools.ParetoFront() 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) algorithms.eaMuPlusLambda(toolbox, pop, MU, LAMBDA, 0.7, 0.2, NGEN, stats, halloffame=hof) logging.info("Best individual is %s, %s", (lambda x: sorted(list(x)))(hof[-1]), hof[-1].fitness.values) NGEN+=30
def ES(evaluate,myparams,pool=None, run_name="runXXX"): """Mu plus lambda ES.""" params={"IND_SIZE":1, "MU":100, "LAMBDA":200, "CXPB":1, "MUTPB":1, "NGEN":1000, "STATS":stats, "MIN": 0, "MAX": 1, "MIN_STRATEGY":0, "MAX_STRATEGY":1, "MU": 100, "LAMBDA": 1000, "ALPHA": 0.1, "C": 1.0, "INDPB": 0.03, "TOURNSIZE":3, "VARIANT": "+" } for key in myparams.keys(): params[key]=myparams[key] if ("MU" in myparams.keys) and ("LAMBDA" not in myparams.keys()): params["LAMBDA"]=int(2*params["MU"]) toolbox = base.Toolbox() toolbox.register("individual", generateES, creator.Individual, creator.Strategy, params["IND_SIZE"], params["MIN"], params["MAX"], params["MIN_STRATEGY"], params["MAX_STRATEGY"]) toolbox.register("population", tools.initRepeat, list, toolbox.individual) toolbox.register("mate", tools.cxESBlend, alpha=params["ALPHA"]) toolbox.register("mutate", tools.mutESLogNormal, c=params["C"], indpb=params["INDPB"]) toolbox.register("select", tools.selTournament, tournsize=params["TOURNSIZE"]) toolbox.register("evaluate", evaluate) # Parallelism # if(not in_ipython): # toolbox.register("map", futures.map) # toolbox.register("map", mymap) if(pool): toolbox.register("map", pool.map) # toolbox.decorate("mate", checkStrategy(params["MIN_STRATEGY"], params["MAX_STRATEGY"])) # toolbox.decorate("mutate", checkStrategy(params["MIN_STRATEGY"], params["MAX_STRATEGY"])) toolbox.decorate("mate", checkStrategyMin(params["MIN_STRATEGY"])) toolbox.decorate("mutate", checkStrategyMin(params["MIN_STRATEGY"])) pop = toolbox.population(n=params["MU"]) hof = tools.HallOfFame(1) if (params["VARIANT"]=="+"): print("Mu+Lambda ES") rpop, logbook = algorithms.eaMuPlusLambda(pop, toolbox, mu=params["MU"], lambda_=params["LAMBDA"], cxpb=params["CXPB"], mutpb=params["MUTPB"], ngen=params["NGEN"], stats=params["STATS"], halloffame=hof, verbose=False) else: print("Mu,Lambda ES") rpop, logbook = algorithms.eaMuCommaLambda(pop, toolbox, mu=params["MU"], lambda_=params["LAMBDA"], cxpb=params["CXPB"], mutpb=params["MUTPB"], ngen=params["NGEN"], stats=params["STATS"], halloffame=hof, verbose=False) return rpop, logbook, hof
def main(): pop = toolbox.population(n=10) #statistics stats = tools.Statistics(key=lambda ind: ind.fitness.values) stats.register("max", numpy.max) stats.register("min", numpy.min) stats.register("mean", numpy.mean) stats.register("std", numpy.std) # define the hall-of-fame object: hof = tools.HallOfFame(1) #eaSimple print("------- EaSimple -------") df = pd.DataFrame() for i in range(10): pop, log = algorithms.eaSimple(pop, toolbox, 1.0, 0.5, 100, stats=stats, halloffame=hof) df2 = pd.DataFrame(log) df2['algoritmo'] = 'eaSimple' df2['corrida'] = i df = df.append(df2) df = df.reset_index(drop=True) for i in range(1010): if i > 0 and df.at[i, 'max'] < df.at[i - 1, 'max']: df.at[i, 'max'] = df.at[i - 1, 'max'] print(df.to_string()) # print best solution found: best = hof.items[0] print("Best Ever Individual = ", best) print("Best Ever Fitness = ", best.fitness.values[0]) print("--- TSP best route --- ") tsp.printResult(best) df_promedios = df.groupby(['algoritmo', 'gen']).agg({'max': ['mean', 'std']}) print(df_promedios.to_string()) x = df['gen'].unique() promedios = df_promedios['max']['mean'].values desviacion = df_promedios['max']['std'].values plt.plot(x, promedios, color='r') plt.plot(x, promedios - desviacion, linestyle='--', color='b') plt.plot(x, promedios + desviacion, linestyle='--', color='g') plt.show() #-------------------------------------------------------------------------------------------- #eaMuPlusLambda print("------- EaMuPlusLambda -------") hof2 = tools.HallOfFame(1) df = pd.DataFrame() for i in range(10): pop, log = algorithms.eaMuPlusLambda(pop, toolbox, 5, 10, 0.5, 0.5, 100, stats=stats, halloffame=hof2) df2 = pd.DataFrame(log) df2['algoritmo'] = 'eaMuPlusLambda' df2['corrida'] = i df = df.append(df2) df = df.reset_index(drop=True) for i in range(1010): if i > 0 and df.at[i, 'max'] < df.at[i - 1, 'max']: df.at[i, 'max'] = df.at[i - 1, 'max'] print(df.to_string()) # print best solution found: best = hof2.items[0] print("Best Ever Individual = ", best) print("Best Ever Fitness = ", best.fitness.values[0]) print("--- TSP best route --- ") tsp.printResult(best) df_promedios = df.groupby(['algoritmo', 'gen']).agg({'max': ['mean', 'std']}) print(df_promedios.to_string()) x = df['gen'].unique() promedios = df_promedios['max']['mean'].values desviacion = df_promedios['max']['std'].values plt.plot(x, promedios, color='r') plt.plot(x, promedios - desviacion, linestyle='--', color='b') plt.plot(x, promedios + desviacion, linestyle='--', color='g') plt.show() #-------------------------------------------------------------------------------------------- #eaMuCommaLambda print("------- EaMuCommaLambda -------") hof3 = tools.HallOfFame(1) df = pd.DataFrame() for i in range(10): pop, log = algorithms.eaMuCommaLambda(pop, toolbox, 5, 10, 0.5, 0.5, 100, stats=stats, halloffame=hof3) df2 = pd.DataFrame(log) df2['algoritmo'] = 'eaMuCommaLambda' df2['corrida'] = i df = df.append(df2) df = df.reset_index(drop=True) for i in range(1010): if i > 0 and df.at[i, 'max'] < df.at[i - 1, 'max']: df.at[i, 'max'] = df.at[i - 1, 'max'] print(df.to_string()) # print best solution found: best = hof3.items[0] print("Best Ever Individual = ", best) print("Best Ever Fitness = ", best.fitness.values[0]) print("--- TSP best route --- ") tsp.printResult(best) df_promedios = df.groupby(['algoritmo', 'gen']).agg({'max': ['mean', 'std']}) print(df_promedios.to_string()) x = df['gen'].unique() promedios = df_promedios['max']['mean'].values desviacion = df_promedios['max']['std'].values plt.plot(x, promedios, color='r') plt.plot(x, promedios - desviacion, linestyle='--', color='b') plt.plot(x, promedios + desviacion, linestyle='--', color='g') plt.show()
def launch_es(mu=100, lambda_=200, cxpb=0.6, mutpb=0.3, ngen=100, display=False, verbose=False): # Initialisation random.seed() population = toolbox.population(n=mu) halloffame = 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', 'nevals'] + (stats.fields if stats else []) invalid_ind = [ind for ind in population if not ind.fitness.valid] fitnesses = toolbox.map(toolbox.evaluate, invalid_ind) for ind, fit in zip(invalid_ind, fitnesses): ind.fitness.values = fit if halloffame is not None: halloffame.update(population) record = stats.compile(population) if stats is not None else {} logbook.record(gen=0, nevals=len(invalid_ind), **record) if verbose: print(logbook.stream) # Boucle de l'algorithme évolutionniste for gen in range(1, ngen + 1): ### A completer pour implementer un ES en affichant regulièrement les resultats a l'aide de la fonction plot_results fournie ### if display: plot_results(ma_func, population) ### Vous pourrez tester plusieurs des algorithmes implémentés dans DEAP pour générer une population d'"enfants" #algorithms.eaSimple(population, toolbox, ngen=1, halloffame=halloffame, stats=stats, cxpb=cxpb, mutpb=mutpb) algorithms.eaMuPlusLambda(population, toolbox, ngen=1, mu=mu, lambda_=lambda_, halloffame=halloffame, stats=stats, cxpb=cxpb, mutpb=mutpb, verbose=verbose) ### à partir de la population courante et pour sélectionner les géniteurs de la prochaine génération # Update the hall of fame with the generated individuals if halloffame is not None: halloffame.update(population) # Update the statistics with the new population record = stats.compile(population) if stats is not None else {} #logbook.record(gen=gen, nevals=len(invalid_ind), **record) if verbose: print(logbook.stream) return population, logbook, halloffame
stats.register("max", numpy.max) try: algorithms.eaSimple(pop, toolbox, 0.5, 0.2, 100, stats, halloffame=hof) except: pass ======= 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.eaMuPlusLambda(pop, toolbox, 100, 100, 0.25, 0.25, 100, stats, halloffame=hof) >>>>>>> ad69d090f391e3b83b16acb24fecdd955a1f1665 return pop, stats, hof if __name__ == "__main__": pop, stats, hof = main() expr = gp.PrimitiveTree(hof[0]) print(expr) <<<<<<< HEAD ======= # print("=============================") # for i in range(5): # print("-----------")
def main(): init_pop_size = 1000 num_offsprings = 400 pop_size = 300 num_generations = 20 mse_fitness_weight = -1.0 symb_equiv_fitness_weight = 2.0 weights = (mse_fitness_weight, symb_equiv_fitness_weight) # list all the functions to analyse functions = [math.sin] # create the toolbox toolbox = get_toolbox(functions[0], weights) # random.seed(318) # create a new population pop = toolbox.population(n=init_pop_size) # hof = tools.HallOfFame(40) hof = tools.ParetoFront() # collect some statistics stats_fit_mse = tools.Statistics(lambda ind: ind.fitness.values[0]) stats_fit_dist = tools.Statistics(lambda ind: ind.fitness.values[1]) stats_size = tools.Statistics(len) mstats = tools.MultiStatistics(fitness_mse=stats_fit_mse, fitness_dist=stats_fit_dist, size=stats_size) mstats.register("avg", lambda data: numpy.around(numpy.mean(data), decimals=4)) # mstats.register("std", lambda data: numpy.around(numpy.std(data), decimals=4)) mstats.register("min", lambda data: numpy.around(numpy.min(data), decimals=4)) mstats.register("max", lambda data: numpy.around(numpy.max(data), decimals=4)) start = time.time() # do the evolution pop, log = algorithms.eaMuPlusLambda(pop, toolbox, mu=pop_size, lambda_=num_offsprings, cxpb=0.5, mutpb=0.1, ngen=num_generations, stats=mstats, halloffame=hof, verbose=True) end = time.time() my_hof = [ind for ind in pop if 0.001 >= ind.fitness.getValues()[0]] my_hof = sorted(my_hof, key=lambda individual: individual.fitness.getValues()[1]) currentTime = datetime.datetime.now().strftime('%Y-%m-%d_%H:%M:%S') file = open("out/MRbyGA_" + currentTime + ".txt", 'w', encoding='utf8') m, s = divmod((end - start), 60) h, m = divmod(m, 60) file.write("Consumed time: %d:%02d:%02d" % (h, m, s) + '\n\n') # print the HoF values without overlap temp = "" for ind in my_hof: if temp == convert_to_sympy_expr(ind): continue else: temp = convert_to_sympy_expr(ind) print('%.4f, %f' % (ind.fitness.getValues()), ':') # print(ind) sympy.pprint(temp) file.write('%.4f, %f: ' % (ind.fitness.getValues()) + '\n' + str(ind) + '\n' + sympy.pretty(temp) + '\n\n') plt.figure(1) plt.subplot(211) plt.axis(ymax=1.0) plt.plot(log.chapters['fitness_mse'].select('avg')) plt.plot(log.chapters['fitness_mse'].select('min')) plt.plot(log.chapters['fitness_mse'].select('max')) plt.subplot(212) plt.axis(ymax=1.0) plt.plot(log.chapters['fitness_dist'].select('avg')) plt.plot(log.chapters['fitness_dist'].select('min')) plt.plot(log.chapters['fitness_dist'].select('max')) plt.show() return pop, log, hof
def run(): """ :param num_iter: number of generations :param num_pop: size of population :param seed: random seed :param strategy: one of 'simple', 'mu_plus_lambda' :param ga parameters file name: ga parameters file name (e.g., "ga_params.json") :param param_file: name of file containing initial parameters """ eqpy.OUT_put("Params") params = eqpy.IN_get() # parse params (num_iter, num_pop, seed, strategy, mut_prob, ga_params_file, param_file) = eval('{}'.format(params)) random.seed(seed) global ga_params ga_params = ga_utils.create_parameters(ga_params_file) creator.create("FitnessMin", base.Fitness, weights=(-1.0, )) creator.create("Individual", list, fitness=creator.FitnessMin) toolbox = base.Toolbox() toolbox.register("individual", tools.initIterate, creator.Individual, make_random_params) toolbox.register("population", tools.initRepeat, list, toolbox.individual) toolbox.register("evaluate", obj_func) toolbox.register("mate", cxUniform, indpb=0.5) mutate_indpb = mut_prob toolbox.register("mutate", custom_mutate, indpb=mutate_indpb) toolbox.register("select", tools.selTournament, tournsize=3) toolbox.register("map", queue_map) pop = toolbox.population(n=num_pop) if param_file != "": update_init_pop(pop, param_file) hof = tools.HallOfFame(1) stats = tools.Statistics(lambda ind: ind.fitness.values) stats.register("avg", np.mean) stats.register("std", np.std) stats.register("min", np.min) stats.register("max", np.max) stats.register("ts", timestamp) # num_iter-1 generations since the initial population is evaluated once first mutpb = mut_prob start_time = time.time() if strategy == 'simple': pop, log = algorithms.eaSimple(pop, toolbox, cxpb=0.5, mutpb=mutpb, ngen=num_iter - 1, stats=stats, halloffame=hof, verbose=True) elif strategy == 'mu_plus_lambda': mu = int(math.floor(float(num_pop) * 0.5)) lam = int(math.floor(float(num_pop) * 0.5)) if mu + lam < num_pop: mu += num_pop - (mu + lam) pop, log = algorithms.eaMuPlusLambda(pop, toolbox, mu=mu, lambda_=lam, cxpb=0.5, mutpb=mutpb, ngen=num_iter - 1, stats=stats, halloffame=hof, verbose=True) else: raise NameError('invalid strategy: {}'.format(strategy)) end_time = time.time() fitnesses = [str(p.fitness.values[0]) for p in pop] eqpy.OUT_put("DONE") # return the final population eqpy.OUT_put("{}\n{}\n{}\n{}\n{}".format(create_list_of_json_strings(pop), ';'.join(fitnesses), start_time, log, end_time))
def run_spea2(population, CSourceOutputForVariousSetUpFileName, operatorSampleFileFullAddress, executableName, executableInputList, rootResultFolderName, CBuildFolder, operandSampleFileName, lOfAccurateValues, nameOfAllOperandFilesList, inputObj, ignoreListIndecies, possibly_worse_case_result_quality,accurateSetUp, allConfs, NGEN, MU, LAMBDA, unique_point_list, output_list, allPointsTried, previous_ideal_setUp, iteration, settings_obj, run_input_list, lOf_accurateValues): #allPointsTried = [] if (settings_obj.maxX): x_direction = 1 else: x_direction = -1 if (settings_obj.maxY): y_direction = 1 else: y_direction = -1 #NGEN = settings.NGEN #MU = settings.MU#number of indi for the next gen #LAMBDA = settings.LAMBDA#number of children CXPB = settings_obj.CXPB MUTPB = settings_obj.MUTPB reminder(settings_obj.reminder_flag, "xdirectino and y_direction are reverese b/c I was returning them in a reverse direction"); creator.create("FitnessMin", base.Fitness, weights=(y_direction, x_direction)) creator.create("Individual", list, fitness=creator.FitnessMin) toolbox = base.Toolbox() # generating the initial population # Operator registering toolbox.register("individual", tools.initRepeat, creator.Individual) stats = tools.Statistics(lambda ind: ind.fitness.values) toolbox.register("evaluate", specializedEval_multiple_inputs, True, possibly_worse_case_result_quality, accurateSetUp, ignoreListIndecies, accurateSetUp, inputObj, nameOfAllOperandFilesList, rootResultFolderName, executableName, executableInputList, CBuildFolder, operandSampleFileName, lOfAccurateValues, allPointsTried, True, unique_point_list, output_list, previous_ideal_setUp, iteration, settings_obj, run_input_list, lOf_accurateValues) toolbox.register("mate", tools.cxTwoPoint) toolbox.register("mutate", specializedMutate, ignoreListIndecies, settings_obj) toolbox.register("select", tools.selSPEA2) """ if (settings_obj.runMode == "parallel"): #the_lock = multiprocessing.Lock() #pool = multiprocessing.Pool() toolbox.register("map", pool.map) #allPointsTried = [] #since deap is not compatible with multiprocessor #library (when it comes to sharing a list accross #processes), we set allPointsTried to empty to #avoid any unwanted consequences """ #--run the genetic algo print("\n......running genetic algo\n") for index in range(len(allConfs)): myGenerator = return_conf(allConfs[index]) population.append(toolbox.individual(lambda: next(myGenerator), len(allConfs[index]))) algorithms.eaMuPlusLambda(population, toolbox, MU, LAMBDA, CXPB, MUTPB, NGEN, stats) return population
stats = tools.Statistics(lambda ind: ind.fitness.values) stats.register("avg", np.mean, axis=0) stats.register("std", np.std, axis=0) stats.register("min", np.min, axis=0) stats.register("max", np.max, axis=0) if (verbose == True): print("Iniciando GA") sys.stdout.flush() algorithms.eaMuPlusLambda(pop, toolbox, qt_selection, nr_children_generation, proba_crossover, proba_mutation, nr_generation, stats, halloffame=hof, verbose=True) # hourtime = time.strftime("Finalizado algoritmo de GA: %H:%M:%S %d/%m/%Y") print(hourtime) sys.stdout.flush() # # # for i in range(len(hof)): print("Accuracy {}: {} {}".format(i, CheckAccuracy(X_test, Y_test, hof[i]), hof[i])) sys.stdout.flush()
stats.register('cache lookups', lambda x: total_cache_lookups) stats.register('pf size', lambda x: len(x[0])) stats.register('best fitness', lambda x: x[0][0].fitness.values) stats.register('best ind', lambda x: str(x[0][0]).strip()) t_test = creator.Individual.from_string( 'float_div(float_sub(array_mean(x1), array_mean(x2)), float_sqrt(float_add(float_div(array_var(x1), array_size(x1)), float_div(array_var(x2), array_size(x2)))))', pset) print('t-test fitness: {}'.format(evaluate_individual(t_test))) t_test = creator.Individual.from_string( 'float_div(float_sub(array_mean(x1), array_mean(x2)), float_add(array_stderr(x1), array_stderr(x2)))', pset) print('t-test fitness: {}'.format(evaluate_individual(t_test))) print('') algorithms.eaMuPlusLambda(population=pop, toolbox=toolbox, cxpb=0.5, mutpb=0.5, mu=pop_size, lambda_=pop_size, ngen=100, stats=stats, halloffame=pareto_front) print('') for index, ind in enumerate(pareto_front): print(index, ind.fitness, ind)
def main(rand, mu, lamb, cxpb, mutpb, ngen, param): random.seed(rand) NGEN = ngen MU = mu LAMBDA = lamb CXPB = cxpb MUTPB = mutpb if param == "rand" or param == "optimal": list_results = [rand] elif param == "mu": list_results = [mu] elif param == "lamb": list_results = [lamb] elif param == "cross": list_results = [cxpb] elif param == "mutate": list_results = [mutpb] elif param == "ngen": list_results = [ngen] elif param == "original": list_results = [param] 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) p, logbook = algorithms.eaMuPlusLambda(pop, toolbox, MU, LAMBDA, CXPB, MUTPB, NGEN, stats, halloffame=hof, verbose=0) #Shows the maximum fitness after all the generations and the first generation where this max_fit was achieved #to comment if you want the original results list_max = [] for elt in logbook: list_max.append(elt['max']) max_fit = maxi(list_max) #list_max[1] list_results.append(max_fit) i = 0 while (logbook[i]['max'][1] != max_fit[1]): i += 1 list_results.append(logbook[i]['gen']) print("{0} {1} {2}".format(list_results[0], list_results[1], list_results[2])) # for ind in hof: # print(ind) # print("\n") # for ind in pop: # print(ind) return pop, stats, hof
def main(popSize, mutation, cx, nGens, tournSize): # popSize, mutation, cx, nGens, tournSize = 2000, 0.1, 0.75, 50, 7 minTreeSize, maxTreeSize = 4, 14 gp_toolbox = base.Toolbox() gp_toolbox.register( "expr", gp.genHalfAndHalf, pset=primitive_set, min_=minTreeSize, max_=maxTreeSize, ) gp_toolbox.register( "individual", tools.initIterate, creator.Individual, gp_toolbox.expr ) gp_toolbox.register("population", tools.initRepeat, list, gp_toolbox.individual) gp_toolbox.register("compile", gp.compile, pset=primitive_set) # https://deap.readthedocs.io/en/master/examples/gp_symbreg.html def evaluate(individual, trainData): func = gp_toolbox.compile(individual) # print(individual) difference = 0 differenceSquared = 0 for i in range(len(trainData)): try: currentValue = func( trainData[i]["TeamExp"], trainData[i]["ManagerExp"], trainData[i]["Length"], trainData[i]["Transactions"], trainData[i]["Entities"], trainData[i]["Adjustment"], trainData[i]["PointsAjust"], trainData[i]["Language"], ) except: print("integer too large!") currentValue = 2, 147, 483, 647 absoluteError = distance(trainData[i]["Effort"], currentValue) difference += absoluteError differenceSquared += pow(absoluteError, 2) # Mean Abolute Error (total error / number of entries) mae = difference / len(trainData) rmse = protectedSqrt(differenceSquared / len(trainData)) return mae, rmse hof = tools.HallOfFame(popSize) gp_toolbox.register("evaluate", evaluate, trainData=trainData) gp_toolbox.register("mate", gp.cxOnePoint) gp_toolbox.register("select", tools.selTournament, tournsize=tournSize) gp_toolbox.register( "expr_mut", gp.genHalfAndHalf, min_=minTreeSize, max_=maxTreeSize ) gp_toolbox.register( "mutate", gp.mutUniform, expr=gp_toolbox.expr_mut, pset=primitive_set ) # https://deap.readthedocs.io/en/master/examples/gp_symbreg.html # limit overall tree height gp_toolbox.decorate( "mate", gp.staticLimit(key=operator.attrgetter("height"), max_value=17) ) # static limit of 17 recomended in deap docs gp_toolbox.decorate( "mutate", gp.staticLimit(key=operator.attrgetter("height"), max_value=17) ) # register stats for fitness and size of each individual (tree) mstats = tools.Statistics(lambda individual: individual.fitness.values) mstats.register("avg", numpy.mean, axis=0) mstats.register("std", numpy.std, axis=0) mstats.register("min", numpy.min, axis=0) mstats.register("max", numpy.max, axis=0) log = tools.Logbook() pop = gp_toolbox.population(n=nGens) print("Starting GA") hof.clear() pop, log = algorithms.eaMuPlusLambda( pop, gp_toolbox, popSize, popSize, cx, mutation, nGens, mstats, hof, True ) print( "GA Complete after %d gens, tournament selection between %d, mutation rate of %f, crossover rate of %f" % (nGens, tournSize, mutation, cx) ) # Results Time # Calculate Correlation Coefficient coefficients = [] maes = [] guesses = [] answers = [] final_function = None final_train_mae = 0 final_train_rmse = 0 for i in range(1): hof_func = gp_toolbox.compile(hof[i]) current_cc = 0 current_mae = 0 guesses.clear() answers.clear() for j in range(len(trainData)): guess = hof_func( trainData[j]["TeamExp"], trainData[j]["ManagerExp"], trainData[j]["Length"], trainData[j]["Transactions"], trainData[j]["Entities"], trainData[j]["Adjustment"], trainData[j]["PointsAjust"], trainData[j]["Language"], ) guesses.append(guess) answers.append(trainData[j]["Effort"]) diff = 0 diffSquared = 0 for j in range(len(guesses)): difference = distance(guesses[j], answers[j]) diff += difference diffSquared += pow(difference, 2) MAE = diff / len(trainData) RMSE = protectedSqrt(diffSquared / len(trainData)) final_train_mae = MAE final_train_rmse = RMSE current_cc = numpy.corrcoef(guesses, answers)[0, 1] coefficients.append(current_cc) print("\nCoefficient for Best Individual on training set = %f" % (current_cc)) print("MAE for Best Individual on training set = %f\n" % (MAE)) print("RMSE for Best Individual on training set = %f\n" % (RMSE)) final_function = hof_func final_answers = [] final_guesses = [] for i in range(len(testData)): currentDataPoint = testData[i] answer = currentDataPoint["Effort"] guess = final_function( currentDataPoint["TeamExp"], currentDataPoint["ManagerExp"], currentDataPoint["Length"], currentDataPoint["Transactions"], currentDataPoint["Entities"], currentDataPoint["Adjustment"], currentDataPoint["PointsAjust"], currentDataPoint["Language"], ) final_answers.append(answer) final_guesses.append(guess) diff = 0 diffSquared = 0 for i in range(len(final_guesses)): absoluteError = distance(final_guesses[i], final_answers[i]) diff += absoluteError diffSquared += pow(absoluteError, 2) final_mae = diff / len(final_guesses) final_mae_diff = distance(final_mae, final_train_mae) final_rmse = protectedSqrt(diffSquared / len(testData)) final_rmse_diff = distance(final_rmse, final_train_rmse) final_cc = numpy.corrcoef(final_guesses, final_answers)[0, 1] print("\nCoefficient for Best Individual on test set = %f" % (final_cc)) print("MAE for Best Individual on test set = %f\n" % (final_mae)) print("RMSE for Best Individual on test set = %f\n" % (final_rmse)) return final_mae, final_mae_diff, final_rmse, final_rmse_diff, final_cc, hof[0]
def main(): """ Main function of the project. """ args = init_parser().parse_args() random.seed(490) dict_info = {} # Problem's definition dict_info['depot'] = model.Point(args.depot[0], args.depot[1]) width, height = 300, 300 ind_size = args.vehicle * args.node dict_info['zoom'] = args.zoom # Genetic parameter crossover_probability = args.crossover mutation_probability = args.mutation ngen = args.generation _mu = args.size _lambda = args.size # Generate a the problem's data set # i.e: Generate N "route" of appointement #list_appointment = model.generate_route(num_route, # num_node_per_route, # width, # height, # dict_info['depot']) dict_info['data'] = load_data.load_dataset(args.path) # Set the routes color dict_info['color'] = visualisation.color_group(args.vehicle) toolbox = init_toolbox( ind_size, args.vehicle, dict_info['data'], dict_info['depot']) # Create the global population # And an elite one pop = toolbox.population(n=args.size) hof = tools.HallOfFame(args.elite) # Create a statistic module to display stats at each generation 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) root = visualisation.Tk() root.geometry(str(width) + "x" + str(height)) # The genetic algorithm in itself algorithms.eaMuPlusLambda(pop, toolbox, _mu, _lambda, crossover_probability, mutation_probability, ngen, stats=stats, halloffame=hof) dict_info['tour'] = visualisation.individual_as_appointment( hof[0], dict_info['data']['appointment'] ) # Create display of the problem and of the best solution visualisation.Example(root, dict_info) # Start the GUI main loop root.mainloop()
return tools.selWorst(indivuduals, 1) def get_worst_individual_from_pop(indivuduals): return tools.selWorst(indivuduals, 1) algo_dict = { "simple" : algorithms.eaSimple, "mu+lambda" : lambda population, toolbox, cxpb, mutpb, ngen, halloffame, verbose : algorithms.eaMuPlusLambda(population=population, toolbox=toolbox, mu=Algorithms.mu, lambda_=Algorithms.lambda_, cxpb=cxpb, mutpb=mutpb, ngen=ngen, halloffame=halloffame, verbose=verbose), "mu,lambda" : lambda population, toolbox, cxpb, mutpb, ngen, halloffame, verbose : algorithms.eaMuCommaLambda(population=population, toolbox=toolbox, mu=Algorithms.mu, lambda_=Algorithms.lambda_, cxpb=cxpb, mutpb=mutpb, ngen=ngen, halloffame=halloffame, verbose=verbose), "custom" : Algorithms.basic_self, "lgml" : Algorithms.lgml_algorithm, "earlyswitcher": Algorithms.early_switcher } def get_algorithm(key): """ Returns the algorithm function associated with the key Defaults to eaSimple if key not found """ if key not in algo_dict.keys(): print(f"Key {key} not found out of available algorithm options. Using Simple Algorithm")
toolbox.attr_float, dim) toolbox.register("population", tools.initRepeat, list, toolbox.individual) toolbox.register("evaluate", evaluate) toolbox.register("mate", tools.cxOnePoint) toolbox.register("mutate", tools.mutGaussian, mu=0, sigma=0.5, indpb=1.0 / dim) toolbox.register("select", tools.selTournament, tournsize=3) nparents, nchilds = 4000, 40 pop = toolbox.population(n=nparents) algorithms.eaMuPlusLambda(pop, toolbox, mu=nparents, lambda_=nchilds, cxpb=0.5, mutpb=0.2, ngen=100000000, verbose=False) # ngen=heel veel : stopconditie wordt geregeld door benchmarks # TODO : maak hier weer een loop van, die kijkt wanneer benchmarks.best_y < 0.01 if True: FITCLSNAME = "FIT_TYPE" INDCLSNAME = "IND_TYPE" NDIM = dim if False: strategy = cma.Strategy(centroid=[0.0] * NDIM, sigma=0.5, lambda_=24 * 8)
def main(): # %% items_fs = arcpy.FeatureSet(os.path.join(args.env_workspace, "items")) faci_fs = arcpy.FeatureSet(os.path.join(args.env_workspace, "healthCenter")) field_name_list_faci = utils_arcpy.get_field_name_list(faci_fs) field_name_list_items = utils_arcpy.get_field_name_list(items_fs) items_dict = utils_arcpy.read_items_dict( items_file=os.path.join(args.env_workspace, "items"), field_list=['OBJECTID','cost','suit','idx_row','idx_col']) items_tupleSet = utils_moea.read_items_tuple(items_fs) # ============================================================================= # DEAP lib: # ============================================================================= # To assure reproductibility, the RNG seed is set prior to the items # dict initialization. It is also seeded in main(). # random.seed(64) np.set_printoptions(precision=3) # Creator, a class factory that can build new classes at run-time # accs_avg: 0.01, accs_var: 0.0001, cost: 1-10, suit: 0-1 creator.create("Fitness", base.Fitness, weights=weights) creator.create("Individual", set, fitness=creator.Fitness) # Toolbox, a DEAP container, storing all the objects, # including: an individual, the population, as well as all functions, operators, and arguments toolbox = base.Toolbox() # Attribute generator toolbox.register("attr_item", utils_moea.attr_item, items_tupleSet, NBR_ITEMS) # Structure initializers toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_item, IND_INIT_SIZE) # toolbox.register("indSet_2_Ind", tools.initRepeat, creator.Individual, toolbox.,) # Define the population to be a list of individuals toolbox.register("population", tools.initRepeat, list, toolbox.individual) # Operator registration toolbox.register("select", tools.selNSGA2) toolbox.register("mate", utils_moea.cxSet, items_tupleSet=items_tupleSet, toolbox=toolbox) toolbox.register("mutate", utils_moea.mutSet, items_tupleSet=items_tupleSet) toolbox.register("evaluate", utils_moea.evalKnapsack, items_dict=items_dict, items_fs=items_fs, faci_fs=faci_fs, field_name_list_items=field_name_list_items, flag_update=False, flag_output=False ) # toolbox.register("evaluate", utils_moea.evalKnapsack, items_dict=items_dict, # items_fs=items_fs, faci_fs=faci_fs, field_name_list_items=field_name_list_items) #evalKnapsack(individual, items, items_fs, faci_fs, field_name_list_faci) # pop = toolbox.population(n=MU) hof = tools.ParetoFront() stats = tools.Statistics(lambda ind: ind.fitness.values) # stats.register("avg", np.mean, axis=0) # stats.register("std", np.std, axis=0) stats.register("min", np.min, axis=0) stats.register("max", np.max, axis=0) time_begin = time.time() population, logbook = algorithms.eaMuPlusLambda(pop, toolbox, MU, LAMBDA, CXPB, MUTPB, NGEN, stats, halloffame=hof) print "Total time used: ", time.time() - time_begin # items_hof = hof.items # for i, item in enumerate(items_hof): # print sum(item.fitness.wvalues) best_ind = hof.items[-1] print "****Best_ind - fitness: %.5e | wvalue: (%.2e, %.2e, %.2e, %.2e)****" \ % tuple([sum(best_ind.fitness.wvalues)]+list(best_ind.fitness.wvalues)) utils_moea.evalKnapsack(best_ind, items_dict, items_fs, faci_fs, field_name_list_items, flag_update=True, flag_output=True) print "Finish computing with Best_ind" return pop, stats, hof, logbook
def main(): """ Main function of the project. """ args = init_parser().parse_args() random.seed(490) dict_info = make_data(args.path) # Problem's definition dict_info['depot'] = model.Point(args.depot[0], args.depot[1]) width, height = 1300, 700 ind_size = args.vehicle * args.node dict_info['zoom'] = args.zoom # Genetic parameter crossover_probability = args.crossover mutation_probability = args.mutation ngen = args.generation _mu = args.size _lambda = args.size # Generate a the problem's data set # i.e: Generate N "route" of appointment #list_appointment = model.generate_route(num_route, # num_node_per_route, # width, # height, # dict_info['depot']) # Set the routes color dict_info['color'] = visualisation.color_group(args.vehicle) # Adjusting values based on the dataset size ind_size = len(dict_info['data']['appointment']) dict_info['zoomx'] = 1250 / max([ dict_info['data']['xrange'][1], dict_info['depot'].get_x()]) dict_info['zoomy'] = 702 / max([ dict_info['data']['yrange'][1], dict_info['depot'].get_y()]) if args.vehicle > ind_size: args.vehicle = ind_size / 2 + 1 toolbox = init_toolbox( ind_size, dict_info['data'], dict_info['depot']) # Create the global population # And an elite one pop = toolbox.population(n=args.size) hof = tools.HallOfFame(args.elite) # Create a statistic module to display stats at each generation 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) # The genetic algorithm in itself algorithms.eaMuPlusLambda( pop, toolbox, _mu, _lambda, crossover_probability, mutation_probability, ngen, stats=stats, halloffame=hof) with open('JSON_result.json', 'w') as open_file: open_file.write(hof[0].make_json(dict_info))
# Initialise population and run algo pop = toolbox.population(n=POP_SIZE) hof = tools.HallOfFame(HOF_SIZE) """ 1. eaSimple - At each gen both crossover and mutation are applied on population with probability 0.9 and 0.1, and the population is replaced by the offspring >>> 2. eaMuPlusLambda - At each gen either crossover or mutation is applied with probability 0.9 and 0.1, and x number of children are produced and the new population is formed by, choosing y children from n+x(previous population+offspring) 3. eaMuCommaLambda - Same as above except the new population is formed from only the x offspring by choosing y of them """ pop, log = algorithms.eaMuPlusLambda(pop, toolbox, MU, LAMBDA, MATING_PROB, MUTATION_PROB, NUM_GENERATIONS, stats=mstats, halloffame=hof, verbose=True) #Store the hof in a pickled file file = open( log_base_path + 'data_and_charts/' + 'evolved_pop_' + str(run), 'wb') pickle.dump(pop, file) file.close() file = open( log_base_path + 'training_logs/training_log_' + str(run) + ".txt", 'w') file.write(str(log))