1 | from ..base.experiment_niching_abc import ExperimentNiching |
---|
2 | from ..structures.individual import Individual |
---|
3 | from ..structures.population import PopulationStructures |
---|
4 | from ..utils import merge_two_parsers |
---|
5 | from .experiment_frams import ExperimentFrams |
---|
6 | |
---|
7 | |
---|
8 | class ExperimentFramsNiching(ExperimentFrams, ExperimentNiching): |
---|
9 | def __init__(self, frams_lib, optimization_criteria, hof_size, popsize, constraints, normalize, dissim, fit, genformat, archive_size, save_only_best, knn_niching, knn_nslc, crowding_dissim=None) -> None: |
---|
10 | ExperimentFrams.__init__(self, hof_size=hof_size, |
---|
11 | popsize=popsize, |
---|
12 | frams_lib=frams_lib, |
---|
13 | constraints=constraints, |
---|
14 | optimization_criteria=optimization_criteria, |
---|
15 | genformat=genformat, |
---|
16 | save_only_best=save_only_best |
---|
17 | ) |
---|
18 | ExperimentNiching.__init__(self, hof_size=hof_size, |
---|
19 | popsize=popsize, |
---|
20 | fit=fit, |
---|
21 | normalize=normalize, |
---|
22 | save_only_best=save_only_best, |
---|
23 | archive_size=archive_size, |
---|
24 | knn_niching=knn_niching, |
---|
25 | knn_nslc=knn_nslc, |
---|
26 | crowding_dissim=crowding_dissim |
---|
27 | ) |
---|
28 | self.dissim = dissim |
---|
29 | |
---|
30 | |
---|
31 | def initialize_evolution(self, genformat, initialgenotype): |
---|
32 | self.current_generation = 0 |
---|
33 | self.time_elapsed = 0 |
---|
34 | self.stats = [] # stores the best individuals, one from each generation |
---|
35 | initial_individual = Individual() |
---|
36 | initial_individual.set_and_evaluate(self.frams_getsimplest( |
---|
37 | '1' if genformat is None else genformat, initialgenotype), self.evaluate) |
---|
38 | self.hof.add(initial_individual) |
---|
39 | self.stats.append( |
---|
40 | initial_individual.rawfitness if self.save_only_best else initial_individual) |
---|
41 | self.population_structures = PopulationStructures( |
---|
42 | initial_individual=initial_individual, archive_size=self.archive_size, popsize=self.popsize) |
---|
43 | if self.fit == "nsga2": |
---|
44 | self.do_nsga2_dissim(self.population_structures.population) |
---|
45 | if self.fit == "nslc": |
---|
46 | self.do_nslc_dissim(self.population_structures, self.population_structures.population) |
---|
47 | |
---|
48 | def dissimilarity(self, population): |
---|
49 | return self.frams_lib.dissimilarity([i.genotype for i in population], self.dissim) |
---|
50 | |
---|
51 | def crowding_distance_dissimilarity(self, population): |
---|
52 | return self.frams_lib.dissimilarity([i.genotype for i in population], self.crowding_dissim) |
---|
53 | |
---|
54 | |
---|
55 | @staticmethod |
---|
56 | def get_args_for_parser(): |
---|
57 | p1 = ExperimentFrams.get_args_for_parser() |
---|
58 | p2 = ExperimentNiching.get_args_for_parser() |
---|
59 | return merge_two_parsers(p1, p2) |
---|
60 | |
---|