コード例 #1
0
    def constructNextPopulation(self, currentFitnesses, currentPopulation):
        newPopulation = []
        while len(newPopulation) < self.populationSize:
            candidate = self.selectCandidateBasedOnFitness(currentFitnesses)

            newCandidate = CandidateSolution(self.vocabulary, self.chromosomeSize)
            newCandidate.setChromosome(candidate.getChromosome())

            if random.random() < self.mutationProbability:
                newCandidate.pointMutation(self.mutationProbability)
                newPopulation.append(newCandidate)
            elif random.random() < self.crossoverProbability:
                # One-point crossover
                # ( offspring1, offspring2 ) = self.recombineTwoCandidates_OnePivot( newCandidate, random.choice( currentPopulation ) )
                # newPopulation.append( offspring1 )
                # newPopulation.append( offspring2 )

                # Two-point crossover
                (offspring1, offspring2, offspring3) = self.recombineTwoCandidates_TwoPivots(
                    newCandidate, random.choice(currentPopulation)
                )
                newPopulation.append(offspring1)
                newPopulation.append(offspring2)
                newPopulation.append(offspring3)
            elif random.random() < self.reproduceProbability:
                newPopulation.append(candidate)

        return newPopulation