def selNSGA2(individuals, k): """Apply NSGA-II selection operator on the *individuals*. Usually, the size of *individuals* will be larger than *k* because any individual present in *individuals* will appear in the returned list at most once. Having the size of *individuals* equals to *k* will have no effect other than sorting the population according to a non-domination scheme. The list returned contains references to the input *individuals*. For more details on the NSGA-II operator see [Deb2002]_. :param individuals: A list of individuals to select from. :param k: The number of individuals to select. :returns: A list of selected individuals. .. [Deb2002] Deb, Pratab, Agarwal, and Meyarivan, "A fast elitist non-dominated sorting genetic algorithm for multi-objective optimization: NSGA-II", 2002. """ pareto_fronts = sortFastND(individuals, k) for front in pareto_fronts: assignCrowdingDist(front) chosen = list(chain(*pareto_fronts[:-1])) k = k - len(chosen) if k > 0: sorted_front = sorted(pareto_fronts[-1], key=attrgetter("fitness.crowding_dist"), reverse=True) chosen.extend(sorted_front[:k]) return chosen, pareto_fronts[0]
def main_computation_body(self,pop,toolbox): # init population self.prepareToolbox() fit_val = toolbox.map(toolbox.evaluate,pop) for ind,fit in zip(pop,fit_val): ind.fitness.values = fit # sort using non domination sort (k is the same as n of population - only sort is applied) pop, main_front = toolbox.select(pop, k=self.N) for g in xrange(self.GEN): print "CURRENT GEN: " + str(g) #select parent pool with tournament dominated selection parent_pool = toolbox.selectTournament(pop, k=self.N) offspring_pool = map(toolbox.clone, parent_pool) # Apply crossover and mutation on the offspring for child1, child2 in zip(offspring_pool[::2], offspring_pool[1::2]): if random.random() < 0.9: toolbox.mate(child1, child2) for mutant in offspring_pool: if random.random() < 0.1: toolbox.mutate(mutant) # evaluate offsprings fit_val = toolbox.map(toolbox.evaluate,offspring_pool) for ind,fit in zip(offspring_pool,fit_val): ind.fitness.values = fit # extend base population with offsprings, pop is now 2N size pop.extend(offspring_pool) # sort and select new population pop, main_front = toolbox.select(pop, k=self.N) self.monitor(g,pop) self.compute_partial_spacing(g, main_front) if self.parallel and "DEMES" in self.parallel: if g % self.migration_rate == 0 and g > 0: print "DEMES MIGRATING" pareto = tools.sortFastND(pop, k=self.N)[0] print "PARETO SIZE: ",len(pareto) toolbox.migrate(pop) self.final_front = tools.sortFastND(pop, k=self.N)[0]
# sort using non domination sort (k is the same as n of population - only sort is applied) pop = toolbox.select(pop, k=N) for g in xrange(GEN): print "CURRENT GEN: " + str(g) #select parent pool with tournament dominated selection parent_pool = toolbox.selectTournament(pop, k=N) offspring_pool = map(toolbox.clone, parent_pool) # Apply crossover and mutation on the offspring for child1, child2 in zip(offspring_pool[::2], offspring_pool[1::2]): if random.random() < 0.9: toolbox.mate(child1, child2) for mutant in offspring_pool: if random.random() < 0.1: toolbox.mutate(mutant) # evaluate offsprings for ind in offspring_pool: ind.fitness.values = toolbox.evaluate(ind) # extend base population with offsprings, pop is now 2N size pop.extend(offspring_pool) # sort and select new population pop = toolbox.select(pop, k=N) final_front = tools.sortFastND(pop, k=N)[0] print final_front
for _ in xrange(GEN): #select parent pool with tournament dominated selection parent_pool = toolbox.selectTournament(pop, k=N) offspring_pool = map(toolbox.clone, parent_pool) # Apply crossover and mutation on the offspring for child1, child2 in zip(offspring_pool[::2], offspring_pool[1::2]): if random.random() < 0.9: toolbox.mate(child1, child2) for mutant in offspring_pool: if random.random() < 0.1: toolbox.mutate(mutant) # evaluate offsprings for ind in offspring_pool: ind.fitness.values = toolbox.evaluate(ind) # extend base population with offsprings, pop is now 2N size pop.extend(offspring_pool) # sort and select new population pop = toolbox.select(pop, k=N) first_front = tools.sortFastND(pop, k=N)[0] with open('nsga2_wyniki', 'w') as f: for ind in first_front: f.write(str(ind.fitness.values[0]) + ' ' + str(ind.fitness.values[1])+ '\n')
def mainnsga2(GEN,pb): def my_rand(): return random.random()*(V-U) - (V+U)/2 file = "pareto_front/"+pbs+"_front.json" optimal_front = json.load(open(file)) optimal_front = [optimal_front[i] for i in range(0, len(optimal_front), 2)] first_point_opt = optimal_front[0] last_point_opt = optimal_front[-1] creator.create("FitnessMax", base.Fitness, weights=(-1.0,-1.0,)) creator.create("Individual", list, fitness=creator.FitnessMax) #@UndefinedVariable toolbox = base.Toolbox() toolbox.register("attr_float", my_rand) toolbox.register("individual", tools.initRepeat, creator.Individual,toolbox.attr_float, n=30) #@UndefinedVariable toolbox.register("evaluate", pb) # toolbox.register("population", tools.initRepeat, list, toolbox.individual) toolbox.register("mate", tools.cxSimulatedBinaryBounded, eta=0.5, low=U, up=V) toolbox.register("mutate", tools.mutPolynomialBounded, eta=0.5, low=U, up=V, indpb=1) toolbox.register("select", tools.selNSGA2) toolbox.register("selectTournament", tools.selTournamentDCD) obj_count = 0 dvrst_list = [] # init population pop = toolbox.population(n=N) for ind in pop: ind.fitness.values = toolbox.evaluate(ind) # sort using non domination sort (k is the same as n of population - only sort is applied) pop = toolbox.select(pop, k=N) for _ in xrange(GEN): #select parent pool with tournament dominated selection parent_pool = toolbox.selectTournament(pop, k=N) offspring_pool = map(toolbox.clone, parent_pool) # Apply crossover and mutation on the offspring for child1, child2 in zip(offspring_pool[::2], offspring_pool[1::2]): if random.random() < 0.9: toolbox.mate(child1, child2) for mutant in offspring_pool: if random.random() < 0.1: toolbox.mutate(mutant) # evaluate offsprings for ind in offspring_pool: ind.fitness.values = toolbox.evaluate(ind) # extend base population with offsprings, pop is now 2N size pop.extend(offspring_pool) # sort and select new population pop = toolbox.select(pop, k=N) obj_count = tools.spea2_count + obj_count dvrst = diversity(pop, first_point_opt, last_point_opt) dvrst_list.append([obj_count , dvrst]) first_front = tools.sortFastND(pop, k=N)[0] return first_front,