Changeset 1194 for framspy/evolalg


Ignore:
Timestamp:
01/30/23 00:27:10 (22 months ago)
Author:
Maciej Komosinski
Message:

make_new_population() targets the desired popsize instead of preserving the input (current) population size

Location:
framspy/evolalg/base
Files:
2 edited

Legend:

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

    r1190 r1194  
    4040        new_individual = Individual()
    4141        new_individual.set_and_evaluate(genotype, self.evaluate)
    42         if new_individual.fitness is not BAD_FITNESS:  # this is how we defined BAD_FITNESS in evaluate()
     42        if new_individual.fitness is not BAD_FITNESS:
    4343            ind_list.append(new_individual)
    4444
     
    4646        """'individuals' is the input population (a list of individuals).
    4747        Assumptions: all genotypes in 'individuals' are valid and evaluated (have fitness set).
    48         Returns: a new population of the same size as 'individuals' with prob_mut mutants, prob_xov offspring, and the remainder of clones."""
     48        Returns: a new population of size 'self.popsize' with prob_mut mutants, prob_xov offspring, and the remainder of clones."""
    4949
    5050        newpop = []
    51         N = len(individuals)
    52         expected_mut = int(N * prob_mut)
    53         expected_xov = int(N * prob_xov)
    54         assert expected_mut + \
    55             expected_xov <= N, "If probabilities of mutation (%g) and crossover (%g) added together exceed 1.0, then the population would grow every generation..." % (
    56                 prob_mut, prob_xov)
    57         ris = RandomIndexSequence(N)
     51        expected_mut = int(self.popsize * prob_mut)
     52        expected_xov = int(self.popsize * prob_xov)
     53        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)
     54        ris = RandomIndexSequence(len(individuals))
    5855
    5956        # adding valid mutants of selected individuals...
    6057        while len(newpop) < expected_mut:
    61             ind = self.select(
    62                 individuals, tournament_size=tournament_size, random_index_sequence=ris)
     58            ind = self.select(individuals, tournament_size=tournament_size, random_index_sequence=ris)
    6359            self.addGenotypeIfValid(newpop, self.mutate(ind.genotype))
    6460
    6561        # adding valid crossovers of selected individuals...
    6662        while len(newpop) < expected_mut + expected_xov:
    67             ind1 = self.select(
    68                 individuals, tournament_size=tournament_size, random_index_sequence=ris)
    69             ind2 = self.select(
    70                 individuals, tournament_size=tournament_size, random_index_sequence=ris)
    71             self.addGenotypeIfValid(
    72                 newpop, self.cross_over(ind1.genotype, ind2.genotype))
     63            ind1 = self.select(individuals, tournament_size=tournament_size, random_index_sequence=ris)
     64            ind2 = self.select(individuals, tournament_size=tournament_size, random_index_sequence=ris)
     65            self.addGenotypeIfValid(newpop, self.cross_over(ind1.genotype, ind2.genotype))
    7366
    7467        # select clones to fill up the new population until we reach the same size as the input population
    75         while len(newpop) < len(individuals):
    76             ind = self.select(
    77                 individuals, tournament_size=tournament_size, random_index_sequence=ris)
     68        while len(newpop) < self.popsize:
     69            ind = self.select(individuals, tournament_size=tournament_size, random_index_sequence=ris)
    7870            newpop.append(Individual().copyFrom(ind))
    7971
  • framspy/evolalg/base/experiment_niching_abc.py

    r1190 r1194  
    2828        ExperimentABC.__init__(self,popsize=popsize, hof_size=hof_size, save_only_best=save_only_best)
    2929        self.fit = fit
     30        self.normalize = normalize
    3031        self.knn_niching = knn_niching
    3132        self.knn_nslc = knn_nslc
    32         self.normalize = normalize
    3333        self.archive_size=archive_size
    3434        if popsize < self.knn_niching:
     
    7272        else:
    7373            raise Exception("Wrong fit type: ", self.fit,
    74                             f" chose correct one or implement new behaviour")
     74                            f" chose correct one or implement new behavior")
    7575        population_structures.update_archive(dissim_matrix, population_archive)
    7676
     
    9898
    9999    def make_new_population_nsga2(self, population, prob_mut, prob_xov):
    100         N = len(population)
    101         expected_mut = int(N * prob_mut)
    102         expected_xov = int(N * prob_xov)
     100        expected_mut = int(self.popsize * prob_mut)
     101        expected_xov = int(self.popsize * prob_xov)
     102        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)
    103103        assignCrowdingDist(population)
    104         offspring = tools.selTournamentDCD(population, N)
     104        offspring = tools.selTournamentDCD(population, self.popsize)
    105105
    106106        def addGenotypeIfValid(ind_list, genotype):
    107107            new_individual = Individual()
    108108            new_individual.set_and_evaluate(genotype, self.evaluate)
    109             if new_individual.fitness is not BAD_FITNESS:  # this is how we defined BAD_FITNESS in frams_evaluate()
     109            if new_individual.fitness is not BAD_FITNESS:
    110110                ind_list.append(new_individual)
    111111
    112112        counter = 0
    113113
    114         def get_indyvidual(pop, c):
     114        def get_individual(pop, c):
    115115            if c < len(pop):
    116116                ind = pop[c]
     
    125125        newpop = []
    126126        while len(newpop) < expected_mut:
    127             ind, counter = get_indyvidual(offspring, counter)
     127            ind, counter = get_individual(offspring, counter)
    128128            addGenotypeIfValid(newpop, self.mutate(ind.genotype))
    129129
    130130        # adding valid crossovers of selected individuals...
    131131        while len(newpop) < expected_mut + expected_xov:
    132             ind1, counter = get_indyvidual(offspring, counter)
    133             ind2, counter = get_indyvidual(offspring, counter)
    134             addGenotypeIfValid(newpop, self.cross_over(
    135                 ind1.genotype, ind2.genotype))
     132            ind1, counter = get_individual(offspring, counter)
     133            ind2, counter = get_individual(offspring, counter)
     134            addGenotypeIfValid(newpop, self.cross_over(ind1.genotype, ind2.genotype))
    136135
    137136        # select clones to fill up the new population until we reach the same size as the input population
    138137        while len(newpop) < len(population):
    139             ind, counter = get_indyvidual(offspring, counter)
     138            ind, counter = get_individual(offspring, counter)
    140139            newpop.append(Individual().copyFrom(ind))
    141140
     
    194193                            help="What normalization use for dissimilarity matrix, max (default}, sum and none")
    195194        parser.add_argument("-knn",type= int, default= 0,
    196                         help="Nearest neighbours parameter for local novelty/niching, if knn==0 global is performed.Default:0")
     195                        help="Nearest neighbors parameter for local novelty/niching, if knn==0 global is performed.Default:0")
    197196        return parser
    198197       
Note: See TracChangeset for help on using the changeset viewer.