Repository: godatadriven/evol

Issue:
Not sure if elitism works
I put elitist to True in my mutation step, but it did not work when looking at the source code it looks like it will not work.

`
        elite_fitness = self.current_best if elitist else None
        for individual in self.individuals:
            if elite_fitness is None or individual.fitness != elite_fitness:
                individual.mutate(mutate_function, probability=probability, **kwargs)
        return self
`

elite_fitness is set as an 'individual' object, then in the if statement it is compared to the floating point fitness instead of the actual object. Therefore individual.fitness will never equal elite_fitness and the current_best is not skipped over like it should be.

Discussion:

Patch:
diff --git a/evol/__init__.py b/evol/__init__.py
index 6746f75..9f44276 100644
--- a/evol/__init__.py
+++ b/evol/__init__.py
@@ -119,4 +119,4 @@ from .population import Population, ContestPopulation
 from .evolution import Evolution
 from .logger import BaseLogger
 
-__version__ = "0.5.2"
+__version__ = "0.5.3"
diff --git a/evol/population.py b/evol/population.py
index 9a7aedd..8e989f5 100644
--- a/evol/population.py
+++ b/evol/population.py
@@ -207,7 +207,7 @@ class BasePopulation(metaclass=ABCMeta):
         :param kwargs: Arguments to pass to the mutation function.
         :return: self
         """
-        elite_fitness = self.current_best if elitist else None
+        elite_fitness: Optional[float] = self.current_best.fitness if elitist else None
         for individual in self.individuals:
             if elite_fitness is None or individual.fitness != elite_fitness:
                 individual.mutate(mutate_function, probability=probability, **kwargs)


Test patch:
diff --git a/tests/test_population.py b/tests/test_population.py
index 0e441b9..a9b772c 100644
--- a/tests/test_population.py
+++ b/tests/test_population.py
@@ -244,7 +244,7 @@ class TestPopulationMutate:
     def test_mutate_elitist(self):
         pop = Population([1, 1, 3], eval_function=lambda x: x).evaluate().mutate(lambda x: x + 1, elitist=True)
         for chromosome in pop.chromosomes:
-            assert chromosome > 1
+            assert 1 < chromosome <= 3
         assert len(pop) == 3