SHARE
TWEET

Untitled

a guest Mar 18th, 2019 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Population(object):
  2.     """
  3.     This class implements the core evolution algorithm:
  4.         1. Evaluate fitness of all genomes.
  5.         2. Check to see if the termination criterion is satisfied; exit if it is.
  6.         3. Generate the next generation from the current population.
  7.         4. Partition the new generation into species based on genetic similarity.
  8.         5. Go to 1.
  9.     """
  10.  
  11.     def __init__(self, config, initial_state=None):
  12.         self.reporters = ReporterSet()
  13.         self.config = config
  14.         stagnation = config.stagnation_type(config.stagnation_config, self.reporters)
  15.         self.reproduction = config.reproduction_type(config.reproduction_config,
  16.                                                      self.reporters,
  17.                                                      stagnation)
  18.         if config.fitness_criterion == 'max':
  19.             self.fitness_criterion = max
  20.         elif config.fitness_criterion == 'min':
  21.             self.fitness_criterion = min
  22.         elif config.fitness_criterion == 'mean':
  23.             self.fitness_criterion = mean
  24.         elif not config.no_fitness_termination:
  25.             raise RuntimeError("Unexpected fitness_criterion: {0!r}".format(config.fitness_criterion))
  26.  
  27.         if initial_state is None:
  28.             # Create a population from scratch, then partition into species.
  29.             self.population = self.reproduction.create_new(config.genome_type,
  30.                                                            config.genome_config,
  31.                                                            config.pop_size)
  32.             self.species = config.species_set_type(config.species_set_config, self.reporters)
  33.             self.generation = 0
  34.             self.species.speciate(config, self.population, self.generation)
  35.         else:
  36.             self.population, self.species, self.generation = initial_state
  37.  
  38.         self.best_genome = None
  39.  
  40.     def add_reporter(self, reporter):
  41.         self.reporters.add(reporter)
  42.  
  43.     def remove_reporter(self, reporter):
  44.         self.reporters.remove(reporter)
  45.  
  46.     def run(self, fitness_function, n=None):
  47.         if self.config.no_fitness_termination and (n is None):
  48.             raise RuntimeError("Cannot have no generational limit with no fitness termination")
  49.  
  50.  
  51.         k = 0
  52.         while n is None or k < n:
  53.             k += 1
  54.  
  55.             self.reporters.start_generation(self.generation)
  56.  
  57.             # Evaluate all genomes using the user-provided function.
  58.             fitness_function(list(iteritems(self.population)), self.config)
  59.  
  60.             # Gather and report statistics.
  61.             best = None
  62.             for g in itervalues(self.population):
  63.                 if best is None or g.fitness > best.fitness:
  64.                     best = g
  65.             self.reporters.post_evaluate(self.config, self.population, self.species, best)
  66.  
  67.             # Track the best genome ever seen.
  68.             if self.best_genome is None or best.fitness > self.best_genome.fitness:
  69.                 self.best_genome = best
  70.  
  71.             if not self.config.no_fitness_termination:
  72.                 # End if the fitness threshold is reached.
  73.                 fv = self.fitness_criterion(g.fitness for g in itervalues(self.population))
  74.                 if fv >= self.config.fitness_threshold:
  75.                     self.reporters.found_solution(self.config, self.generation, best)
  76.                     break
  77.  
  78.             # Create the next generation from the current generation.
  79.             self.population = self.reproduction.reproduce(self.config, self.species,
  80.                                                           self.config.pop_size, self.generation)
  81.  
  82.             # Check for complete extinction.
  83.             if not self.species.species:
  84.                 self.reporters.complete_extinction()
  85.  
  86.                 # If requested by the user, create a completely new population,
  87.                 # otherwise raise an exception.
  88.                 if self.config.reset_on_extinction:
  89.                     self.population = self.reproduction.create_new(self.config.genome_type,
  90.                                                                    self.config.genome_config,
  91.                                                                    self.config.pop_size)
  92.                 else:
  93.                     raise CompleteExtinctionException()
  94.  
  95.             # Divide the new population into species.
  96.             self.species.speciate(self.config, self.population, self.generation)
  97.  
  98.             self.reporters.end_generation(self.config, self.population, self.species)
  99.  
  100.             self.generation += 1
  101.  
  102.         if self.config.no_fitness_termination:
  103.             self.reporters.found_solution(self.config, self.generation, self.best_genome)
  104.  
  105.         return self.best_genome
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top