eckity.algorithms.simple_evolution

This module implements the SimpleEvolution class.

  1"""
  2This module implements the SimpleEvolution class.
  3"""
  4
  5from overrides import overrides
  6
  7from eckity.algorithms.algorithm import Algorithm
  8from eckity.breeders.simple_breeder import SimpleBreeder
  9from eckity.evaluators import SimplePopulationEvaluator
 10from eckity.random import RNG
 11from eckity.termination_checkers import ThresholdFromTargetTerminationChecker
 12
 13
 14class SimpleEvolution(Algorithm):
 15    """
 16    Simple case evolutionary algorithm.
 17
 18    Basic evolutionary algorithm that contains one subpopulation.
 19    Does not include and is not meant for multi-objective, co-evolution etc.
 20    Such algorithms should be implemented in a new Algorithm subclass.
 21
 22    Parameters
 23    ----------
 24    population: Population
 25        The population to be evolved.
 26        Contains only one subpopulation in simple case.
 27
 28
 29    statistics: Union[Statistics, List[Statistics]], default=None
 30        Provide multiple statistics on the population during the evolution.
 31
 32    breeder: SimpleBreeder
 33        Responsible for applying selection and operators on individuals
 34        in each generation. Applies on one subpopulation in simple case.
 35
 36    population_evaluator: SimplePopulationEvaluator,
 37                          default=SimplePopulationEvaluator instance
 38            Responsible for evaluating each individual's fitness concurrently
 39            and returns the best individual of each subpopulation
 40            (returns a single individual in simple case).
 41
 42    max_generation: int, default=1000
 43            Maximal number of generations to run the evolutionary process.
 44            Note the evolution could end before reaching max_generation,
 45            depending on the termination checker.
 46
 47    events: dict(str, dict(object, function)), default=None
 48            Dictionary of events, where each event holds
 49            a dictionary of (subscriber, callback method).
 50
 51    event_names: list of strings, default=None
 52            Names of events to publish during the evolution.
 53
 54    termination_checker: TerminationChecker
 55            Checks if the evolution should perform early termination.
 56
 57    max_workers: int, default=None
 58            Maximal number of worker nodes for the Executor object that
 59            evaluates the fitness of the individuals.
 60
 61    random_generator: RNG
 62            Random number generator
 63
 64    random_seed: int, default=current system time
 65            Initial random seed for deterministic experiment.
 66
 67    generation_seed: int, default=None
 68            Current generation seed.
 69            Useful for resuming a previously paused experiment.
 70
 71    best_of_run_: Individual, default=None
 72            The individual with the best fitness in the entire evolution.
 73
 74    best_of_gen: Individual, default=None
 75            The individual that has the best fitness in the current generation.
 76
 77    worst_of_gen: Individual, default=None
 78            The individual that has the worst fitness in current generation.
 79
 80    generation_num: int, default=0
 81            Current generation number
 82    """
 83
 84    def __init__(
 85        self,
 86        population,
 87        statistics=None,
 88        breeder: SimpleBreeder = SimpleBreeder(),
 89        population_evaluator: SimplePopulationEvaluator = None,
 90        max_generation=500,
 91        events=None,
 92        event_names=None,
 93        termination_checker=None,
 94        executor="thread",
 95        max_workers=None,
 96        random_generator: RNG = RNG(),
 97        random_seed=None,
 98        generation_seed=None,
 99        best_of_run_=None,
100        best_of_gen=None,
101        worst_of_gen=None,
102        generation_num=0,
103    ):
104
105        if event_names is None:
106            _event_names = [
107                "before_eval",
108                "after_eval",
109                "before_breeding",
110                "after_breeding",
111            ]
112        else:
113            _event_names = event_names
114
115        if statistics is None:
116            statistics = []
117
118        if breeder is None:
119            breeder = SimpleBreeder()
120        
121        if population_evaluator is None:
122            population_evaluator = SimplePopulationEvaluator()
123
124        if not isinstance(breeder, SimpleBreeder):
125            raise ValueError(
126                f"Expected SimpleBreeder, got {type(breeder)}."
127            )
128        
129        if not isinstance(population_evaluator, SimplePopulationEvaluator):
130            raise ValueError(
131                "Expected SimplePopulationEvaluator, "
132                f"got {type(population_evaluator)}."
133            )
134
135        super().__init__(
136            population,
137            statistics=statistics,
138            breeder=breeder,
139            population_evaluator=population_evaluator,
140            events=events,
141            event_names=_event_names,
142            executor=executor,
143            max_workers=max_workers,
144            random_generator=random_generator,
145            random_seed=random_seed,
146            generation_seed=generation_seed,
147            termination_checker=termination_checker,
148            generation_num=generation_num,
149        )
150
151        self.termination_checker = termination_checker
152        self.best_of_run_ = best_of_run_
153        self.best_of_gen = best_of_gen
154        self.worst_of_gen = worst_of_gen
155        self.max_generation = max_generation
156
157        self.final_generation_ = None
158
159    def initialize(self):
160        """
161        Initialize the evolutionary algorithm
162
163        Register statistics to `after_generation` event
164        """
165        super().initialize()
166        for stat in self.statistics:
167            self.register("after_generation", stat.write_statistics)
168
169    @overrides
170    def generation_iteration(self, gen: int) -> bool:
171        """
172        Performs one iteration of the evolutionary run,
173        for the current generation
174
175        Parameters
176        ----------
177        gen:
178                current generation number (for example, generation #100)
179
180        Returns
181        -------
182        None.
183        """
184
185        # breed population
186        self.breeder.breed(self.population)
187
188        # Evaluate the entire population and get the best individual
189        self.best_of_gen = self.population_evaluator.act(self.population)
190
191        if self.best_of_gen.better_than(self.best_of_run_):
192            self.best_of_run_ = self.best_of_gen
193
194        self.worst_of_gen = self.population.sub_populations[
195            0
196        ].get_worst_individual()
197
198    def execute(self, **kwargs):
199        """
200        Compute output using best evolved individual.
201        Use `execute` in a non-sklearn setting.
202        Input keyword arguments that set variable values.
203        For example if `terminal_set=['x', 'y', 1, -1]`
204        then call `execute(x=..., y=...)`.
205
206        Parameters
207        ----------
208        **kwargs : keyword arguments
209                The input to the program (tree).
210
211        Returns
212        -------
213        object
214                Output as computed by the best individual of the evolution.
215
216        """
217        return self.best_of_run_.execute(**kwargs)
218
219    def finish(self):
220        """
221        Finish the evolutionary run by showing the best individual
222        and printing the best fitness
223        """
224        super().finish()
225        self.best_of_run_.show()
226
227    def get_individual_evaluator(self):
228        return self.population.sub_populations[0].evaluator
229
230    def get_average_fitness(
231        self,
232    ):  # TODO move this function to statistics
233        return self.population.get_average_fitness()
234
235    def event_name_to_data(self, event_name):
236        if event_name == "init":
237            return {
238                "population": self.population,
239                "statistics": self.statistics,
240                "breeder": self.breeder,
241                "termination_checker": self.termination_checker,
242                "max_generation": self.max_generation,
243                "events": self.events,
244                "max_workers": self.max_workers,
245                "generation_num": self.generation_num,
246            }
247
248        # default case
249        return {
250            "population": self.population,
251            "best_of_run_": self.best_of_run_,
252            "best_of_gen": self.best_of_gen,
253            "generation_num": self.generation_num,
254        }
class SimpleEvolution(eckity.algorithms.algorithm.Algorithm):
 15class SimpleEvolution(Algorithm):
 16    """
 17    Simple case evolutionary algorithm.
 18
 19    Basic evolutionary algorithm that contains one subpopulation.
 20    Does not include and is not meant for multi-objective, co-evolution etc.
 21    Such algorithms should be implemented in a new Algorithm subclass.
 22
 23    Parameters
 24    ----------
 25    population: Population
 26        The population to be evolved.
 27        Contains only one subpopulation in simple case.
 28
 29
 30    statistics: Union[Statistics, List[Statistics]], default=None
 31        Provide multiple statistics on the population during the evolution.
 32
 33    breeder: SimpleBreeder
 34        Responsible for applying selection and operators on individuals
 35        in each generation. Applies on one subpopulation in simple case.
 36
 37    population_evaluator: SimplePopulationEvaluator,
 38                          default=SimplePopulationEvaluator instance
 39            Responsible for evaluating each individual's fitness concurrently
 40            and returns the best individual of each subpopulation
 41            (returns a single individual in simple case).
 42
 43    max_generation: int, default=1000
 44            Maximal number of generations to run the evolutionary process.
 45            Note the evolution could end before reaching max_generation,
 46            depending on the termination checker.
 47
 48    events: dict(str, dict(object, function)), default=None
 49            Dictionary of events, where each event holds
 50            a dictionary of (subscriber, callback method).
 51
 52    event_names: list of strings, default=None
 53            Names of events to publish during the evolution.
 54
 55    termination_checker: TerminationChecker
 56            Checks if the evolution should perform early termination.
 57
 58    max_workers: int, default=None
 59            Maximal number of worker nodes for the Executor object that
 60            evaluates the fitness of the individuals.
 61
 62    random_generator: RNG
 63            Random number generator
 64
 65    random_seed: int, default=current system time
 66            Initial random seed for deterministic experiment.
 67
 68    generation_seed: int, default=None
 69            Current generation seed.
 70            Useful for resuming a previously paused experiment.
 71
 72    best_of_run_: Individual, default=None
 73            The individual with the best fitness in the entire evolution.
 74
 75    best_of_gen: Individual, default=None
 76            The individual that has the best fitness in the current generation.
 77
 78    worst_of_gen: Individual, default=None
 79            The individual that has the worst fitness in current generation.
 80
 81    generation_num: int, default=0
 82            Current generation number
 83    """
 84
 85    def __init__(
 86        self,
 87        population,
 88        statistics=None,
 89        breeder: SimpleBreeder = SimpleBreeder(),
 90        population_evaluator: SimplePopulationEvaluator = None,
 91        max_generation=500,
 92        events=None,
 93        event_names=None,
 94        termination_checker=None,
 95        executor="thread",
 96        max_workers=None,
 97        random_generator: RNG = RNG(),
 98        random_seed=None,
 99        generation_seed=None,
100        best_of_run_=None,
101        best_of_gen=None,
102        worst_of_gen=None,
103        generation_num=0,
104    ):
105
106        if event_names is None:
107            _event_names = [
108                "before_eval",
109                "after_eval",
110                "before_breeding",
111                "after_breeding",
112            ]
113        else:
114            _event_names = event_names
115
116        if statistics is None:
117            statistics = []
118
119        if breeder is None:
120            breeder = SimpleBreeder()
121        
122        if population_evaluator is None:
123            population_evaluator = SimplePopulationEvaluator()
124
125        if not isinstance(breeder, SimpleBreeder):
126            raise ValueError(
127                f"Expected SimpleBreeder, got {type(breeder)}."
128            )
129        
130        if not isinstance(population_evaluator, SimplePopulationEvaluator):
131            raise ValueError(
132                "Expected SimplePopulationEvaluator, "
133                f"got {type(population_evaluator)}."
134            )
135
136        super().__init__(
137            population,
138            statistics=statistics,
139            breeder=breeder,
140            population_evaluator=population_evaluator,
141            events=events,
142            event_names=_event_names,
143            executor=executor,
144            max_workers=max_workers,
145            random_generator=random_generator,
146            random_seed=random_seed,
147            generation_seed=generation_seed,
148            termination_checker=termination_checker,
149            generation_num=generation_num,
150        )
151
152        self.termination_checker = termination_checker
153        self.best_of_run_ = best_of_run_
154        self.best_of_gen = best_of_gen
155        self.worst_of_gen = worst_of_gen
156        self.max_generation = max_generation
157
158        self.final_generation_ = None
159
160    def initialize(self):
161        """
162        Initialize the evolutionary algorithm
163
164        Register statistics to `after_generation` event
165        """
166        super().initialize()
167        for stat in self.statistics:
168            self.register("after_generation", stat.write_statistics)
169
170    @overrides
171    def generation_iteration(self, gen: int) -> bool:
172        """
173        Performs one iteration of the evolutionary run,
174        for the current generation
175
176        Parameters
177        ----------
178        gen:
179                current generation number (for example, generation #100)
180
181        Returns
182        -------
183        None.
184        """
185
186        # breed population
187        self.breeder.breed(self.population)
188
189        # Evaluate the entire population and get the best individual
190        self.best_of_gen = self.population_evaluator.act(self.population)
191
192        if self.best_of_gen.better_than(self.best_of_run_):
193            self.best_of_run_ = self.best_of_gen
194
195        self.worst_of_gen = self.population.sub_populations[
196            0
197        ].get_worst_individual()
198
199    def execute(self, **kwargs):
200        """
201        Compute output using best evolved individual.
202        Use `execute` in a non-sklearn setting.
203        Input keyword arguments that set variable values.
204        For example if `terminal_set=['x', 'y', 1, -1]`
205        then call `execute(x=..., y=...)`.
206
207        Parameters
208        ----------
209        **kwargs : keyword arguments
210                The input to the program (tree).
211
212        Returns
213        -------
214        object
215                Output as computed by the best individual of the evolution.
216
217        """
218        return self.best_of_run_.execute(**kwargs)
219
220    def finish(self):
221        """
222        Finish the evolutionary run by showing the best individual
223        and printing the best fitness
224        """
225        super().finish()
226        self.best_of_run_.show()
227
228    def get_individual_evaluator(self):
229        return self.population.sub_populations[0].evaluator
230
231    def get_average_fitness(
232        self,
233    ):  # TODO move this function to statistics
234        return self.population.get_average_fitness()
235
236    def event_name_to_data(self, event_name):
237        if event_name == "init":
238            return {
239                "population": self.population,
240                "statistics": self.statistics,
241                "breeder": self.breeder,
242                "termination_checker": self.termination_checker,
243                "max_generation": self.max_generation,
244                "events": self.events,
245                "max_workers": self.max_workers,
246                "generation_num": self.generation_num,
247            }
248
249        # default case
250        return {
251            "population": self.population,
252            "best_of_run_": self.best_of_run_,
253            "best_of_gen": self.best_of_gen,
254            "generation_num": self.generation_num,
255        }

