def customMutation(self, ind, low=5, up=100, indpb=0.05): split = int(len(ind) / 2) half_ind_values = ind[:split] half_ind_presence = ind[split:] final_ind_values = tools.mutUniformInt(half_ind_values, low, up, indpb) final_ind_presences = tools.mutUniformInt(half_ind_presence, low, up, indpb) ind[:split], ind[split:] = final_ind_values[0], final_ind_presences[0] return ind,
def mutate_pop(best_policy, nindividuals, n_controls): list_of_inds = [] for i in range(nindividuals): # split because there may be more than one control split_lists = split_list(list(best_policy), n_controls) mutated_ind = [] for l in split_lists: l = l[1:] tools.mutUniformInt(l, 0, 10, 0.2) l.append(random.randint(0, 10)) mutated_ind.extend(l) if mutated_ind not in list_of_inds: list_of_inds.append(mutated_ind) return list_of_inds
def my_mutation(self,LL,UU,individual): t=self.t typeOfInput=len(t) individual_ = [] for i in range(typeOfInput): if t[i] =='float': ind1=tools.mutPolynomialBounded([individual[i]], low=LL[i], up=UU[i], eta=20.0,indpb=1.0/len(self.t)) individual_.append(ind1[0][0]) elif t[i] =='int' or t[i]== 'bool': ind2=tools.mutUniformInt([individual[i]], low=LL[i], up=UU[i], indpb= 0.9) individual_.append(ind2[0][0]) return individual_
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 __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)
# else: # job.types.append(furnace_id + '_heating') # entity_mgr.update(job) # product_id_list = job.product_id_list() # for product_id in product_id_list: # product = entity_mgr.get(product_id) # product.properties['report'][0]['equipment_id'] = furnace_id # product.properties['report'][0]['start_time'] = None # product.properties['report'][0]['end_time'] = None return furnace_schedule def score(self): E = self.simulator.total_results() T = self.simulator.total_time() T = (T - self.simulator.start_time).total_seconds() score = E * self.w1 + T * self.w2 return score def get_total_weight(self): total = 0 for j in self.job_list: weight = j['properties']['ingot']['current_weight'] #weight = job.get_initial_weight(self.entity_mgr) total += weight return total if __name__ == "__main__": for i in range(5): print(tools.mutUniformInt([4, 1], 0, 4, 1)) tools.mutFlipBit(array('i', [4, 1, 2, 1, 1, 0, 2, 0, 2, 1, 4, 1, 4, 0, 3, 4, 0, 3, 3]), 1)
for i in ind2: print(i) #toolbox.register("mate", tools.cxTwoPoint) toolbox.register("mate", tools.cxUniform, indpb=0.5) #toolbox.register("mutate", tools.mutGaussian, mu=0, sigma=1, indpb=0.2) toolbox.register("mutate", tools.mutUniformInt, low=0, up=1, indpb=0.2) #toolbox.register("select", tools.selTournament, tournsize=3) toolbox.register("select", tools.selNSGA2, nd="standard") toolbox.register("evaluate", evalKnapsack) ##Mutation mutant = toolbox.clone(ind1) #ind2 = tools.mutGaussian(mutant, mu=0.0, sigma=0.2, indpb=0.2) ind2, = tools.mutUniformInt(mutant, low=0, up=1, indpb=0.5) del mutant.fitness.values ind2 ind1 ind2.fitness.values = evalKnapsack(ind2) ind2.fitness.values ind1.fitness.values ##CROSSOVER child1, child2 = [toolbox.clone(ind) for ind in (ind1, ind2)] tools.cxUniform(child1, child2, 0.5) #tools.cxSimulatedBinary(child1, child2,0.5) #tools.cxPartialyMatched(child1, child2) del child1.fitness.values
def mutate(ind, low, up): return tools.mutUniformInt(ind, low=low, up=up, indpb=0.5)
def main(): random.seed(64) pop = toolbox.population(n=3000) CXPB, MUTPB = 0.5, 0.2 print("Start of evolution") fitnesses = list(map(toolbox.evaluate, pop)) for ind, fit in zip(pop, fitnesses): ind.fitness.values = fit print(" Evaluated %i individuals" % len(pop)) # fits = [ind.fitness.values[0] for ind in pop] g = 0 while g < 50: g = g + 1 print("Generation %i" % g) offspring = toolbox.select(pop, len(pop)) # print("selected individuals %i" % len(offspring)) offspring = list(map(toolbox.clone, offspring)) for child1, child2 in zip(offspring[::2], offspring[1::2]): if random.random() < CXPB: # tools.cxOnePoint(child1, child2) tools.cxTwoPoint(child1, child2) # tools.cxUniform(child1, child2, indpb=0.01) del child1.fitness.values del child2.fitness.values for mutant in offspring: if random.random() < MUTPB: # tools.mutGaussian(mutant,1,1, indpb=0.01) # tools.mutFlipBit(mutant, indpb=0.01) tools.mutUniformInt(mutant, 0, 150, indpb=0.02) del mutant.fitness.values invalid_ind = [ind for ind in offspring if not ind.fitness.valid] fitnesses = map(toolbox.evaluate, invalid_ind) for ind, fit in zip(invalid_ind, fitnesses): ind.fitness.values = fit print(" Evaluated %i individuals" % len(invalid_ind)) # G=1 pop[:] = offspring fits = [ind.fitness.values[0] for ind in pop] length = len(pop) mean = sum(fits) / length sum2 = sum(x * x for x in fits) std = abs(sum2 / length - mean ** 2) ** 0.5 min_fitness.append(min(fits)) max_fitness.append(max(fits)) mean_fitness.append(mean) std_fitness.append(std) print(" Minimum fitness %s" % min(fits)) print(" Maximum fitness %s" % max(fits)) print(" Avg fitness %s" % mean) print(" Std deviation %s" % std) best_ind = tools.selBest(pop, 1)[0] print("Best individual is %s, %s" % (best_ind, best_ind.fitness.values)) for index in range(0, len(best_ind)): print("Station %i location x--> %i y--> %i" % ( index + 1, int(data[best_ind[index]][0]), int(data[best_ind[index]][1])))
def mutacion(seleccionados, tasa_mutacion=0.01): for n, individuo in enumerate(seleccionados): seleccionados[n] = mutUniformInt(individuo, 0, 10, tasa_mutacion)[0] return seleccionados
if SEL == 'SLR': #Selecting 10 random candidates Temp_pop = tools.selRandom(Temp_pop, 10) if SEL == 'SLM': #Selecting 8 best and 2 random candidates BestMatingCandidates = tools.selBest(Temp_pop, 8, fit_attr='fitness') BestMates = list(map(toolbox.clone, BestMatingCandidates)) RandomMatingCandidates = tools.selRandom(Temp_pop, 2) RandomMates = list(map(toolbox.clone, RandomMatingCandidates)) Temp_pop = BestMates + RandomMates #Now we can begin the mating for child1, child2 in zip(Temp_pop[::2], Temp_pop[1::2]): #Mating selected candidates if random.random() < CXPB: #Mating happens with probability toolbox.mate(child1, child2) for mutant in Temp_pop: #Mutating based on settings if MUT == 'MUF': tools.mutUniformInt(mutant, 1, 5, MUTPB) if MUT == 'MCP': EA_Utilities.mutRsCreepInt(mutant, -1, 1, MUTPB) if MUT == 'MGS': EA_Utilities.mutRsGaussianInt(mutant, 0.1, MUTPB) #Now here is the bit: We want to see how generation improves over time. No penalty for querying FRT generation_fitness = list(map(toolbox.evaluate_generation, Temp_pop)) for ind, fit in zip(Temp_pop, generation_fitness): ind.fitness.values = fit Generation.append(fit[0]) Avg_Generation_F = sum(Generation) / len(Generation) Max_Generation_F = max(Generation) Min_Generation_F = min(Generation) for ind in Temp_pop: #We don't want to fill our population with identical points if EA_Utilities.CachingTest(cache, ind) == False: pop.append(ind)
ind2 = random.sample(range(10), 10) tools.cxTwoPoint(ind1, ind2) ind = [ind1, ind2] tools.cxOrdered(ind1, ind2) for i in range(100): ind1 = random.sample(range(10), 10) ind2 = random.sample(range(10), 10) tools.mutShuffleIndexes(ind1, 1) print(ind1) for i in range(100): ind1 = random.sample(range(10), 10) tools.mutUniformInt(ind1, 20, 100, 1) print(ind1) for i in range(100): ind1 = random.sample(range(100), 10) ind2 = random.sample(range(100), 10) tools.cxPartialyMatched(ind1, ind2) print(ind1) print(ind2) print(len(set(ind1)), len(set(ind2))) while True: ind1 = random.sample(range(100), 10) ind2 = random.sample(range(100), 10) # print(ind1, ind2) # print(len(set(ind1)), len(set(ind2)))
def mutUniformFP(guess): return tools.mutUniformInt(guess, low=0, up=NB_COLORS - 1, indpb=guess.fitness.values[0] / NB_SLOTS)