eckity.multi_objective_evolution.moe_best_worst_statistics

 1from sys import stdout
 2
 3from eckity.statistics.statistics import Statistics
 4
 5
 6class MOEBestWorstStatistics(Statistics):
 7    """
 8    Concrete Statistics class.
 9    Intended for Multi Objective Evolution.
10    Provides statistics about the best and worst fitness fronts 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
25    def __init__(self, format_string=None, output_stream=stdout):
26        if format_string is None:
27            format_string = "first front has {} individuals ({:.1f}%)\nfirst front's corners: {}\nlast front's corners: {}\n"
28        super().__init__(format_string)
29        self.output_stream = output_stream
30
31    def write_statistics(self, sender, data_dict):
32        print(
33            f'generation #{data_dict["generation_num"]}',
34            file=self.output_stream,
35        )
36        for index, sub_pop in enumerate(
37            data_dict["population"].sub_populations
38        ):
39            first_front_corners = self.get_corners(sub_pop, 1)
40            last_rank = sorted(
41                list(
42                    set(
43                        [
44                            indiv.fitness.front_rank
45                            for indiv in sub_pop.individuals
46                        ]
47                    )
48                )
49            )[-1]
50            last_front_corners = self.get_corners(sub_pop, last_rank)
51            first_front = [
52                indiv.get_pure_fitness()
53                for indiv in sub_pop.individuals
54                if indiv.fitness.front_rank == 1
55            ]
56            print(
57                f"subpopulation #{index} has {last_rank} fronts",
58                file=self.output_stream,
59            )
60            print(
61                self.format_string.format(
62                    len(first_front),
63                    len(first_front) / len(sub_pop.individuals) * 100,
64                    first_front_corners,
65                    last_front_corners,
66                ),
67                file=self.output_stream,
68            )
69
70    def get_corners(self, sub_pop, rank):
71        result = [
72            indiv.get_pure_fitness()
73            for indiv in sub_pop.individuals
74            if indiv.fitness.front_rank == rank
75            and indiv.fitness.crowding == float("inf")
76        ]
77        result = [list(x) for x in set(tuple(x) for x in result)]  # unique
78        assert len(result) in [1, 2]
79        return result
class MOEBestWorstStatistics(eckity.statistics.statistics.Statistics):
 7class MOEBestWorstStatistics(Statistics):
 8    """
 9    Concrete Statistics class.
10    Intended for Multi Objective Evolution.
11    Provides statistics about the best and worst fitness fronts of every sub-population in
12    some generation.
13
14    Parameters
15    ----------
16    format_string: str
17        String format of the data to output.
18        Value depends on the information the statistics provides.
19        For more information, check out the concrete classes who extend this class.
20
21    output_stream: Optional[SupportsWrite[str]], default=stdout
22        Output file for the statistics.
23        By default, the statistics will be written to stdout.
24    """
25
26    def __init__(self, format_string=None, output_stream=stdout):
27        if format_string is None:
28            format_string = "first front has {} individuals ({:.1f}%)\nfirst front's corners: {}\nlast front's corners: {}\n"
29        super().__init__(format_string)
30        self.output_stream = output_stream
31
32    def write_statistics(self, sender, data_dict):
33        print(
34            f'generation #{data_dict["generation_num"]}',
35            file=self.output_stream,
36        )
37        for index, sub_pop in enumerate(
38            data_dict["population"].sub_populations
39        ):
40            first_front_corners = self.get_corners(sub_pop, 1)
41            last_rank = sorted(
42                list(
43                    set(
44                        [
45                            indiv.fitness.front_rank
46                            for indiv in sub_pop.individuals
47                        ]
48                    )
49                )
50            )[-1]
51            last_front_corners = self.get_corners(sub_pop, last_rank)
52            first_front = [
53                indiv.get_pure_fitness()
54                for indiv in sub_pop.individuals
55                if indiv.fitness.front_rank == 1
56            ]
57            print(
58                f"subpopulation #{index} has {last_rank} fronts",
59                file=self.output_stream,
60            )
61            print(
62                self.format_string.format(
63                    len(first_front),
64                    len(first_front) / len(sub_pop.individuals) * 100,
65                    first_front_corners,
66                    last_front_corners,
67                ),
68                file=self.output_stream,
69            )
70
71    def get_corners(self, sub_pop, rank):
72        result = [
73            indiv.get_pure_fitness()
74            for indiv in sub_pop.individuals
75            if indiv.fitness.front_rank == rank
76            and indiv.fitness.crowding == float("inf")
77        ]
78        result = [list(x) for x in set(tuple(x) for x in result)]  # unique
79        assert len(result) in [1, 2]
80        return result

Concrete Statistics class. Intended for Multi Objective Evolution. Provides statistics about the best and worst fitness fronts 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.
MOEBestWorstStatistics( format_string=None, output_stream=<_io.TextIOWrapper encoding='UTF-8'>)
26    def __init__(self, format_string=None, output_stream=stdout):
27        if format_string is None:
28            format_string = "first front has {} individuals ({:.1f}%)\nfirst front's corners: {}\nlast front's corners: {}\n"
29        super().__init__(format_string)
30        self.output_stream = output_stream
output_stream
def write_statistics(self, sender, data_dict):
32    def write_statistics(self, sender, data_dict):
33        print(
34            f'generation #{data_dict["generation_num"]}',
35            file=self.output_stream,
36        )
37        for index, sub_pop in enumerate(
38            data_dict["population"].sub_populations
39        ):
40            first_front_corners = self.get_corners(sub_pop, 1)
41            last_rank = sorted(
42                list(
43                    set(
44                        [
45                            indiv.fitness.front_rank
46                            for indiv in sub_pop.individuals
47                        ]
48                    )
49                )
50            )[-1]
51            last_front_corners = self.get_corners(sub_pop, last_rank)
52            first_front = [
53                indiv.get_pure_fitness()
54                for indiv in sub_pop.individuals
55                if indiv.fitness.front_rank == 1
56            ]
57            print(
58                f"subpopulation #{index} has {last_rank} fronts",
59                file=self.output_stream,
60            )
61            print(
62                self.format_string.format(
63                    len(first_front),
64                    len(first_front) / len(sub_pop.individuals) * 100,
65                    first_front_corners,
66                    last_front_corners,
67                ),
68                file=self.output_stream,
69            )

Write the statistics information using the format string 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.
def get_corners(self, sub_pop, rank):
71    def get_corners(self, sub_pop, rank):
72        result = [
73            indiv.get_pure_fitness()
74            for indiv in sub_pop.individuals
75            if indiv.fitness.front_rank == rank
76            and indiv.fitness.crowding == float("inf")
77        ]
78        result = [list(x) for x in set(tuple(x) for x in result)]  # unique
79        assert len(result) in [1, 2]
80        return result