Beispiel #1
0
    def createNextGeneration(self, generation):
        if generation == None:
            nextGeneration = Generation(0)

            while nextGeneration.getSize() < self.noOfChromosomesPerGeneration:
                chromosome = Chromosome(nextGeneration.getNo(),
                                        nextGeneration.getSize())

                chromosome.seed()

                nextGeneration.addChromosome(chromosome)
        else:
            generation.sortAccordingToFitness()

            nextGeneration = Generation(generation.getNo() + 1)

            while nextGeneration.getSize() < self.noOfChromosomesPerGeneration:
                i1 = int(math.fabs(math.ceil(random.gauss(0, 0.1) * 199)))
                parent1 = generation.getChromosomeAt(i1)
                i2 = i1

                while i2 == i1:
                    i2 = int(math.fabs(math.ceil(random.gauss(0, 0.1) * 199)))

                parent2 = generation.getChromosomeAt(i2)

                children = parent1.mate(nextGeneration.getNo(),
                                        nextGeneration.getSize(), parent2)

                for child in children:
                    child.mutate()

                    nextGeneration.addChromosome(child)

        return nextGeneration