Changeset 1304


Ignore:
Timestamp:
04/27/24 16:47:37 (12 days ago)
Author:
Maciej Komosinski
Message:

Added archive to the NSLC algorithm

Location:
framspy/evolalg
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • framspy/evolalg/base/experiment_niching_abc.py

    r1289 r1304  
    8585            i.fitness = DeapFitness(tuple((d, i.rawfitness)))
    8686
    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)
    8990        normalized_matrix = self.normalize_dissim(dissim_matrix)
    9091        for i in range(len(normalized_matrix)):
     
    9394            dissim_value = np.mean(np.take_along_axis(
    9495                temp_dissim, index_array, axis=-1))
    95             temp_fitness = population[i].rawfitness
     96            temp_fitness = population_archive[i].rawfitness
    9697            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)))
    9899            temp_ind_fit = sum(
    99100                [1 for ind in population_of_most_similar if ind.rawfitness < temp_fitness])
    100             population[i].fitness = DeapFitness(
     101            population_archive[i].fitness = DeapFitness(
    101102                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):
    104106        expected_mut = int(self.popsize * prob_mut)
    105107        expected_xov = int(self.popsize * prob_xov)
    106108        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)
    109111
    110112        def addGenotypeIfValid(ind_list, genotype):
     
    139141
    140142        # 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):
    142144            ind, counter = get_individual(offspring, counter)
    143145            newpop.append(Individual().copyFrom(ind))
    144146
    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)
    149151        elif self.fit == "nsga2":
    150152            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))
    152154        return out_pop
    153155
     
    169171            if type(self.population_structures.population[0].fitness) == DeapFitness:
    170172                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)
    172174            else:
    173175                self.population_structures.population = self.make_new_population(
  • framspy/evolalg/frams_base/experiment_frams_niching.py

    r1270 r1304  
    4343            self.do_nsga2_dissim(self.population_structures.population)
    4444        if self.fit == "nslc":
    45             self.do_nslc_dissim(self.population_structures.population)
     45            self.do_nslc_dissim(self.population_structures, self.population_structures.population)
    4646
    4747    def dissimilarity(self, population):
Note: See TracChangeset for help on using the changeset viewer.