eckity.multi_objective_evolution.nsga2_evolution
1import logging 2from time import time 3 4from overrides import overrides 5 6from eckity.algorithms.algorithm import Algorithm 7from eckity.breeders.simple_breeder import SimpleBreeder 8from eckity.evaluators import SimplePopulationEvaluator 9from eckity.multi_objective_evolution.nsga2_front_sorting import ( 10 NSGA2FrontSorting, 11) 12from eckity.random import RNG 13from eckity.termination_checkers import ThresholdFromTargetTerminationChecker 14 15logger = logging.getLogger(__name__) 16 17 18class NSGA2Evolution(Algorithm): 19 def __init__( 20 self, 21 population, 22 statistics=None, 23 breeder=SimpleBreeder(), 24 population_evaluator=SimplePopulationEvaluator(), 25 NSGA2FrontSorting=NSGA2FrontSorting(), 26 max_generation=500, 27 events=None, 28 event_names=None, 29 termination_checker=ThresholdFromTargetTerminationChecker(threshold=0), 30 executor="thread", 31 max_workers=None, 32 random_generator=RNG(), 33 random_seed=None, 34 generation_seed=None, 35 best_of_run_=None, 36 best_of_run_evaluator=None, 37 best_of_gen=None, 38 worst_of_gen=None, 39 generation_num=0, 40 ): 41 42 if event_names is None: 43 _event_names = [ 44 "before_eval", 45 "after_eval", 46 "before_breeding", 47 "after_breeding", 48 ] 49 else: 50 _event_names = event_names 51 52 if statistics is None: 53 statistics = [] 54 55 super().__init__( 56 population, 57 statistics=statistics, 58 breeder=breeder, 59 population_evaluator=population_evaluator, 60 events=events, 61 event_names=_event_names, 62 executor=executor, 63 max_workers=max_workers, 64 random_generator=random_generator, 65 random_seed=random_seed, 66 generation_seed=generation_seed, 67 termination_checker=termination_checker, 68 generation_num=generation_num, 69 ) 70 71 self.termination_checker = termination_checker 72 self.best_of_run_ = best_of_run_ 73 self.best_of_run_evaluator = best_of_run_evaluator 74 self.best_of_gen = best_of_gen 75 self.worst_of_gen = worst_of_gen 76 self.max_generation = max_generation 77 78 self.final_generation_ = None 79 self.NSGA2FrontSorting = NSGA2FrontSorting 80 81 # 82 @overrides 83 def generation_iteration(self, gen: int) -> bool: 84 """ 85 Performs one iteration of the evolutionary run, for the current generation 86 87 Parameters 88 ---------- 89 gen: 90 current generation number (for example, generation #100) 91 92 Returns 93 ------- 94 None. 95 """ 96 97 # breed population 98 self.breeder.breed(self.population) 99 # Evaluate the entire population and get the best individual 100 101 self.population_evaluator.act(self.population) 102 103 self.NSGA2FrontSorting.select_for_population(self.population) 104 self.best_of_gen = self._get_pareto_fronts() 105 106 def _get_pareto_fronts(self): 107 """ 108 Returns: the pareto front for each sub_population 109 ------- 110 """ 111 pareto_fronts = [] 112 for sub_pop in self.population.sub_populations: 113 pareto_fronts.append( 114 [ 115 ind 116 for ind in sub_pop.individuals 117 if ind.fitness.front_rank == 1 118 ] 119 ) 120 return pareto_fronts 121 122 def initialize(self): 123 """ 124 Initialize the evolutionary algorithm 125 126 Register statistics to `after_generation` event 127 """ 128 super().initialize() 129 for stat in self.statistics: 130 self.register("after_generation", stat.write_statistics) 131 132 def execute(self, **kwargs): 133 """ 134 Compute output using best evolved individual. 135 Use `execute` in a non-sklearn setting. 136 Input keyword arguments that set variable values. 137 For example if `terminal_set=['x', 'y', 1, -1]` then call `execute(x=..., y=...)`. 138 139 Parameters 140 ---------- 141 **kwargs : keyword arguments 142 The input to the program (tree). 143 144 Returns 145 ------- 146 object 147 Output as computed by the best individual of the evolutionary run. 148 149 """ 150 return self.best_of_run_.execute(**kwargs) 151 152 @overrides 153 def finish(self): 154 """ 155 Finish the evolutionary run by showing the best individual and printing the best fitness 156 """ 157 self.best_of_run_.show() 158 logger.info(self.best_of_run_.get_pure_fitness()) 159 160 def event_name_to_data(self, event_name): 161 if event_name == "init": 162 return { 163 "population": self.population, 164 "statistics": self.statistics, 165 "breeder": self.breeder, 166 "termination_checker": self.termination_checker, 167 "max_generation": self.max_generation, 168 "events": self.events, 169 "max_workers": self.max_workers, 170 } 171 172 # default case 173 return { 174 "population": self.population, 175 "best_of_run_": self.best_of_run_, 176 "best_of_gen": self.best_of_gen, 177 "generation_num": self.generation_num, 178 }
logger =
<Logger eckity.multi_objective_evolution.nsga2_evolution (INFO)>
19class NSGA2Evolution(Algorithm): 20 def __init__( 21 self, 22 population, 23 statistics=None, 24 breeder=SimpleBreeder(), 25 population_evaluator=SimplePopulationEvaluator(), 26 NSGA2FrontSorting=NSGA2FrontSorting(), 27 max_generation=500, 28 events=None, 29 event_names=None, 30 termination_checker=ThresholdFromTargetTerminationChecker(threshold=0), 31 executor="thread", 32 max_workers=None, 33 random_generator=RNG(), 34 random_seed=None, 35 generation_seed=None, 36 best_of_run_=None, 37 best_of_run_evaluator=None, 38 best_of_gen=None, 39 worst_of_gen=None, 40 generation_num=0, 41 ): 42 43 if event_names is None: 44 _event_names = [ 45 "before_eval", 46 "after_eval", 47 "before_breeding", 48 "after_breeding", 49 ] 50 else: 51 _event_names = event_names 52 53 if statistics is None: 54 statistics = [] 55 56 super().__init__( 57 population, 58 statistics=statistics, 59 breeder=breeder, 60 population_evaluator=population_evaluator, 61 events=events, 62 event_names=_event_names, 63 executor=executor, 64 max_workers=max_workers, 65 random_generator=random_generator, 66 random_seed=random_seed, 67 generation_seed=generation_seed, 68 termination_checker=termination_checker, 69 generation_num=generation_num, 70 ) 71 72 self.termination_checker = termination_checker 73 self.best_of_run_ = best_of_run_ 74 self.best_of_run_evaluator = best_of_run_evaluator 75 self.best_of_gen = best_of_gen 76 self.worst_of_gen = worst_of_gen 77 self.max_generation = max_generation 78 79 self.final_generation_ = None 80 self.NSGA2FrontSorting = NSGA2FrontSorting 81 82 # 83 @overrides 84 def generation_iteration(self, gen: int) -> bool: 85 """ 86 Performs one iteration of the evolutionary run, for the current generation 87 88 Parameters 89 ---------- 90 gen: 91 current generation number (for example, generation #100) 92 93 Returns 94 ------- 95 None. 96 """ 97 98 # breed population 99 self.breeder.breed(self.population) 100 # Evaluate the entire population and get the best individual 101 102 self.population_evaluator.act(self.population) 103 104 self.NSGA2FrontSorting.select_for_population(self.population) 105 self.best_of_gen = self._get_pareto_fronts() 106 107 def _get_pareto_fronts(self): 108 """ 109 Returns: the pareto front for each sub_population 110 ------- 111 """ 112 pareto_fronts = [] 113 for sub_pop in self.population.sub_populations: 114 pareto_fronts.append( 115 [ 116 ind 117 for ind in sub_pop.individuals 118 if ind.fitness.front_rank == 1 119 ] 120 ) 121 return pareto_fronts 122 123 def initialize(self): 124 """ 125 Initialize the evolutionary algorithm 126 127 Register statistics to `after_generation` event 128 """ 129 super().initialize() 130 for stat in self.statistics: 131 self.register("after_generation", stat.write_statistics) 132 133 def execute(self, **kwargs): 134 """ 135 Compute output using best evolved individual. 136 Use `execute` in a non-sklearn setting. 137 Input keyword arguments that set variable values. 138 For example if `terminal_set=['x', 'y', 1, -1]` then call `execute(x=..., y=...)`. 139 140 Parameters 141 ---------- 142 **kwargs : keyword arguments 143 The input to the program (tree). 144 145 Returns 146 ------- 147 object 148 Output as computed by the best individual of the evolutionary run. 149 150 """ 151 return self.best_of_run_.execute(**kwargs) 152 153 @overrides 154 def finish(self): 155 """ 156 Finish the evolutionary run by showing the best individual and printing the best fitness 157 """ 158 self.best_of_run_.show() 159 logger.info(self.best_of_run_.get_pure_fitness()) 160 161 def event_name_to_data(self, event_name): 162 if event_name == "init": 163 return { 164 "population": self.population, 165 "statistics": self.statistics, 166 "breeder": self.breeder, 167 "termination_checker": self.termination_checker, 168 "max_generation": self.max_generation, 169 "events": self.events, 170 "max_workers": self.max_workers, 171 } 172 173 # default case 174 return { 175 "population": self.population, 176 "best_of_run_": self.best_of_run_, 177 "best_of_gen": self.best_of_gen, 178 "generation_num": self.generation_num, 179 }
Evolutionary algorithm to be executed.
Abstract Algorithm that can be extended to concrete algorithms, such as SimpleEvolution, Coevolution etc.
Parameters
- population (Population): The population to be evolved. Consists of several sub-populations. ref: https://api.eckity.org/eckity/population.html
- statistics (Statistics or list of Statistics, default=None): Provide statistics on the population during the evolution. ref: https://api.eckity.org/eckity/statistics.html
- breeder (Breeder, default=SimpleBreeder()): Responsible for applying selection and operator sequence on individuals in each generation. Applies on one sub-population in simple case. ref: https://api.eckity.org/eckity/breeders.html
- population_evaluator (PopulationEvaluator,): default=SimplePopulationEvaluator() Evaluates individual fitness scores concurrently and returns the best individual of each subpopulation (one individual in simple case). ref: https://api.eckity.org/eckity/evaluators.html
- max_generation (int, default=100):
Maximal number of generations to run the evolutionary process.
Note the evolution could end before reaching max_generation,
depends on the termination checker.
Note there are up to
max_generation + 1fitness calculations, but only up tomax_generationselections - events (dict(str, dict(object, function)), default=None): dict of events, each event holds a dict (subscriber, callback).
- event_names (list of strings, default=None): Names of events to publish during the evolution.
- termination_checker (TerminationChecker or a list of TerminationCheckers,): default=None Checks if the algorithm should terminate early. ref: https://api.eckity.org/eckity/termination_checkers.html
- max_workers (int, default=None): Maximal number of worker nodes for the Executor object that evaluates the fitness of the individuals. ref: https://docs.python.org/3/library/concurrent.futures.html
- random_generator (RNG, default=RNG()): Random Number Generator.
- random_seed (int, default=current system time): Random seed for deterministic experiment.
- generation_seed (int, default=None): Current generation seed. Useful for resuming a previously paused experiment.
- generation_num (int, default=0): Current generation number
Attributes
- final_generation_ (int): The generation in which the evolution ended.
NSGA2Evolution( population, statistics=None, breeder=<eckity.breeders.simple_breeder.SimpleBreeder object>, population_evaluator=<eckity.evaluators.simple_population_evaluator.SimplePopulationEvaluator object>, NSGA2FrontSorting=<eckity.multi_objective_evolution.nsga2_front_sorting.NSGA2FrontSorting object>, max_generation=500, events=None, event_names=None, termination_checker=<eckity.termination_checkers.threshold_from_target_termination_checker.ThresholdFromTargetTerminationChecker object>, executor='thread', max_workers=None, random_generator=<eckity.random.rng.RNG object>, random_seed=None, generation_seed=None, best_of_run_=None, best_of_run_evaluator=None, best_of_gen=None, worst_of_gen=None, generation_num=0)
20 def __init__( 21 self, 22 population, 23 statistics=None, 24 breeder=SimpleBreeder(), 25 population_evaluator=SimplePopulationEvaluator(), 26 NSGA2FrontSorting=NSGA2FrontSorting(), 27 max_generation=500, 28 events=None, 29 event_names=None, 30 termination_checker=ThresholdFromTargetTerminationChecker(threshold=0), 31 executor="thread", 32 max_workers=None, 33 random_generator=RNG(), 34 random_seed=None, 35 generation_seed=None, 36 best_of_run_=None, 37 best_of_run_evaluator=None, 38 best_of_gen=None, 39 worst_of_gen=None, 40 generation_num=0, 41 ): 42 43 if event_names is None: 44 _event_names = [ 45 "before_eval", 46 "after_eval", 47 "before_breeding", 48 "after_breeding", 49 ] 50 else: 51 _event_names = event_names 52 53 if statistics is None: 54 statistics = [] 55 56 super().__init__( 57 population, 58 statistics=statistics, 59 breeder=breeder, 60 population_evaluator=population_evaluator, 61 events=events, 62 event_names=_event_names, 63 executor=executor, 64 max_workers=max_workers, 65 random_generator=random_generator, 66 random_seed=random_seed, 67 generation_seed=generation_seed, 68 termination_checker=termination_checker, 69 generation_num=generation_num, 70 ) 71 72 self.termination_checker = termination_checker 73 self.best_of_run_ = best_of_run_ 74 self.best_of_run_evaluator = best_of_run_evaluator 75 self.best_of_gen = best_of_gen 76 self.worst_of_gen = worst_of_gen 77 self.max_generation = max_generation 78 79 self.final_generation_ = None 80 self.NSGA2FrontSorting = NSGA2FrontSorting
@overrides
def
generation_iteration(self, gen: int) -> bool:
83 @overrides 84 def generation_iteration(self, gen: int) -> bool: 85 """ 86 Performs one iteration of the evolutionary run, for the current generation 87 88 Parameters 89 ---------- 90 gen: 91 current generation number (for example, generation #100) 92 93 Returns 94 ------- 95 None. 96 """ 97 98 # breed population 99 self.breeder.breed(self.population) 100 # Evaluate the entire population and get the best individual 101 102 self.population_evaluator.act(self.population) 103 104 self.NSGA2FrontSorting.select_for_population(self.population) 105 self.best_of_gen = self._get_pareto_fronts()
Performs one iteration of the evolutionary run, for the current generation
Parameters
- gen:: current generation number (for example, generation #100)
Returns
- None.
def
initialize(self):
123 def initialize(self): 124 """ 125 Initialize the evolutionary algorithm 126 127 Register statistics to `after_generation` event 128 """ 129 super().initialize() 130 for stat in self.statistics: 131 self.register("after_generation", stat.write_statistics)
Initialize the evolutionary algorithm
Register statistics to after_generation event
def
execute(self, **kwargs):
133 def execute(self, **kwargs): 134 """ 135 Compute output using best evolved individual. 136 Use `execute` in a non-sklearn setting. 137 Input keyword arguments that set variable values. 138 For example if `terminal_set=['x', 'y', 1, -1]` then call `execute(x=..., y=...)`. 139 140 Parameters 141 ---------- 142 **kwargs : keyword arguments 143 The input to the program (tree). 144 145 Returns 146 ------- 147 object 148 Output as computed by the best individual of the evolutionary run. 149 150 """ 151 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 evolutionary run.
@overrides
def
finish(self):
153 @overrides 154 def finish(self): 155 """ 156 Finish the evolutionary run by showing the best individual and printing the best fitness 157 """ 158 self.best_of_run_.show() 159 logger.info(self.best_of_run_.get_pure_fitness())
Finish the evolutionary run by showing the best individual and printing the best fitness
def
event_name_to_data(self, event_name):
161 def event_name_to_data(self, event_name): 162 if event_name == "init": 163 return { 164 "population": self.population, 165 "statistics": self.statistics, 166 "breeder": self.breeder, 167 "termination_checker": self.termination_checker, 168 "max_generation": self.max_generation, 169 "events": self.events, 170 "max_workers": self.max_workers, 171 } 172 173 # default case 174 return { 175 "population": self.population, 176 "best_of_run_": self.best_of_run_, 177 "best_of_gen": self.best_of_gen, 178 "generation_num": self.generation_num, 179 }
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