1
1
import axelrod as axl
2
2
import numpy as np
3
3
import matplotlib .pyplot as plt
4
+ import tqdm
4
5
from axelrod .strategy_transformers import JossAnnTransformer , DualTransformer
5
6
from axelrod .interaction_utils import compute_final_score_per_turn , read_interactions_from_file
6
7
from axelrod import on_windows
11
12
Point = namedtuple ('Point' , 'x y' )
12
13
13
14
14
- def create_points (step ):
15
+ def create_points (step , progress_bar = True ):
15
16
"""Creates a set of Points over the unit square.
16
17
17
18
A Point has coordinates (x, y). This function constructs points that are
@@ -23,15 +24,29 @@ def create_points(step):
23
24
step : float
24
25
The separation between each Point. Smaller steps will produce more
25
26
Points with coordinates that will be closer together.
27
+ progress_bar : bool
28
+ Whether or not to create a progress bar which will be updated
26
29
27
30
Returns
28
31
----------
29
32
points : list
30
33
of Point objects with coordinates (x, y)
31
34
"""
32
35
num = int ((1 / step ) // 1 ) + 1
33
- points = [Point (j , k ) for j in np .linspace (0 , 1 , num )
34
- for k in np .linspace (0 , 1 , num )]
36
+
37
+ if progress_bar :
38
+ p_bar = tqdm .tqdm (total = num ** 2 , desc = "Generating points" )
39
+
40
+ points = []
41
+ for x in np .linspace (0 , 1 , num ):
42
+ for y in np .linspace (0 , 1 , num ):
43
+ points .append (Point (x , y ))
44
+
45
+ if progress_bar :
46
+ p_bar .update ()
47
+
48
+ if progress_bar :
49
+ p_bar .close ()
35
50
36
51
return points
37
52
@@ -76,7 +91,7 @@ def create_jossann(point, probe):
76
91
return joss_ann
77
92
78
93
@staticmethod
79
- def create_edges (points ):
94
+ def create_edges (points , progress_bar = True ):
80
95
"""Creates a set of edges for a spatial tournament.
81
96
82
97
Constructs edges that correspond to `points`. All edges begin at 0, and
@@ -86,6 +101,9 @@ def create_edges(points):
86
101
----------
87
102
points : list
88
103
of Point objects with coordinates (x, y)
104
+ progress_bar : bool
105
+ Whether or not to create a progress bar which will be updated
106
+
89
107
90
108
Returns
91
109
----------
@@ -94,10 +112,12 @@ def create_edges(points):
94
112
first element. The second element is the index of the
95
113
corresponding probe (+1 to allow for including the Strategy).
96
114
"""
115
+ if progress_bar :
116
+ points = tqdm .tqdm (points , desc = "Generating network edges" )
97
117
edges = [(0 , index + 1 ) for index , point in enumerate (points )]
98
118
return edges
99
119
100
- def create_probes (self , probe , points ):
120
+ def create_probes (self , probe , points , progress_bar = True ):
101
121
"""Creates a set of probe strategies over the unit square.
102
122
103
123
Constructs probe strategies that correspond to points with coordinates
@@ -109,24 +129,31 @@ def create_probes(self, probe, points):
109
129
A class that must be descended from axelrod.strategies.
110
130
points : list
111
131
of Point objects with coordinates (x, y)
132
+ progress_bar : bool
133
+ Whether or not to create a progress bar which will be updated
112
134
113
135
Returns
114
136
----------
115
137
probes : list
116
138
A list of `JossAnnTransformer` players with parameters that
117
139
correspond to point.
118
140
"""
141
+ if progress_bar :
142
+ points = tqdm .tqdm (points , desc = "Generating probes" )
119
143
probes = [self .create_jossann (point , probe ) for point in points ]
120
144
return probes
121
145
122
- def construct_tournament_elements (self , step ):
146
+ def construct_tournament_elements (self , step , progress_bar = True ):
123
147
"""Build the elements required for a spatial tournament
124
148
125
149
Parameters
126
150
----------
127
151
step : float
128
152
The separation between each Point. Smaller steps will
129
153
produce more Points that will be closer together.
154
+ progress_bar : bool
155
+ Whether or not to create a progress bar which will be updated
156
+
130
157
131
158
Returns
132
159
----------
@@ -140,11 +167,11 @@ def construct_tournament_elements(self, step):
140
167
original player, the rest are the probes.
141
168
142
169
"""
143
- probe_points = create_points (step )
144
- self .points = probe_points
145
- edges = self .create_edges (probe_points )
170
+ self .points = create_points (step , progress_bar = progress_bar )
171
+ edges = self .create_edges (self .points , progress_bar = progress_bar )
172
+ probe_players = self .create_probes (self .probe , self .points ,
173
+ progress_bar = progress_bar )
146
174
147
- probe_players = self .create_probes (self .probe , probe_points )
148
175
tournament_players = [self .strategy ()] + probe_players
149
176
150
177
return edges , tournament_players
@@ -219,7 +246,8 @@ def fingerprint(self, turns=50, repetitions=10, step=0.01, processes=None,
219
246
outputfile = NamedTemporaryFile (mode = 'w' )
220
247
filename = outputfile .name
221
248
222
- edges , tourn_players = self .construct_tournament_elements (step )
249
+ edges , tourn_players = self .construct_tournament_elements (step ,
250
+ progress_bar = progress_bar )
223
251
self .step = step
224
252
self .spatial_tournament = axl .SpatialTournament (tourn_players ,
225
253
turns = turns ,
@@ -233,7 +261,8 @@ def fingerprint(self, turns=50, repetitions=10, step=0.01, processes=None,
233
261
if in_memory :
234
262
self .interactions = self .spatial_tournament .interactions_dict
235
263
else :
236
- self .interactions = read_interactions_from_file (filename )
264
+ self .interactions = read_interactions_from_file (filename ,
265
+ progress_bar = progress_bar )
237
266
238
267
self .data = self .generate_data (self .interactions , self .points , edges )
239
268
return self .data
0 commit comments