def start(inp: WusnInput, pop_size=100, max_iters=10000, patience=100, cross_rate=0.8, mut_rate=0.1, init_fn=init.kmeans_greedy_init, eco_sys=EcoSys.get_instance()): evaluator = utils.Evaluator(inp) toolbox = base.Toolbox() toolbox.register('select_tour', tools.selTournament, tournsize=5) # toolbox.register('select_random', tools.selRandom) toolbox.register('select', tools.selBest) # toolbox.register('select', tools.selRoulette) toolbox.register('initialize', init_fn) toolbox.register('crossover', utils.crossover, relays_num=inp.relay_num, all_relays=inp.relays) toolbox.register('mutate', utils.mutate, all_relays=inp.relays) toolbox.register('evaluate', mxf_eval) logbook = tools.Logbook() logbook.header = 'Gen', 'Nic', 'Min', 'PrevMut', 'PrevCross', 'Best', 'StdDev' population = toolbox.initialize(inp, pop_size) for indv in population: indv.fitness.values = toolbox.evaluate(indv, eco_sys) nic = 0 best_loss = float('inf') best_indv = None prev_mutates = 0 prev_cross = 0 stats = tools.Statistics(key=lambda _ind: _ind.fitness.values[0]) stats.register("Min", np.min) stats.register("StdDev", np.std) for it in range(max_iters): if nic > patience: print('No improvement after %d generations. Stopping.' % nic) break # Compile statistics records = stats.compile(population) min_loss = records['Min'] if min_loss < best_loss: nic = 0 best_loss = min_loss best_indv = evaluator.best_indv(population) else: nic += 1 logbook.record(Gen=it, Nic=nic, Best=best_loss, PrevMut=prev_mutates, PrevCross=prev_cross, **records) print(logbook.stream) current_pop = list(map(lambda x: x.clone(), population)) prev_mutates, prev_cross = 0, 0 for i, c1 in enumerate(current_pop): if random.random() < cross_rate: cand = list(filter(lambda x: x != c1, current_pop)) if len(cand) < 1: c2 = c1 else: c2 = random.choice(cand) prev_cross += 1 n1, n2 = toolbox.crossover(c1, c2) # next_pop.extend([n1, n2]) for mutant in [n1, n2]: if random.random() < mut_rate: prev_mutates += 1 nc = toolbox.mutate(mutant) nc.fitness.values = toolbox.evaluate(nc, eco_sys) population.append(nc) else: mutant.fitness.values = toolbox.evaluate( mutant, eco_sys) population.append(mutant) a = int(pop_size / 10) b = pop_size - a population1 = toolbox.select(population, a) population2 = toolbox.select_tour(population, b) population = population1 + population2 # population = toolbox.select_tour(population, pop_size) mcf = solve_maximum_flow(eco_sys=ec, individual=best_indv) output_temp = get_output(mcf, ec) return output_temp, logbook
graph = shared.getConst("graph") contents = init_generation(NB_POP, MAX_MACH, graph) return pcls(ind_init(c) for c in contents) # Creating and registering toolbox toolbox = base.Toolbox() # The next line is for parallel evaluations. Comment it out when using parallel runs. # toolbox.register("map", futures.map) toolbox.register("individual_guess", initChromosome, creator.Individual) toolbox.register("mutate", mutate, MUTATION_PROBABILITY) toolbox.register("mate", mate) toolbox.register("select", tools.selTournament, tournsize=3) # Creating and registering stats stats_cost = tools.Statistics(key=lambda ind: ind.fitness.values[0]) stats_dur = tools.Statistics(key=lambda ind: ind.fitness.values[1]) mstats = tools.MultiStatistics(cost=stats_cost, duration=stats_dur) mstats.register("min", numpy.min) mstats.register("avg", numpy.average) mstats.register("max", numpy.max) def genetic_algo(): # Shared constants graph_name = shared.getConst("graph_name") graph = shared.getConst("graph") max_duration = shared.getConst("max_duration") # Extra toolbox registers toolbox.register("population_guess", initPopulation, list, toolbox.individual_guess)
def main(num_Gens, size_Pops, cx, seed=None): # toolbox.register("attr_float", iniPops) # toolbox.register("individual", tools.initIterate, creator.Individuals, toolbox.attr_float) # toolbox.register("population", tools.initRepeat, list, toolbox.individual) # toolbox.register("evaluate", calBenefitandCost) # toolbox.register("mate", tools.cxOnePoint) # toolbox.register("mutate", mutModel, indpb=MutateRate) # toolbox.register("select", tools.selNSGA2) random.seed(seed) stats = tools.Statistics(lambda ind: ind.fitness.values) stats.register("min", numpy.min, axis=0) stats.register("max", numpy.max, axis=0) # stats.register("avg", numpy.mean, axis=0) # stats.register("std", numpy.std, axis=0) logbook = tools.Logbook() logbook.header = "gen", "evals", "min", "max" pop = toolbox.population(n=size_Pops) # Evaluate the individuals with an invalid fitness invalid_ind = [ind for ind in pop if not ind.fitness.valid] try: # parallel on multiprocesor or clusters using SCOOP from scoop import futures fitnesses = futures.map(toolbox.evaluate, invalid_ind) # print "parallel-fitnesses: ",fitnesses except ImportError or ImportWarning: # serial fitnesses = toolbox.map(toolbox.evaluate, invalid_ind) # print "serial-fitnesses: ",fitnesses for ind, fit in zip(invalid_ind, fitnesses): ind.fitness.values = fit # This is just to assign the crowding distance to the individuals # no actual selection is done pop = toolbox.select(pop, int(len(pop) * SelectRate)) record = stats.compile(pop) logbook.record(gen=0, evals=len(invalid_ind), **record) print(logbook.stream) # Begin the generational process for gen in range(1, num_Gens): printInfo("###### Iteration: %d ######" % gen) # Vary the population offspring = tools.selTournamentDCD(pop, int(len(pop) * SelectRate)) offspring = [toolbox.clone(ind) for ind in offspring] for ind1, ind2 in zip(offspring[::2], offspring[1::2]): if random.random() <= cx: toolbox.mate(ind1, ind2) toolbox.mutate(ind1) toolbox.mutate(ind2) del ind1.fitness.values, ind2.fitness.values # Evaluate the individuals with an invalid fitness invalid_ind = [ind for ind in offspring if not ind.fitness.valid] try: # parallel on multiprocesor or clusters using SCOOP from scoop import futures fitnesses = futures.map(toolbox.evaluate, invalid_ind) except ImportError or ImportWarning: # serial fitnesses = toolbox.map(toolbox.evaluate, invalid_ind) # invalid_ind = [ind for ind in offspring if not ind.fitness.valid] # fitnesses = toolbox.map(toolbox.evaluate, invalid_ind) for ind, fit in zip(invalid_ind, fitnesses): ind.fitness.values = fit # Select the next generation population pop = toolbox.select(pop + offspring, size_Pops) record = stats.compile(pop) logbook.record(gen=gen, evals=len(invalid_ind), **record) # print "\nlogbook.stream: ", logbook.stream if gen % 1 == 0: # Create plot createPlot(pop, model_Workdir, num_Gens, size_Pops, gen) # save in file outputStr = "### Generation number: %d, Population size: %d ###" % ( num_Gens, size_Pops) + LF outputStr += "### Generation_%d ###" % gen + LF outputStr += "cost\tbenefit\tscenario" + LF for indi in pop: outputStr += str(indi) + LF outfilename = model_Workdir + os.sep + "NSGAII_OUTPUT" + os.sep + "Gen_" \ + str(GenerationsNum) + "_Pop_" + str(PopulationSize) + os.sep + "Gen_" \ + str(GenerationsNum) + "_Pop_" + str(PopulationSize) + "_resultLog.txt" WriteLog(outfilename, outputStr, MODE='append') printInfo("Final population hypervolume is %f" % hypervolume(pop, [11.0, 11.0])) return pop, logbook
def main(): maxList = [] avgList = [] minList = [] stdList = [] # max value of all runs maxValue = [] # corresponding weight maxWeight = [] for r in range(0, N_RUNS): print('\nAt the run:', r) # create initial population (generation 0): population = toolbox.populationCreator(n=POPULATION_SIZE) # define the hall-of-fame object: hof = tools.HallOfFame(HALL_OF_FAME_SIZE) # prepare the statistics object: 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) # perform the Genetic Algorithm flow: 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("\nBest Ever Individual = ", hof.items[0], "\nFitness: ", r, " ", knapsack(hof.items[0])) # hof.items[0] --> (value,weight) # append max value to a list maxValue.append(knapsack(hof.items[0])[0]) # append max weight to a list maxWeight.append(knapsack(hof.items[0])[1]) # Genetic Algorithm is done with this run - extract statistics: meanFitnessValues, stdFitnessValues, minFitnessValues, maxFitnessValues = logbook.select( "avg", "std", "min", "max") # Save statistics for this run: avgList.append(meanFitnessValues) stdList.append(stdFitnessValues) minList.append(minFitnessValues) maxList.append(maxFitnessValues) print() print('Max Value from all the runs:', max(maxValue)) # len(maxValue) == len(maxWeight) # print corresponding weight for max value for i in range(len(maxValue)): if maxValue[i] == max(maxValue): print('Corresponding Weight:', maxWeight[i]) # Genetic Algorithm is done (all runs) - plot statistics: x = numpy.arange(0, MAX_GENERATIONS + 1) avgArray = numpy.array(avgList) stdArray = numpy.array(stdList) minArray = numpy.array(minList) maxArray = numpy.array(maxList) plt.xlabel('Generation') plt.ylabel('Fitness') plt.title( 'Max and Average Fitness for Knapsack with Tournament Selection - 300 Runs' ) plt.errorbar(x, avgArray.mean(0), yerr=stdArray.mean(0), label="Average", color="Red") plt.errorbar(x, maxArray.mean(0), yerr=maxArray.std(0), label="Best", color="Green") plt.show()
def find_shapelets_pso(timeseries, labels, max_len=100, min_len=1, particles=25, iterations=25, verbose=True, stop_iterations=10): candidates = generate_candidates(timeseries, labels, max_len, min_len) creator.create("FitnessMax", base.Fitness, weights=(1.0,)) creator.create("Particle", np.ndarray, fitness=creator.FitnessMax, speed=list, smin=None, smax=None, best=None) def generate(smin, smax, n): rand_cands = np.array(candidates)[np.random.choice(range(len(candidates)), size=n)] parts = [] for rand_cand in rand_cands: rand_cand = rand_cand[0] part = creator.Particle(rand_cand) part.speed = np.random.uniform(smin, smax, len(rand_cand)) part.smin = smin part.smax = smax parts.append(part) return parts def updateParticle(part, best, phi1, phi2): u1 = np.random.uniform(0, phi1, len(part)) u2 = np.random.uniform(0, phi2, len(part)) v_u1 = u1 * (part.best - part) v_u2 = u2 * (best - part) # These magic numbers are found in http://www.ijmlc.org/vol5/521-C016.pdf part.speed = 0.729*part.speed + np.minimum(np.maximum(1.49445 * (v_u1 + v_u2), part.smin), part.smax) part += part.speed def cost(shapelet): return (check_candidate(timeseries, labels, shapelet)[0], ) toolbox = base.Toolbox() toolbox.register("population", generate, smin=-0.25, smax=0.25) toolbox.register("update", updateParticle, phi1=1, phi2=1) toolbox.register("evaluate", cost) pop = toolbox.population(n=particles) stats = tools.Statistics(lambda ind: ind.fitness.values) stats.register("avg", np.mean) stats.register("std", np.std) stats.register("max", np.max) logbook = tools.Logbook() logbook.header = ["gen", "evals"] + stats.fields GEN = 10000 best = None it_wo_improvement = 0 gain_ub = information_gain_ub(labels) for g in range(GEN): it_wo_improvement += 1 for part in pop: part.fitness.values = toolbox.evaluate(part) if part.best is None or part.best.fitness < part.fitness: part.best = creator.Particle(part) part.best.fitness.values = part.fitness.values if best is None or best.fitness < part.fitness: best = creator.Particle(part) best.fitness.values = part.fitness.values it_wo_improvement = 0 for part in pop: toolbox.update(part, best) # Gather all the fitnesses in one list and print the stats logbook.record(gen=g, evals=len(pop), **stats.compile(pop)) print(logbook.stream) if it_wo_improvement == stop_iterations or best.fitness.values[0] >= gain_ub: break return best
def prepare_statistics(): stats = tools.Statistics(key=lambda ind: ind.fitness.values) stats.register("min", numpy.min) return stats
import random import numpy from deap import algorithms from deap import base from deap import creator from deap import tools random.seed(0) stats = tools.Statistics(key=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) def evalOneMax(individual): return sum(individual), creator.create("FitnessMax", base.Fitness, weights=(1.0, )) creator.create("Individual", list, fitness=creator.FitnessMax) toolbox = base.Toolbox() toolbox.register("attr_bool", random.randint, 0, 1) toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_bool, 100) toolbox.register("population", tools.initRepeat, list, toolbox.individual)
def main(seed=None, play = 0, NGEN = 40, MU = 4 * 10): random.seed(seed) # this has to be a multiple of 4. period. CXPB = 0.9 stats = tools.Statistics(lambda ind: ind.fitness.values[1]) # stats.register("avg", numpy.mean, axis=0) # stats.register("std", numpy.std, axis=0) stats.register("min", numpy.min, axis=0) stats.register("max", numpy.max, axis=0) logbook = tools.Logbook() logbook.header = "gen", "evals", "std", "min", "avg", "max" pop = toolbox.population(n=MU) #network_obj = Neterr(indim, outdim, n_hidden, np.random) # Evaluate the individuals with an invalid fitness invalid_ind = [ind for ind in pop if not ind.fitness.valid] fitnesses = toolbox.map(toolbox.evaluate, invalid_ind) for ind, fit in zip(invalid_ind, fitnesses): ind.fitness.values = fit # This is just to assign the crowding distance to the individuals # no actual selection is done pop = toolbox.select(pop, len(pop)) # print(pop) record = stats.compile(pop) logbook.record(gen=0, evals=len(invalid_ind), **record) print(logbook.stream) maxi = 0 stri = '' flag= 0 # Begin the generational process # print(pop.__dir__()) for gen in range(1, NGEN): # Vary the population offspring = tools.selTournamentDCD(pop, len(pop)) offspring = [toolbox.clone(ind) for ind in offspring] if play : if play == 1: pgen = NGEN*0.1 elif play == 2 : pgen = NGEN*0.9 if gen == int(pgen): print("gen:",gen, "doing clustering") to_bp_lis = cluster.give_cluster_head(offspring, int(MU*bp_rate)) assert (to_bp_lis[0] in offspring ) print( "doing bp") [ item.modify_thru_backprop(indim, outdim, network_obj.rest_setx, network_obj.rest_sety, epochs=10, learning_rate=0.1, n_par=10) for item in to_bp_lis] for ind1, ind2 in zip(offspring[::2], offspring[1::2]): # print(ind1.fitness.values) """if not flag : ind1.modify_thru_backprop(indim, outdim, network_obj.rest_setx, network_obj.rest_sety, epochs=10, learning_rate=0.1, n_par=10) flag = 1 print("just testing") """ if random.random() <= CXPB: toolbox.mate(ind1, ind2, gen) maxi = max(maxi, ind1.node_ctr, ind2.node_ctr) toolbox.mutate(ind1) toolbox.mutate(ind2) del ind1.fitness.values, ind2.fitness.values # Evaluate the individuals with an invalid fitness invalid_ind = [ind for ind in offspring if not ind.fitness.valid] fitnesses = toolbox.map(toolbox.evaluate, invalid_ind) for ind, fit in zip(invalid_ind, fitnesses): ind.fitness.values = fit # Select the next generation population pop = toolbox.select(pop + offspring, MU) record = stats.compile(pop) logbook.record(gen=gen, evals=len(invalid_ind), **record) anost = logbook.stream liso = [item.rstrip() for item in anost.split("\t")] mse = float(liso[3]) if (mse <= 115 ): print("already achieved a decent performance(validation), breaking at gen_no.", gen) break print(anost) stri += anost + '\n' # file_ob.write(str(logbook.stream)) # print(len(pop)) # file_ob.close() #print(stri) return pop, logbook
def ga_opt(load_dir_store, hparams): # Load all the saved data_store.pkl into data_store list data_store = prepare_grand_data_store(load_dir_store) yt = data_store[0]['train']['df'].iloc[:, -6:-3].values p_yt_store = np.array( [data['train']['df'].iloc[:, -3:].values for data in data_store]) yv = data_store[0]['val']['df'].iloc[:, -6:-3].values p_yv_store = np.array( [data['val']['df'].iloc[:, -3:].values for data in data_store]) def eval(individual): # Individual is a list of 0 or 1, where if the j entry is 1, the j model is included and vice versa selected_mask = [ idx for idx, value in enumerate(individual) if value == 1 ] # Calculate mean relative error for the selected models re_t = mean_relative_error( yt, np.mean(p_yt_store[selected_mask, :, :], axis=0)) re_v = mean_relative_error( yv, np.mean(p_yv_store[selected_mask, :, :], axis=0)) re = (re_t + 2 * re_v) / 3 return (re, ) creator.create("FitnessMax", base.Fitness, weights=(-1, )) creator.create("Individual", list, fitness=creator.FitnessMax) toolbox = base.Toolbox() toolbox.register("attr_bool", np.random.choice, np.arange(0, 2), p=hparams['init']) toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_bool, n=len(data_store)) toolbox.register("population", tools.initRepeat, list, toolbox.individual) toolbox.register("evaluate", eval) toolbox.register("mate", tools.cxTwoPoint) toolbox.register("mutate", tools.mutFlipBit, indpb=0.2) toolbox.register("select", tools.selTournament, tournsize=3) # Logging stats = tools.Statistics(key=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) pop = toolbox.population(n=hparams['n_pop']) hof = tools.HallOfFame(1) # Run the GA algorithm pop, logbook = algorithms.eaSimple(toolbox=toolbox, population=pop, cxpb=0.5, mutpb=0.2, ngen=hparams['n_gen'], halloffame=hof, stats=stats, verbose=True) # Create the ga results dir based on the load dir name results_dir = create_results_directory(f'./results/ga/ga_opt', folders=['plots'], excels=['ga_results']) # Plotting gen = logbook.select("gen") fit_min = [x.item() for x in logbook.select("min")] fit_avg = [x.item() for x in logbook.select("avg")] fit_max = [x.item() for x in logbook.select("max")] fig, ax1 = plt.subplots() line1 = ax1.plot(gen, fit_min, label="Min MRE") line2 = ax1.plot(gen, fit_avg, label="Avg MRE") line3 = ax1.plot(gen, fit_max, label="Max MRE") plt.legend() ax1.set_xlabel("Generation") ax1.set_ylabel("Relative Error") plt.savefig('{}/plots/GA_opt_MRE_all.png'.format(results_dir), bbox_inches="tight") fig, ax1 = plt.subplots() line1 = ax1.plot(gen, fit_min, label="Min MRE") plt.legend() ax1.set_xlabel("Generation") ax1.set_ylabel("Total Generation Cost") plt.savefig('{}/plots/GA_opt_min_only.png'.format(results_dir), bbox_inches="tight") # Store final results av = hof[-1] # av stands for allocation vector results_dict = defaultdict(list) data_names = [k for k in data_store[0].keys() if k not in ['info']] for data, indicator in zip(data_store, av): if indicator == 1: # Means include the model for k in data_names: results_dict[k].append(data[k]['df'].iloc[:, -3:].values) # Create excel workbook to print GA results to wb = openpyxl.Workbook() # Print allocation vector to excel wb.create_sheet('av') ws = wb['av'] model_names = [data['info']['model_name'] for data in data_store] print_df_to_excel(df=pd.DataFrame([av, model_names], index=['av', 'model_names']).T, ws=ws) summary_df = {} for k, v in results_dict.items( ): # Print the prediction for each dataset to excel y = data_store[0][k]['df'].iloc[:, -6:-3].values v = np.array(v) p_y = np.mean(v, axis=0) mse = mean_squared_error(y, p_y) mre = mean_relative_error(y, p_y) var = np.mean(np.var(v, axis=0)) summary_df[k] = {'mse': mse, 'mre': mre, 'var': var} df = pd.DataFrame(np.hstack((y, p_y)), columns=[f'y{i + 1}' for i in range(3)] + [f'P_y{i + 1}' for i in range(3)]) wb.create_sheet(k) ws = wb[k] print_df_to_excel(df=df, ws=ws) print_df_to_excel(df=pd.DataFrame.from_dict({ 'mse': [mse], 'mre': [mre] }), ws=ws, start_col=10) # Print summary of losses for different dataset in the summary worksheet summary_df = pd.DataFrame.from_dict(summary_df) def move_column_inplace(df, col, pos): col = df.pop(col) df.insert(pos, col.name, col) move_column_inplace(summary_df, 'train', 0) move_column_inplace(summary_df, 'val', 1) ws = wb['Sheet'] print_df_to_excel(df=summary_df, ws=ws, start_row=5) print_df_to_excel(df=pd.DataFrame(hparams), ws=ws) # Save and close excel workbook wb.save(f'{results_dir}/ga_results.xlsx') wb.close()
def ga_fit(_rrl, min_ind, max_ind, random_state, nind, ngen): import random from deap import algorithms from deap import base from deap import creator from deap import tools creator.create("FitnessMax", base.Fitness, weights=(1.0,)) creator.create("Individual", np.ndarray, fitness=creator.FitnessMax) toolbox = base.Toolbox() def create_ind_uniform(min_ind, max_ind): ind = [] for min, max in zip(min_ind, max_ind): ind.append(random.uniform(min, max)) return ind def create_ind_gauss(mu_ind, sigma_ind): ind = [] for mu, sigma in zip(mu_ind, sigma_ind): ind.append(random.gauss(mu, sigma)) return ind toolbox.register("create_ind", create_ind_uniform, min_ind, max_ind) # toolbox.register("create_ind", create_ind_gauss, mu_ind, sigma_ind) toolbox.register("individual", tools.initIterate, creator.Individual, toolbox.create_ind) toolbox.register("population", tools.initRepeat, list, toolbox.individual) def evalOneMax(individual): return _rrl.calc_S(individual), def cxTwoPointCopy(ind1, ind2): size = len(ind1) cxpoint1 = random.randint(1, size) cxpoint2 = random.randint(1, size - 1) if cxpoint2 >= cxpoint1: cxpoint2 += 1 else: # Swap the two cx points cxpoint1, cxpoint2 = cxpoint2, cxpoint1 ind1[cxpoint1:cxpoint2], ind2[cxpoint1:cxpoint2] = ind2[cxpoint1:cxpoint2].copy(), ind1[cxpoint1:cxpoint2].copy() return ind1, ind2 def mutUniformDbl(individual, min_ind, max_ind, indpb): size = len(individual) for i, min, max in zip(xrange(size), min_ind, max_ind): if random.random() < indpb: individual[i] = random.uniform(min, max) return individual, toolbox.register("evaluate", evalOneMax) toolbox.register("mate", cxTwoPointCopy) toolbox.register("mutate", mutUniformDbl, min_ind=min_ind, max_ind=max_ind, indpb=0.05) toolbox.register("select", tools.selTournament, tournsize=3) random.seed(random_state) pop = toolbox.population(n=nind) hof = tools.HallOfFame(1, similar=np.array_equal) 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) # Get elapsed time import time tic = time.clock() def get_elapsedtime(data): return time.clock() - tic stats.register("elapsed time",get_elapsedtime) pop_last, logbook = algorithms.eaSimple(pop, toolbox, cxpb=0.5, mutpb=0.2, ngen=ngen, stats=stats, halloffame=hof) best_ind = tools.selBest(pop_last, 1)[0] return best_ind, logbook
heft_gen = lambda n: [ deepcopy(heft_mapping) if random.random() > 1.0 else generate(_wf, rm, estimator, 1)[0] for _ in range(n) ] W, C1, C2 = 0.1, 0.6, 0.2 GEN, N = 10, 4 toolbox = Toolbox() toolbox.register("population", heft_gen) toolbox.register("fitness", fitness, _wf, rm, estimator, sorted_tasks) toolbox.register("update", update) stats = tools.Statistics(lambda ind: ind.fitness.values[0]) stats.register("avg", numpy.mean) stats.register("std", numpy.std) stats.register("min", numpy.min) stats.register("max", numpy.max) logbook = tools.Logbook() logbook.header = ["gen", "evals"] + stats.fields def do_exp(): pop, log, best = run_pso( toolbox=toolbox, logbook=logbook, stats=stats,
def GP_deap(evolved_train): global HOWMANYITERS import operator import math import random from deap import algorithms from deap import base, creator from deap import tools from deap import gp # dropping Survived and Passenger ID because we can not use them for training outputs = evolved_train['Survived'].values.tolist() evolved_train = evolved_train.drop(["Survived", "PassengerId"], axis=1) inputs = evolved_train.values.tolist() # to np array def protectedDiv(left, right): try: return left / right except ZeroDivisionError: return 1 def randomString(stringLength=10): """Generate a random string of fixed length """ letters = string.ascii_lowercase return ''.join(random.choice(letters) for i in range(stringLength)) #choosing Primitives pset = gp.PrimitiveSet("MAIN", len(evolved_train.columns)) # add here pset.addPrimitive(operator.add, 2) pset.addPrimitive(operator.sub, 2) pset.addPrimitive(operator.mul, 2) pset.addPrimitive(protectedDiv, 2) pset.addPrimitive(math.cos, 1) pset.addPrimitive(math.sin, 1) pset.addPrimitive(math.tanh, 1) pset.addPrimitive(max, 2) pset.addPrimitive(min, 2) pset.addEphemeralConstant(randomString(), lambda: random.uniform(-10, 10)) # 50 as a precaution. 34 would be enough pset.renameArguments(ARG0='x1') pset.renameArguments(ARG1='x2') pset.renameArguments(ARG2='x3') pset.renameArguments(ARG3='x4') pset.renameArguments(ARG4='x5') pset.renameArguments(ARG5='x6') pset.renameArguments(ARG6='x7') pset.renameArguments(ARG7='x8') pset.renameArguments(ARG8='x9') pset.renameArguments(ARG9='x10') pset.renameArguments(ARG10='x11') pset.renameArguments(ARG11='x12') pset.renameArguments(ARG12='x13') pset.renameArguments(ARG13='x14') pset.renameArguments(ARG14='x15') pset.renameArguments(ARG15='x16') pset.renameArguments(ARG16='x17') pset.renameArguments(ARG17='x18') pset.renameArguments(ARG18='x19') pset.renameArguments(ARG19='x20') pset.renameArguments(ARG20='x21') pset.renameArguments(ARG21='x22') pset.renameArguments(ARG22='x23') pset.renameArguments(ARG23='x24') pset.renameArguments(ARG24='x25') pset.renameArguments(ARG25='x26') pset.renameArguments(ARG26='x27') pset.renameArguments(ARG27='x28') pset.renameArguments(ARG28='x29') pset.renameArguments(ARG29='x30') pset.renameArguments(ARG30='x31') pset.renameArguments(ARG31='x32') pset.renameArguments(ARG32='x33') pset.renameArguments(ARG33='x34') pset.renameArguments(ARG34='x35') pset.renameArguments(ARG35='x36') pset.renameArguments(ARG36='x37') pset.renameArguments(ARG37='x38') pset.renameArguments(ARG38='x39') pset.renameArguments(ARG39='x40') pset.renameArguments(ARG40='x41') pset.renameArguments(ARG41='x42') pset.renameArguments(ARG42='x43') pset.renameArguments(ARG43='x44') pset.renameArguments(ARG44='x45') pset.renameArguments(ARG45='x46') pset.renameArguments(ARG46='x47') pset.renameArguments(ARG47='x48') pset.renameArguments(ARG48='x49') pset.renameArguments(ARG49='x50') # two object types is needed: an individual containing the genotype # and a fitness - The reproductive success of a genotype (a measure of quality of a solution) creator.create("FitnessMin", base.Fitness, weights=(1.0, )) creator.create("Individual", gp.PrimitiveTree, fitness=creator.FitnessMin) #register some parameters specific to the evolution process. toolbox = base.Toolbox() toolbox.register("expr", gp.genHalfAndHalf, pset=pset, min_=1, max_=3) # toolbox.register("individual", tools.initIterate, creator.Individual, toolbox.expr) toolbox.register("population", tools.initRepeat, list, toolbox.individual) toolbox.register("compile", gp.compile, pset=pset) #evaluation function, which will receive an individual as input, and return the corresponding fitness. def evalSymbReg(individual): # Transform the tree expression in a callable function func = toolbox.compile(expr=individual) # Evaluate the accuracy of individuals // 1|0 == survived return math.fsum( np.round(1. - (1. / (1. + np.exp(-func(*in_))))) == out for in_, out in zip(inputs, outputs)) / len(evolved_train), toolbox.register("evaluate", evalSymbReg) toolbox.register("select", tools.selTournament, tournsize=3) toolbox.register("mate", gp.cxOnePoint) toolbox.register("expr_mut", gp.genFull, min_=0, max_=3) 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)) pop = toolbox.population(n=300) hof = tools.HallOfFame(1) #Statistics over the individuals fitness and size stats_fit = tools.Statistics(lambda ind: ind.fitness.values) stats_size = tools.Statistics(len) stats = tools.MultiStatistics(fitness=stats_fit, size=stats_size) stats.register("avg", np.mean) stats.register("std", np.std) stats.register("min", np.min) stats.register("max", np.max) pop, log = algorithms.eaSimple(pop, toolbox, cxpb=0.7, mutpb=0.3, ngen=HOWMANYITERS, stats=stats, halloffame=hof, verbose=True) #Parameters: #population – A list of individuals. #toolbox – A Toolbox that contains the evolution operators. #cxpb – The probability of mating two individuals. #mutpb – The probability of mutating an individual. #ngen – The number of generation. #stats – A Statistics object that is updated inplace, optional. #halloffame – A HallOfFame object that will contain the best individuals, optional. #verbose – Whether or not to log the statistics. # Transform the tree expression of hof[0] in a callable function and return it func2 = toolbox.compile(expr=hof[0]) return func2
def main(): global snake global pset ## THIS IS WHERE YOUR CORE EVOLUTIONARY ALGORITHM WILL GO # pset = gp.PrimitiveSet("MAIN", 0) pset.addPrimitive(prog2, 2) pset.addPrimitive(prog3, 3) pset.addPrimitive(snake.if_obstacle_ahead, 2) pset.addPrimitive(snake.if_next_obstacle_ahead, 2) pset.addPrimitive(snake.if_obstacle_right, 2) pset.addPrimitive(snake.if_obstacle_left, 2) #pset.addPrimitive(snake.if_obstacle_up, 2) #pset.addPrimitive(snake.if_obstacle_down, 2) #pset.addPrimitive(snake.if_next_obstacle_up, 2) #pset.addPrimitive(snake.if_next_obstacle_down, 2) #pset.addPrimitive(snake.if_next_obstacle_left, 2) #pset.addPrimitive(snake.if_next_obstacle_right, 2) pset.addPrimitive(snake.if_move_up, 2) pset.addPrimitive(snake.if_move_down, 2) pset.addPrimitive(snake.if_move_left, 2) pset.addPrimitive(snake.if_move_right, 2) pset.addTerminal(snake.changeDirectionUp) pset.addTerminal(snake.changeDirectionDown) pset.addTerminal(snake.changeDirectionLeft) pset.addTerminal(snake.changeDirectionRight) pset.addTerminal(snake.moveForward, name="forward") creator.create("FitnessMax", base.Fitness, weights=(1.0, 1.0)) creator.create("Individual", gp.PrimitiveTree, fitness=creator.FitnessMax, pset=pset) toolbox = base.Toolbox() # Attribute generator toolbox.register("expr_init", gp.genGrow, pset=pset, min_=1, max_=5) # Structure initializers toolbox.register("individual", tools.initIterate, creator.Individual, toolbox.expr_init) toolbox.register("population", tools.initRepeat, list, toolbox.individual) def evalSteps(individual): score, steps = runGame(individual) return steps, def evalScore(individual): score, steps = runGame(individual) return score, def evalScoreAndSteps(individual): score, steps = runGame(individual) return score, steps toolbox.register("evaluate", evalScoreAndSteps) toolbox.register("select", tools.selNSGA2) toolbox.register("mate", gp.cxOnePoint) toolbox.register("expr_mut", gp.genHalfAndHalf, min_=1, max_=4) 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)) random.seed(69) pop = toolbox.population(n=400) hof = tools.HallOfFame(5) 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, log = algorithms.eaMuPlusLambda(pop, toolbox, MU, LAMBDA, MUTPB, CXPB, NGEN, stats, halloffame=hof) expr = tools.selBest(pop, 1)[0] nodes, edges, labels = gp.graph(expr) # g = pgv.AGraph(nodeSep=1.0) # g.add_nodes_from(nodes) # g.add_edges_from(edges) # g.layout(prog="dot") # for i in nodes: # n = g.get_node(i) # n.attr["label"] = labels[i] # g.draw("tree.pdf") return pop, hof, stats
def main(): random.seed(24) # cria populacao inicial pop = toolbox.population(n=30) # CXPB - probabilidade de crossover # MUTPB - probabilidade de mutacao # NGEN - numero de geracoes CXPB, MUTPB, NGEN =0.8, 0.05, 10 #stats a serem guardados stats = tools.Statistics(key=lambda ind: ind.fitness.values) stats.register("std", numpy.std) stats.register("min", numpy.min) stats.register("avg", numpy.mean) stats.register("max", numpy.max) #Roda o algoritmo pop, logbook = algorithms.eaSimple(pop, toolbox, CXPB, MUTPB, NGEN, stats=stats) #Seleciona o melhor individuo da populacao resultante best_ind = tools.selSPEA2(pop, 1) #Imprime as infromações do melhor individuo print_ind(best_ind[0]) # Plota o Gráfico # plot_log(logbook) mse = 0 image = cv2.imread(target_image) rows, cols, _ = image.shape # Size of background Image new_image = numpy.zeros((rows, cols, 3)) for i in range(rows): for j in range(cols): input_data = [[i * 1.0 / rows], [j * 1.0 / cols], [np.sin(20 * 3.14 * (i + j) * 1.0 / cols)]] red_bin = int_to_bin(image[i, j, 0]) green_bin = int_to_bin(image[i, j, 1]) blue_bin = int_to_bin(image[i, j, 2]) # target_data = [[r], # [g], # [b]] target_data = [[red_bin[0] * 1.0], [red_bin[1] * 1.0], [red_bin[2] * 1.0], [red_bin[3] * 1.0], [red_bin[4] * 1.0], [red_bin[5] * 1.0], [red_bin[6] * 1.0], [red_bin[7] * 1.0], [green_bin[0] * 1.0], [green_bin[1] * 1.0], [green_bin[2] * 1.0], [green_bin[3] * 1.0], [green_bin[4] * 1.0], [green_bin[5] * 1.0], [green_bin[6] * 1.0], [green_bin[7] * 1.0], [blue_bin[0] * 1.0], [blue_bin[1] * 1.0], [blue_bin[2] * 1.0], [blue_bin[3] * 1.0], [blue_bin[4] * 1.0], [blue_bin[5] * 1.0], [blue_bin[6] * 1.0], [blue_bin[7] * 1.0]] nn_output = nn.get_output(input_data) if np.isnan(np.sum(nn_output)): continue # print('For epoch %s and input %s got output %s given target %s' \ # % (e, i, nn_output, targets[i])) # print("Current weights: ") # print(str(nn.layers[0].weights)) # print("Current bias: ") # print(str(nn.layers[0].bias)) nn_error = target_data - nn_output # print("Current error: ") # print(str(nn_error)) mse = mse + np.inner(np.transpose(nn_error), np.transpose(nn_error)) # new_image[i, j, 0] = min(255, max(0, int(255 * nn_output[0]))) # new_image[i, j, 1] = min(255, max(0, int(255 * nn_output[1]))) # new_image[i, j, 2] = min(255, max(0, int(255 * nn_output[2]))) def norm_data(x): if x > 0.5: return 1 else: return 0 red = norm_data(nn_output[0]) + norm_data(nn_output[1]) * 2 + norm_data(nn_output[2]) * 4 + \ norm_data(nn_output[3]) * 8 + norm_data(nn_output[4]) * 16 + norm_data(nn_output[5]) * 32 + \ norm_data(nn_output[6]) * 64 + norm_data(nn_output[7]) * 128 blue = norm_data(nn_output[8]) + norm_data(nn_output[9]) * 2 + norm_data(nn_output[10]) * 4 + \ norm_data(nn_output[11]) * 8 + norm_data(nn_output[12]) * 16 + norm_data(nn_output[13]) * 32 + \ norm_data(nn_output[14]) * 64 + norm_data(nn_output[15]) * 128 green = norm_data(nn_output[16]) + norm_data(nn_output[17]) * 2 + norm_data(nn_output[18]) * 4 + \ norm_data(nn_output[19]) * 8 + norm_data(nn_output[20]) * 16 + norm_data(nn_output[21]) * 32 + \ norm_data(nn_output[22]) * 64 + norm_data(nn_output[23]) * 128 new_image[i, j, 0] = int(min(255, max(0, 255 * red))) new_image[i, j, 1] = int(min(255, max(0, 255 * blue))) new_image[i, j, 2] = int(min(255, max(0, 255 * green))) print("Error on testing dataset: " + str(mse)) cv2.imwrite("generated_image.png", new_image)
toolbox.register('evaluate', avaliacao) toolbox.register('mate', tools.cxOnePoint) toolbox.register('mutate', tools.mutFlipBit, indpb=0.01) toolbox.register('select', tools.selRoulette) if __name__ == '__main__': random.seed(1) populacao = toolbox.population(n=20) probabilidade_crossover = 1.0 probabilidade_mutacao = 0.01 numero_geracoes = 100 estatisticas = tools.Statistics( key=lambda individuo: individuo.fitness.values) estatisticas.register('max', np.max) estatisticas.register('min', np.min) estatisticas.register('med', np.mean) estatisticas.register('std', np.std) populacao, info = algorithms.eaSimple(populacao, toolbox, probabilidade_crossover, probabilidade_mutacao, numero_geracoes, estatisticas) melhores = tools.selBest(populacao, 2) for individuo in melhores: print(individuo) print(individuo.fitness) soma = 0
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 main(pop=10000, CXPB=0.75, MUTPB=0.1, NumGenWithoutConverge=300, file=None): """ Execute Genetic Algorithm. args: pop -> population of GA CXPB -> Crossover Probability MUTPB -> MUTATION Probability NumGenWithoutConverge -> Number of generations without converge file -> if write results in file """ pop = toolbox.population(n=pop) gen, genMelhor = 0, 0 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) # Evaluate the entire population list(toolbox.map(toolbox.evaluate, pop)) melhor = min([i.fitness.values for i in pop]) logbook = tools.Logbook() p = stats.compile(pop) logbook.record(gen=0, **p) logbook.header = "gen", 'min', 'max', "avg", "std" print(logbook.stream, file=file) while gen - genMelhor <= NumGenWithoutConverge: # Select the next generation individuals offspring = toolbox.select(pop, len(pop)) # Clone the selected individuals offspring = list(toolbox.map(toolbox.clone, offspring)) # Apply crossover and mutation on the offspring for child1, child2 in zip(offspring[::2], offspring[1::2]): if random.random() < CXPB: toolbox.mate0(child1[0], child2[0]) toolbox.mate1(child1[1], child2[1]) del child1.fitness.values del child2.fitness.values for mutant in offspring: if random.random() < MUTPB: toolbox.mutate0(mutant[0]) toolbox.mutate1(mutant[1]) del mutant.fitness.values # Evaluate the individuals with an invalid fitness invalid_ind = [ind for ind in offspring if not ind.fitness.valid] list(toolbox.map(toolbox.evaluate, invalid_ind)) # The population is entirely replaced by the offspring pop[:] = offspring gen += 1 minF = min([i.fitness.values for i in pop]) if minF < melhor: melhor = minF genMelhor = gen p = stats.compile(pop) logbook.record(gen=gen, **p) if gen - genMelhor <= NumGenWithoutConverge and gen != 1: print(logbook.stream) else: print(logbook.stream, file=file) hof.update(pop) return pop, stats, hof
# AUTOGENERATED! DO NOT EDIT! File to edit: nbs/06_quick.ipynb (unless otherwise specified). __all__ = ['qtuneSimple', 'default_stats', 'qtuneIterate'] # Cell from .algorithms import eaSimpleWithExtraLog from .utils import ConcurrentMap from .crossover import cxDictUniform from .mutation import mutDictRand from deap import base, creator, tools from functools import partial import random import numpy default_stats = tools.Statistics(lambda ind: ind.fitness.values) default_stats.register("avg", numpy.mean, axis=0) default_stats.register("std", numpy.std, axis=0) default_stats.register("min", numpy.min, axis=0) default_stats.register("max", numpy.max, axis=0) def qtuneSimple(params, evaluate, n_pop=10, cxpb=0.6, mutpb=0.6, ngen=10, hof=2, elitism=True, stats=default_stats, crossover=partial(cxDictUniform, indpb=0.6),
def ga_mu_plus_lambda(self, mu, lambda_, checkpoint_load, checkpoint_fname, checkpoint_freq, sel_best, verbose): """The (mu + lambda) evolutionary algorithm. Adapted from: https://github.com/DEAP/deap/blob/master/deap/algorithms.py The pseudo-code goes as follows: evaluate(population) for g in range(ngen): offspring = varOr(population, toolbox, lambda_, cxpb, mutpb) evaluate(offspring) population = select(population + offspring, mu) First, the individuals having an invalid fitness are evaluated. Second, the evolutionary loop begins by producing "lambda_" offspring from the population, the offspring are generated by the "varOr" function. The offspring are then evaluated and the next generation population is selected from both the offspring and the population. Finally, when "max_gen" generations are done, the algorithm returns a tuple with the final population and a "deap.tools.Logbook" of the evolution. This function expects "toolbox.mate", "toolbox.mutate", "toolbox.select", and "toolbox.evaluate" aliases to be registered in the toolbox. Arguments: mu (float): number of individuals to select for the next generation. lambda_ (int): number of children to produce at each generation. checkpoint_load (str or None): checkpoint file to load, if provided. checkpoint_fname (str): name of the checkpoint file to save. checkpoint_freq (str): checkpoint saving frequency (relative to gen). sel_best (int): number of best individuals to log at each generation. verbose (bool): run in verbosity mode. Returns: tuple: final population and the logbook of the evolution. """ # Create the statistics stats_pop = tools.Statistics() stats_fit = tools.Statistics(key=lambda ind: ind.fitness.values) stats_res = tools.Statistics(key=lambda ind: ind.result) stats = tools.MultiStatistics(population=stats_pop, fitness=stats_fit, result=stats_res) stats.register("value", copy.deepcopy) # If a checkpoint is provided, continue from the given generation if checkpoint_load: # Load the dictionary from the pickled file cp = file.read_pickle(checkpoint_load) # Load the stored parameters population = cp['population'] start_gen = cp['generation'] + 1 logbook = cp['logbook'] random.setstate(cp['rnd_state']) logger.info("Running from a checkpoint!") logger.info("-- Population size: %d", len(population)) logger.info("-- Current generation: %d\n", start_gen) else: # Create the population population = self.toolbox.population(n=self.pop_size) start_gen = 1 # Create the logbook logbook = tools.Logbook() logbook.header = 'gen', 'evals', 'population', 'fitness', 'result' # Get the individuals that are not evaluated invalid_inds = [ind for ind in population if not ind.fitness.valid] # The number of simulation calls to the server is the number of # invalid individuals num_sims = len(invalid_inds) logger.info("Starting the initial evaluation | evaluations: %d", num_sims) # Evaluation start time start_time = time.time() # Evaluate the individuals with an invalid fitness results = self.toolbox.evaluate(invalid_inds) for ind, res_ind in zip(invalid_inds, results): ind.fitness.values = res_ind[0] ind.result = res_ind[1] # Assign the crowding distance to the individuals (no selection is done) population = self.toolbox.select(population, len(population)) record = stats.compile(population) logbook.record(gen=0, evals=num_sims, **record) # Evaluation time total_time = time.time() - start_time mins, secs = divmod(total_time, 60) hours, mins = divmod(mins, 60) msg = f"Finished generation. Elapsed time: {hours:02.0f}h{mins:02.0f}m{secs:02.0f}s" secs = total_time / num_sims mins, secs = divmod(secs, 60) msg += f" | avg: {mins:02.0f}m{secs:02.2f}s/ind\n" logger.info(msg) print( "====================== Starting Optimization ======================\n" ) # Begin the generational process for gen in range(start_gen, self.max_gen + 1): # Vary the population offspring = algorithms.varOr(population, self.toolbox, lambda_, self.cx_prob, self.mut_prob) # Evaluate the individuals with an invalid fitness invalid_inds = [ind for ind in offspring if not ind.fitness.valid] # The number of simulation calls to the server is the number of # invalid individuals num_sims = len(invalid_inds) msg = f"Starting generation {gen}/{self.max_gen} | evaluations: {num_sims}" logger.info(msg) # Evaluation start time start_time = time.time() # Evaluate the individuals with an invalid fitness results = self.toolbox.evaluate(invalid_inds) for ind, res_ind in zip(invalid_inds, results): ind.fitness.values = res_ind[0] ind.result = res_ind[1] # Update the statistics with the population record = stats.compile(population) logbook.record(gen=gen, evals=num_sims, **record) # Save a checkpoint of the evolution if gen % checkpoint_freq == 0: cp = dict(generation=gen, population=population, logbook=logbook, rnd_state=random.getstate()) file.write_pickle(checkpoint_fname, cp) # Evaluation time total_time = time.time() - start_time mins, secs = divmod(total_time, 60) hours, mins = divmod(mins, 60) msg = f"Finished generation. " msg += f"Elapsed time: {hours:02.0f}h{mins:02.0f}m{secs:02.0f}s | " secs = total_time / num_sims mins, secs = divmod(secs, 60) msg += f"avg: {mins:02.0f}m{secs:02.2f}s/ind\n" logger.info(msg) # Show the best individuals of each generation print(f"---- Best {sel_best} individuals of this generation ----") best_inds = tools.selBest(population, sel_best) for i, ind in enumerate(best_inds): print(f"Ind #{i + 1} => ", end='') # Circuit variables/parameters formatted_params = [ f"{key}: {ind[idx]:0.2g}" for idx, key in enumerate(self.circuit_vars) ] print(' | '.join(formatted_params)) # Fitness print("\t Fitness -> ", end='') formatted_fits = [ f"{key}: {ind.fitness.values[idx]:0.2g}" for idx, key in enumerate(list(self.objectives.keys())) ] print(' | '.join(formatted_fits)) # Simulation results print("\t Results -> ", end='') formatted_res = [ f"{key}: {val:0.2g}" for key, val in ind.result.items() ] print(' | '.join(formatted_res)) print("") # Select the next generation population population[:] = self.toolbox.select(population + offspring, mu) return population, logbook
def main(): # test if file is ok before starting the test if args.filename: open(args.filename).close() random.seed(64) beginTime = time.time() evaluationTime = 0 population = toolbox.population(n=args.population) 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) logger = tools.EvolutionLogger(["gen", "evals", "time"] + [str(k) for k in stats.functions.keys()]) logger.logHeader() CXPB, MUTPB, ADDPB, DELPB, NGEN = 0.5, 0.2, 0.01, 0.01, args.generations evalBegin = time.time() # Evaluate every individuals fitnesses = toolbox.map(toolbox.evaluate, population) for ind, fit in zip(population, fitnesses): ind.fitness.values = fit evaluationTime += (time.time() - evalBegin) hof.update(population) stats.update(population) logger.logGeneration(gen=0, evals=len(population), stats=stats, time=evaluationTime) # Begin the evolution for g in range(1, NGEN): offspring = [toolbox.clone(ind) for ind in population] # Apply crossover and mutation for ind1, ind2 in zip(offspring[::2], offspring[1::2]): if random.random() < CXPB: toolbox.mate(ind1, ind2) del ind1.fitness.values del ind2.fitness.values # Note here that we have a different sheme of mutation than in the # original algorithm, we use 3 different mutations subsequently. for ind in offspring: if random.random() < MUTPB: toolbox.mutate(ind) del ind.fitness.values if random.random() < ADDPB: toolbox.addwire(ind) del ind.fitness.values if random.random() < DELPB: toolbox.delwire(ind) del ind.fitness.values # Evaluate the individuals with an invalid fitness invalid_ind = [ind for ind in offspring if not ind.fitness.valid] evalBegin = time.time() fitnesses = toolbox.map(toolbox.evaluate, invalid_ind) for ind, fit in zip(invalid_ind, fitnesses): ind.fitness.values = fit evaluationTime += (time.time() - evalBegin) population = toolbox.select(population + offspring, len(offspring)) hof.update(population) stats.update(population) logger.logGeneration(gen=g, evals=len(invalid_ind), stats=stats, time=evaluationTime) best_network = sn.SortingNetwork(INPUTS, hof[0]) print(best_network) print(best_network.draw()) print("%i errors, length %i, depth %i" % hof[0].fitness.values) totalTime = time.time() - beginTime print("Total time: {0}\nEvaluation time: {1}".format( totalTime, evaluationTime)) if args.filename: f = open(args.filename, "a") f.write("{0};{1};{2};{3}\n".format(args.cores, INPUTS, totalTime, evaluationTime)) f.close() return population, stats, hof
def run_opt(self, pop_size, generations, cores=1, plot=False, log=False, log_path=None, run_id=None, store_params=True, **kwargs): """Runs the optimizer. Parameters ---------- pop_size: int Size of the population each generation. generation: int Number of generations in optimisation. cores: int, optional Number of CPU cores used to run the optimisation. If the 'mp_disabled' keyword is passed to the optimizer, this will be ignored and one core will be used. plot: bool, optional If true, matplotlib will be used to plot information about the minimisation. log: bool, optional If true, a log file describing the optimisation will be created. By default it will be written to the current directory and named according to the time the minimisation finished. This can be manually specified by passing the 'output_path' and 'run_id' keyword arguments. log_path : str Path to write output file. run_id : str An identifier used as the name of your log file. store_params: bool, optional If true, the parameters for each model created during the optimisation will be stored. This can be used to create funnel data later on. """ self._cores = cores self._store_params = store_params self.parameter_log = [] self._model_count = 0 self.halloffame = tools.HallOfFame(1) self.stats = tools.Statistics(lambda thing: thing.fitness.values) self.stats.register("avg", numpy.mean) self.stats.register("std", numpy.std) self.stats.register("min", numpy.min) self.stats.register("max", numpy.max) self.logbook = tools.Logbook() self.logbook.header = ["gen", "evals"] + self.stats.fields start_time = datetime.datetime.now() self._initialize_pop(pop_size) for g in range(generations): self._update_pop(pop_size) self.halloffame.update(self.population) self.logbook.record(gen=g, evals=self._evals, **self.stats.compile(self.population)) print(self.logbook.stream) end_time = datetime.datetime.now() time_taken = end_time - start_time print("Evaluated {} models in total in {}".format( self._model_count, time_taken)) print("Best fitness is {0}".format(self.halloffame[0].fitness)) print("Best parameters are {0}".format(self.parse_individual( self.halloffame[0]))) for i, entry in enumerate(self.halloffame[0]): if entry > 0.95: print( "Warning! Parameter {0} is at or near maximum allowed " "value\n".format(i + 1)) elif entry < -0.95: print( "Warning! Parameter {0} is at or near minimum allowed " "value\n".format(i + 1)) if log: self.log_results(output_path=output_path, run_id=run_id) if plot: print('----Minimisation plot:') plt.figure(figsize=(5, 5)) plt.plot(range(len(self.logbook.select('min'))), self.logbook.select('min')) plt.xlabel('Iteration', fontsize=20) plt.ylabel('Score', fontsize=20) return
def main(n_corr, p, problem, database_name, pset, config): pop_size = config["population_size"] cxpb = config["cxpb"] # 0.9 mutpb = config["mutpb"] # 0.1 train_p = config["train_p"] test_p = config["test_p"] tournament_size = config["tournament_size"] ngen = config["generations"] params = ['best_of_each_specie', 2, 'yes'] neat_cx = config["neat_cx"] neat_alg = config["neat_alg"] neat_pelit = config["neat_pelit"] neat_h = config["neat_h"] beta = config["neat_beta"] random_speciation = config["nRandom_speciation"] funcEval.LS_flag = config["ls_flag"] LS_select = config["ls_select"] funcEval.cont_evalp = 0 num_salto = config["num_salto"] # 500 cont_evalf = config["cont_evalf"] SaveMatrix = config["save_matrix"] GenMatrix = config["gen_matrix"] version = 3 testing = config["test_flag"] benchmark_flag = config["benchmark"] creator.create("FitnessMin", base.Fitness, weights=(-1.0, )) creator.create("FitnessTest", base.Fitness, weights=(-1.0, )) creator.create("Individual", neat_gp.PrimitiveTree, fitness=creator.FitnessMin, fitness_test=creator.FitnessTest) toolbox = base.Toolbox() if neat_cx: toolbox.register("expr", gp.genFull, pset=pset, min_=0, max_=3) else: toolbox.register("expr", gp.genHalfAndHalf, pset=pset, min_=0, max_=7) toolbox.register("individual", tools.initIterate, creator.Individual, toolbox.expr) toolbox.register("population", init_conf.initRepeat, list, toolbox.individual) toolbox.register("compile", gp.compile, pset=pset) name_database = database_name direccion = "./data_corridas/%s/train_%d_%d.txt" train_test(n_corr, p, problem, name_database, toolbox, config, train_p, test_p) toolbox.register("select", tools.selTournament, tournsize=tournament_size) toolbox.register("mate", neat_gp.cxSubtree) if neat_cx: toolbox.register("expr_mut", gp.genFull, min_=0, max_=3) else: toolbox.register("expr_mut", gp.genHalfAndHalf, min_=0, max_=7) toolbox.register("mutate", neat_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)) pop = toolbox.population(n=pop_size) hof = tools.HallOfFame(3) 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) pop, log, train_f, test_f = neatGPLS.neat_GP_LS(pop, toolbox, cxpb, mutpb, ngen, neat_alg, neat_cx, neat_h, neat_pelit, funcEval.LS_flag, LS_select, cont_evalf, num_salto, SaveMatrix, GenMatrix, pset, n_corr, p, params, direccion, problem, testing, version, benchmark_flag, beta, random_speciation, config, stats=mstats, halloffame=hof, verbose=True) # Regresa el mejor de las generaciones return pop, log, hof, train_f, test_f
def main(): # The cma module uses the numpy random number generator # numpy.random.seed(128) MU, LAMBDA = 10, 10 NGEN = 500 verbose = True # The MO-CMA-ES algorithm takes a full population as argument population = [ creator.Individual(x) for x in (numpy.random.uniform(0, 1, (MU, N))) ] for ind in population: ind.fitness.values = toolbox.evaluate(ind) strategy = cma.StrategyMultiObjective(population, sigma=1.0, mu=MU, lambda_=LAMBDA) toolbox.register("generate", strategy.generate, creator.Individual) toolbox.register("update", strategy.update) stats = tools.Statistics(lambda ind: ind.fitness.values) stats.register("min", numpy.min, axis=0) stats.register("max", numpy.max, axis=0) logbook = tools.Logbook() logbook.header = ["gen", "nevals"] + (stats.fields if stats else []) for gen in range(NGEN): # Generate a new population population = toolbox.generate() # Evaluate the individuals fitnesses = toolbox.map(toolbox.evaluate, population) for ind, fit in zip(population, fitnesses): ind.fitness.values = fit # Update the strategy with the evaluated individuals toolbox.update(population) record = stats.compile(population) if stats is not None else {} logbook.record(gen=gen, nevals=len(population), **record) if verbose: print(logbook.stream) if verbose: print("Final population hypervolume is %f" % hypervolume(strategy.parents, [11.0, 11.0])) # import matplotlib.pyplot as plt # valid_front = numpy.array([ind.fitness.values for ind in strategy.parents if valid(ind)]) # invalid_front = numpy.array([ind.fitness.values for ind in strategy.parents if not valid(ind)]) # fig = plt.figure() # if len(valid_front) > 0: # plt.scatter(valid_front[:,0], valid_front[:,1], c="g") # if len(invalid_front) > 0: # plt.scatter(invalid_front[:,0], invalid_front[:,1], c="r") # plt.show() return strategy.parents
def main(seed=None): random.seed(seed) NGEN = 250 MU = 100 CXPB = 0.9 stats = tools.Statistics(lambda ind: ind.fitness.values) # stats.register("avg", numpy.mean, axis=0) # stats.register("std", numpy.std, axis=0) stats.register("min", numpy.min, axis=0) stats.register("max", numpy.max, axis=0) logbook = tools.Logbook() logbook.header = "gen", "evals", "std", "min", "avg", "max" pop = toolbox.population(n=MU) # Evaluate the individuals with an invalid fitness invalid_ind = [ind for ind in pop if not ind.fitness.valid] fitnesses = toolbox.map(toolbox.evaluate, invalid_ind) for ind, fit in zip(invalid_ind, fitnesses): ind.fitness.values = fit # This is just to assign the crowding distance to the individuals # no actual selection is done pop = toolbox.select(pop, len(pop)) record = stats.compile(pop) logbook.record(gen=0, evals=len(invalid_ind), **record) print((logbook.stream)) # Begin the generational process for gen in range(1, NGEN): # Vary the population offspring = tools.selTournamentDCD(pop, len(pop)) offspring = [toolbox.clone(ind) for ind in offspring] for ind1, ind2 in zip(offspring[::2], offspring[1::2]): if random.random() <= CXPB: toolbox.mate(ind1, ind2) toolbox.mutate(ind1) toolbox.mutate(ind2) del ind1.fitness.values, ind2.fitness.values # Evaluate the individuals with an invalid fitness invalid_ind = [ind for ind in offspring if not ind.fitness.valid] fitnesses = toolbox.map(toolbox.evaluate, invalid_ind) for ind, fit in zip(invalid_ind, fitnesses): ind.fitness.values = fit # Select the next generation population pop = toolbox.select(pop + offspring, MU) record = stats.compile(pop) logbook.record(gen=gen, evals=len(invalid_ind), **record) print((logbook.stream)) print(("Final population hypervolume is %f" % hypervolume(pop, [11.0, 11.0]))) return pop, logbook
def run(): for i in range(number_of_runs): ################################################################### #EVOLUTIONARY ALGORITHM ################################################################### #TYPE #Create minimizing fitness class w/ single objective: creator.create('FitnessMin', base.Fitness, weights=(-1.0, )) #Create individual class: creator.create('Individual', list, fitness=creator.FitnessMin) #TOOLBOX toolbox = base.Toolbox() #Register function to create a number in the interval [1-100?]: #toolbox.register('init_params', ) #Register function to use initRepeat to fill individual w/ n calls to rand_num: toolbox.register('individual', tools.initRepeat, creator.Individual, np.random.random, n=number_of_params) #Register function to use initRepeat to fill population with individuals: toolbox.register('population', tools.initRepeat, list, toolbox.individual) #GENETIC OPERATORS: # Register evaluate fxn = evaluation function, individual to evaluate given later toolbox.register('evaluate', scorefxn_helper) # Register mate fxn = two points crossover function toolbox.register('mate', tools.cxTwoPoint) # Register mutate by swapping two points of the individual: toolbox.register('mutate', tools.mutPolynomialBounded, eta=0.1, low=0.0, up=1.0, indpb=0.2) # Register select = size of tournament set to 3 toolbox.register('select', tools.selTournament, tournsize=3) #EVOLUTION! pop = toolbox.population(n=number_of_individuals) hof = tools.HallOfFame(1) stats = tools.Statistics(key=lambda ind: [ind.fitness.values, ind]) stats.register('all', np.copy) # using built in eaSimple algo pop, logbook = algorithms.eaSimple(pop, toolbox, cxpb=crossover_rate, mutpb=mutation_rate, ngen=number_of_generations, stats=stats, halloffame=hof, verbose=False) # print(f'Run number completed: {i}') ################################################################### #MAKE LISTS ################################################################### # Find best scores and individuals in population arr_best_score = [] arr_best_ind = [] for a in range(len(logbook)): scores = [] for b in range(len(logbook[a]['all'])): scores.append(logbook[a]['all'][b][0][0]) #print(a, np.nanmin(scores), np.nanargmin(scores)) arr_best_score.append(np.nanmin(scores)) #logbook is of type 'deap.creator.Individual' and must be loaded later #don't want to have to load it to view data everytime, thus numpy ind_np = np.asarray(logbook[a]['all'][np.nanargmin(scores)][1]) ind_np_conv = convert_individual(ind_np, arr_conversion_matrix, number_of_params) arr_best_ind.append(ind_np_conv) #arr_best_ind.append(np.asarray(logbook[a]['all'][np.nanargmin(scores)][1])) # print('Best individual is:\n %s\nwith fitness: %s' %(arr_best_ind[-1],arr_best_score[-1])) ################################################################### #PICKLE ################################################################### arr_to_pickle = [arr_best_score, arr_best_ind] def get_filename(val): filename_base = dir_to_use + '/' + stripped_name + '_' if val < 10: toret = '000' + str(val) elif 10 <= val < 100: toret = '00' + str(val) elif 100 <= val < 1000: toret = '0' + str(val) else: toret = str(val) return filename_base + toret + '.pickled' counter = 0 filename = get_filename(counter) while os.path.isfile(filename) == True: counter += 1 filename = get_filename(counter) pickle.dump(arr_to_pickle, open(filename, 'wb'))
def run(self, file_name=None): """ Starts simple evolutionary algorithm. :param file_name: Previously saved population file. """ start_time = time.time() logs_dir = self.init_directories() stats = tools.Statistics(lambda ind: ind.fitness.values) stats.register("avg", np.mean) stats.register("min", np.min) stats.register("max", np.max) toolbox = self.deap_toolbox_init() population = toolbox.population( pop_size=self.evolution_params.pop_size, file_name=file_name) logbook = tools.Logbook() logbook.header = ['gen', 'nevals'] + (stats.fields if stats else []) if (self.evolution_params.hof_size > 0): halloffame = tools.HallOfFame(self.evolution_params.hof_size) else: halloffame = None # invalid_ind = [ind for ind in population if not ind.fitness.valid] invalid_ind = population seeds = [np.random.randint(0, 2**16) for _ in range(len(invalid_ind))] fitnesses = toolbox.map(toolbox.evaluate, invalid_ind, seeds) for ind, fit in zip(invalid_ind, fitnesses): ind.fitness.values = fit if halloffame is not None: halloffame.update(population) record = stats.compile(population) if stats else {} logbook.record(gen=0, nevals=str(len(invalid_ind)), **record) print(logbook.stream) population.sort(key=lambda ind: ind.fitness.values, reverse=True) # Begin the generational process for gen in range(1, self.evolution_params.ngen + 1): # Select the next generation individuals offspring = toolbox.select( population, len(population) - self.evolution_params.elite) offspring = [toolbox.clone(ind) for ind in offspring] # Apply crossover and mutation on the offspring for i in range(1, len(offspring), 2): if np.random.random() < self.evolution_params.cxpb: offspring[i - 1], offspring[i] = toolbox.mate( offspring[i - 1], offspring[i]) del offspring[ i - 1].fitness.values, offspring[i].fitness.values for i in range(len(offspring)): if np.random.random() < self.evolution_params.mut[1]: offspring[i], = toolbox.mutate(offspring[i]) del offspring[i].fitness.values # Add elite individuals (they lived through mutation and x-over) for i in range(self.evolution_params.elite): offspring.append(toolbox.clone(population[i])) # invalid_ind = [ind for ind in offspring if not ind.fitness.valid] invalid_ind = offspring seeds = [ np.random.randint(0, 2**16) for _ in range(len(invalid_ind)) ] fitnesses = toolbox.map(toolbox.evaluate, invalid_ind, seeds) for ind, fit in zip(invalid_ind, fitnesses): ind.fitness.values = fit # Update the hall of fame with the generated individuals if halloffame is not None: halloffame.update(offspring) # Replace the current population by the offspring population[:] = offspring population.sort(key=lambda ind: ind.fitness.values, reverse=True) # Append the current generation statistics to the logbook record = stats.compile(population) if stats else {} logbook.record(gen=gen, nevals=str(len(invalid_ind)), **record) print(logbook.stream) if (gen % self.logs_every == 0): self.log_all(logs_dir, population, halloffame, logbook, start_time) self.log_all(logs_dir, population, halloffame, logbook, start_time)
def GeneticMake(self, train, test, features, params, iteration, feature_limit, gen_num=10): ''' iteration: 反復回数, 特徴量作成の試行回数 feature_limit: 許容する特徴量数の限界値 gen_num: 進化する世代数 ''' # 分母が0の場合を考慮した, 除算関数 def protectedDiv(left, right): eps = 1.0e-7 tmp = np.zeros(len(left)) tmp[np.abs(right) >= eps] = left[np.abs(right) >= eps] / right[np.abs(right) >= eps] tmp[np.abs(right) < eps] = 1.0 return tmp # 初期値 base_score = self.Model(train, features, params) print("validation mean score:", base_score['score'].mean()) # 適合度を最大化するような木構造を個体として定義 creator.create("FitnessMax", base.Fitness, weights=(1.0, )) creator.create("Individual", gp.PrimitiveTree, fitness=creator.FitnessMax) # setting prev_score = np.mean(base_score['score']) # base score exprs = [] # 生成した特徴量 results = pd.DataFrame( columns=['n_features', 'best_score', 'val_score']) # 結果を格納する. (best_score == val_score ??) n_features = len(features) # 初期時点の特徴量数 X_train = train[features] # 訓練データの特徴量 X_test = test[features] # テストデータの特徴量 y_train = train['y'] # 訓練データのターゲット変数 # main for i in tqdm(range(iteration)): pset = gp.PrimitiveSet("MAIN", n_features) pset.addPrimitive(operator.add, 2) pset.addPrimitive(operator.sub, 2) pset.addPrimitive(operator.mul, 2) pset.addPrimitive(protectedDiv, 2) pset.addPrimitive(operator.neg, 1) pset.addPrimitive(np.cos, 1) pset.addPrimitive(np.sin, 1) pset.addPrimitive(np.tan, 1) # function def eval_genfeat(individual): func = toolbox.compile(expr=individual) # make new features features_train = [ np.array(X_train)[:, j] for j in range(n_features) ] new_feat_train = func(*features_train) # combine table and select features name train_tmp = pd.concat([ X_train, pd.DataFrame(new_feat_train, columns=['tmp']), y_train ], axis=1) features_tmp = train_tmp.drop("y", axis=1).columns.values tmp_score = self.Model(train_tmp, features_tmp, params) # print(np.mean(tmp_score['score'])) return np.mean(tmp_score['score']), # 関数のデフォルト値の設定 toolbox = base.Toolbox() toolbox.register("expr", gp.genHalfAndHalf, pset=pset, min_=1, max_=3) toolbox.register("individual", tools.initIterate, creator.Individual, toolbox.expr) toolbox.register("population", tools.initRepeat, list, toolbox.individual) toolbox.register("compile", gp.compile, pset=pset) # 評価、選択、交叉、突然変異の設定 # 選択はサイズ10のトーナメント方式、交叉は1点交叉、突然変異は深さ2のランダム構文木生成と定義 toolbox.register("evaluate", eval_genfeat) toolbox.register("select", tools.selTournament, tournsize=10) 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) # 構文木の制約の設定 # 交叉や突然変異で深さ5以上の木ができないようにする toolbox.decorate( "mate", gp.staticLimit(key=operator.attrgetter("height"), max_value=5)) toolbox.decorate( "mutate", gp.staticLimit(key=operator.attrgetter("height"), max_value=5)) # 世代ごとの個体とベスト解を保持するクラスの生成 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) # 進化の実行 # 交叉確率50%、突然変異確率10%、?世代まで進化 start_time = time.time() pop, log = algorithms.eaSimple(pop, toolbox, 0.5, 0.1, gen_num, stats=mstats, halloffame=hof, verbose=True) end_time = time.time() # ベスト解とscoreの保持 best_expr = hof[0] best_score = mstats.compile(pop)["fitness"]["max"] # 生成変数を学習、テストデータに追加し、ベストスコアを更新する if prev_score < best_score: # 生成変数の追加 func = toolbox.compile(expr=best_expr) features_train = [ np.array(X_train)[:, j] for j in range(n_features) ] features_test = [ np.array(X_test)[:, j] for j in range(n_features) ] new_feat_train = func(*features_train) new_feat_test = func(*features_test) # データ更新 X_train = pd.concat([ X_train, pd.DataFrame(new_feat_train, columns=['NEW' + str(i)]) ], axis=1) X_test = pd.concat([ X_test, pd.DataFrame(new_feat_test, columns=['NEW' + str(i)]) ], axis=1) new_features = X_train.columns.values # テストスコアの計算(プロット用) val_score = self.Model(pd.concat([X_train, y_train], axis=1), new_features, params) # test_pred = DecisionTree.prediction(train,test,features,params) # ベストスコアの更新と特徴量数の加算 prev_score = best_score n_features += 1 # 表示と出力用データの保持 print("n_features: %i, best_score: %f, time: %f second" % (n_features, best_score, end_time - start_time)) # 結果の格納 ( スコアの記録と作成した特徴量) tmp = pd.Series( [n_features, best_score, np.mean(val_score['score'])], index=results.columns) results = results.append(tmp, ignore_index=True) exprs.append(best_expr) # save with file name Process.write_feather(pd.concat([X_train, y_train], axis=1), file_name='train_gen') Process.write_feather(X_test, file_name='test_gen') # 変数追加後の特徴量数が??を超えた場合break if n_features >= feature_limit: break return pd.concat([X_train, y_train], axis=1), X_test, results, exprs
def main(extended=True, verbose=True): target_set = [] stats = tools.Statistics(lambda ind: ind.fitness.values) stats.register("avg", numpy.mean) stats.register("std", numpy.std) stats.register("min", numpy.min) stats.register("max", numpy.max) logbook = tools.Logbook() logbook.header = "gen", "species", "evals", "std", "min", "avg", "max" ngen = 300 adapt_length = 100 g = 0 add_next = [adapt_length] for i in range(len(schematas)): target_set.extend( toolbox.target_set(schematas[i], int(TARGET_SIZE / len(schematas)))) species = [toolbox.species() for _ in range(NUM_SPECIES)] # Init with random a representative for each species representatives = [random.choice(s) for s in species] if plt and extended: # We must save the match strength to plot them t1, t2, t3 = list(), list(), list() while g < ngen: # Initialize a container for the next generation representatives next_repr = [None] * len(species) for i, s in enumerate(species): # Vary the species individuals s = algorithms.varAnd(s, toolbox, 0.6, 1.0) r = representatives[:i] + representatives[i + 1:] for ind in s: ind.fitness.values = toolbox.evaluate([ind] + r, target_set) record = stats.compile(s) logbook.record(gen=g, species=i, evals=len(s), **record) if verbose: print(logbook.stream) # Select the individuals species[i] = toolbox.select(s, len(s)) # Tournament selection next_repr[i] = toolbox.get_best(s)[0] # Best selection g += 1 if plt and extended: # Compute the match strength without noise for the # representatives on the three schematas t1.append( toolbox.evaluate_nonoise( representatives, toolbox.target_set(schematas[0], 1), noise)[0]) t2.append( toolbox.evaluate_nonoise( representatives, toolbox.target_set(schematas[1], 1), noise)[0]) t3.append( toolbox.evaluate_nonoise( representatives, toolbox.target_set(schematas[2], 1), noise)[0]) representatives = next_repr # Add a species at every *adapt_length* generation if add_next[-1] <= g < ngen: species.append(toolbox.species()) representatives.append(random.choice(species[-1])) add_next.append(add_next[-1] + adapt_length) if extended: for r in representatives: # print individuals without noise print("".join(str(x) for x, y in zip(r, noise) if y == "*")) if plt and extended: # Do the final plotting plt.plot(t1, '-', color="k", label="Target 1") plt.plot(t2, '--', color="k", label="Target 2") plt.plot(t3, ':', color="k", label="Target 3") max_t = max(max(t1), max(t2), max(t3)) for n in add_next: plt.plot([n, n], [0, max_t + 1], "--", color="k") plt.legend(loc="lower right") plt.axis([0, ngen, 0, max_t + 1]) plt.xlabel("Generations") plt.ylabel("Number of matched bits") plt.show()
def optimize_diameters(path, inpfile, **kwargs): """ Optimize diameters Optimize pipe diameters of a hydraulic network using Genetic Algorithms. :param str path: path to the input file. :param str inpfile: EPANET's input file (INP) with network data. :param int pop: population size or number of individuals. :param int gen: number of generations. :param float cxbp: crossover (mating) probability. :param float mutpb: mutation probability. :param float indpb: individual mutation probability? """ _unit_price = kwargs.get('prices', {}) _popsize = kwargs.get('pop', 200) _cxpb = kwargs.get('cxpb', 0.9) _mutpb = kwargs.get('mutpb', 0.02) _indpb = kwargs.get('indpb', 0.10) _generations = kwargs.get('gen', 500) # Create the appropiate types for diameter optimization creator.create("FitnessMin", base.Fitness, weights=(-1.0, )) # Minimize creator.create("Individual", list, fitness=creator.FitnessMin) # Create the Network object needed for EPANET simulation and analysis network = Network(path, inpfile) network.open_network() network.initialize() # Create the individuals and population # dimension = network.links # individual size = network.links toolbox = base.Toolbox() toolbox.register("attr_diameter", random.randint, 0, len(_unit_price) - 1) toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_diameter, network.links) toolbox.register("population", tools.initRepeat, list, toolbox.individual) # Genetic operators and evaluation function toolbox.register("evaluate", lambda x: network_diameters(x, network, _unit_price)) toolbox.register("mate", tools.cxTwoPoint) toolbox.register("mutate", tools.mutShuffleIndexes, indpb=_indpb) toolbox.register("select", tools.selRoulette) # Create the population pop = toolbox.population(n=_popsize) hof = tools.HallOfFame(1) # To remember best solution 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) # Use simplest evolutionary algorithm as in chapter 7 of Back (2000) pop, log = algorithms.eaSimple(pop, toolbox, cxpb=_cxpb, mutpb=_mutpb, ngen=_generations, stats=stats, halloffame=hof, verbose=True) return hof, pop, log
def nsgaII(NObj, objective, pbounds, seed=None, NGEN=20000, MU=400, CXPB=0.9): random.seed(seed) global FirstCall if FirstCall: creator.create('FitnessMin', base.Fitness, weights=(-1.0, ) * NObj) creator.create('Individual', array.array, typecode='d', fitness=creator.FitnessMin) FirstCall = False toolbox = base.Toolbox() NDIM = len(pbounds) toolbox.register('attr_float', uniform, pbounds) toolbox.register('individual', tools.initIterate, creator.Individual, toolbox.attr_float) toolbox.register('population', tools.initRepeat, list, toolbox.individual) toolbox.register('evaluate', objective) toolbox.register('mate', tools.cxSimulatedBinaryBounded, low=pbounds[:, 0].tolist(), up=pbounds[:, 1].tolist(), eta=20.0) toolbox.register('mutate', tools.mutPolynomialBounded, low=pbounds[:, 0].tolist(), up=pbounds[:, 1].tolist(), eta=20.0, indpb=1.0 / NDIM) toolbox.register('select', tools.selNSGA2) stats = tools.Statistics(lambda ind: ind.fitness.values) stats.register('min', np.min, axis=0) stats.register('max', np.max, axis=0) logbook = tools.Logbook() logbook.header = 'gen', 'evals', 'std', 'min', 'avg', 'max' pop = toolbox.population(n=MU) # Evaluate the individuals with an invalid fitness invalid_ind = [ind for ind in pop if not ind.fitness.valid] fitnesses = toolbox.map(toolbox.evaluate, invalid_ind) for ind, fit in zip(invalid_ind, fitnesses): ind.fitness.values = fit pop = toolbox.select(pop, len(pop)) record = stats.compile(pop) logbook.record(gen=0, evals=len(invalid_ind), **record) for gen in range(1, NGEN): # Vary the population offspring = tools.selTournamentDCD(pop, len(pop)) offspring = [toolbox.clone(ind) for ind in offspring] for ind1, ind2 in zip(offspring[::2], offspring[1::2]): if random.random() <= CXPB: toolbox.mate(ind1, ind2) toolbox.mutate(ind1) toolbox.mutate(ind2) del ind1.fitness.values, ind2.fitness.values # Evaluate the individuals with an invalid fitness invalid_ind = [ind for ind in offspring if not ind.fitness.valid] fitnesses = toolbox.map(toolbox.evaluate, invalid_ind) for ind, fit in zip(invalid_ind, fitnesses): ind.fitness.values = fit # Select the next generation population pop = toolbox.select(pop + offspring, MU) record = stats.compile(pop) logbook.record(gen=gen, evals=len(invalid_ind), **record) # print(logbook.stream) front = np.array([ind.fitness.values for ind in pop]) return pop, logbook, front