def run(self, iterations):
        """Runs the Differential Evolution algorithm.

        Args:
            iterations (int): Number of iterations to be made by the algorithm.
        """
        # print(f'Before:\n {self.population}\n')
        # self.best()
        # print(f'Best Genome before: {self.best_genome.array}, fitness={self.best_genome.fitness} ')

        mutator = Rand1MutationOperator(self.population, self.bounds, 0.2)
        mixer = ExponentialCrossoverOperator(self.minfun)
        replacer = ElitistReplacementOperator()

        for _ in range(iterations):
            candidate_population = Population(None, None, 0)
            for target in self.population.collection:
                # List with genomes who will be the donors
                mutant = mutator.apply(target)
                # Genome modified by replacing a few random positions
                candidate_genome = mixer.apply(target, mutant)

                candidate_population.add(candidate_genome)

            # Targets are replaced by candidates from the population if candidate has less fitness than target
            self.population = replacer.apply(self.population,
                                             candidate_population)
Ejemplo n.º 2
0
    def apply(self, current_population, candidate_population):
        """
        Each genome of 'current_population' is compared with the genome which is in the same position than
        'candidate_population' and the one with the best fitness of these two genomes is added to
        'result_population'.

        Args:
            current_population (Population): Object which contains a list of genomes.
            candidate_population (list): List which contains a list of genomes.

        Returns:
            result_population (Population) : Resulting population with the best fitness in each position.
        """
        result_population = Population(None, None, 0)
        for target, candidate in zip(current_population.collection, candidate_population.collection):
            result_population.add(candidate if candidate.fitness < target.fitness else target)

        return result_population