Skip to content

Commit a7bab91

Browse files
authored
Improve extend method in LimitedHistory to enforce memory depth (#1458)
* Improve extend method in LimitedHistory to enforce memory depth - Updated the extend method in the LimitedHistory class to properly truncate oldest entries when the combined history exceeds the defined memory depth. - Added a corresponding unit test in axelrod/tests/unit/test_history.py to validate this behavior. - Formatted all committed files using black for consistent code style. * Ignore workflow failures by adding suppress_health_check - add suppress_health_check to test_equality function in test_game.py to prevent workflow failure in Github remote. - Update HealthCheck module import order in test_game.py
1 parent d2184c6 commit a7bab91

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

axelrod/history.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ def __init__(self, memory_depth, plays=None, coplays=None):
117117
memory_depth, int:
118118
length of history to retain
119119
"""
120-
super().__init__(plays=plays, coplays=coplays)
121120
self.memory_depth = memory_depth
121+
super().__init__(plays=plays, coplays=coplays)
122122

123123
def flip_plays(self):
124124
"""Creates a flipped plays history for use with DualTransformer."""
@@ -138,3 +138,11 @@ def append(self, play, coplay):
138138
first_play, first_coplay = self._plays.pop(0), self._coplays.pop(0)
139139
self._actions[first_play] -= 1
140140
self._state_distribution[(first_play, first_coplay)] -= 1
141+
142+
def extend(self, new_plays, new_coplays):
143+
"""A function that emulates list.extend, respecting the stated memory depth."""
144+
self._plays.extend(new_plays)
145+
self._coplays.extend(new_coplays)
146+
if len(self._plays) > self.memory_depth:
147+
self._plays = self._plays[-self.memory_depth :]
148+
self._coplays = self._coplays[-self.memory_depth :]

axelrod/tests/unit/test_game.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import unittest
22

33
import numpy as np
4-
from hypothesis import given, settings
4+
from hypothesis import HealthCheck, given, settings
55
from hypothesis.extra.numpy import array_shapes, arrays
66
from hypothesis.strategies import integers
77

@@ -123,7 +123,7 @@ def test_random_repr(self, asymgame):
123123
self.assertEqual(expected_repr, str(asymgame))
124124

125125
@given(asymgame1=asymmetric_games(), asymgame2=asymmetric_games())
126-
@settings(max_examples=5)
126+
@settings(max_examples=5, suppress_health_check=(HealthCheck.too_slow,))
127127
def test_equality(self, asymgame1, asymgame2):
128128
"""Tests equality of AsymmetricGames based on their matrices."""
129129
self.assertFalse(asymgame1 == "foo")

axelrod/tests/unit/test_history.py

+11
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,14 @@ def test_memory_depth(self):
123123
h.state_distribution,
124124
Counter({(D, D): 1, (C, D): 1, (D, C): 1, (C, C): 0}),
125125
)
126+
127+
def test_extend(self):
128+
h1 = LimitedHistory(3, plays=[C, C, D], coplays=[C, C, C])
129+
self.assertEqual(list(h1), [C, C, D])
130+
h1.extend([C, C], [D, D])
131+
self.assertEqual(list(h1), [D, C, C])
132+
h1.extend([D, C], [D, D])
133+
self.assertEqual(list(h1), [C, D, C])
134+
h1.memory_depth = 4
135+
h1.extend([D, C], [D, D])
136+
self.assertEqual(list(h1), [D, C, D, C])

0 commit comments

Comments
 (0)