Simple case evolutionary algorithm.

Basic evolutionary algorithm that contains one subpopulation. Does not include and is not meant for multi-objective, co-evolution etc. Such algorithms should be implemented in a new Algorithm subclass.

Parameters
  • population (Population): The population to be evolved. Contains only one subpopulation in simple case.
  • statistics (Union[Statistics, List[Statistics]], default=None): Provide multiple statistics on the population during the evolution.
  • breeder (SimpleBreeder): Responsible for applying selection and operators on individuals in each generation. Applies on one subpopulation in simple case.
  • population_evaluator (SimplePopulationEvaluator,): default=SimplePopulationEvaluator instance Responsible for evaluating each individual's fitness concurrently and returns the best individual of each subpopulation (returns a single individual in simple case).
  • max_generation (int, default=1000): Maximal number of generations to run the evolutionary process. Note the evolution could end before reaching max_generation, depending on the termination checker.
  • events (dict(str, dict(object, function)), default=None): Dictionary of events, where each event holds a dictionary of (subscriber, callback method).
  • event_names (list of strings, default=None): Names of events to publish during the evolution.
  • termination_checker (TerminationChecker): Checks if the evolution should perform early termination.
  • max_workers (int, default=None): Maximal number of worker nodes for the Executor object that evaluates the fitness of the individuals.
  • random_generator (RNG): Random number generator
  • random_seed (int, default=current system time): Initial random seed for deterministic experiment.
  • generation_seed (int, default=None): Current generation seed. Useful for resuming a previously paused experiment.
  • best_of_run_ (Individual, default=None): The individual with the best fitness in the entire evolution.
  • best_of_gen (Individual, default=None): The individual that has the best fitness in the current generation.
  • worst_of_gen (Individual, default=None): The individual that has the worst fitness in current generation.
  • generation_num (int, default=0): Current generation number
