def __init__(self, chromosome, maxNIndividuals, valueInitializer=Randomization(-0.1,0.1), **kwargs): """ @param chromosome: The prototype chromosome @param maxNIndividuals: The maximum allowed number of individuals """ SimplePopulation.__init__(self) self._prototype = EvolinoSubIndividual(chromosome) self._maxNIndividuals = maxNIndividuals self._valueInitializer = valueInitializer self.setArgs(**kwargs) for _ in range(maxNIndividuals): self.addIndividual(self._prototype.copy()) self._valueInitializer.apply(self)
def apply(self, population): """ First determines the number of individuals to be created. Then clones the fittest individuals (=parents), mutates these clones and adds them to the population. """ max_n = population.getMaxNIndividuals() n = population.getIndividualsN() freespace = max_n - n best = population.getBestIndividualsSorted(freespace) children=set() while True: if len(children)>=freespace: break for parent in best: children.add( parent.copy() ) if len(children)>=freespace: break dummy_population = SimplePopulation() dummy_population.addIndividuals(children) self.mutation.apply(dummy_population) population.addIndividuals(dummy_population.getIndividuals()) assert population.getMaxNIndividuals() == population.getIndividualsN()