source: framspy/evolalg/frams_base/experiment_frams_niching.py @ 1322

Last change on this file since 1322 was 1308, checked in by Maciej Komosinski, 6 months ago

Added the ability to select a dissimilarity measure for crowding distance (for NSGA2 and NSLC) independently from the main dissimilarity measure

File size: 3.0 KB
RevLine 
[1190]1from ..base.experiment_niching_abc import ExperimentNiching
2from ..structures.individual import Individual
3from ..structures.population import PopulationStructures
4from ..utils import merge_two_parsers
5from .experiment_frams import ExperimentFrams
6
7
8class ExperimentFramsNiching(ExperimentFrams, ExperimentNiching):
[1308]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:
[1190]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,
[1270]23                                   archive_size=archive_size,
24                                   knn_niching=knn_niching,
[1308]25                                   knn_nslc=knn_nslc,
26                                   crowding_dissim=crowding_dissim
[1190]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":
[1304]46            self.do_nslc_dissim(self.population_structures, self.population_structures.population)
[1190]47
48    def dissimilarity(self, population):
49        return self.frams_lib.dissimilarity([i.genotype for i in population], self.dissim)
[1308]50   
51    def crowding_distance_dissimilarity(self, population):
52        return self.frams_lib.dissimilarity([i.genotype for i in population], self.crowding_dissim)
[1190]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
Note: See TracBrowser for help on using the repository browser.