eckity.statistics.best_average_worst_statistics
1from sys import stdout 2 3from eckity.statistics.statistics import Statistics 4 5 6class BestAverageWorstStatistics(Statistics): 7 """ 8 Concrete Statistics class. 9 Provides statistics about the best fitness, average fitness and worst fitness of every sub-population in 10 some generation. 11 12 Parameters 13 ---------- 14 format_string: str 15 String format of the data to output. 16 Value depends on the information the statistics provides. 17 For more information, check out the concrete classes who extend this class. 18 19 output_stream: Optional[SupportsWrite[str]], default=stdout 20 Output file for the statistics. 21 By default, the statistics will be written to stdout. 22 """ 23 def __init__(self, format_string=None, output_stream=stdout): 24 if format_string is None: 25 format_string = 'best fitness {}\nworst fitness {}\naverage fitness {}\n' 26 super().__init__(format_string, output_stream) 27 28 def write_statistics(self, sender, data_dict): 29 print(f'generation #{data_dict["generation_num"]}', file=self.output_stream) 30 for index, sub_pop in enumerate(data_dict["population"].sub_populations): 31 print(f'subpopulation #{index}', file=self.output_stream) 32 best_individual = sub_pop.get_best_individual() 33 print(self.format_string.format(best_individual.get_pure_fitness(), 34 sub_pop.get_worst_individual().get_pure_fitness(), 35 sub_pop.get_average_fitness()), file=self.output_stream) 36 # best_individual.show() 37 38 # TODO tostring to indiv 39 40 # Necessary for valid pickling, since modules cannot be pickled 41 def __getstate__(self): 42 state = self.__dict__.copy() 43 del state['output_stream'] 44 return state 45 46 # Necessary for valid unpickling, since modules cannot be pickled 47 def __setstate__(self, state): 48 self.__dict__.update(state) 49 self.output_stream = stdout
7class BestAverageWorstStatistics(Statistics): 8 """ 9 Concrete Statistics class. 10 Provides statistics about the best fitness, average fitness and worst fitness of every sub-population in 11 some generation. 12 13 Parameters 14 ---------- 15 format_string: str 16 String format of the data to output. 17 Value depends on the information the statistics provides. 18 For more information, check out the concrete classes who extend this class. 19 20 output_stream: Optional[SupportsWrite[str]], default=stdout 21 Output file for the statistics. 22 By default, the statistics will be written to stdout. 23 """ 24 def __init__(self, format_string=None, output_stream=stdout): 25 if format_string is None: 26 format_string = 'best fitness {}\nworst fitness {}\naverage fitness {}\n' 27 super().__init__(format_string, output_stream) 28 29 def write_statistics(self, sender, data_dict): 30 print(f'generation #{data_dict["generation_num"]}', file=self.output_stream) 31 for index, sub_pop in enumerate(data_dict["population"].sub_populations): 32 print(f'subpopulation #{index}', file=self.output_stream) 33 best_individual = sub_pop.get_best_individual() 34 print(self.format_string.format(best_individual.get_pure_fitness(), 35 sub_pop.get_worst_individual().get_pure_fitness(), 36 sub_pop.get_average_fitness()), file=self.output_stream) 37 # best_individual.show() 38 39 # TODO tostring to indiv 40 41 # Necessary for valid pickling, since modules cannot be pickled 42 def __getstate__(self): 43 state = self.__dict__.copy() 44 del state['output_stream'] 45 return state 46 47 # Necessary for valid unpickling, since modules cannot be pickled 48 def __setstate__(self, state): 49 self.__dict__.update(state) 50 self.output_stream = stdout
Concrete Statistics class. Provides statistics about the best fitness, average fitness and worst fitness of every sub-population in some generation.
Parameters
- format_string (str): String format of the data to output. Value depends on the information the statistics provides. For more information, check out the concrete classes who extend this class.
- output_stream (Optional[SupportsWrite[str]], default=stdout): Output file for the statistics. By default, the statistics will be written to stdout.
def
write_statistics(self, sender, data_dict):
29 def write_statistics(self, sender, data_dict): 30 print(f'generation #{data_dict["generation_num"]}', file=self.output_stream) 31 for index, sub_pop in enumerate(data_dict["population"].sub_populations): 32 print(f'subpopulation #{index}', file=self.output_stream) 33 best_individual = sub_pop.get_best_individual() 34 print(self.format_string.format(best_individual.get_pure_fitness(), 35 sub_pop.get_worst_individual().get_pure_fitness(), 36 sub_pop.get_average_fitness()), file=self.output_stream) 37 # best_individual.show()
Write the statistics information using the format string field to the output stream field.
Parameters
- sender (object): The object that this statistics provides information about. This class registers to a certain event that the sender object publishes. The statistics are shown as a callback to the event publication. For example, we can register a concrete Statistics sub-class to provide statistics after every generation of a concrete Algorithm sub-class.
- data_dict (dict(str, object)): Relevant data to the statistics. Used to gain and provide information from the sender.
Returns
- None.