eckity.statistics.best_avg_worst_size_tree_statistics

 1import sys
 2
 3import numpy as np
 4
 5from eckity.statistics.statistics import Statistics
 6
 7
 8class BestAverageWorstSizeTreeStatistics(Statistics):
 9    def __init__(self, format_string=None, output_stream=sys.stdout):
10        if format_string is None:
11            format_string = 'best fitness {}\nworst fitness {}\naverage fitness {}\naverage size {}\n'
12        super().__init__(format_string, output_stream)
13
14    def write_statistics(self, sender, data_dict):
15        print(f'generation #{data_dict["generation_num"]}', file=self.output_stream)
16        for index, sub_pop in enumerate(data_dict['population'].sub_populations):
17            print(f'subpopulation #{index}', file=self.output_stream)
18            best_individual = sub_pop.get_best_individual()
19            print(
20                self.format_string.format(best_individual.get_pure_fitness(),
21                                          sub_pop.get_worst_individual().get_pure_fitness(),
22                                          sub_pop.get_average_fitness(),
23                                          np.average([ind.size() for ind in sub_pop.individuals])),
24                file=self.output_stream
25            )
26
27    # TODO tostring to indiv
class BestAverageWorstSizeTreeStatistics(eckity.statistics.statistics.Statistics):
 9class BestAverageWorstSizeTreeStatistics(Statistics):
10    def __init__(self, format_string=None, output_stream=sys.stdout):
11        if format_string is None:
12            format_string = 'best fitness {}\nworst fitness {}\naverage fitness {}\naverage size {}\n'
13        super().__init__(format_string, output_stream)
14
15    def write_statistics(self, sender, data_dict):
16        print(f'generation #{data_dict["generation_num"]}', file=self.output_stream)
17        for index, sub_pop in enumerate(data_dict['population'].sub_populations):
18            print(f'subpopulation #{index}', file=self.output_stream)
19            best_individual = sub_pop.get_best_individual()
20            print(
21                self.format_string.format(best_individual.get_pure_fitness(),
22                                          sub_pop.get_worst_individual().get_pure_fitness(),
23                                          sub_pop.get_average_fitness(),
24                                          np.average([ind.size() for ind in sub_pop.individuals])),
25                file=self.output_stream
26            )
27
28    # TODO tostring to indiv

Abstract Statistics class. Provides statistics about the current evolution state.

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.
BestAverageWorstSizeTreeStatistics(format_string=None, output_stream=<_io.StringIO object>)
10    def __init__(self, format_string=None, output_stream=sys.stdout):
11        if format_string is None:
12            format_string = 'best fitness {}\nworst fitness {}\naverage fitness {}\naverage size {}\n'
13        super().__init__(format_string, output_stream)
def write_statistics(self, sender, data_dict):
15    def write_statistics(self, sender, data_dict):
16        print(f'generation #{data_dict["generation_num"]}', file=self.output_stream)
17        for index, sub_pop in enumerate(data_dict['population'].sub_populations):
18            print(f'subpopulation #{index}', file=self.output_stream)
19            best_individual = sub_pop.get_best_individual()
20            print(
21                self.format_string.format(best_individual.get_pure_fitness(),
22                                          sub_pop.get_worst_individual().get_pure_fitness(),
23                                          sub_pop.get_average_fitness(),
24                                          np.average([ind.size() for ind in sub_pop.individuals])),
25                file=self.output_stream
26            )

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.