|
2 | 2 | import itertools
|
3 | 3 | import pickle
|
4 | 4 | import types
|
| 5 | +import warnings |
| 6 | + |
5 | 7 | import numpy as np
|
6 | 8 |
|
7 | 9 | from hypothesis import given, settings
|
@@ -452,45 +454,34 @@ def test_reset_clone(self):
|
452 | 454 | clone = player.clone()
|
453 | 455 | self.assertEqual(player, clone)
|
454 | 456 |
|
455 |
| - def test_reproducibility_of_play(self): |
456 |
| - player = self.player() |
457 |
| - player_clone = player.clone() |
458 |
| - coplayer = axl.Random(0.5) |
459 |
| - coplayer_clone = coplayer.clone() |
460 |
| - m1 = axl.Match((player, coplayer), turns=10, seed=10) |
461 |
| - m2 = axl.Match((player_clone, coplayer_clone), turns=10, seed=10) |
462 |
| - m1.play() |
463 |
| - m2.play() |
464 |
| - self.assertEqual(m1.result, m2.result) |
465 |
| - |
466 |
| - @given(seed=integers(min_value=1, max_value=20000000)) |
467 |
| - @settings(max_examples=1) |
468 |
| - def test_clone(self, seed): |
| 457 | + @given(seed=integers(min_value=1, max_value=20000000), |
| 458 | + turns=integers(min_value=5, max_value=10), |
| 459 | + noise=integers(min_value=0, max_value=10)) |
| 460 | + @settings(max_examples=1, deadline=None) |
| 461 | + def test_clone_reproducible_play(self, seed, turns, noise): |
469 | 462 | # Test that the cloned player produces identical play
|
470 |
| - player1 = self.player() |
471 |
| - if player1.name in ["Darwin", "Human"]: |
| 463 | + player = self.player() |
| 464 | + if player.name in ["Darwin", "Human"]: |
472 | 465 | # Known exceptions
|
473 | 466 | return
|
474 |
| - player2 = player1.clone() |
475 |
| - self.assertEqual(len(player2.history), 0) |
476 |
| - self.assertEqual(player2.cooperations, 0) |
477 |
| - self.assertEqual(player2.defections, 0) |
478 |
| - self.assertEqual(player2.state_distribution, {}) |
479 |
| - self.assertEqual(player2.classifier, player1.classifier) |
480 |
| - self.assertEqual(player2.match_attributes, player1.match_attributes) |
481 | 467 |
|
482 |
| - turns = 5 |
483 | 468 | for op in [
|
484 | 469 | axl.Cooperator(),
|
485 | 470 | axl.Defector(),
|
486 | 471 | axl.TitForTat(),
|
487 | 472 | axl.Random(p=0.5),
|
488 | 473 | ]:
|
489 |
| - for p in [player1, player2]: |
490 |
| - m = axl.Match((p, op), turns=turns, reset=True, seed=seed) |
491 |
| - m.play() |
492 |
| - self.assertEqual(len(player1.history), turns) |
493 |
| - self.assertEqual(player1.history, player2.history) |
| 474 | + player = self.player() |
| 475 | + player_clone = player.clone() |
| 476 | + op = op.clone() |
| 477 | + op_clone = op.clone() |
| 478 | + m1 = axl.Match((player, op), turns=turns, seed=seed, noise=noise/100.) |
| 479 | + m2 = axl.Match((player_clone, op_clone), turns=turns, seed=seed, noise=noise/100.) |
| 480 | + m1.play() |
| 481 | + m2.play() |
| 482 | + self.assertEqual(m1.result, m2.result) |
| 483 | + self.assertEqual(player, player_clone) |
| 484 | + self.assertEqual(op, op_clone) |
494 | 485 |
|
495 | 486 | @given(
|
496 | 487 | strategies=strategy_lists(
|
@@ -582,6 +573,10 @@ def versus_test(
|
582 | 573 | match_attributes=match_attributes,
|
583 | 574 | seed=seed
|
584 | 575 | )
|
| 576 | + if match._stochastic and (seed is None): |
| 577 | + warnings.warn( |
| 578 | + "Test Match in TestPlayer.versus_test is stochastic " |
| 579 | + "but no random seed was given.") |
585 | 580 | self.assertEqual(match.play(), expected_actions)
|
586 | 581 |
|
587 | 582 | if attrs:
|
@@ -645,8 +640,12 @@ def versus_test(
|
645 | 640 | raise ValueError("Mismatched History lengths.")
|
646 | 641 | turns = len(expected_actions1)
|
647 | 642 | match = axl.Match((player1, player2), turns=turns, noise=noise, seed=seed)
|
648 |
| - match.play() |
649 |
| - self.assertEqual(match.result, list(zip(expected_actions1, expected_actions2))) |
| 643 | + if match._stochastic and (seed is None): |
| 644 | + warnings.warn( |
| 645 | + "Test Match in TestMatch.versus_test is stochastic " |
| 646 | + "but no random seed was given.") |
| 647 | + result = match.play() |
| 648 | + self.assertEqual(result, list(zip(expected_actions1, expected_actions2))) |
650 | 649 |
|
651 | 650 | def test_versus_with_incorrect_history_lengths(self):
|
652 | 651 | """Test the error raised by versus_test if expected actions do not
|
|
0 commit comments