def crossOver_twoPoint(ind1, ind2): # need to cross over all types of obstacles for obstacle_gene in ind1.decisionGenes.keys(): #Crossover of the x-position (outer) list tools.cxTwoPoint(ind1.decisionGenes[obstacle_gene], ind2.decisionGenes[obstacle_gene]) return (ind1, ind2)
def mate(ind1, ind2): child1, child2 = [toolbox.clone(ind) for ind in (ind1, ind2)] assert len(child1) == len(child2) for i in range(len(child1[0])): tools.cxTwoPoint(child1[0][i], child2[0][i]) for i in range(len(child1[1])): tools.cxTwoPoint(child1[1][i], child2[1][i]) del child1.fitness.values del child2.fitness.values
def try_mate(schedules, prob): for child1, child2 in zip(schedules[::2], schedules[1::2]): if random.random() < prob: for occupation in Occupation: tools.cxTwoPoint(child1.list_map[occupation], child2.list_map[occupation]) child1.list_map[occupation] = [[x for x in child1.person_map[occupation] if x.id == person.id][0]\ for person in child1.list_map[occupation]] child2.list_map[occupation] = [[x for x in child2.person_map[occupation] if x.id == person.id][0]\ for person in child2.list_map[occupation]]
def customCrossover(self, ind1, ind2): split = int(len(ind1) / 2) half_ind1_values = ind1[:split] half_ind1_presence = ind1[split:] half_ind2_values = ind2[:split] half_ind2_presence = ind2[split:] final_ind_values1, final_ind_values2 = tools.cxTwoPoint( half_ind1_values, half_ind2_values) final_ind_presence1, final_ind_presence2 = tools.cxTwoPoint( half_ind1_presence, half_ind2_presence) ind1[:split], ind1[split:] = final_ind_values1, final_ind_presence1 ind2[:split], ind2[split:] = final_ind_values2, final_ind_presence2 return ind1, ind2
def cruza(seleccionados, tasa_cruza=0.8): aMutar = [] for i in range(pob): r = random.uniform(0, 1) if r < tasa_cruza: aMutar.append(i) for n, iPadre in enumerate(aMutar): if iPadre == aMutar[-1]: seleccionados[iPadre] = cxTwoPoint(seleccionados[iPadre], seleccionados[aMutar[0]])[0] else: seleccionados[iPadre] = cxTwoPoint(seleccionados[iPadre], seleccionados[aMutar[n + 1]])[0] return seleccionados
def crossOver(ind1,ind2): news = tools.cxTwoPoint(ind1,ind2) chanc = random.randint(1,10) bigBrother = 0 if len(news[0]) > len(news[1]) else 1 if chanc < 3 and len(news[bigBrother]) >= RULE_SIZE*2: newsT = (news[bigBrother][24:],news[not bigBrother]) elif chanc >= 9: news2 = tools.cxTwoPoint(ind1,ind2) newsT = (news[bigBrother] + news2[random.randint(0,1)][:24],news[not bigBrother]) else: newsT = news return ( creator.Individual(newsT[0]) , creator.Individual(newsT[1]) )
def cxTwo_Uniform(ind1, ind2): x = tools.cxTwoPoint(ind1, ind2) y = tools.cxUniform(ind1, ind2, indpb=0.9) if sum(max(x)) > sum(max(y)): return x else: return y
def crossover(ind1, ind2, all_relays=tuple()): """ Performs 2 two-point crossovers on the sensor and relay part of the genes. :param all_relays: List containing all possible relay positions :param ind1: :param ind2: :return: A tuple of two individuals. """ ind1, ind2 = ind1.clone(), ind2.clone() s1 = ind1[:ind1.sensor_count] s2 = ind2[:ind2.sensor_count] r1 = ind1[ind1.sensor_count:] r2 = ind2[ind2.sensor_count:] nr1, nr2 = tools.cxTwoPoint(r1[:], r2[:]) nr1 = normalize_relays(nr1, all_relays) nr2 = normalize_relays(nr2, all_relays) ns1, ns2 = cxPartialyMatched(s1[:], s2[:]) # nr1, nr2 = cxPartialyMatched(r1[:], r2[:]) # assert len(set(nr1)) == len(set(r1)), '[%d %d]' % (len(set(nr1)), len(set(r1))) # assert len(set(nr2)) == len(set(r2)), '[%d %d]' % (len(set(nr2)), len(set(r2))) ni1 = ns1 + nr1 ni2 = ns2 + nr2 return ni1, ni2
def _mate(ind1, ind2): global _delta_trace raw = tools.cxTwoPoint(ind1, ind2) rlt = [_normalize(e) for e in raw] _delta_trace.append(['mate'] + rlt[0]) _delta_trace.append(['mate'] + rlt[1]) return rlt
def crossover(ind1, ind2, relays_num=None, all_relays=None): _ind1, _ind2 = ind1.clone(), ind2.clone() n1, n2 = tools.cxTwoPoint(_ind1, _ind2) n1 = normalize(n1, relays_num, all_relays) n2 = normalize(n2, relays_num, all_relays) return n1, n2
def point_crossover(ind1, ind2): """Apply one- or two-point crossover on the shapelet sets""" if len(ind1) > 1 and len(ind2) > 1: if np.random.random() < 0.5: ind1, ind2 = tools.cxOnePoint(list(ind1), list(ind2)) else: ind1, ind2 = tools.cxTwoPoint(list(ind1), list(ind2)) return ind1, ind2
def originalcx(ind1, ind2): probability = random.randint(1, 1000) if probability >= 10: return tools.cxTwoPoint(ind1, ind2) else: size = min(len(ind1), len(ind2)) for num in range(size): if ind1[num] == ind2[num]: ind1[num] = 0 ind2[num] = 1 return ind1, ind2
def cxCapacity(R, ind1, ind2): r = random.random() if r < R: # cross capacity for i in range(len(ind1)): r = random.random() if r < 0.5: ind1[0][i], ind2[0][i] = ind2[0][i], ind1[0][i] ind1[0] = randToTotalCapacity(ind1[0]) ind2[0] = randToTotalCapacity(ind2[0]) else: # cross position ind1[1], ind2[1] = tools.cxTwoPoint(ind1[1], ind2[1]) return ind1, ind2
def customXover(ind1, ind2): g_01, g_02 = tools.cxUniform([ind1[0]], [ind2[0]], CXPROB) g1, g2 = tools.cxTwoPoint(ind1[1:], ind2[1:]) ind1[0] = g_01[0] ind2[0] = g_02[0] for i in range(1, len(ind1)): ind1[i] = g1[i - 1] for i in range(1, len(ind2)): ind2[i] = g2[i - 1] return ind1, ind2
def cxCapacity(R, ind1, ind2): r = random.random() if r < R: # cross capacity for i in range(len(ind1)): r = random.random() if r < 0.5: ind1[0][i], ind2[0][i] = ind2[0][i], ind1[0][i] ind1[0] = randToTotalCapacity(ind1[0]) ind2[0] = randToTotalCapacity(ind2[0]) else: # cross position ind1[1], ind2[1] = tools.cxTwoPoint(ind1[1], ind2[1]) return ind1, ind2
def main(): # 杂交函数测试 ind1 = [1, 1, 1, 1, 1] ind2 = [0, 0, 0, 0, 0] print(ind1, ind2) """交换从任意位置开始并且到结尾的一段基因""" n1, n2 = tools.cxOnePoint(ind1, ind2) print(n1, n2) """交换连续的一段长度随机,位置随机的基因""" n3, n4 = tools.cxTwoPoint(ind1, ind2) print(n3, n4) """进行min(len(ind1), len(ind2))次杂交,每轮交换前产生一个随机数a,若a小于indpb则交换该轮次位置的两个基因,否则不执行交换""" n5, n6 = tools.cxUniform(ind1, ind2, indpb=0.5) print(n5, n6)
def mate(self, parent1, parent2): # Changing the shapes to 1d solut1, solut2 = self._solut_to_1d_shape(parent1.get_solut()), self._solut_to_1d_shape(parent2.get_solut()) # Two point crossover if np.random.random() < self.crossver_prob: solut1, solut2 = tools.cxTwoPoint(solut1, solut2) # Flipbit mutation solut1 = tools.mutFlipBit(solut1, self.mutation_prob) solut2 = tools.mutFlipBit(solut2, self.mutation_prob) if not self.should_validate: solut1, solut2 = self._solut_to_original_shape(solut1), self._solut_to_original_shape(solut2) else: solut1, solut2 = self._solut_to_validation_shape(solut1), self._solut_to_validation_shape(solut2) return solut1, solut2
def crossover(self, child1, child2): tools.cxTwoPoint(child1, child2) pass
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 blendImgs(img1, img2): tools.cxTwoPoint(img1, img2) img1.weight = img2.weight = (img1.weight + img2.weight) / 2
def call_mate(ind1, ind2): tools.cxTwoPoint(ind1.Chromossomes[0], ind2.Chromossomes[1])
def customXover(self, ind1, ind2, indpb): """ Parameters ---------- ind1 : Deap invidual parent 1 ind2 : Deap invidual parent 2 indpb : float Defined the probability that a semantically equal slices of genetic code are recombined Returns ------- ind1 : Deap invidual ind2 : Deap invidual Recombined Individuals Semantically equal slice are recombined together with indpb probability. If the slice is a single attribute cxUniform is used, CxTwoPoint deap crossover is used in the other case. """ #Q g_q1, g_q2 = tools.cxUniform([ind1[0]], [ind2[0]], indpb=indpb) #GED g_ged1, g_ged2 = tools.cxTwoPoint(ind1[1:7], ind2[1:7]) #Tau g_tau1, g_tau2 = tools.cxUniform([ind1[7]], [ind2[7]], indpb=indpb) #Test weight comp card #g_eta1,g_eta2 = tools.cxUniform([ind1[8]], [ind2[8]], indpb = indpb) #################### #Set if crossovered ind1[0] = g_q1[0] ind2[0] = g_q2[0] #two point crossover individuals are always modified. We edit this slice of genetic code only if condition is valid if random.random() < indpb: for i, (g1, g2) in enumerate(zip(g_ged1, g_ged2), start=1): ind1[i] = g1 ind2[i] = g2 # for i in range(1,7): # ind2[i]=g2 # ind1[7] = g_tau1[0] ind2[7] = g_tau2[0] #Test weight comp card # ind1[8]=g_eta1[0] # ind2[8]=g_eta2[0] #############à if self._problemName == 'GREC': g_add1, g_add2 = tools.cxTwoPoint(ind1[8:13], ind2[8:13]) #Weight eta #g_add1, g_add2 = tools.cxTwoPoint(ind1[9:14], ind2[9:14]) ################ #Same for ged attributes if random.random() < indpb: for i, (g1, g2) in enumerate(zip(g_add1, g_add2), start=8): #Weight eta #for i,(g1,g2) in enumerate(zip(g_add1,g_add2),start = 9): ########### ind1[i] = g1 ind2[i] = g2 return ind1, ind2
def gcd(a, b): if (b == 0): return a if (b > a): return gcd(b, a) a = a % b return gcd(b, a) import sys a = int(sys.argv[1]) b = int(sys.argv[2]) print(gcd(a, b)) from deap import tools print(tools.cxTwoPoint([1, 1, 1, 1, 1, 1], [2, 2, 2, 2, 2, 2]))
def crossover(self, child1, child2): tools.cxTwoPoint(child1, child2) pass
def mate(self, other): return tools.cxTwoPoint(self, other)
def __cross(self, pl1, pl2): crossed = tools.cxTwoPoint(pl1, pl2) self.__mutate_reapeats(crossed[0]) self.__mutate_reapeats(crossed[1]) return crossed
def crossOver(chrome1, chrome2, prob): if np.random.random() < prob: return tools.cxTwoPoint(chrome1, chrome2) return chrome1, chrome2
def tspd_crossover(ind1, ind2): tools.cxPartialyMatched(ind1[0], ind2[0]) tools.cxTwoPoint(ind1[1], ind2[1]) return ind1, ind2
import random from deap import tools random.seed(0) x = random.sample(range(10), 10) print(x) print(tools.mutShuffleIndexes(x, 1)) print(x) ind1 = random.sample(range(10), 10) 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):