|
3 | 3 | import axelrod
|
4 | 4 | from .test_player import TestHeadsUp, TestPlayer
|
5 | 5 |
|
| 6 | +from hypothesis import given |
| 7 | +from hypothesis.strategies import integers |
| 8 | +from axelrod.tests.property import strategy_lists |
| 9 | + |
| 10 | +import random |
| 11 | + |
6 | 12 | C, D = axelrod.Actions.C, axelrod.Actions.D
|
7 | 13 |
|
8 | 14 |
|
@@ -362,3 +368,72 @@ def test_output_from_literature(self):
|
362 | 368 | match = axelrod.Match((player, opp2), 1000)
|
363 | 369 | match.play()
|
364 | 370 | self.assertEqual(match.final_score(), (3472, 767))
|
| 371 | + |
| 372 | + |
| 373 | +class TestContriteTitForTat(TestPlayer): |
| 374 | + |
| 375 | + name = "Contrite Tit For Tat" |
| 376 | + player = axelrod.ContriteTitForTat |
| 377 | + expected_classifier = { |
| 378 | + 'memory_depth': 3, |
| 379 | + 'stochastic': False, |
| 380 | + 'makes_use_of': set(), |
| 381 | + 'inspects_source': False, |
| 382 | + 'manipulates_source': False, |
| 383 | + 'manipulates_state': False |
| 384 | + } |
| 385 | + |
| 386 | + deterministic_strategies = [s for s in axelrod.ordinary_strategies |
| 387 | + if not s().classifier['stochastic']] |
| 388 | + |
| 389 | + @given(strategies=strategy_lists(strategies=deterministic_strategies, |
| 390 | + max_size=1), |
| 391 | + turns=integers(min_value=1, max_value=20)) |
| 392 | + def test_is_tit_for_tat_with_no_noise(self, strategies, turns): |
| 393 | + tft = axelrod.TitForTat() |
| 394 | + ctft = self.player() |
| 395 | + opponent = strategies[0]() |
| 396 | + m1 = axelrod.Match((tft, opponent), turns) |
| 397 | + m2 = axelrod.Match((ctft, opponent), turns) |
| 398 | + self.assertEqual(m1.play(), m2.play()) |
| 399 | + |
| 400 | + def test_strategy_with_noise(self): |
| 401 | + ctft = self.player() |
| 402 | + opponent = axelrod.Defector() |
| 403 | + self.assertEqual(ctft.strategy(opponent), C) |
| 404 | + self.assertEqual(ctft._recorded_history, [C]) |
| 405 | + ctft.reset() # Clear the recorded history |
| 406 | + self.assertEqual(ctft._recorded_history, []) |
| 407 | + |
| 408 | + random.seed(0) |
| 409 | + ctft.play(opponent, noise=.9) |
| 410 | + self.assertEqual(ctft.history, [D]) |
| 411 | + self.assertEqual(ctft._recorded_history, [C]) |
| 412 | + self.assertEqual(opponent.history, [C]) |
| 413 | + |
| 414 | + # After noise: is contrite |
| 415 | + ctft.play(opponent) |
| 416 | + self.assertEqual(ctft.history, [D, C]) |
| 417 | + self.assertEqual(ctft._recorded_history, [C, C]) |
| 418 | + self.assertEqual(opponent.history, [C, D]) |
| 419 | + self.assertTrue(ctft.contrite) |
| 420 | + |
| 421 | + # Cooperates and no longer contrite |
| 422 | + ctft.play(opponent) |
| 423 | + self.assertEqual(ctft.history, [D, C, C]) |
| 424 | + self.assertEqual(ctft._recorded_history, [C, C, C]) |
| 425 | + self.assertEqual(opponent.history, [C, D, D]) |
| 426 | + self.assertFalse(ctft.contrite) |
| 427 | + |
| 428 | + # Goes back to playing tft |
| 429 | + ctft.play(opponent) |
| 430 | + self.assertEqual(ctft.history, [D, C, C, D]) |
| 431 | + self.assertEqual(ctft._recorded_history, [C, C, C, D]) |
| 432 | + self.assertEqual(opponent.history, [C, D, D, D]) |
| 433 | + self.assertFalse(ctft.contrite) |
| 434 | + |
| 435 | + def test_reset_cleans_all(self): |
| 436 | + p = self.player() |
| 437 | + p.contrite = True |
| 438 | + p.reset() |
| 439 | + self.assertFalse(p.contrite) |
0 commit comments