Changeset 1304 for framspy/evolalg/base/experiment_niching_abc.py
- Timestamp:
- 04/27/24 16:47:37 (7 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
framspy/evolalg/base/experiment_niching_abc.py
r1289 r1304 85 85 i.fitness = DeapFitness(tuple((d, i.rawfitness))) 86 86 87 def do_nslc_dissim(self, population): 88 dissim_matrix = self.dissimilarity(population) 87 def do_nslc_dissim(self, population_structures, pop_offspring): 88 population_archive = population_structures.archive + pop_offspring 89 dissim_matrix = self.dissimilarity(population_archive) 89 90 normalized_matrix = self.normalize_dissim(dissim_matrix) 90 91 for i in range(len(normalized_matrix)): … … 93 94 dissim_value = np.mean(np.take_along_axis( 94 95 temp_dissim, index_array, axis=-1)) 95 temp_fitness = population [i].rawfitness96 temp_fitness = population_archive[i].rawfitness 96 97 population_of_most_similar = list( 97 map(population .__getitem__, self.transform_indexes(i, index_array)))98 map(population_archive.__getitem__, self.transform_indexes(i, index_array))) 98 99 temp_ind_fit = sum( 99 100 [1 for ind in population_of_most_similar if ind.rawfitness < temp_fitness]) 100 population [i].fitness = DeapFitness(101 population_archive[i].fitness = DeapFitness( 101 102 tuple((dissim_value, temp_ind_fit))) 102 103 def make_new_population_nsga2(self, population, prob_mut, prob_xov): 103 population_structures.update_archive(dissim_matrix, population_archive) 104 105 def make_new_population_nsga2(self, population_structures, prob_mut, prob_xov): 104 106 expected_mut = int(self.popsize * prob_mut) 105 107 expected_xov = int(self.popsize * prob_xov) 106 108 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) 107 assignCrowdingDist(population )108 offspring = tools.selTournamentDCD(population , self.popsize)109 assignCrowdingDist(population_structures.population) 110 offspring = tools.selTournamentDCD(population_structures.population, self.popsize) 109 111 110 112 def addGenotypeIfValid(ind_list, genotype): … … 139 141 140 142 # select clones to fill up the new population until we reach the same size as the input population 141 while len(newpop) < len(population ):143 while len(newpop) < len(population_structures.population): 142 144 ind, counter = get_individual(offspring, counter) 143 145 newpop.append(Individual().copyFrom(ind)) 144 146 145 pop_offspring = population + newpop # this is OK for NSGA2, but TODO verify if this should also be used for NSLC?146 print(len(pop_offspring))147 if self.fit == "nslc": # TODO should NSLC be also equipped with a novelty archive? (with an admittance threshold?)148 self.do_nslc_dissim(pop _offspring)147 pop_offspring = population_structures.population + newpop # used both for nsga2 and nslc 148 # print(len(pop_offspring)) # for debugging 149 if self.fit == "nslc": 150 self.do_nslc_dissim(population_structures, pop_offspring) 149 151 elif self.fit == "nsga2": 150 152 self.do_nsga2_dissim(pop_offspring) 151 out_pop = tools.selNSGA2(pop_offspring, len(population ))153 out_pop = tools.selNSGA2(pop_offspring, len(population_structures.population)) 152 154 return out_pop 153 155 … … 169 171 if type(self.population_structures.population[0].fitness) == DeapFitness: 170 172 self.population_structures.population = self.make_new_population_nsga2( # used both for nsga2 and nslc 171 self.population_structures .population, pmut, pxov)173 self.population_structures, pmut, pxov) 172 174 else: 173 175 self.population_structures.population = self.make_new_population(
Note: See TracChangeset
for help on using the changeset viewer.