Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

635 Implementing spatial tournaments #654

Merged
merged 25 commits into from
Jul 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
cb0e930
Have tests for Spatial Match generator.
Nikoleta-v3 Jun 21, 2016
9a75263
Added a new class in the match generator, the SpatialMatches.
Nikoleta-v3 Jun 22, 2016
b6d2b9e
Altering the arguments of super function in Spatial Match Generator.
Nikoleta-v3 Jun 24, 2016
e432b87
Tests for the class SpatialTournament in the Tournament file.
Nikoleta-v3 Jun 24, 2016
e5332a1
Adding Spatial Tournament Class.
Nikoleta-v3 Jun 24, 2016
87a8ec7
Adding Spatial Tournament to be imported into Axelrod.
Nikoleta-v3 Jun 24, 2016
d018a24
Adding a Value Error for tournaments with disconnected nodes-players.
Nikoleta-v3 Jun 24, 2016
ba53219
Fixing test that was passing by chance.
Nikoleta-v3 Jun 28, 2016
eaee751
Fixed self interactions being counted twice.
Nikoleta-v3 Jun 28, 2016
7733d86
Adding three tests for spatial.
Nikoleta-v3 Jun 28, 2016
27dc71e
Adding test property based test for spatial.
Nikoleta-v3 Jun 28, 2016
6526c3b
Refactored tests.
Nikoleta-v3 Jun 28, 2016
613b924
Removed a print command that was accidentally left here.
Nikoleta-v3 Jun 28, 2016
a71c65f
Pep tests.
Nikoleta-v3 Jun 28, 2016
e525ca1
Created a documentation for the spatial tournament.
Nikoleta-v3 Jul 11, 2016
cbc445d
Last corrections on the spatial tournament docs.
Nikoleta-v3 Jul 13, 2016
f94d022
Adding an extra test for a complete multigraph.
Nikoleta-v3 Jul 13, 2016
3a44249
Removing property and property tests for Spatial tournament.
Nikoleta-v3 Jul 13, 2016
dfe7755
Removing spatial tournament from import.
Nikoleta-v3 Jul 13, 2016
16d7178
Changing the word "dictionary" to "list".
Nikoleta-v3 Jul 13, 2016
1e64dbc
Fixing doc string for SpatialMatches class.
Nikoleta-v3 Jul 13, 2016
0c65804
Removed a forgotten test which is not being used.
Nikoleta-v3 Jul 13, 2016
7a82a2c
PEP8 corrections that were pointed out.
Nikoleta-v3 Jul 13, 2016
5b3f81e
Expanding the doc string of the test_match_lenghts.
Nikoleta-v3 Jul 14, 2016
9fcaaa5
Replacing "TestResult_SpatialStructure" to "TestResultSpatialStructure".
Nikoleta-v3 Jul 14, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion axelrod/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
from .strategies import *
from .deterministic_cache import DeterministicCache
from .match_generator import *
from .tournament import Tournament, ProbEndTournament
from .tournament import Tournament, ProbEndTournament, SpatialTournament
from .result_set import ResultSet, ResultSetFromFile
from .ecosystem import Ecosystem
40 changes: 40 additions & 0 deletions axelrod/match_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,43 @@ def estimated_size(self):
"""Rough estimate of the number of matches that will be generated."""
size = self.__len__() * (1. / self.prob_end) * self.repetitions
return size

class SpatialMatches(RoundRobinMatches):
"""
A class that generates spatially-structured matches.
In these matches, players interact only with their neighbors rather than the
entire population. This reduces to a well-mixed population when the spatial
graph is a complete graph.

Parameters
----------
players : list
A list of axelrod.Player objects
turns : integer
The number of turns per match
game : axelrod.Game
The game object used to score the match
repetitions : int
The number of repetitions of a given match
edges : list
A list of tuples containing the existing edges
"""

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

