def newGeneration():

    global reproduction_count
    reproduction_count = 0
    global next_generation
    # if len(next_generation) > 1:
    # 	if len(next_generation) < 10:
    # 		neat.species_threshold -= 0.3
    # 	elif len(next_generation) >= 10:
    # 		neat.species_threshold +=0.3
    children = []
    calculateAverageFitness(next_generation)
    updateStagnationInformation()
    removeStagnantSpecies()
    calculateAverageFitness(next_generation)
    totalFitness = totalAverageFitness(next_generation)
    for x in next_generation:
        genelog.info("[Species : " + str(x.number) + " Organisms: " +
                     str(len(x.organisms)) + " ]")
        x.sort()
        if (len(x.organisms) > 10):
            x.removeUnfit()
        genelog.info("[Trim Step- Organisms Survived: " +
                     str(len(x.organisms)) + " ]")
        breedCount = int((x.repFitness / totalFitness) * gen_iterations) - 1
        for i in range(breedCount):
            if random.random() < crossover_rate and len(x.organisms) > 1:
                genelog.info("[ORGANISM]")
                xx = random.randrange(0, len(x.organisms))
                xy = random.randrange(0, len(x.organisms))
                while xx == xy:
                    xx = random.randrange(0, len(x.organisms))
                    xy = random.randrange(0, len(x.organisms))

                if x.organisms[xy].global_fitness > x.organisms[
                        xx].global_fitness:
                    temp = xx
                    xx = xy
                    xy = temp
                childGenome = Genome.crossover(x.organisms[xx].genome,
                                               x.organisms[xy].genome)
                reproduction_count += 1
                # apply random chance of further mutation
                childGenome.mutate()
                childGenome.mutate_topology()
                childOrganism = Agent(i_shape, o_shape, childGenome,
                                      agent_name)
                # TODO: random enable disable genes
                children.append(childOrganism)
            else:
                xx = random.randrange(0, len(x.organisms))
                childGenome = copy.deepcopy(x.organisms[xx].genome)
                childGenome.mutate()
                childOrganism = Agent(i_shape, o_shape, childGenome,
                                      agent_name)
                children.append(childOrganism)
                reproduction_count += 1

    if random.random() < mating_rate and len(next_generation) > 1:
        print("Interspecies Breeding")
        xx = x.organisms[0]
        xy = next_generation[random.randrange(
            0, len(next_generation))].organisms[0]
        while xx == xy:
            xy = next_generation[random.randrange(
                0, len(next_generation))].organisms[0]
        childGenome = Genome.crossover(xx.genome, xy.genome)
        reproduction_count += 1
        childGenome.mutate()
        childGenome.mutate_topology()
        childOrganism = Agent(i_shape, o_shape, childGenome, agent_name)
        children.append(childOrganism)

    for species in next_generation:
        species.reduce()

    for organism in children:
        speciate(organism)
    global exceeded
Exemple #2
0
 def test(genome, node_inno, con_inno):
     genome[2] = Genome.crossover(genome[0], genome[1], 0.10)
     print(genome[2])
     pass