11import axelrod as axl
22import numpy as np
33import matplotlib .pyplot as plt
4+ import tqdm
45from axelrod .strategy_transformers import JossAnnTransformer , DualTransformer
56from axelrod .interaction_utils import compute_final_score_per_turn , read_interactions_from_file
67from axelrod import on_windows
1112Point = namedtuple ('Point' , 'x y' )
1213
1314
14- def create_points (step ):
15+ def create_points (step , progress_bar = True ):
1516 """Creates a set of Points over the unit square.
1617
1718 A Point has coordinates (x, y). This function constructs points that are
@@ -23,15 +24,29 @@ def create_points(step):
2324 step : float
2425 The separation between each Point. Smaller steps will produce more
2526 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
2629
2730 Returns
2831 ----------
2932 points : list
3033 of Point objects with coordinates (x, y)
3134 """
3235 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 ()
3550
3651 return points
3752
@@ -76,7 +91,7 @@ def create_jossann(point, probe):
7691 return joss_ann
7792
7893 @staticmethod
79- def create_edges (points ):
94+ def create_edges (points , progress_bar = True ):
8095 """Creates a set of edges for a spatial tournament.
8196
8297 Constructs edges that correspond to `points`. All edges begin at 0, and
@@ -86,6 +101,9 @@ def create_edges(points):
86101 ----------
87102 points : list
88103 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+
89107
90108 Returns
91109 ----------
@@ -94,10 +112,12 @@ def create_edges(points):
94112 first element. The second element is the index of the
95113 corresponding probe (+1 to allow for including the Strategy).
96114 """
115+ if progress_bar :
116+ points = tqdm .tqdm (points , desc = "Generating network edges" )
97117 edges = [(0 , index + 1 ) for index , point in enumerate (points )]
98118 return edges
99119
100- def create_probes (self , probe , points ):
120+ def create_probes (self , probe , points , progress_bar = True ):
101121 """Creates a set of probe strategies over the unit square.
102122
103123 Constructs probe strategies that correspond to points with coordinates
@@ -109,24 +129,31 @@ def create_probes(self, probe, points):
109129 A class that must be descended from axelrod.strategies.
110130 points : list
111131 of Point objects with coordinates (x, y)
132+ progress_bar : bool
133+ Whether or not to create a progress bar which will be updated
112134
113135 Returns
114136 ----------
115137 probes : list
116138 A list of `JossAnnTransformer` players with parameters that
117139 correspond to point.
118140 """
141+ if progress_bar :
142+ points = tqdm .tqdm (points , desc = "Generating probes" )
119143 probes = [self .create_jossann (point , probe ) for point in points ]
120144 return probes
121145
122- def construct_tournament_elements (self , step ):
146+ def construct_tournament_elements (self , step , progress_bar = True ):
123147 """Build the elements required for a spatial tournament
124148
125149 Parameters
126150 ----------
127151 step : float
128152 The separation between each Point. Smaller steps will
129153 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+
130157
131158 Returns
132159 ----------
@@ -140,11 +167,11 @@ def construct_tournament_elements(self, step):
140167 original player, the rest are the probes.
141168
142169 """
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 )
146174
147- probe_players = self .create_probes (self .probe , probe_points )
148175 tournament_players = [self .strategy ()] + probe_players
149176
150177 return edges , tournament_players
@@ -219,7 +246,8 @@ def fingerprint(self, turns=50, repetitions=10, step=0.01, processes=None,
219246 outputfile = NamedTemporaryFile (mode = 'w' )
220247 filename = outputfile .name
221248
222- edges , tourn_players = self .construct_tournament_elements (step )
249+ edges , tourn_players = self .construct_tournament_elements (step ,
250+ progress_bar = progress_bar )
223251 self .step = step
224252 self .spatial_tournament = axl .SpatialTournament (tourn_players ,
225253 turns = turns ,
@@ -233,7 +261,8 @@ def fingerprint(self, turns=50, repetitions=10, step=0.01, processes=None,
233261 if in_memory :
234262 self .interactions = self .spatial_tournament .interactions_dict
235263 else :
236- self .interactions = read_interactions_from_file (filename )
264+ self .interactions = read_interactions_from_file (filename ,
265+ progress_bar = progress_bar )
237266
238267 self .data = self .generate_data (self .interactions , self .points , edges )
239268 return self .data
0 commit comments