- Timestamp:
- 06/05/24 22:35:50 (7 months ago)
- Location:
- framspy/evolalg
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
framspy/evolalg/base/experiment_niching_abc.py
r1306 r1308 25 25 archive_size: int = None 26 26 27 def __init__(self, fit, normalize, popsize, hof_size, save_only_best, knn_niching, knn_nslc, archive_size ) -> None:27 def __init__(self, fit, normalize, popsize, hof_size, save_only_best, knn_niching, knn_nslc, archive_size, crowding_dissim = None) -> None: 28 28 ExperimentABC.__init__(self,popsize=popsize, hof_size=hof_size, save_only_best=save_only_best) 29 29 self.fit = fit … … 32 32 self.knn_nslc = knn_nslc 33 33 self.archive_size=archive_size 34 self.crowding_dissim = crowding_dissim 34 35 35 36 # np.argpartition requires these parameters to be at most popsize-2; popsize is decreased by 1 because we remove_diagonal() … … 68 69 69 70 if Individual.fitness_set_negative_to_zero is False and ("niching" in self.fit or "novelty" in self.fit): 70 71 raise ValueError("Negative fitness values not tested in combination with niching or novelty. When using these techniques, verify formulas or consider using the flag -fitness_set_negative_to_zero") # once the formulas are verified/improved, the command-line flag and this conditional check can be removed. 71 72 72 73 if "niching" in self.fit: … … 104 105 population_structures.update_archive(dissim_matrix, population_archive) 105 106 107 def assignCrowdingDistFramspy(self, individuals): 108 """Assign a crowding distance to each individual's fitness. The 109 crowding distance can be retrieved via the :attr:`crowding_dist` 110 attribute of each individual's fitness. 111 """ 112 if len(individuals) == 0: 113 return 114 115 if self.crowding_dissim is DissimMethod.FITNESS: # if crowding dissim was not specified (DissimMethod.FITNESS is our default) or was set to fitness, use the default DEAP implementation that relies on fitness 116 return assignCrowdingDist(individuals) 117 118 dissim_matrix = self.crowding_distance_dissimilarity(individuals) 119 assert len(dissim_matrix) == len(individuals), f'Dissimilarity matrix does not match the size of the population {len(dissim_matrix)}:{len(individuals)}' 120 121 for i in range(len(individuals)): 122 individuals[i].fitness.crowding_dist = np.mean(dissim_matrix[i]) 123 124 106 125 def make_new_population_nsga2(self, population_structures, prob_mut, prob_xov): 107 126 expected_mut = int(self.popsize * prob_mut) 108 127 expected_xov = int(self.popsize * prob_xov) 109 128 assert expected_mut + expected_xov <= self.popsize, "If probabilities of mutation (%g) and crossover (%g) added together exceed 1.0, then the population would grow every generation..." % (prob_mut, prob_xov) 110 assignCrowdingDist(population_structures.population)129 self.assignCrowdingDistFramspy(population_structures.population) 111 130 offspring = tools.selTournamentDCD(population_structures.population, self.popsize) 112 131 … … 192 211 parser.add_argument("-dissim", type = lambda arg: DissimMethod[arg], choices = DissimMethod, 193 212 default=DissimMethod.PHENE_STRUCT_OPTIM, 194 help="Dissimilarity measure type. Available: " + str(DissimMethod._member_names_)) 213 help="The type of the dissimilarity measure to be used in novelty and niching diversity control. Available: " + str(DissimMethod._member_names_)) 214 215 parser.add_argument("-crowding_dissim", type = lambda arg: DissimMethod[arg], choices = DissimMethod, 216 default=DissimMethod.FITNESS, 217 help="The type of the dissimilarity measure to be used as NSGA2 and NSLC crowding distance. Available: " + str(DissimMethod._member_names_)) 195 218 parser.add_argument("-fit",type= str, default="raw", 196 219 help="Fitness type, availible types: niching, novelty, knn_niching (local), knn_novelty (local), nsga2, nslc and raw (default)") … … 206 229 @abstractmethod 207 230 def dissimilarity(self, population: list): 231 """ 232 Used for calculating dissimilarity in novelty and niching methods 233 """ 208 234 pass 235 236 @abstractmethod 237 def crowding_distance_dissimilarity(self, population: list): 238 """ 239 Used for calculating dissimilarity for nsga2 crowding distance, 240 currently used in NSGA2 and NSLC 241 """ 242 pass -
framspy/evolalg/frams_base/experiment_frams_niching.py
r1304 r1308 7 7 8 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 ) -> None: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 10 ExperimentFrams.__init__(self, hof_size=hof_size, 11 11 popsize=popsize, … … 23 23 archive_size=archive_size, 24 24 knn_niching=knn_niching, 25 knn_nslc=knn_nslc 25 knn_nslc=knn_nslc, 26 crowding_dissim=crowding_dissim 26 27 ) 27 28 self.dissim = dissim … … 47 48 def dissimilarity(self, population): 48 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) 49 53 50 54 -
framspy/evolalg/run_frams_niching.py
r1289 r1308 30 30 normalize=parsed_args.normalize, 31 31 dissim=parsed_args.dissim, 32 crowding_dissim=parsed_args.crowding_dissim, 32 33 fit=parsed_args.fit, 33 34 genformat=parsed_args.genformat, -
framspy/evolalg/tests/test_diferent_settings.py
r1306 r1308 47 47 'hof_size': [0, 10], 48 48 'normalize': ['none', 'max', 'sum'], 49 'dissim': [DissimMethod.GENE_LEVENSHTEIN, DissimMethod.PHENE_STRUCT_OPTIM, DissimMethod.PHENE_DESCRIPTORS, DissimMethod.PHENE_DENSITY_FREQ], 49 'dissim': [DissimMethod.GENE_LEVENSHTEIN],# DissimMethod.PHENE_STRUCT_OPTIM, DissimMethod.PHENE_DESCRIPTORS, DissimMethod.PHENE_DENSITY_COUNT, DissimMethod.PHENE_DENSITY_FREQ], 50 'crowding_dissim': [DissimMethod.FITNESS, DissimMethod.GENE_LEVENSHTEIN, DissimMethod.PHENE_STRUCT_OPTIM],#, DissimMethod.PHENE_STRUCT_OPTIM, DissimMethod.PHENE_DESCRIPTORS, DissimMethod.PHENE_DENSITY_COUNT, DissimMethod.PHENE_DENSITY_FREQ], 50 51 'fit': ['niching', 'novelty', 'nsga2', 'nslc', 'raw'], 51 52 'genformat': ['1'], … … 130 131 normalize=params['normalize'], 131 132 dissim=params['dissim'], 133 crowding_dissim=params['crowding_dissim'], 132 134 fit=params['fit'], 133 135 genformat=params['genformat'],
Note: See TracChangeset
for help on using the changeset viewer.