Skip to content

Commit 39d08c5

Browse files
bing-jmarcharper
authored andcommitted
Fixed update_rewards() to correctly refer to attributes, added test file
1 parent 22d9ad0 commit 39d08c5

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

axelrod/strategies/epsilon_greedy.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,20 @@ def update_rewards(self, opponent: Player):
7070
last_score = game.score(last_round)[0]
7171

7272
# update the expected rewards based on previous play
73-
num_plays = (
74-
self.cooperations() if last_play == C else self.defections()
75-
)
73+
if last_play == C:
74+
num_plays = self.history.cooperations
75+
else:
76+
num_plays = self.history.defections
77+
7678
self._rewards[last_play] = self._rewards[last_play] + (
7779
1 / num_plays
7880
) * (last_score - self._rewards[last_play])
7981

8082
def strategy(self, opponent: Player) -> Action:
8183
"""Actual strategy definition that determines player's action."""
84+
# if not the first turn
85+
if len(self.history) != 0:
86+
self.update_rewards(opponent)
8287

8388
# explore
8489
if self._random.uniform(0.0, 1.0) <= self.epsilon:
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""Tests for the epsilon greedy strategy."""
2+
3+
import axelrod as axl
4+
5+
from .test_player import TestPlayer
6+
7+
C, D = axl.Action.C, axl.Action.D
8+
9+
10+
class TestEpsilonGreedy(TestPlayer):
11+
12+
name = "$\varepsilon$-greedy"
13+
player = axl.EpsilonGreedy
14+
expected_classifier = {
15+
"memory_depth": float("inf"),
16+
"stochastic": True,
17+
"makes_use_of": {"game"},
18+
"long_run_time": False,
19+
"inspects_source": False,
20+
"manipulates_source": False,
21+
"manipulates_state": False,
22+
}
23+
24+
def test_random(self):
25+
"""Test that strategy is randomly picked (not affected by history)."""
26+
opponent = axl.MockPlayer()
27+
actions = [(C, C), (C, D), (C, D), (C, C), (C, D)]
28+
self.versus_test(opponent, expected_actions=actions, seed=0, init_kwargs={"epsilon": 1})

0 commit comments

Comments
 (0)