Skip to content

Commit e934a98

Browse files
authored
Merge pull request #638 from Axelrod-Python/637
Fixing reset method for FSMs
2 parents 89651f4 + c14a9eb commit e934a98

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

axelrod/strategies/finite_state_machines.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ def __init__(self, transitions=None, initial_state=None, initial_action=None):
5454
initial_state = 1
5555
initial_action = C
5656
Player.__init__(self)
57+
self.initial_state = initial_state
5758
self.initial_action = initial_action
5859
self.fsm = SimpleFSM(transitions, initial_state)
5960

@@ -67,6 +68,10 @@ def strategy(self, opponent):
6768
self.state = self.fsm.state
6869
return action
6970

71+
def reset(self):
72+
Player.reset(self)
73+
self.fsm.state = self.initial_state
74+
7075

7176
class Fortress3(FSMPlayer):
7277
"""Finite state machine player specified in DOI:10.1109/CEC.2006.1688322.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""Tests for some expected match behaviours"""
2+
import unittest
3+
import axelrod
4+
5+
from hypothesis import given
6+
from hypothesis.strategies import integers
7+
from axelrod.tests.property import strategy_lists
8+
9+
C, D = axelrod.Actions.C, axelrod.Actions.D
10+
11+
deterministic_strategies = [s for s in axelrod.ordinary_strategies
12+
if not s().classifier['stochastic']] # Well behaved strategies
13+
14+
class TestMatchOutcomes(unittest.TestCase):
15+
16+
@given(strategies=strategy_lists(strategies=deterministic_strategies,
17+
min_size=2, max_size=2),
18+
turns=integers(min_value=1, max_value=20))
19+
def test_outcome_repeats(self, strategies, turns):
20+
"""A test that if we repeat 3 matches with deterministic and well
21+
behaved strategies then we get the same result"""
22+
players = [s() for s in strategies]
23+
matches = [axelrod.Match(players, turns) for _ in range(3)]
24+
self.assertEqual(matches[0].play(), matches[1].play())
25+
self.assertEqual(matches[1].play(), matches[2].play())

axelrod/tests/unit/test_finite_state_machines.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,12 @@ def test_transitions(self):
111111
fsm = player.fsm
112112
self.assertTrue(check_state_transitions(fsm.state_transitions))
113113

114+
def test_reset_initial_state(self):
115+
player = self.player()
116+
player.fsm.state = -1
117+
player.reset()
118+
self.assertFalse(player.fsm.state == -1)
119+
114120

115121
class TestFortress3(TestFSMPlayer):
116122

0 commit comments

Comments
 (0)