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