Skip to content

Commit

Permalink
Improve extend method in LimitedHistory to enforce memory depth (#1458)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
LindyZh authored Dec 9, 2024
1 parent d2184c6 commit a7bab91
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
10 changes: 9 additions & 1 deletion axelrod/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ def __init__(self, memory_depth, plays=None, coplays=None):
memory_depth, int:
length of history to retain
"""
super().__init__(plays=plays, coplays=coplays)
self.memory_depth = memory_depth
super().__init__(plays=plays, coplays=coplays)

def flip_plays(self):
"""Creates a flipped plays history for use with DualTransformer."""
Expand All @@ -138,3 +138,11 @@ def append(self, play, coplay):
first_play, first_coplay = self._plays.pop(0), self._coplays.pop(0)
self._actions[first_play] -= 1
self._state_distribution[(first_play, first_coplay)] -= 1

def extend(self, new_plays, new_coplays):
"""A function that emulates list.extend, respecting the stated memory depth."""
self._plays.extend(new_plays)
self._coplays.extend(new_coplays)
if len(self._plays) > self.memory_depth:
self._plays = self._plays[-self.memory_depth :]
self._coplays = self._coplays[-self.memory_depth :]
4 changes: 2 additions & 2 deletions axelrod/tests/unit/test_game.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import unittest

import numpy as np
from hypothesis import given, settings
from hypothesis import HealthCheck, given, settings
from hypothesis.extra.numpy import array_shapes, arrays
from hypothesis.strategies import integers

Expand Down Expand Up @@ -123,7 +123,7 @@ def test_random_repr(self, asymgame):
self.assertEqual(expected_repr, str(asymgame))

@given(asymgame1=asymmetric_games(), asymgame2=asymmetric_games())
@settings(max_examples=5)
@settings(max_examples=5, suppress_health_check=(HealthCheck.too_slow,))
def test_equality(self, asymgame1, asymgame2):
"""Tests equality of AsymmetricGames based on their matrices."""
self.assertFalse(asymgame1 == "foo")
Expand Down
11 changes: 11 additions & 0 deletions axelrod/tests/unit/test_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,14 @@ def test_memory_depth(self):
h.state_distribution,
Counter({(D, D): 1, (C, D): 1, (D, C): 1, (C, C): 0}),
)

def test_extend(self):
h1 = LimitedHistory(3, plays=[C, C, D], coplays=[C, C, C])
self.assertEqual(list(h1), [C, C, D])
h1.extend([C, C], [D, D])
self.assertEqual(list(h1), [D, C, C])
h1.extend([D, C], [D, D])
self.assertEqual(list(h1), [C, D, C])
h1.memory_depth = 4
h1.extend([D, C], [D, D])
self.assertEqual(list(h1), [D, C, D, C])

0 comments on commit a7bab91

Please sign in to comment.