Skip to content

Commit 0692432

Browse files
authored
Merge pull request #1123 from Axelrod-Python/add-ability-to-pass-game-to-moran-process
Add ability to pass game to Moran process.
2 parents 1266335 + 13e429e commit 0692432

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

axelrod/moran.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import matplotlib.pyplot as plt
77
import numpy as np
88

9-
from axelrod import DEFAULT_TURNS, Player
9+
from axelrod import DEFAULT_TURNS, Player, Game
1010
from .deterministic_cache import DeterministicCache
1111
from .graph import complete_graph, Graph
1212
from .match import Match
@@ -40,6 +40,7 @@ def fitness_proportionate_selection(scores: List) -> int:
4040
class MoranProcess(object):
4141
def __init__(self, players: List[Player], turns: int = DEFAULT_TURNS,
4242
prob_end: float = None, noise: float = 0,
43+
game: Game = None,
4344
deterministic_cache: DeterministicCache = None,
4445
mutation_rate: float = 0., mode: str = 'bd',
4546
interaction_graph: Graph = None,
@@ -94,6 +95,7 @@ def __init__(self, players: List[Player], turns: int = DEFAULT_TURNS,
9495
"""
9596
self.turns = turns
9697
self.prob_end = prob_end
98+
self.game = game
9799
self.noise = noise
98100
self.initial_players = players # save initial population
99101
self.players = [] # type: List
@@ -322,6 +324,7 @@ def score_all(self) -> List:
322324
match = Match((player1, player2),
323325
turns=self.turns, prob_end=self.prob_end,
324326
noise=self.noise,
327+
game=self.game,
325328
deterministic_cache=self.deterministic_cache)
326329
match.play()
327330
match_scores = match.final_score_per_turn()

axelrod/tests/unit/test_moran.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def test_init(self):
2222
mp = MoranProcess(players)
2323
self.assertEqual(mp.turns, axelrod.DEFAULT_TURNS)
2424
self.assertIsNone(mp.prob_end)
25+
self.assertIsNone(mp.game)
2526
self.assertEqual(mp.noise, 0)
2627
self.assertEqual(mp.initial_players, players)
2728
self.assertEqual(mp.players, list(players))
@@ -166,6 +167,15 @@ def test_two_prob_end(self):
166167
self.assertEqual(populations, mp.populations)
167168
self.assertEqual(mp.winning_strategy_name, str(p1))
168169

170+
def test_different_game(self):
171+
# Possible for Cooperator to become fixed when using a different game
172+
p1, p2 = axelrod.Cooperator(), axelrod.Defector()
173+
axelrod.seed(0)
174+
game = axelrod.Game(r=4, p=2, s=1, t=6)
175+
mp = MoranProcess((p1, p2), turns=5, game=game)
176+
populations = mp.play()
177+
self.assertEqual(mp.winning_strategy_name, str(p1))
178+
169179
def test_death_birth(self):
170180
"""Two player death-birth should fixate after one round."""
171181
p1, p2 = axelrod.Cooperator(), axelrod.Defector()

0 commit comments

Comments
 (0)