Changeset 1308 for framspy/evolalg/base
- Timestamp:
- 06/05/24 22:35:50 (3 months ago)
- File:
-
- 1 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
Note: See TracChangeset
for help on using the changeset viewer.