Skip to content

Commit ff721b8

Browse files
committed
Refactor function to check that graph is connected
1 parent defcc38 commit ff721b8

File tree

2 files changed

+40
-20
lines changed

2 files changed

+40
-20
lines changed

axelrod/match_generator.py

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,31 @@ def estimated_size(self):
169169
return size
170170

171171

172+
def test_graph_is_connected(edges, players):
173+
"""
174+
Test if a set of edges defines a complete graph on a set of players.
175+
176+
This is used by the spatial tournaments and will raise a ValueError if the
177+
graph would have some players not playing anyone.
178+
179+
Parameters:
180+
-----------
181+
182+
edges : a list of 2 tuples
183+
players : a list of player names
184+
"""
185+
# Check if all players are connected.
186+
player_indices = set(range(len(players)))
187+
node_indices = set()
188+
for edge in edges:
189+
for node in edge:
190+
node_indices.add(node)
191+
192+
if player_indices != node_indices:
193+
raise ValueError("The graph edges do not include all players.")
194+
195+
196+
172197
class SpatialMatches(RoundRobinMatches):
173198
"""
174199
A class that generates spatially-structured matches.
@@ -192,16 +217,7 @@ class SpatialMatches(RoundRobinMatches):
192217

193218
def __init__(self, players, turns, game, repetitions, edges):
194219

195-
# Check if all players are connected.
196-
player_indices = set(range(len(players)))
197-
node_indices = set()
198-
for edge in edges:
199-
for node in edge:
200-
node_indices.add(node)
201-
202-
if player_indices != node_indices:
203-
raise ValueError("The graph edges do not include all players.")
204-
220+
test_graph_is_connected(edges, players)
205221
self.edges = edges
206222
super(SpatialMatches, self).__init__(players, turns, game, repetitions)
207223

@@ -238,16 +254,7 @@ class ProbEndSpatialMatches(SpatialMatches, ProbEndRoundRobinMatches):
238254

239255
def __init__(self, players, prob_end, game, repetitions, noise, edges):
240256

241-
# Check if all players are connected.
242-
player_indices = set(range(len(players)))
243-
node_indices = set()
244-
for edge in edges:
245-
for node in edge:
246-
node_indices.add(node)
247-
248-
if player_indices != node_indices:
249-
raise ValueError("The graph edges do not include all players.")
250-
257+
test_graph_is_connected(edges, players)
251258
self.edges = edges
252259
ProbEndRoundRobinMatches.__init__(self, players, prob_end,
253260
game, repetitions, noise)

axelrod/tests/unit/test_match_generator.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from hypothesis.strategies import floats, integers
66

77
import axelrod
8+
from axelrod.match_generator import test_graph_is_connected
89

910

1011
test_strategies = [
@@ -319,3 +320,15 @@ def test_noise(self, noise, prob_end):
319320
for (index_pair, match_params, repetitions) in chunks]
320321
for value in noise_values:
321322
self.assertEqual(noise, value)
323+
324+
325+
class TestUtilityFunctions(unittest.TestCase):
326+
def test_connected_graph(self):
327+
edges = [(0, 0), (0, 1), (1, 1)]
328+
players = ["Cooperator", "Defector"]
329+
self.assertEqual(test_graph_is_connected(edges, players), None)
330+
331+
def test_unconnected_graph(self):
332+
edges = [(0, 0), (0, 1), (1, 1)]
333+
players = ["Cooperator", "Defector", "Alternator"]
334+
self.assertRaises(ValueError, test_graph_is_connected, edges, players)

0 commit comments

Comments
 (0)