Changeset 1194 for framspy/evolalg/base/experiment_abc.py
- Timestamp:
- 01/30/23 00:27:10 (22 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
framspy/evolalg/base/experiment_abc.py
r1190 r1194 40 40 new_individual = Individual() 41 41 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: 43 43 ind_list.append(new_individual) 44 44 … … 46 46 """'individuals' is the input population (a list of individuals). 47 47 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.""" 49 49 50 50 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)) 58 55 59 56 # adding valid mutants of selected individuals... 60 57 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) 63 59 self.addGenotypeIfValid(newpop, self.mutate(ind.genotype)) 64 60 65 61 # adding valid crossovers of selected individuals... 66 62 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)) 73 66 74 67 # 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) 78 70 newpop.append(Individual().copyFrom(ind)) 79 71
Note: See TracChangeset
for help on using the changeset viewer.