player_indices = list(range(len(players)))
node_indices = sorted(set([node for edge in edges for node in edge]))
if player_indices != node_indices:
raise ValueError("The graph edges do not include all players.")

self.edges = edges
super(SpatialMatches, self).__init__(players, turns, game, repetitions)

def build_match_chunks(self):
for edge in self.edges:
match_params = self.build_single_match_params()
index_pair = edge
yield (index_pair, match_params, self.repetitions)

def __len__(self):
return len(self.edges)
8 changes: 4 additions & 4 deletions axelrod/result_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ def build_payoffs(self):
if (player, opponent) == index_pair:
for interaction in repetitions:
utilities.append(iu.compute_final_score_per_turn(interaction)[0])
if (opponent, player) == index_pair:
elif (opponent, player) == index_pair:
for interaction in repetitions:
utilities.append(iu.compute_final_score_per_turn(interaction)[1])

Expand Down Expand Up @@ -460,7 +460,7 @@ def build_payoff_diffs_means(self):
for interaction in repetitions:
scores = iu.compute_final_score_per_turn(interaction)
diffs.append(scores[0] - scores[1])
if (opponent, player) == index_pair:
elif (opponent, player) == index_pair:
for interaction in repetitions:
scores = iu.compute_final_score_per_turn(interaction)
diffs.append(scores[1] - scores[0])
Expand Down Expand Up @@ -501,7 +501,7 @@ def build_cooperation(self):
if (player, opponent) == index_pair:
for interaction in repetitions:
coop_count += iu.compute_cooperations(interaction)[0]
if (opponent, player) == index_pair:
elif (opponent, player) == index_pair:
for interaction in repetitions:
coop_count += iu.compute_cooperations(interaction)[1]

Expand Down Expand Up @@ -625,7 +625,7 @@ def build_good_partner_matrix(self):
if coops[0] >= coops[1]:
good_partner_matrix[player][opponent] += 1

if (opponent, player) == index_pair:
elif (opponent, player) == index_pair:
for interaction in repetitions:
coops = iu.compute_cooperations(interaction)
if coops[0] <= coops[1]:
Expand Down
29 changes: 29 additions & 0 deletions axelrod/tests/unit/test_match_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,32 @@ def test_len(self, prob_end):
self.players, prob_end, game=None, repetitions=repetitions)
self.assertEqual(len(rr), len(list(rr.build_match_chunks())))
self.assertAlmostEqual(rr.estimated_size(), len(rr) * 1. / prob_end * repetitions)


class TestSpatialMatches(unittest.TestCase):

@classmethod
def setUpClass(cls):
cls.players = [s() for s in test_strategies]

@given(repetitions=integers(min_value=1, max_value=test_repetitions),
turns=integers(min_value=1, max_value=test_turns))
@example(repetitions=test_repetitions, turns=test_turns)
def test_build_match_chunks(self, repetitions, turns):
edges = [(0, 1), (1, 2), (3, 4)]
sp = axelrod.SpatialMatches(
self.players, turns, test_game, repetitions, edges)
chunks = list(sp.build_match_chunks())
match_definitions = [tuple(list(index_pair) + [repetitions])
for (index_pair, match_params, repetitions) in chunks]
expected_match_definitions = [(edge[0], edge[1], repetitions)
for edge in edges]

self.assertEqual(sorted(match_definitions), sorted(expected_match_definitions))

def test_len(self):
edges = [(0, 1), (1, 2), (3, 4)]
sp = axelrod.SpatialMatches(
self.players, test_turns, test_game, test_repetitions, edges)
self.assertEqual(len(sp), len(list(sp.build_match_chunks())))
self.assertEqual(len(sp), len(edges))
1 change: 1 addition & 0 deletions axelrod/tests/unit/test_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ def test_decorator_with_stochastic_strategies(self, tournament):
for p in tournament.players:
self.assertIn(str(p), stochastic_player_names)


class TestGame(unittest.TestCase):

def test_call(self):
Expand Down
Loading