Commit c0d2f548 authored by David A.. Werner's avatar David A.. Werner
Browse files

Single list of fitness values per GenerationInfo: inital fitness added to history upon creation

parent 4e95350d
......@@ -3,7 +3,7 @@ import copy
import tqdm
from collections import namedtuple
class GenerationInfo(namedtuple('GenerationInfo', ['generation_number', 'incoming_fitness', 'outgoing_fitness',
class GenerationInfo(namedtuple('GenerationInfo', ['generation_number', 'fitness_values',
'surviving_ancestors', 'surviving_children',
'surviving_mutants',
'num_ancestors', 'num_matings', 'num_mutants',
......@@ -11,7 +11,7 @@ class GenerationInfo(namedtuple('GenerationInfo', ['generation_number', 'incomin
'duplicate_children', 'duplicate_mutants'])):
@property
def max_fitness(self):
return max(self.outgoing_fitness)
return max(self.fitness_values)
class EvolutionParameters(object):
def __init__(self, max_population, num_matings, num_mutants, max_mutations, **kwargs):
......@@ -36,6 +36,10 @@ class EvolutionSimulator(object):
self.population = [population[0]() for _ in range(population[1])]
else:
self.population = population
n_before = len(self.population)
self.population = list(set(self.population))
n_after = len(self.population)
dups_in_pop = n_before - n_after
self._children = []
self._mutants = []
self.history = []
......@@ -50,9 +54,19 @@ class EvolutionSimulator(object):
if random_seed:
self.random_number_generator.seed(random_seed)
self.pool = pool
self.update_fitness()
initial_generation_info = GenerationInfo(0, self.current_fitnesses,
None, None, None,
None, None, None,
dups_in_pop,None,None)
self.history.append(initial_generation_info)
@property
def current_fitnesses(self):
return sorted([m.fitness for m in self.population], reverse=True)
def next_generation(self, current_generation):
incoming_fitness = sorted([m.fitness for m in self.population], reverse=True)
# mate: create children of two random parents
self.mate()
# mutate: create mutations of random individuals
......@@ -75,14 +89,14 @@ class EvolutionSimulator(object):
num_after_survival = self.survive()
surviving_ancestors, surviving_children, surviving_mutants = num_after_survival
outgoing_fitness = sorted([m.fitness for m in self.population], reverse=True)
fitness_values = self.current_fitnesses
num_ancestors = self.evolution_parameters.max_population
num_matings = self.evolution_parameters.num_matings
num_mutants = self.evolution_parameters.num_mutants
self.evolution_parameters.update_parameters(self.history)
return GenerationInfo(current_generation, incoming_fitness, outgoing_fitness,
return GenerationInfo(current_generation+1, fitness_values,
surviving_ancestors, surviving_children,
surviving_mutants, num_ancestors,
num_matings, num_mutants,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment