def crossover(parent_one, parent_two): if randint(0, 100) > crossover_rate * 100: return (parent_one, parent_two) midpoint = randint(1, task_count - 1) offspring_one, offspring_two = Genome(False), Genome(False) offspring_one.genes = parent_one.genes[:midpoint] + parent_two.genes[ midpoint:] offspring_two.genes = parent_two.genes[:midpoint] + parent_one.genes[ midpoint:] offspring_one.update_duration() offspring_two.update_duration() return (offspring_one, offspring_two)
def crossover(self, genome1, genome2): #Once in a while genome2 is not set, pull request if you found why if type(genome2) == type(None): genome2 = randomGenome(self.genomes) if Config.crossoverVersion == 1: genome = Genome() for i in range(genome1.size()): # Crossover if random.random() <= Config.uniformRate: genome.setGene(i, genome1.getGene(i)) else: genome.setGene(i, genome2.getGene(i)) return genome #I know it's cryptic... if Config.crossoverVersion == 2: genome = Genome() genes1 = genome1.genes genes2 = genome2.genes if random.random() < 0.5: tmp = genes1 genes1 = genes2[:] genes2 = tmp[:] finalweights = genes1[:-genome1.network.numberOfBiases] biases1 = genes1[genome1.network.numberOfWeights:] biases2 = genes2[genome2.network.numberOfWeights:] cutLocation = int(math.floor(len(biases1) * random.random())) part1 = biases1[:cutLocation] part2 = biases2[cutLocation:] #Form the final genes part1.extend(part2) finalbiases = part1 finalweights.extend(finalbiases) genome.genes = finalweights[:] return genome