1
1
"""Lookup Evolve.
2
2
3
3
Usage:
4
- lookup_evolve.py [-h] [-p PLIES] [-s STARTING_PLIES] [-g GENERATIONS] [-k STARTING_POPULATION] [-u MUTATION_RATE] [-b BOTTLENECK] [-i PROCESSORS] [-o OUTPUT_FILE]
4
+ lookup_evolve.py [-h] [-p PLIES] [-s STARTING_PLIES] [-g GENERATIONS] [-k
5
+ STARTING_POPULATION] [-u MUTATION_RATE] [-b BOTTLENECK] [-i PROCESSORS] [-o
6
+ OUTPUT_FILE] [-z INITIAL_POPULATION_FILE]
5
7
6
8
Options:
7
- -h --help show this
8
- -p PLIES specify the number of recent plays in the lookup table [default: 2]
9
- -s STARTING_PLIES specify the number of opponent starting plays in the lookup table [default: 2]
10
- -g GENERATIONS how many generations to run the program for [default: 100]
11
- -k STARTING_POPULATION starting population for the simulation [default: 5]
12
- -u MUTATION_RATE mutation rate i.e. probability that a given value will flip [default: 0.1]
13
- -b BOTTLENECK number of individuals to keep from each generation [default: 10]
14
- -i PROCESSORS number of processors to use [default: 1]
15
- -o OUTPUT_FILE file to write statistics to [default: evolve.csv]
9
+ -h --help show this
10
+ -p PLIES specify the number of recent plays in the lookup table [default: 2]
11
+ -s STARTING_PLIES specify the number of opponent starting plays in the lookup table [default: 2]
12
+ -g GENERATIONS how many generations to run the program for [default: 100]
13
+ -k STARTING_POPULATION starting population size for the simulation [default: 5]
14
+ -u MUTATION_RATE mutation rate i.e. probability that a given value will flip [default: 0.1]
15
+ -b BOTTLENECK number of individuals to keep from each generation [default: 10]
16
+ -i PROCESSORS number of processors to use [default: 1]
17
+ -o OUTPUT_FILE file to write statistics to [default: evolve.csv]
18
+ -z INITIAL_POPULATION_FILE file to read an initial population from [default: None]
19
+
16
20
17
21
18
22
"""
25
29
from docopt import docopt
26
30
27
31
import axelrod_utils
32
+ import analyze_data
28
33
29
34
30
35
"""
@@ -57,7 +62,7 @@ def evolve(starting_tables, mutation_rate, generations, bottleneck, pool, plys,
57
62
"""
58
63
The function that does everything. Take a set of starting tables, and in each generation:
59
64
- add a bunch more random tables
60
- - simulate recombination between each pair of tables
65
+ - simulate recombination between each pair of tables
61
66
- randomly mutate the current population of tables
62
67
- calculate the fitness function i.e. the average score per turn
63
68
- keep the best individuals and discard the rest
@@ -117,7 +122,7 @@ def evolve(starting_tables, mutation_rate, generations, bottleneck, pool, plys,
117
122
return (current_bests )
118
123
119
124
def table_keys (plys , opponent_start_plys ):
120
- """Return randomly-generated lookup tables """
125
+ """Return key for given size of table """
121
126
122
127
# generate all the possible recent histories for the player and opponent
123
128
player_histories = ['' .join (x ) for x in itertools .product ('CD' , repeat = plys )]
@@ -148,8 +153,8 @@ def get_random_tables(plys, opponent_start_plys, number):
148
153
149
154
if __name__ == '__main__' :
150
155
arguments = docopt (__doc__ , version = 'Lookup Evolver 0.1' )
151
- # set up the process pool
152
- pool = Pool (processes = int (arguments ['-i' ]))
156
+ # set up the process pool
157
+ pool = Pool (processes = int (arguments ['-i' ]))
153
158
154
159
# vars for the genetic algorithm
155
160
starting_pop = int (arguments ['-k' ])
@@ -158,10 +163,25 @@ def get_random_tables(plys, opponent_start_plys, number):
158
163
bottleneck = int (arguments ['-b' ])
159
164
plys = int (arguments ['-p' ])
160
165
start_plys = int (arguments ['-s' ])
166
+ initial_population = arguments ['-z' ]
161
167
162
168
# generate a starting population of tables and score them
163
169
# these will start off the first generation
164
- starting_tables = get_random_tables (plys , start_plys , starting_pop )
170
+ if initial_population == 'None' :
171
+ starting_tables = get_random_tables (plys , start_plys , starting_pop )
172
+ else :
173
+ keys = table_keys (plys , start_plys )
174
+ data = analyze_data .read_top_tables (initial_population , starting_pop )
175
+ starting_tables = []
176
+ for row in data :
177
+ starting_tables .append (axelrod_utils .table_from_id (row [1 ], keys ))
178
+ # If insufficient size in data augment with random tables:
179
+ if len (starting_tables ) < starting_pop :
180
+ needed_num = starting_pop - len (starting_tables )
181
+ starting_tables = starting_tables + get_random_tables (plys ,
182
+ start_plys ,
183
+ needed_num )
184
+
165
185
real_starting_tables = axelrod_utils .score_tables (starting_tables , pool )
166
186
167
187
# kick off the evolve function
0 commit comments