def crossover_roulette(self,parent1,parent2,previous_layer,upper_layer): # cruzamiento CROSSOVER_POINT = random.randint(1,Chromosome.L_CHROMOSOME - 1) if parent1 < self.n_individuals: # se checa de que poblacion viene el parent escogido ya que la ruleta regresa un numero de 0 a 2*n_individuals - 1 newChromosome1=self.population[parent1].chromosome[0:CROSSOVER_POINT] elif parent1 >= self.n_individuals: newChromosome1=previous_layer.population[parent1%previous_layer.n_individuals].chromosome[0:CROSSOVER_POINT] if parent2 < self.n_individuals: newChromosome1 = np.append(newChromosome1, self.population[parent2].chromosome[CROSSOVER_POINT:Chromosome.L_CHROMOSOME], 0) newChromosome2=self.population[parent2].chromosome[0:CROSSOVER_POINT] elif parent2 >= self.n_individuals: parent2 %= previous_layer.n_individuals newChromosome1 = np.append(newChromosome1, previous_layer.population[parent2].chromosome[CROSSOVER_POINT:Chromosome.L_CHROMOSOME], 0) newChromosome2=previous_layer.population[parent2].chromosome[0:CROSSOVER_POINT] if parent1 < self.n_individuals: newChromosome2 = np.append(newChromosome2, self.population[parent1].chromosome[CROSSOVER_POINT:Chromosome.L_CHROMOSOME], 0) elif parent1 >= self.n_individuals: parent1 %= previous_layer.n_individuals newChromosome2 = np.append(newChromosome2, previous_layer.population[parent1].chromosome[CROSSOVER_POINT:Chromosome.L_CHROMOSOME], 0) newAge = self.population[parent1].age if self.population[parent1].age >= self.population[parent2].age else self.population[parent2].age descendant1 = Chromosome.constructor_crossover(newAge,newChromosome1) descendant2 = Chromosome.constructor_crossover(newAge,newChromosome2) descendant1.incrementAge() descendant2.incrementAge() return descendant1,descendant2
def reproduce(self): parents = self.tourney() newChromosome1,newChromosome2 = self.crossover(parents) newAge = parents[0].age if parents[0].age >= parents[1].age else parents[1].age descendant1 = Chromosome.constructor_crossover(newAge+1,newChromosome1) descendant2 = Chromosome.constructor_crossover(newAge+1,newChromosome2) return descendant1,descendant2
def reproduce(self): parents = self.tourney() newChromosome1, newChromosome2 = self.crossover(parents) newAge = parents[ 0].age if parents[0].age >= parents[1].age else parents[1].age descendant1 = Chromosome.constructor_crossover(newAge + 1, newChromosome1) descendant2 = Chromosome.constructor_crossover(newAge + 1, newChromosome2) return descendant1, descendant2
def crossover_roulette(self, parent1, parent2, previous_layer, upper_layer): # cruzamiento CROSSOVER_POINT = random.randint(1, Chromosome.L_CHROMOSOME - 1) if parent1 < self.n_individuals: # se checa de que poblacion viene el parent escogido ya que la ruleta regresa un numero de 0 a 2*n_individuals - 1 newChromosome1 = self.population[parent1].chromosome[ 0:CROSSOVER_POINT] elif parent1 >= self.n_individuals: newChromosome1 = previous_layer.population[ parent1 % previous_layer.n_individuals].chromosome[0:CROSSOVER_POINT] if parent2 < self.n_individuals: newChromosome1 = np.append( newChromosome1, self.population[parent2]. chromosome[CROSSOVER_POINT:Chromosome.L_CHROMOSOME], 0) newChromosome2 = self.population[parent2].chromosome[ 0:CROSSOVER_POINT] elif parent2 >= self.n_individuals: parent2 %= previous_layer.n_individuals newChromosome1 = np.append( newChromosome1, previous_layer.population[parent2]. chromosome[CROSSOVER_POINT:Chromosome.L_CHROMOSOME], 0) newChromosome2 = previous_layer.population[parent2].chromosome[ 0:CROSSOVER_POINT] if parent1 < self.n_individuals: newChromosome2 = np.append( newChromosome2, self.population[parent1]. chromosome[CROSSOVER_POINT:Chromosome.L_CHROMOSOME], 0) elif parent1 >= self.n_individuals: parent1 %= previous_layer.n_individuals newChromosome2 = np.append( newChromosome2, previous_layer.population[parent1]. chromosome[CROSSOVER_POINT:Chromosome.L_CHROMOSOME], 0) newAge = self.population[parent1].age if self.population[ parent1].age >= self.population[parent2].age else self.population[ parent2].age descendant1 = Chromosome.constructor_crossover(newAge, newChromosome1) descendant2 = Chromosome.constructor_crossover(newAge, newChromosome2) descendant1.incrementAge() descendant2.incrementAge() return descendant1, descendant2
def generate_population(self): self.population = [Chromosome] * self.n_individuals for i in xrange(0, self.n_individuals): self.population[i] = Chromosome() self.populated = 1