1 """Evolution Strategies for a Population.
2
3 Evolver classes manage a population of individuals, and are responsible
4 for taking care of the transition from one generation to the next.
5 """
6
7 import sys
8
10 """Evolve a population in place.
11
12 This implements a Steady State GA, where the population of individuals
13 is evolved in place.
14 """
16 raise NotImplementedError("Need to code this.")
17
19 """Evolve a population from generation to generation.
20
21 This implements a Generational GA, in which the population moves from
22 generation to generation.
23 """
24 - def __init__(self, starting_population, selector):
25 """Initialize the evolver.
26
27 Arguments:
28
29 o starting_population -- An initial set of individuals to begin
30 the evolution process from. This should be a list of Organism
31 objects.
32
33 o selector -- A Selection object that implements selection, along
34 with mutation and crossover to select a new population from a
35 given population.
36 """
37 self._population = starting_population
38 self._selector = selector
39
40 - def evolve(self, stopping_criteria):
41 """Evolve the population through multiple generations.
42
43 Arguments:
44
45 o stoppping_criteria -- A function which, when passed the current
46 individuals in the population, will determine when to stop
47 the evolution process.
48
49 Returns:
50
51 o The final evolved population.
52 """
53 while not(stopping_criteria(self._population)):
54 try:
55
56 self._population = self._selector.select(self._population)
57
58
59 for organism in self._population:
60 organism.recalculate_fitness()
61
62
63
64 except KeyboardInterrupt:
65
66 self._population.sort()
67 for org in self._population:
68 print org
69 sys.exit()
70
71 return self._population
72