eckity.statistics.minimal_print_statistics

 1from sys import stdout
 2
 3from eckity.statistics.statistics import Statistics
 4
 5
 6class MinimalPrintStatistics(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
31    # TODO tostring to indiv
32
33    # Necessary for valid pickling, since modules cannot be pickled
34    def __getstate__(self):
35        state = self.__dict__.copy()
36        del state['output_stream']
37        return state
38
39    # Necessary for valid unpickling, since modules cannot be pickled
40    def __setstate__(self, state):
41        self.__dict__.update(state)
42        self.output_stream = stdout
class MinimalPrintStatistics(eckity.statistics.statistics.Statistics):
 7class MinimalPrintStatistics(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
32    # TODO tostring to indiv
33
34    # Necessary for valid pickling, since modules cannot be pickled
35    def __getstate__(self):
36        state = self.__dict__.copy()
37        del state['output_stream']
38        return state
39
40    # Necessary for valid unpickling, since modules cannot be pickled
41    def __setstate__(self, state):
42        self.__dict__.update(state)
43        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.
MinimalPrintStatistics(format_string=None, output_stream=<_io.StringIO object>)
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)
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)

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.