|
1 | 1 | """Tests for the tit for tat strategies."""
|
2 | 2 |
|
3 | 3 | import random
|
| 4 | + |
| 5 | +import copy |
4 | 6 | from hypothesis import given
|
5 | 7 | from hypothesis.strategies import integers
|
6 | 8 | import axelrod
|
@@ -79,10 +81,14 @@ class TestTitFor2Tats(TestPlayer):
|
79 | 81 | }
|
80 | 82 |
|
81 | 83 | def test_strategy(self):
|
82 |
| - # Will punish sequence of 2 defections but will forgive |
| 84 | + # Will punish sequence of 2 defections but will forgive one |
83 | 85 | opponent = axelrod.MockPlayer(actions=[D, D, D, C, C])
|
84 | 86 | actions = [(C, D), (C, D), (D, D), (D, C), (C, C), (C, D)]
|
85 | 87 | self.versus_test(opponent, expected_actions=actions)
|
| 88 | + opponent = axelrod.MockPlayer(actions=[C, C, D, D, C, D, D, C, C, D, D]) |
| 89 | + actions = [(C, C), (C, C), (C, D), (C, D), (D, C), (C, D), (C, D), |
| 90 | + (D, C), (C, C), (C, D), (C, D)] |
| 91 | + self.versus_test(opponent, expected_actions=actions) |
86 | 92 |
|
87 | 93 |
|
88 | 94 | class TestTwoTitsForTat(TestPlayer):
|
@@ -473,28 +479,6 @@ def test_strategy_with_noise(self):
|
473 | 479 | self.assertFalse(ctft.contrite)
|
474 | 480 |
|
475 | 481 |
|
476 |
| -class TestSlowTitForTwoTats(TestPlayer): |
477 |
| - |
478 |
| - name = "Slow Tit For Two Tats" |
479 |
| - player = axelrod.SlowTitForTwoTats |
480 |
| - expected_classifier = { |
481 |
| - 'memory_depth': 2, |
482 |
| - 'stochastic': False, |
483 |
| - 'makes_use_of': set(), |
484 |
| - 'inspects_source': False, |
485 |
| - 'manipulates_source': False, |
486 |
| - 'manipulates_state': False |
487 |
| - } |
488 |
| - |
489 |
| - def test_strategy(self): |
490 |
| - # If opponent plays the same move twice, repeats last action of |
491 |
| - # opponent history. |
492 |
| - opponent = axelrod.MockPlayer(actions=[C, C, D, D, C, D, D, C, C, D, D]) |
493 |
| - actions = [(C, C), (C, C), (C, D), (C, D), (D, C), (C, D), (C, D), |
494 |
| - (D, C), (C, C), (C, D), (C, D)] |
495 |
| - self.versus_test(opponent, expected_actions=actions) |
496 |
| - |
497 |
| - |
498 | 482 | class TestAdaptiveTitForTat(TestPlayer):
|
499 | 483 |
|
500 | 484 | name = "Adaptive Tit For Tat: 0.5"
|
@@ -642,3 +626,94 @@ def test_strategy(self):
|
642 | 626 | actions = [(C, D), (D, C), (C, D), (D, D),
|
643 | 627 | (D, D), (D, D), (D, C), (D, C)]
|
644 | 628 | self.versus_test(opponent, expected_actions=actions)
|
| 629 | + |
| 630 | + |
| 631 | + |
| 632 | +class TestNTitsForMTats(TestPlayer): |
| 633 | + """ |
| 634 | + Tests for the N Tit(s) For M Tat(s) strategy |
| 635 | + """ |
| 636 | + |
| 637 | + name = 'N Tit(s) For M Tat(s): 3, 2' |
| 638 | + player = axelrod.NTitsForMTats |
| 639 | + expected_classifier = { |
| 640 | + 'memory_depth': 3, |
| 641 | + 'stochastic': False, |
| 642 | + 'makes_use_of': set(), |
| 643 | + 'long_run_time': False, |
| 644 | + 'inspects_source': False, |
| 645 | + 'manipulates_source': False, |
| 646 | + 'manipulates_state': False |
| 647 | + } |
| 648 | + |
| 649 | + expected_class_classifier = copy.copy(expected_classifier) |
| 650 | + expected_class_classifier['memory_depth'] = float('inf') |
| 651 | + |
| 652 | + def test_strategy(self): |
| 653 | + # TitForTat test_strategy |
| 654 | + init_kwargs = {'N': 1, 'M': 1} |
| 655 | + actions = [(C, C), (C, D), (D, C), (C, D), (D, C)] |
| 656 | + self.versus_test(axelrod.Alternator(), expected_actions=actions, init_kwargs=init_kwargs) |
| 657 | + actions = [(C, C), (C, C), (C, C), (C, C), (C, C)] |
| 658 | + self.versus_test(axelrod.Cooperator(), expected_actions=actions, init_kwargs=init_kwargs) |
| 659 | + actions = [(C, D), (D, D), (D, D), (D, D), (D, D)] |
| 660 | + self.versus_test(axelrod.Defector(), expected_actions=actions, init_kwargs=init_kwargs) |
| 661 | + actions = [(C, C), (C, D), (D, C), (C, D), (D, C)] |
| 662 | + self.versus_test(axelrod.Alternator(), expected_actions=actions, |
| 663 | + match_attributes={"length": -1}, init_kwargs=init_kwargs) |
| 664 | + actions = [(C, D), (D, D), (D, C), (C, C), (C, D)] |
| 665 | + self.versus_test(axelrod.Random(), expected_actions=actions, |
| 666 | + seed=0, init_kwargs=init_kwargs) |
| 667 | + actions = [(C, C), (C, D), (D, D), (D, C)] |
| 668 | + self.versus_test(axelrod.Random(), expected_actions=actions, |
| 669 | + seed=1, init_kwargs=init_kwargs) |
| 670 | + opponent = axelrod.MockPlayer(actions=[C, D]) |
| 671 | + actions = [(C, C), (C, D), (D, C), (C, D)] |
| 672 | + self.versus_test(opponent, expected_actions=actions, init_kwargs=init_kwargs) |
| 673 | + opponent = axelrod.MockPlayer(actions=[C, C, D, D, C, D]) |
| 674 | + actions = [(C, C), (C, C), (C, D), (D, D), (D, C), (C, D)] |
| 675 | + self.versus_test(opponent, expected_actions=actions, init_kwargs=init_kwargs) |
| 676 | + |
| 677 | + # TitFor2Tats test_strategy |
| 678 | + init_kwargs = {'N': 1, 'M': 2} |
| 679 | + opponent = axelrod.MockPlayer(actions=[D, D, D, C, C]) |
| 680 | + actions = [(C, D), (C, D), (D, D), (D, C), (C, C), (C, D)] |
| 681 | + self.versus_test(opponent, expected_actions=actions, init_kwargs=init_kwargs) |
| 682 | + |
| 683 | + # TwoTitsForTat test_strategy |
| 684 | + init_kwargs = {'N': 2, 'M': 1} |
| 685 | + opponent = axelrod.MockPlayer(actions=[D, C, C, D, C]) |
| 686 | + actions = [(C, D), (D, C), (D, C), (C, D), (D, C)] |
| 687 | + self.versus_test(opponent, expected_actions=actions, init_kwargs=init_kwargs) |
| 688 | + actions = [(C, C), (C, C)] |
| 689 | + self.versus_test(opponent=axelrod.Cooperator(), |
| 690 | + expected_actions=actions, init_kwargs=init_kwargs) |
| 691 | + actions = [(C, D), (D, D), (D, D)] |
| 692 | + self.versus_test(opponent=axelrod.Defector(), |
| 693 | + expected_actions=actions, init_kwargs=init_kwargs) |
| 694 | + |
| 695 | + # Cooperator test_strategy |
| 696 | + actions = [(C, C)] + [(C, D), (C, C)] * 9 |
| 697 | + self.versus_test(opponent=axelrod.Alternator(), |
| 698 | + expected_actions=actions, init_kwargs={'N': 0, 'M': 1}) |
| 699 | + self.versus_test(opponent=axelrod.Alternator(), |
| 700 | + expected_actions=actions, init_kwargs={'N': 0, 'M': 5}) |
| 701 | + self.versus_test(opponent=axelrod.Alternator(), |
| 702 | + expected_actions=actions, init_kwargs={'N': 0, 'M': 0}) |
| 703 | + |
| 704 | + # Defector test_strategy |
| 705 | + actions = [(D, C)] + [(D, D), (D, C)] * 9 |
| 706 | + self.versus_test(opponent=axelrod.Alternator(), |
| 707 | + expected_actions=actions, init_kwargs={'N': 1, 'M': 0}) |
| 708 | + self.versus_test(opponent=axelrod.Alternator(), |
| 709 | + expected_actions=actions, init_kwargs={'N': 5, 'M': 0}) |
| 710 | + |
| 711 | + # Default init args |
| 712 | + actions = [(C, C), (C, D), (C, D), (D, C), (D, C), (D, D), (C, C)] |
| 713 | + opponent = axelrod.MockPlayer(actions=[acts[1] for acts in actions]) |
| 714 | + self.versus_test(opponent=opponent, expected_actions=actions) |
| 715 | + |
| 716 | + def test_varying_memory_depth(self): |
| 717 | + self.assertEqual(self.player(1, 1).classifier['memory_depth'], 1) |
| 718 | + self.assertEqual(self.player(0, 3).classifier['memory_depth'], 3) |
| 719 | + self.assertEqual(self.player(5, 3).classifier['memory_depth'], 5) |
0 commit comments