SimpleEvolution( population, statistics=None, breeder: eckity.breeders.simple_breeder.SimpleBreeder = <eckity.breeders.simple_breeder.SimpleBreeder object>, population_evaluator: eckity.evaluators.simple_population_evaluator.SimplePopulationEvaluator = None, max_generation=500, events=None, event_names=None, termination_checker=None, executor='thread', max_workers=None, random_generator: eckity.random.rng.RNG = <eckity.random.rng.RNG object>, random_seed=None, generation_seed=None, best_of_run_=None, best_of_gen=None, worst_of_gen=None, generation_num=0)
 85    def __init__(
 86        self,
 87        population,
 88        statistics=None,
 89        breeder: SimpleBreeder = SimpleBreeder(),
 90        population_evaluator: SimplePopulationEvaluator = None,
 91        max_generation=500,
 92        events=None,
 93        event_names=None,
 94        termination_checker=None,
 95        executor="thread",
 96        max_workers=None,
 97        random_generator: RNG = RNG(),
 98        random_seed=None,
 99        generation_seed=None,
100        best_of_run_=None,
101        best_of_gen=None,
102        worst_of_gen=None,
103        generation_num=0,
104    ):
105
106        if event_names is None:
107            _event_names = [
108                "before_eval",
109                "after_eval",
110                "before_breeding",
111                "after_breeding",
112            ]
113        else:
114            _event_names = event_names
115
116        if statistics is None:
117            statistics = []
118
119        if breeder is None:
120            breeder = SimpleBreeder()
121        
122        if population_evaluator is None:
123            population_evaluator = SimplePopulationEvaluator()
124
125        if not isinstance(breeder, SimpleBreeder):
126            raise ValueError(
127                f"Expected SimpleBreeder, got {type(breeder)}."
128            )
129        
130        if not isinstance(population_evaluator, SimplePopulationEvaluator):
131            raise ValueError(
132                "Expected SimplePopulationEvaluator, "
133                f"got {type(population_evaluator)}."
134            )
135
136        super().__init__(
137            population,
138            statistics=statistics,
139            breeder=breeder,
140            population_evaluator=population_evaluator,
141            events=events,
142            event_names=_event_names,
143            executor=executor,
144            max_workers=max_workers,
145            random_generator=random_generator,
146            random_seed=random_seed,
147            generation_seed=generation_seed,
148            termination_checker=termination_checker,
149            generation_num=generation_num,
150        )
151
152        self.termination_checker = termination_checker
153        self.best_of_run_ = best_of_run_
154        self.best_of_gen = best_of_gen
155        self.worst_of_gen = worst_of_gen
156        self.max_generation = max_generation
157
158        self.final_generation_ = None
termination_checker
best_of_run_
best_of_gen
worst_of_gen
max_generation
final_generation_
def initialize(self):
160    def initialize(self):
161        """
162        Initialize the evolutionary algorithm
163
164        Register statistics to `after_generation` event
165        """
166        super().initialize()
167        for stat in self.statistics:
168            self.register("after_generation", stat.write_statistics)

