def selTournamentWithSharing(individuals, k, tournsize, fit_attr="fitness"): # get orig fitnesses: origFitnesses = [ind.fitness.values[0] for ind in individuals] # apply sharing to each individual: for i in range(len(individuals)): sharingSum = 1 # iterate over all other individuals for j in range(len(individuals)): if i != j: # calculate eucledean distance between individuals: distance = math.sqrt( ((individuals[i][0] - individuals[j][0]) ** 2) + ((individuals[i][1] - individuals[j][1]) ** 2)) if distance < DISTANCE_THRESHOLD: sharingSum += (1 - distance / (SHARING_EXTENT * DISTANCE_THRESHOLD)) # reduce fitness accordingly: individuals[i].fitness.values = origFitnesses[i] / sharingSum, # apply original tools.selTournament() using modified fitness: selected = tools.selTournament(individuals, k, tournsize, fit_attr) # retrieve original fitness: for i, ind in enumerate(individuals): ind.fitness.values = origFitnesses[i], return selected
def main(): pop = toolbox.population(n=POP_SIZE) # Evaluate the entire population fitnesses = list(map(toolbox.evaluate, pop)) for ind, fit in zip(pop, fitnesses): ind.fitness.values = fit for g in range(NGEN): # Select the next generation individuals offspring = toolbox.select(pop, len(pop)) # Clone the selected individuals offspring = list(map(toolbox.clone, offspring)) #print(offspring) # Apply crossover and mutation on the offspring bestIndiv = toolbox.clone( tools.selTournament(offspring, 1, len(offspring))) print("Step:{0} | BestIndiv:{1} | Relative Error:{2} \n".format( [g], bestIndiv[0], list(bestIndiv[0].fitness.values))) with open(speciesAdd + "bestresultforM.csv", "a+") as csvFile: csvWriter = csv.writer(csvFile) csvWriter.writerow([g] + bestIndiv[0] + list(bestIndiv[0].fitness.values)) for child1, child2 in zip(offspring[::2], offspring[1::2]): #print(child1,child2) if random.random() < CXPB: toolbox.mate(child1, child2) del child1.fitness.values del child2.fitness.values for mutant in offspring: if random.random() < MUTPB: toolbox.mutate(mutant) del mutant.fitness.values # Evaluate the individuals with an invalid fitness invalid_ind = [ind for ind in offspring if not ind.fitness.valid] fitnesses = list(map(toolbox.evaluate, invalid_ind)) for ind, fit in zip(invalid_ind, fitnesses): ind.fitness.values = fit # The population is entirely replaced by the offspring pop[:] = offspring print(pop) fitnessesPOP = [] for inds in pop: fitnessesPOP.append(inds.fitness.values) print(fitnessesPOP) input_stream = ( """step{0} pop: {1} \n step{0} fitnessesPOP {2} \n """.format( g, pop, fitnessesPOP)) with open("logFile.txt", 'a+') as stream: stream.write(input_stream) return pop
def register_evo_functions( toolbox, evalfunc=default_evaluation, # This method __MUST__ be the evaluation function that takes a guess as a list and returns the number of broken constraints matefunc=tools. cxTwoPoint, # This method will be used to get the offspring of two guesses mutatefunc=swap_cells_once, # This method will be applied to mutate guesses selectfunc=lambda individuals, k: tools.selTournament( individuals, k, tournsize=4 ) # This method will be used to select the best guesses in a population ): toolbox.register("mate", lambda sudo1, sudo2: [ toolbox.sudoku(grid) for grid in matefunc(sudo1.export_grid(), sudo2.export_grid()) ]) # INPUT : LIST OF INTS, OUTPUT: LIST OF INTS toolbox.register( "mutate", lambda sudo1: toolbox.sudoku(mutatefunc(sudo1.export_grid( )))) # INPUT : LIST OF INTS, OUTPUT: LIST OF INTS toolbox.register("select", selectfunc) toolbox.register("evaluate", evalfunc) # Functions you can now call: # toolbox.mate(a, b) takes two list objects a and b, and returns a list of their crossover children return toolbox
def default_config(wf, rm, estimator): selector = lambda env, pop: tools.selTournament(pop, len(pop), 4) return { "interact_individuals_count": 22, "generations": 5, "env": Env(wf, rm, estimator), "species": [Specie(name=MAPPING_SPECIE, pop_size=10, cxb=0.8, mb=0.5, mate=lambda env, child1, child2: tools.cxOnePoint(child1, child2), mutate=mapping_default_mutate, select=selector, initialize=mapping_default_initialize ), Specie(name=ORDERING_SPECIE, pop_size=10, cxb=0.8, mb=0.5, mate=ordering_default_crossover, mutate=ordering_default_mutate, select=selector, initialize=ordering_default_initialize, ) ], "operators": { # "choose": default_choose, "build_solutions": default_build_solutions, "fitness": fitness_mapping_and_ordering, "assign_credits": default_assign_credits } }
def get_subset(self): if self.population: # random selection #sample = tools.selRandom(self.population, self.subset_size) # best selection #sample = tools.selBest(self.population, self.subset_size) # worst selection #sample = tools.selWorst(self.population, self.subset_size) # bestish selection sample = tools.selTournament(self.population, self.subset_size, 3) if self.best: sample[0] = self.best # if not unique, sample one more time sample_strings = [ind.__str__() for ind in sample] if len(set(sample_strings)) < len(sample_strings): for i, ind_i in enumerate(sample): for j, ind_j in enumerate(sample[i + 1:]): if ind_i == ind_j: sample[i + 1 + j] = tools.selRandom( self.population, 1)[0] print 'sample', sample self.subset = sample sample = self.pre_process(sample) else: sample = self.get_default() return sample
def sel_elitist_tournament(individuals, mu, k_elitist, k_tournament, tournsize, fit_attr="fitness"): return tools.selBest(individuals, int(k_elitist * mu), fit_attr="fitness") + \ tools.selTournament(individuals, int(k_tournament * mu), tournsize=tournsize, fit_attr="fitness")
def evolve(self, population, cxpb, mutpb, mutfq, ngen, goal): # Cheapest classifier. clf = LinearRegression(normalize=True) # Evaluate fitnesses of starting population. fitness_list = map(lambda x: self.evaluate(x, clf), population) # Assign fitness values. for individual, fitness in zip(population, fitness_list): individual.fitness.values = fitness best = max(population, key=lambda x: x.fitness.values[0]) # So that we know things are happening. bar = Bar('Evolving', max=ngen) # Evolution! for gen in xrange(ngen): if best.fitness.values[0] > goal: break # Select the next generation of individuals. offspring = [] offspring.append(best) offspring += tools.selTournament(population, len(population)-1, 10) offspring = map(self.toolbox.clone, offspring) # Apply crossovers. for child_a, child_b in zip(offspring[::2], offspring[1::2]): # Staggered. if random.random() < cxpb: self.crossover(child_a, child_b, cxpb) del child_a.fitness.values del child_b.fitness.values # Apply mutations. for child in offspring: if random.random() < mutpb: self.mutate(child, mutfq) del child.fitness.values # Reevaluate fitness of changed individuals. new_children = [e for e in offspring if not e.fitness.valid] fitness_list = map(lambda x: self.evaluate(x, clf), population) for individual, fitness in zip(new_children, fitness_list): individual.fitness.values = fitness # Replace old population with new generation. best = max(population, key=lambda x: x.fitness.values[0]) population = offspring # Progress! bar.next() # Done! Return the most fit evolved individual. bar.finish() return best
def co_weightsIH(self, pop = None): """ Cross over the weights between the input and hidden layer """ if pop == None: pop = self.pop['connWeights_IH_pop']; parents = tools.selTournament(pop,2,)
def selection(individuals, k, tournsize, fit_attr="fitness",icls=None,numberOfFilters=20): best = individuals[0] for ind in individuals: if best.fitness.values[0] < ind.fitness.values[0]: best = ind newGen = tools.selTournament(individuals, k - 1, tournsize, fit_attr=fit_attr) if best in newGen: newGen.append(generate(icls,numberOfFilters)) else: newGen.append(best) return newGen
def selTournament(problem, individuals): # Format a population data structure usable by DEAP's package dIndividuals = deap_format(problem, individuals) # Select elites selectees = tools.selTournament(dIndividuals, len(individuals), 4) # Update beginning population data structure selectedIndices = [i for i,sel in enumerate(selectees)] return [individuals[s] for s in selectedIndices], len(individuals)
def selTournament(problem, individuals): # Format a population data structure usable by DEAP's package dIndividuals = deap_format(problem, individuals) # Select elites selectees = tools.selTournament(dIndividuals, len(individuals), 4) # Update beginning population data structure selectedIndices = [i for i, sel in enumerate(selectees)] return [individuals[s] for s in selectedIndices], len(individuals)
def genetic(evaluate_func, limits, generations=300, popsize=20, elitepercent=.1, crossoveredpercent=.4, sbbx_eta=1.0, log=False, callback=None): assert elitepercent + crossoveredpercent <= 1.0 elitesize = int(popsize * elitepercent) crossoveredsize = int(popsize * crossoveredpercent) crossoveredsize = crossoveredsize - 1 if crossoveredsize & 1 else crossoveredsize # make it even mutatedsize = popsize - elitesize - crossoveredsize assert mutatedsize >= 0 toolbox.register("individual", tools.initIterate, creator.Individual, get_ind_function(limits)) #toolbox.register("population", tools.initRepeat, list, toolbox.individual) toolbox.register("evaluate", evaluate_func) toolbox.register("mutate", get_mutate_func(limits)) toolbox.register("mate", lambda a, b: tools.cxSimulatedBinaryBounded(a, b, sbbx_eta, low=list(map(lambda t: t[0], limits)), up=list(map(lambda t: t[1], limits)))) #toolbox.register("select", tools.selTournament, tournsize=3) #toolbox.register("select", tools.selBest) pop = [toolbox.individual() for _ in range(popsize)] #logbook = tools.Logbook() for gen in range(generations): update_fitnesses(pop) #record = stats.compile(pop) #logbook.record(gen=gen, **record) best=max(pop, key=lambda x: x.fitness.values[0]) if log: print("Gen {}: best: {} !!! {}".format(gen, best, best.fitness.values[0]))#record['max'])) if callback is not None: #if callback([(t, t.fitness.values[0]) for t in pop]) == True: if callback(best.fitness.values[0]) == True: break elite = tools.selBest(pop, k=elitesize) crossovered = list(map(toolbox.clone, tools.selTournament(pop, k=crossoveredsize, tournsize=2))) for child1, child2 in zip(crossovered[::2], crossovered[1::2]): toolbox.mate(child1, child2) del child1.fitness.values del child2.fitness.values mutated = list(map(toolbox.clone, random.sample(pop, mutatedsize))) for mutant in mutated: toolbox.mutate(mutant) del mutant.fitness.values pop[:] = elite + crossovered + mutated return best, best.fitness.values[0]#logbook
def train(self, pop = 20, gen = 10): from deap import algorithms from deap import base from deap import creator from deap import tools from deap.tools import Statistics # import random from scipy.stats import rv_discrete # creator.create("FitnessMulti", base.Fitness, weights=(1.0, -1.0)) # creator.create("Individual", list, fitness=creator.FitnessMulti) creator.create("FitnessMin", base.Fitness, weights=(-1.0,)) creator.create("Individual", list, fitness=creator.FitnessMin) toolbox = base.Toolbox() # Attribute generator custm = rv_discrete(name='custm', values=(self.a_w.index, self.a_w.values)) toolbox.register("attr_int", custm.rvs) # Structure initializers toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_int, n=len(self.s)) toolbox.register("population", tools.initRepeat, list, toolbox.individual, n=pop) # Operator registering toolbox.register("evaluate", self.eval_classifer) toolbox.register("mate", tools.cxUniform, indpb=0.5) toolbox.register("mutate", tools.mutUniformInt, low=min(self.a.index), up=max(self.a.index), indpb=0.1) toolbox.register("select", tools.selNSGA2) MU, LAMBDA = pop, pop population = toolbox.population(n=MU) hof = tools.ParetoFront() s = Statistics(key=lambda ind: ind.fitness.values) s.register("mean", np.mean) s.register("min", min) # pop, logbook = algorithms.eaMuPlusLambda(pop, toolbox, mu=MU, lambda_=LAMBDA, cxpb=0.7, mutpb=0.3, ngen=gen, stats=s, halloffame=hof) for i in range(gen): offspring = algorithms.varAnd(population, toolbox, cxpb=0.95, mutpb=0.1) fits = toolbox.map(toolbox.evaluate, offspring) for fit, ind in zip(fits, offspring): ind.fitness.values = fit population = tools.selBest(offspring, int(0.05*len(offspring))) + tools.selTournament(offspring, len(offspring)-int(0.05*len(offspring)), tournsize=3) # population = toolbox.select(offspring, k=len(population)) print s.compile(population) top10 = tools.selBest(population, k=10) return top10
def optimize(self, pop): mstats, log = support.statistics(), support.logbook( ) # DEAP statistics and logbook front = tools.ParetoFront() # Initialize ParetoFront class evals = len(pop) archive, gds = [], [ ] # Initialize offspring and generational distance list gen = totalEvals = gd = 0 while True: # Step 3: Environmental selection archive = self.tb.select(pop + archive, k=self.sizeArchive) prevFront = deepcopy(front) front.update(archive) # Record statistics record = mstats.compile(front) log.record(gen=gen, evals=evals, gd=gd, **record) print('\r', end='') print(log.stream) totalEvals += evals # Step 4: Termination gd = self.termination(prevFront, front, gen, gds, totalEvals) if not isinstance(gd, float): return front, log, totalEvals # Step 5: Mating Selection matingPool = tools.selTournament(archive, k=self.sizePop, tournsize=2) # Step 6: Variation pop = algorithms.varAnd(matingPool, self.tb, cxpb=1, mutpb=self.mutpb) # Step 2: Fitness assignment of both pop and archive archive_ex_pop = [ind for ind in archive if ind not in pop] invInds = [ ind for ind in pop + archive_ex_pop if not ind.fitness.valid ] fits = self.tb.map(self.evaluator.evaluate, invInds) for ind, fit in zip(invInds, fits): ind.fitness.values = fit evals = len(invInds) gen += 1
def select(population, individual_to_compute_novelty, archive): # select the individuals for evaluation # select the most similar to choreography new_population = create_individuals(population, individual_to_compute_novelty, False) pop_selected = tools.selTournament(new_population, k=4, tournsize=5) # select the most similar individuals to choreography in archive U selected before arch = create_individuals(archive, individual_to_compute_novelty, True) # the whole population is composed by selected individuals and archive pop_resulting = pop_selected + arch ind_selected = tools.selBest(pop_resulting, 4) return ind_selected
def train(self, pop = 20, gen = 10): from deap import algorithms from deap import base from deap import creator from deap import tools import random import numpy as np from deap.tools import Statistics # creator.create("FitnessMulti", base.Fitness, weights=(1.0, -1.0)) # creator.create("Individual", list, fitness=creator.FitnessMulti) creator.create("FitnessMax", base.Fitness, weights=(1.0,)) creator.create("Individual", list, fitness=creator.FitnessMax) toolbox = base.Toolbox() # Attribute generator toolbox.register("attr_bool", random.randint, 0, 1) # Structure initializers toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_bool, n=len(self.X.columns)) toolbox.register("population", tools.initRepeat, list, toolbox.individual, n=pop) # Operator registering toolbox.register("evaluate", self.eval_classifer) toolbox.register("mate", tools.cxUniform, indpb=0.1) toolbox.register("mutate", tools.mutFlipBit, indpb=0.05) # toolbox.register("select", tools.selNSGA2) MU, LAMBDA = pop, pop population = toolbox.population(n=MU) # hof = tools.ParetoFront() s = Statistics(key=lambda ind: ind.fitness.values) s.register("mean", np.mean) s.register("max", max) # pop, logbook = algorithms.eaMuPlusLambda(pop, toolbox, mu=MU, lambda_=LAMBDA, cxpb=0.7, mutpb=0.3, ngen=gen, stats=s, halloffame=hof) for i in range(gen): offspring = algorithms.varAnd(population, toolbox, cxpb=0.95, mutpb=0.1) fits = toolbox.map(toolbox.evaluate, offspring) for fit, ind in zip(fits, offspring): ind.fitness.values = fit population = tools.selBest(offspring, int(0.05*len(offspring))) + tools.selTournament(offspring, len(offspring)-int(0.05*len(offspring)), tournsize=3) # population = toolbox.select(offspring, k=len(population)) print s.compile(population) top10 = tools.selBest(population, k=10) print top10 return top10[0]
def populationPhenotypicDivergence(constructPhenotype, population): model = tools.selTournament(population, 1, len(population) // 3)[0] PDDmedian = 0 for IND in population: if IND == model: IND.PDDscore = 1000 continue IND.PDDscore = individualPhenotypicDivergence(constructPhenotype, model, IND) PDDmedian += IND.PDDscore PDDmedian = PDDmedian / (len(population) - 1) for I in range(len(population)): if population[I].PDDscore < PDDmedian / 3: if random.random() < 0.3: population[I] = None population = [x for x in population if x] return population
def default_recombination( population, parents, children ): # Takes the original population, the selected parental candidates, and their offspring, and returns a new population length = len(population) # Take the original length of the population numChildren = len(children) # And the length of the child population if ( length > numChildren ): # Ensure population stability by selecting individuals from the original population until there are the same number of individuals in the new population best = tools.selTournament(population, length - numChildren, tournsize=4) return best + children return children
def main(): spec = Spec(bits=100, pc=0.5, pm=0.2, weights=(1.0,), m=300) spec.evaluate = lambda ind: (sum(ind),) spec.judgeExitCondition = judgeExitCondition spec.selectReproduction = lambda pop: tools.selTournament( pop, len(pop), tournsize=3) spec.mate = tools.cxTwoPoint spec.mutate = lambda ind: tools.mutFlipBit(ind, indpb=0.05) spec.selectSurvival = lambda parents, children: children director = Director(spec) population, i = director.evolveLoop() best = selectBest(population) print("evolving times: ", i) print("best individual: %s %s" % (best, best.fitness.values[0])) return
def __init__(self, runner): self.runner = runner self.toolbox = base.Toolbox() creator.create("FitnessMax", base.Fitness, weights=(1.0,)) creator.create("Individual", list, fitness=creator.FitnessMax) self.populationSize = 50 self.numberGenerations = 100 self.crossoverProb = 0.5 self.mutateProb = 0.1 self.mate = tools.cxTwoPoint self.mutate = lambda individual: tools.mutUniformInt(individual, low=self.minInput, up=self.maxInput, indpb=0.05) self.select = lambda population, selectCount: tools.selTournament(population, selectCount, tournsize=3) self.toolbox.register("individual", self.individual) self.toolbox.register("population", self.population) self.toolbox.register("evaluate", self.evaluate) self.toolbox.register("mate", self.mate) self.toolbox.register("mutate", self.mutate) self.toolbox.register("select", self.select)
def get_subset(self): if self.population: # random selection #sample = tools.selRandom(self.population, self.subset_size) # best selection #sample = tools.selBest(self.population, self.subset_size) # worst selection #sample = tools.selWorst(self.population, self.subset_size) # bestish selection sample = tools.selTournament(self.population, self.subset_size, 3) self.subset = sample sample = self.pre_process(sample) else: sample = self.get_default() return sample
def init_algorithm(self, params): toolbox = base.Toolbox() ind_size = self.problem.ind_size ngen = params['generations'] nind = params['num_individuals'] cxpb, mutpb, lb, ub, mu, lam = 0.7, 0.2, -1.0, 1.0, nind, nind if hasattr(creator, 'FitnessMin') is False: creator.create('FitnessMin', base.Fitness, weights=(-1.0, )) if hasattr(creator, 'Individual') is False: kw0 = {'typecode': 'd', 'fitness': creator.FitnessMin} creator.create('Individual', array.array, **kw0) atr = lambda: [random.uniform(lb, ub) for _ in range(ind_size)] ind = lambda: tools.initIterate(creator.Individual, atr) population = [ind() for _ in range(nind)] kw1 = {'low': lb, 'up': ub, 'eta': 20.0, 'indpb': 1.0 / ind_size} mut = lambda xs: tools.mutPolynomialBounded(xs, **kw1) kw2 = {'low': lb, 'up': ub, 'eta': 20.0} crs = lambda i1, i2: tools.cxSimulatedBinaryBounded(i1, i2, **kw2) sel = lambda p, n: tools.selTournament(p, n, tournsize=3) toolbox.register('evaluate', self.problem.objective_function) toolbox.register('mate', crs) toolbox.register('mutate', mut) toolbox.register('select', sel) stats = tools.Statistics(lambda ind: ind.fitness.values) stats.register('min', np.min) self.hof = tools.HallOfFame(5) args = (population, toolbox, mu, lam, cxpb, mutpb, ngen) kw3 = {'stats': stats, 'halloffame': self.hof, 'verbose': True} self.algorithm = lambda: algorithms.eaMuPlusLambda(*args, **kw3)
def default_config(wf, rm, estimator): selector = lambda env, pop: tools.selTournament(pop, len(pop), 4) return { "interact_individuals_count": 22, "generations": 5, "env": Env(wf, rm, estimator), "species": [ Specie(name=MAPPING_SPECIE, pop_size=10, cxb=0.8, mb=0.5, mate=lambda env, child1, child2: tools.cxOnePoint( child1, child2), mutate=mapping_default_mutate, select=selector, initialize=mapping_default_initialize), Specie( name=ORDERING_SPECIE, pop_size=10, cxb=0.8, mb=0.5, mate=ordering_default_crossover, mutate=ordering_default_mutate, select=selector, initialize=ordering_default_initialize, ) ], "operators": { # "choose": default_choose, "build_solutions": default_build_solutions, "fitness": fitness_mapping_and_ordering, "assign_credits": default_assign_credits } }
def __init__(self, runner): self.runner = runner self.toolbox = base.Toolbox() creator.create("FitnessMax", base.Fitness, weights=(1.0, )) creator.create("Individual", list, fitness=creator.FitnessMax) self.populationSize = 50 self.numberGenerations = 100 self.crossoverProb = 0.5 self.mutateProb = 0.1 self.mate = tools.cxTwoPoint self.mutate = lambda individual: tools.mutUniformInt( individual, low=self.minInput, up=self.maxInput, indpb=0.05) self.select = lambda population, selectCount: tools.selTournament( population, selectCount, tournsize=3) self.toolbox.register("individual", self.individual) self.toolbox.register("population", self.population) self.toolbox.register("evaluate", self.evaluate) self.toolbox.register("mate", self.mate) self.toolbox.register("mutate", self.mutate) self.toolbox.register("select", self.select)
def optim(self): """ """ ##Create population of species to work with self.createPopulations(); #select representatives repr = dict(); repr['hidden_nodes'] = tools.selRandom(self.pop['node_pop'], self.params['numHiddenNodes']); repr['out_nodes'] = tools.selRandom(self.pop['node_pop'], self.params['numOutputNodes']); #select random connections and weights repr['model'] = tools.selRandom(self.pop['model_pop'], 1); repr['connActive_IH'] = tools.selRandom(self.pop['connActive_IH_pop'], 1); repr['connActive_HH'] = tools.selRandom(self.pop['connActive_HH_pop'], 1); repr['connActive_HO'] = tools.selRandom(self.pop['connActive_HO_pop'], 1); repr['connWeights_IH'] = tools.selRandom(self.pop['connWeights_IH_pop'], 1); repr['connWeights_HH'] = tools.selRandom(self.pop['connWeights_HH_pop'], 1); repr['connWeights_HO'] = tools.selRandom(self.pop['connWeights_HO_pop'], 1); # print "Representatives", repr; #Co-op Coevolution g = 0; for g in xrange(self.params['NGEN']): #Go through components population for comp_key in self.pop.keys(): next_repr = self.tbox.clone(repr); if comp_key == 'node_pop': #Output units for ind in self.pop['node_pop']: #clone representative components = self.tbox.clone(repr); #evaluate the components population components['out_nodes'] = [ind]; #assign fitness ind.fitness.values = self.evaluate(components),-1; print "n", ind.fitness.values[0]; del components; self.pop['node_pop'] = tools.selTournament(self.pop['node_pop'], len(self.pop['node_pop']), 3); next_repr['out_nodes'] = tools.selBest(self.pop['node_pop'],1); # #clone # components = self.tbox.clone(repr); # #Hidden units # for nodes in self.pop['node_pop']: # #get representatives # node_repr = self.tbox.clone(repr['hidden_nodes']); # for i, n in enumerate(nodes): # r1 = (node_repr[:i]); # r2 = (node_repr[i+1:]); # r1.extend(r2); # print "Representative:", r1; # #evaluate the components population # components['hidden_nodes'] = r1.extend([n]); # #assign fitness # ind.fitness.values = self.evaluate(components),-1; # # print "fitness:", ind.fitness.values[0]; if comp_key == 'connActive_IH_pop': #CONN IH for ind in self.pop['connActive_IH_pop']: #clone representative components = self.tbox.clone(repr); #evaluate the components population components['connActive_IH'] = ind; #assign fitness ind.fitness.values = self.evaluate(components),-1; print "fitness:", ind.fitness.values[0]; del components; self.pop['connActive_IH_pop'] = tools.selTournament(self.pop['connActive_IH_pop'], len(self.pop['connActive_IH_pop']), 3); next_repr['connActive_IH'] = tools.selBest(self.pop['connActive_IH_pop'],1); if comp_key == 'connActive_HH_pop': #CONN IH for ind in self.pop['connWeights_HH_pop']: #clone representative components = self.tbox.clone(repr); #evaluate the components population components['connActive_HH'] = ind; #assign fitness ind.fitness.values = self.evaluate(components),-1; print "fitness:", ind.fitness.values[0]; del components; self.pop['connActive_HH_pop'] = tools.selTournament(self.pop['connActive_HH_pop'], len(self.pop['connActive_HH_pop']), 3); next_repr['connActive_HH'] = tools.selBest(self.pop['connActive_HH_pop'],1); if comp_key == 'connActive_HO_pop': #CONN IH for ind in self.pop['connWeights_HO_pop']: #clone representative components = self.tbox.clone(repr); #evaluate the components population components['connActive_HO'] = ind; #assign fitness ind.fitness.values = self.evaluate(components),-1; print "fitness:", ind.fitness.values[0]; del components; self.pop['connActive_HO_pop'] = tools.selTournament(self.pop['connActive_HO_pop'], len(self.pop['connActive_HO_pop']), 3); next_repr['connActive_HO'] = tools.selBest(self.pop['connActive_HO_pop'],1); if comp_key == 'connWeights_IH_pop': #CONN IH for ind in self.pop['connWeights_IH_pop']: #clone representative components = self.tbox.clone(repr); #evaluate the components population components['connWeights_IH'] = ind; #assign fitness ind.fitness.values = self.evaluate(components),-1; print "fitness:", ind.fitness.values[0]; del components; self.pop['connWeights_IH_pop'] = tools.selTournament(self.pop['connWeights_IH_pop'], len(self.pop['connWeights_IH_pop']), 3); next_repr['connWeights_IH'] = tools.selBest(self.pop['connWeights_IH_pop'],1); if comp_key == 'connWeights_HH_pop': #clone representative components = self.tbox.clone(repr); #CONN IH for ind in self.pop['connWeights_HH_pop']: #evaluate the components population components['connWeights_HH'] = ind; #assign fitness ind.fitness.values = self.evaluate(components),-1; print "fitness:", ind.fitness.values[0]; self.pop['connWeights_HH_pop'] = tools.selTournament(self.pop['connWeights_HH_pop'], len(self.pop['connWeights_HH_pop']), 3); next_repr['connWeights_HH'] = tools.selBest(self.pop['connWeights_HH_pop'],1); if comp_key == 'connWeights_HO_pop': #clone representative components = self.tbox.clone(repr); #CONN IH for ind in self.pop['connWeights_HO_pop']: #evaluate the components population components['connWeights_HO'] = ind; #assign fitness ind.fitness.values = self.evaluate(components),-1; print "fitness:", ind.fitness.values[0]; self.pop['connWeights_HO_pop'] = tools.selTournament(self.pop['connWeights_HO_pop'], len(self.pop['connWeights_HO_pop']), 3); next_repr['connWeights_HO'] = tools.selBest(self.pop['connWeights_HO_pop'],1); repr = next_repr; ##### TEST ######## # p = pyNDMOptim(); # p.optim(); # sys.exit(); #TODO: Coevolution of neural computation paths #TODO:
def selectTeams(individuals, k): return tools.selTournament(individuals, int(0.95*len(individuals)), tournsize=4) + \ tools.selWorst(individuals, int(0.05*len(individuals)))
def main(): listTemperature = np.linspace(1100, 1800, 8) #listTemperature=np.array([1400,1700]) for counter, temperatureI in enumerate(listTemperature): calculatorIter = evltFun.sncr4AllResidenceCalculator( temperatureX=temperatureI) toolbox.register("evaluate", evaluate, calculator=calculatorIter) pop = toolbox.population(n=POP_SIZE) # Evaluate the entire population fitnesses = list(map(toolbox.evaluate, pop)) for ind, fit in zip(pop, fitnesses): ind.fitness.values = fit for g in range(NGEN): # Select the next generation individuals offspring = toolbox.select(pop, len(pop)) # Clone the selected individuals offspring = list(map(toolbox.clone, offspring)) fitnessesSelected = [] for inds in offspring: fitnessesSelected.append(inds.fitness.values) input_stream = ( "After selecting:temperature:{3} step{0} pop: {1} \n step{0} fitnessesPOP {2} \n " .format(g, pop, fitnessesSelected, counter)) with open("logFile.txt", 'a+') as stream: stream.write(input_stream) bestIndiv = toolbox.clone( tools.selTournament(offspring, 1, len(offspring))) print([counter, temperatureI, g] + bestIndiv[0] + list(bestIndiv[0].fitness.values)) with open("bestresult.csv", "a+") as csvFile: csvWriter = csv.writer(csvFile) csvWriter.writerow([counter, temperatureI, g] + bestIndiv[0] + list(bestIndiv[0].fitness.values)) #print(offspring) # Apply crossover and mutation on the offspring for child1, child2 in zip(offspring[::2], offspring[1::2]): #print(child1,child2) if random.random() < CXPB: toolbox.mate(child1, child2) del child1.fitness.values del child2.fitness.values for mutant in offspring: if random.random() < MUTPB: toolbox.mutate(mutant) del mutant.fitness.values # Evaluate the individuals with an invalid fitness invalid_ind = [ind for ind in offspring if not ind.fitness.valid] fitnesses = list(map(toolbox.evaluate, invalid_ind)) for ind, fit in zip(invalid_ind, fitnesses): ind.fitness.values = fit # The population is entirely replaced by the offspring pop[:] = offspring fitnessesPOP = [] for inds in pop: fitnessesPOP.append(inds.fitness.values) input_stream = ( "After Cross Over and mutation:\n temperature:{3} step{0} pop: {1} \n step{0} fitnessesPOP {2} \n " .format(g, pop, fitnessesPOP, counter)) print(input_stream) with open("logFile.txt", 'a+') as stream: stream.write(input_stream) with open("lastBestResult.csv", "a+") as csvFile: csvWriter = csv.writer(csvFile) csvWriter.writerow([counter, temperatureI, g] + bestIndiv[0] + list(bestIndiv[0].fitness.values))
def sel(pop, k): return tools.selTournament(pop, k, TSIZE)
def main(): # random.seed(64) schedules = Scheduling.create_schedules() cross_probability = 0.5 mutation_probability = 0.2 gene_mutation_probability = 0.05 print("Start Evolution!") start = datetime.datetime.now() g = 0 fits = [schedule.fitness for schedule in schedules] means = [] mins = [] maxs = [] length = len(schedules) mean = sum(fits) / length sum2 = sum(x*x for x in fits) std = abs(sum2 / length - mean**2)**0.5 print(" Min %s" % min(fits)) print(" Max %s" % max(fits)) print(" Avg %s" % mean) print(" Std %s" % std) means.append(mean) mins.append(min(fits)) maxs.append(max(fits)) while max(fits) < 0 and g < 100: # 世代数更新 g = g + 1 print("-- Generation %i --" % g) schedules = tools.selTournament(schedules, len(schedules), 10) schedules = [Scheduling.Schedule(s.list_map) for s in schedules] Scheduling.try_mate(schedules, cross_probability) for mutant in schedules: if random.random() < mutation_probability: mutant.try_mutate(gene_mutation_probability) for schedule in schedules: schedule.assign_person() schedule.evaluate() fits = [schedule.fitness for schedule in schedules] length = len(schedules) mean = sum(fits) / length sum2 = sum(x*x for x in fits) std = abs(sum2 / length - mean**2)**0.5 print(" Min %s" % min(fits)) print(" Max %s" % max(fits)) print(" Avg %s" % mean) print(" Std %s" % std) means.append(mean) mins.append(min(fits)) maxs.append(max(fits)) print("-- End of (successful) evolution --") best_ind = tools.selBest(schedules, 1)[0] print("Best individual is followed") best_ind.console_out_persons() print(f"Fitness is {best_ind.fitness}") end = datetime.datetime.now() print(end - start) print(maxs) print(means) print(mins)
def novelty_search(population, toolbox, end_time, cxpb=0, mutpb=1, stats=None, verbose=__debug__): logbook = tools.Logbook() logbook.header = ['gen', 'nevals'] + (stats.fields if stats else []) offspring = population gen = 0 K = 3 # Num neighbours # Store an archive of most novel individuals at the time of occurence archive_behaviour = [] # Begin the generational process while time.time() < end_time: if archive_behaviour: print("Archive", len(archive_behaviour), len(archive_behaviour[0])) # Evaluate the individuals without a behaviour invalid_ind = [ind for ind in offspring if ind.behaviour is None] population_behaviour = toolbox.map(toolbox.behaviour, invalid_ind) population_behaviour = list(population_behaviour) all_behaviour = population_behaviour + archive_behaviour # At this point every individual has a behaviour, so we can calculate pairwise differences in behaviour # All pairwise differences for each predictor. Shape (#predictors, #predictors). behavioral_distance = euclidean_distances(all_behaviour) num_predictors = behavioral_distance.shape[1] # Sort the columns to be in terms of increasing distance for each row behavioral_distance.sort(axis=1) # Safety check to cap K to be the max number of neighbours K = min(num_predictors - 1, K) # Get the average distance to the K nearest predictors. Exclude the first one since thats the distance to self (0) average_distance_to_neighbours = behavioral_distance[:, 1:K + 1].mean(axis=1) # for ind, behaviour, novelty in zip(invalid_ind, population_behaviour, average_distance_to_neighbours): ind.behaviour = behaviour ind.fitness.values = novelty, 0 archive_behaviour += population_behaviour # Replace the current population by the offspring population[:] = offspring # Append the current generation statistics to the logbook record = stats.compile(population) if stats else {} logbook.record(gen=gen, nevals=len(invalid_ind), **record) if verbose: print(logbook.stream) # Select the next generation individuals offspring = tools.selTournament(population, len(population), tournsize=2) # Vary the pool of individuals offspring = varOr(offspring, toolbox, len(population), cxpb, mutpb) gen += 1 # Once done we now want to search all the resulting novel models for the best. candidates = population # Need to compute fitness for entire population every generation, as the fitness function changes each step fitnesses = toolbox.map(partial(toolbox.evaluate, timeout=False), candidates) # Store the fitness. The first objective is averaged across all generations. for ind, fitness in zip(candidates, fitnesses): ind.fitness.values = fitness # We only save the single best hof = tools.HallOfFame(1) hof.update(candidates) return hof, logbook, gen
def selectNN(pop, k): return tools.selTournament(pop, k, params["tsize"])
def standard_loop(World, locale): assert (len(locale.population)) # --validate individuals; locale.population = promoterz.validation.validatePopulation( World.tools.constructPhenotype, {World.genconf.Strategy: World.TargetParameters}, locale.population) # --evaluate individuals; nb_evaluated = World.parallel.evaluatePopulation(locale) assert (len(locale.population)) # --send best individue to HallOfFame; if not locale.EPOCH % 15: BestSetting = tools.selBest(locale.population, 1)[0] locale.HallOfFame.insert(BestSetting) assert (len(locale.population)) assert (sum([x.fitness.valid for x in locale.population]) == len(locale.population)) # --compile stats; locale.compileStats() # --population ages qpop = len(locale.population) locale.population = locale.extratools.populationAges( locale.population, locale.EvolutionStatistics[locale.EPOCH]) wpop = len(locale.population) elder = qpop - wpop # --remove equal citizens locale.population = locale.extratools.populationPD(locale.population) # --show stats; locale.showStats(nb_evaluated, elder) # --calculate new population size; if locale.EPOCH: PRoFIGA = promoterz.supplement.PRoFIGA.calculatePRoFIGA( World.genconf.PRoFIGA_beta, locale.EPOCH, World.genconf.NBEPOCH, locale.EvolutionStatistics[locale.EPOCH - 1], locale.EvolutionStatistics[locale.EPOCH]) locale.POP_SIZE += locale.POP_SIZE * PRoFIGA minps, maxps = World.genconf.POP_SIZE // 2, 899 try: locale.POP_SIZE = int( round(max(min(locale.POP_SIZE, maxps), minps))) except: locale.POP_SIZE = 30 M = "POP_SIZE PROFIGA ERROR;" print(M) # --filter best inds; locale.population[:] = tools.selBest(locale.population, locale.POP_SIZE) assert (len(locale.population)) assert (None not in locale.population) #print(EvolutionStatistics) #FinalBestScores.append(Stats['max']) ''' print("Loading new date range;") print("\t%s to %s" % (locale.DateRange['from'], locale.DateRange['to'])) for I in range(len(locale.population)): del locale.population[I].fitness.values toolbox.register("evaluate", coreFunctions.Evaluate, GenerationMethod.constructPhenotype, DateRange) FirstEpochOfDataset = True bestScore = 0 ''' # --select best individues to procreate offspring = tools.selTournament(locale.population, World.genconf._lambda, 2 * World.genconf._lambda) offspring = [deepcopy(x) for x in offspring] # is deepcopy necessary? # --modify and integrate offspring; offspring = algorithms.varAnd(offspring, World.tools, World.genconf.cxpb, World.genconf.mutpb) locale.extratools.ageZero(offspring) locale.population += offspring # --NOW DOESN'T MATTER IF SOME INDIVIDUE LACKS FITNESS VALUES; assert (None not in locale.population) # --immigrate individual from HallOfFame; if random.random() < 0.2: locale.population = locale.extratools.ImmigrateHoF(locale.population) # --immigrate random number of random individues; if random.random() < 0.5: locale.population = locale.extratools.ImmigrateRandom( (2, 7), locale.population) assert (len(locale.population)) ''' if FirstEpochOfDataset: InitialBestScores.append(Stats['max']) Stats['dateRange'] = "%s ~ %s" % (locale.DateRange['from'], locale.DateRange['to']) else: Stats['dateRange'] = None ''' assert (None not in locale.population)
def train(self, pop=20, gen=10): from deap import algorithms from deap import base from deap import creator from deap import tools from deap.tools import Statistics # import random from scipy.stats import rv_discrete # creator.create("FitnessMulti", base.Fitness, weights=(1.0, -1.0)) # creator.create("Individual", list, fitness=creator.FitnessMulti) creator.create("FitnessMin", base.Fitness, weights=(-1.0, )) creator.create("Individual", list, fitness=creator.FitnessMin) toolbox = base.Toolbox() # Attribute generator custm = rv_discrete(name='custm', values=(self.a_w.index, self.a_w.values)) toolbox.register("attr_int", custm.rvs) # Structure initializers toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_int, n=len(self.s)) toolbox.register("population", tools.initRepeat, list, toolbox.individual, n=pop) # Operator registering toolbox.register("evaluate", self.eval_classifer) toolbox.register("mate", tools.cxUniform, indpb=0.5) toolbox.register("mutate", tools.mutUniformInt, low=min(self.a.index), up=max(self.a.index), indpb=0.1) toolbox.register("select", tools.selNSGA2) MU, LAMBDA = pop, pop population = toolbox.population(n=MU) hof = tools.ParetoFront() s = Statistics(key=lambda ind: ind.fitness.values) s.register("mean", np.mean) s.register("min", min) # pop, logbook = algorithms.eaMuPlusLambda(pop, toolbox, mu=MU, lambda_=LAMBDA, cxpb=0.7, mutpb=0.3, ngen=gen, stats=s, halloffame=hof) for i in range(gen): offspring = algorithms.varAnd(population, toolbox, cxpb=0.95, mutpb=0.1) fits = toolbox.map(toolbox.evaluate, offspring) for fit, ind in zip(fits, offspring): ind.fitness.values = fit population = tools.selBest(offspring, int( 0.05 * len(offspring))) + tools.selTournament( offspring, len(offspring) - int(0.05 * len(offspring)), tournsize=3) # population = toolbox.select(offspring, k=len(population)) print s.compile(population) top10 = tools.selBest(population, k=10) return top10
def selElitistAndTournament(individuals, k, tournsize, elitism): return tools.selBest(individuals, elitism) + tools.selTournament(individuals, k-elitism, tournsize)
def selElitistAndTournament(individuals, k_elitist, k_tournament, tournsizeTour): return tools.selBest(individuals, k_elitist) + tools.selTournament( individuals, k_tournament, tournsize=tournsizeTour)
def _select(self, individuals, k): return tools.selTournament(individuals, k, 2)
def select(population, k, num_best, tournsize): return tools.selBest(population, int(num_best)) + tools.selTournament(population, k-int(num_best), tournsize=tournsize)
def run(self, verbose=False): creator.create("FitnessMulti", base.Fitness, weights=(-1.0, -1.0, -1.0)) creator.create("Individual", gp.PrimitiveTree, fitness=creator.FitnessMulti) start = time.time() self.population = self.init_pop() elapsed = time.time() - start #print(f'elapsed seconds for population initialization: {elapsed}') self.population = [ creator.Individual(indv) for indv in self.population ] toolbox = base.Toolbox() toolbox.register("expr", self.genRampedRegexTree, min_=self.min_ht, max_=self.max_ht, ratio=self.term_ratio, classes=self.classes, pset=self.pset) toolbox.register("individual", tools.initIterate, creator.Individual, toolbox.expr) toolbox.register("population", tools.initRepeat, list, toolbox.individual) toolbox.register("mutate", gp.mutUniform, expr=toolbox.expr, pset=self.pset) 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) logbook = tools.Logbook() logbook.header = ['gen'] + stats.fields for g in range(self.ngen): start_time = time.time() # get the fitnesses for every individual in the population fitnesses = futures.map(self.evaluate_regex, self.population) for indv, fits in zip(self.population, fitnesses): indv.fitness.values = fits # log and record progress record = stats.compile(self.population) logbook.record(gen=g, **record) if verbose: print(logbook.stream) hof.update(self.population) # sort by Pareto-fronts (NSGA-II, Deb, et) self.population = [ indv for front in tools.sortNondominated( self.population, self.pop_size) for indv in front ] keep_num = int(self.pop_size * 0.9) # keep 90% of old gen new_pop = [] while len(new_pop) < keep_num: rnum = random.random() if rnum < self.CXPB: cx_indv1 = tools.selTournament(self.population, k=1, tournsize=7)[0] cx_indv2 = tools.selTournament(self.population, k=1, tournsize=7)[0] # cx_indv1, cx_indv2 = gp.cxOnePointLeafBiased(cx_indv1, cx_indv2, self.term_ratio) cx_indv1, cx_indv2 = self.cxLeafOrSubTree( cx_indv1, cx_indv2, self.term_ratio) new_pop.append(cx_indv1) new_pop.append(cx_indv2) elif rnum < self.CXPB + self.MUTPB: mutant = toolbox.mutate( tools.selTournament(self.population, k=1, tournsize=7)[0])[0] new_pop.append(mutant) else: new_pop.append( tools.selTournament(self.population, k=1, tournsize=7)[0]) self.population = new_pop + toolbox.population(n=self.pop_size - keep_num) best = tools.selBest(self.population, k=1)[0] tree = gp.PrimitiveTree(best) print('Best of that gen:') print( gp.compile(tree, pset=self.pset).s + '\nFitness: ' + str({best.fitness.values})) elapsed_time = time.time() - start_time remaining_min = (elapsed_time * (self.ngen - g)) / 60 remaining_hours = remaining_min / 60 print( f"Time for last gen: {elapsed_time} secs, Remaining: {remaining_min} minutes, {remaining_hours} hours." ) print('[' + ('*' * (g // self.ngen)) + ((100 - (g // self.ngen)) * ' ') + ']') return hof, logbook
def nsga_2(task_names, type_names, type_info_price, type_info_ecu, task_base_time, task_preds, comm_speeds, comm_sizes, task_seccs, archive_name=None): n_tasks = len(task_names) n_nodes = n_tasks n_types = len(type_names) creator.create("FitnessMulti", base.Fitness, weights=(-1.0, -1.0)) creator.create("Individual", array.array, typecode='I', fitness=creator.FitnessMulti) toolbox = base.Toolbox() toolbox.register("task2node", random.randint, 0, n_tasks-1) toolbox.register("node2type", random.randint, 0, n_types-1) toolbox.register("individual", tools.initCycle, creator.Individual, (toolbox.task2node, toolbox.node2type), n=n_tasks) toolbox.register("population", tools.initRepeat, list, toolbox.individual) toolbox.register("evaluate", evaluate, type_info_price=type_info_price, type_info_ecu=type_info_ecu, task_base_time=task_base_time, task_preds=task_preds, comm_speeds=comm_speeds, comm_sizes=comm_sizes, n_tasks=n_tasks, n_nodes=n_nodes, n_types=n_types) toolbox.register("mate", cross_tool, n_tasks=n_tasks, inplace=True) toolbox.register("mutate", mutate_tool, n_tasks=n_tasks, n_types=n_types, multi=True) toolbox.register("select", tools.selNSGA2) # pool = multiprocessing.Pool() # toolbox.register("map", pool.map) pop = toolbox.population(50) _, loc, ts = heft2(type_info_price, type_info_ecu, task_base_time,\ task_preds, comm_speeds, comm_sizes, task_seccs) pop[0][:] = get_chromosome(loc, ts) # hof = tools.ParetoFront() # hof.update(pop) fits = toolbox.map(toolbox.evaluate, pop) for fit, ind in zip(fits, pop): ind.fitness.values = fit for gen in range(100000): offspring = tools.selTournament(pop, 50, 2) offspring = toolbox.map(toolbox.clone, offspring) for ind1, ind2 in zip(offspring[::2], offspring[1::2]): if random.random() <= 0.5: toolbox.mate(ind1, ind2) del ind1.fitness.values, ind2.fitness.values for ind in offspring: if random.random() <= 0.3: toolbox.mutate(ind) del ind.fitness.values 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 pop = toolbox.select(pop + offspring, 50) # hof.update(offspring) # pop = hof s = set() ar = [] for i in pop: if tuple(i.fitness.values) not in s: s.add(tuple(i.fitness.values)) ar.append(i) ar.sort(key=lambda x: x.fitness.values[0]) if archive_name: dump(archive_name, ar, task_names, type_names, type_info_price, type_info_ecu, task_base_time, task_preds, comm_speeds, comm_sizes, n_tasks, n_nodes, n_types) return ar
def selElitistAndTournament(individuals, k, frac_elitist, tournsize): return tools.selBest(individuals, int(k * frac_elitist)) + tools.selTournament( individuals, int(k * (1 - frac_elitist)), tournsize=tournsize )
def evolve(self, population, CXPB, MUTPB, MUTFQ, NGEN, GOAL): # CXPB = 0.5 # Crossover probability. # MUTPB = 0.75 # Chance for individual to mutate. # MUTFQ = 0.20 # Frequency of mutate() mutation events. # NGEN = 75 # Number of generations. # GOAL = 0.95 # Goal AUC. POP = len(population) # Data to use for plots. plots = defaultdict(list) # Evaluation model. linreg = LinearRegression(normalize=True) # Evaluate fitnesses of starting population. fitness_list = map(lambda x: self.evaluate(x), population) # Assign fitness values. for ind, fitness in zip(population, fitness_list): ind.fitness.values = fitness # EVOLUTION. for gen in xrange(NGEN): print "\nGeneration:", str(gen + 1) print "---------------" print "Mean AUC:", str(self.population_fitness(population)) print "Mean RMSD:", str(self.population_rmsd(population)) print "Mean Count:", str(self.population_count(population)) plots["PopRMSD"].append(self.population_rmsd(population)) plots["PopFitness"].append(self.population_fitness(population)) plots["PopCount"].append(self.population_count(population)) plots["PopFreq"].append(self.population_lig_freq(population)) # Goal achieved! if self.population_fitness(population) > GOAL: break # Select the next generation of individuals. offspring = list() offspring.append(max(population, key=lambda x: x.fitness.values[0])) offspring += tools.selTournament(population, POP-1, 10) # Select the rest with tournament. offspring = map(self.toolbox.clone, offspring) # Apply crossovers. for child_a, child_b in zip(offspring[::2], offspring[1::2]): # Staggered. if random.random() < CXPB: # crossover(child_a, child_b, CXPB) self.sp_crossover(child_a, child_b, CXPB) del child_a.fitness.values del child_b.fitness.values # Apply mutations. for child in offspring: if random.random() < MUTPB: # mutate(child, MUTFQ) self.sp_mutate(child, MUTFQ) # child = single_ppl_ind(train_actives) # Mutation event is just creating a new individual. del child.fitness.values # Reevaluate fitness of changed individuals. new_children = [e for e in offspring if not e.fitness.valid] fitness_list = map(lambda x: self.evaluate(x), new_children) for individual, fitness in zip(new_children, fitness_list): individual.fitness.values = fitness # Replace population with the new generation. population[:] = offspring best = max(population, key=lambda x: x.fitness.values[0]) print "Best Individual: \n {count} Poses \n {auc} AUC \n {rmsd} AvgRMSD".format( count=sum(best), auc=best.fitness.values[0], rmsd=self.individual_rmsd(best) ) plots["BestCount"].append(sum(best)) plots["BestFitness"].append(best.fitness.values[0]) plots["BestRMSD"].append(self.individual_rmsd(best)) plots["BestFrequencies"].append(self.ligand_freq(best)) plots["examples"] = best return plots
with open(path, 'r') as f: data = json.load(f) ## TODO: this is pure hack. It is needed to be refactored or removed nodes = {node.flops: node.name for node in rm.get_nodes()} mapping = ListBasedIndividual([(t, nodes[n]) for t, n in data["mapping"]]) return mapping def extract_ordering_from_ga_file(path): with open(path, 'r') as f: data = json.load(f) ordering = ListBasedIndividual([t for t in data["ordering"]]) return ordering tourn = lambda ctx, pop: tools.selTournament(pop, len(pop), 2) ## TODO: remove this hack later class Fitness(ComparableMixin): def __init__(self, fitness): self.values = [fitness] self.valid = True def _cmpkey(self): return self.values[0] ## TODO: remove it later def _compare(self, other, method): try: return method(self._cmpkey(), other._cmpkey())
def sel_elitist_tournament(individuals, mu, k_elitist, k_tournament, tournsize): return tools.selBest(individuals, int(k_elitist * mu)) + \ tools.selTournament(individuals, int(k_tournament * mu), tournsize=tournsize)