Initialize the evolutionary algorithm

Register statistics to after_generation event

@overrides
def generation_iteration(self, gen: int) -> bool:
170    @overrides
171    def generation_iteration(self, gen: int) -> bool:
172        """
173        Performs one iteration of the evolutionary run,
174        for the current generation
175
176        Parameters
177        ----------
178        gen:
179                current generation number (for example, generation #100)
180
181        Returns
182        -------
183        None.
184        """
185
186        # breed population
187        self.breeder.breed(self.population)
188
189        # Evaluate the entire population and get the best individual
190        self.best_of_gen = self.population_evaluator.act(self.population)
191
192        if self.best_of_gen.better_than(self.best_of_run_):
193            self.best_of_run_ = self.best_of_gen
194
195        self.worst_of_gen = self.population.sub_populations[
196            0
197        ].get_worst_individual()

Performs one iteration of the evolutionary run, for the current generation

Parameters
  • gen:: current generation number (for example, generation #100)
Returns
  • None.
def execute(self, **kwargs):
199    def execute(self, **kwargs):
200        """
201        Compute output using best evolved individual.
202        Use `execute` in a non-sklearn setting.
203        Input keyword arguments that set variable values.
204        For example if `terminal_set=['x', 'y', 1, -1]`
205        then call `execute(x=..., y=...)`.
206
207        Parameters
208        ----------
209        **kwargs : keyword arguments
210                The input to the program (tree).
211
212        Returns
213        -------
214        object
215                Output as computed by the best individual of the evolution.
216
217        """
218        return self.best_of_run_.execute(**kwargs)

Compute output using best evolved individual. Use execute in a non-sklearn setting. Input keyword arguments that set variable values. For example if terminal_set=['x', 'y', 1, -1] then call execute(x=..., y=...).

Parameters
  • **kwargs (keyword arguments): The input to the program (tree).
Returns
  • object: Output as computed by the best individual of the evolution.
def finish(self):
220    def finish(self):
221        """
222        Finish the evolutionary run by showing the best individual
223        and printing the best fitness
224        """
225        super().finish()
226        self.best_of_run_.show()

Finish the evolutionary run by showing the best individual and printing the best fitness

def get_individual_evaluator(self):
228    def get_individual_evaluator(self):
229        return self.population.sub_populations[0].evaluator
def get_average_fitness(self):
231    def get_average_fitness(
232        self,
233    ):  # TODO move this function to statistics
234        return self.population.get_average_fitness()
def event_name_to_data(self, event_name):
236    def event_name_to_data(self, event_name):
237        if event_name == "init":
238            return {
239                "population": self.population,
240                "statistics": self.statistics,
241                "breeder": self.breeder,
242                "termination_checker": self.termination_checker,
243                "max_generation": self.max_generation,
244                "events": self.events,
245                "max_workers": self.max_workers,
246                "generation_num": self.generation_num,
247            }
248
249        # default case
250        return {
251            "population": self.population,
252            "best_of_run_": self.best_of_run_,
253            "best_of_gen": self.best_of_gen,
254            "generation_num": self.generation_num,
255        }

Convert event name to relevant data of the Algorithm for the event

Parameters
  • event_name (string): name of the event that is happening
Returns
  • Dict[str, object]: Algorithm data regarding the given event