-
Notifications
You must be signed in to change notification settings - Fork 270
SteinAndRapoport Strategy #1012
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
204d498
SteinAndRapoport Strategy
MariosZoulias 100e511
New changes
MariosZoulias 23840d8
More changes
MariosZoulias dc0fb13
More and More Changes
MariosZoulias 5a6cc56
Change build command for appveyor
drvinceknight 9c03615
Correct SteinRap strategy and add relevant test.
drvinceknight b72e03c
Merge pull request #1 from Axelrod-Python/MariosZoulias-SteinRap-vk
MariosZoulias File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,18 @@ | ||
environment: | ||
matrix: | ||
- PYTHON: "C:\\Python35" | ||
- PYTHON: "C:\\Python36" | ||
- PYTHON_VERSION: "3.5" | ||
MINICONDA: "C:\\Miniconda35" | ||
- PYTHON_VERSION: "3.6" | ||
MINICONDA: "C:\\Miniconda36" | ||
install: | ||
- "%PYTHON%\\python.exe -m pip install -r requirements.txt" | ||
- "set PATH=%MINICONDA%;%MINICONDA%\\Scripts;%PATH%" | ||
- "conda config --set always_yes yes --set changeps1 no" | ||
- "conda update -q conda" | ||
- "conda create -q -n test-environment python=%PYTHON_VERSION% scipy>=0.19.0 numpy>=1.9.2" | ||
- "activate test-environment" | ||
- "python -m pip install -r requirements.txt" | ||
build: off | ||
test_script: | ||
- "%PYTHON%\\python.exe -m unittest discover" | ||
- "%PYTHON%\\python.exe doctests.py" | ||
- "%PYTHON%\\python.exe setup.py install" | ||
- "python -m unittest discover" | ||
- "python doctests.py" | ||
- "python setup.py install" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,8 @@ | |
from axelrod.player import Player | ||
from axelrod.random_ import random_choice | ||
from.memoryone import MemoryOnePlayer | ||
from axelrod.strategy_transformers import FinalTransformer | ||
from scipy.stats import chisquare | ||
|
||
from typing import List, Dict, Tuple | ||
|
||
|
@@ -487,3 +489,67 @@ class UnnamedStrategy(Player): | |
def strategy(opponent: Player) -> Action: | ||
r = random.uniform(3, 7) / 10 | ||
return random_choice(r) | ||
|
||
@FinalTransformer((D, D), name_prefix=None) | ||
class SteinAndRapoport(Player): | ||
""" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't quite formatted correctly (see: http://axelrod.readthedocs.io/en/stable/tutorials/contributing/strategy/docstrings.html). Can you remove one indentation level for the description (it should be at the same level as the Then can you add:
|
||
A player who plays according to statistic methods. | ||
Begins by playing C for the first four (4) rounds, then it plays | ||
tit for tat and at the last 2 round it Defects. Every 15 turns it | ||
runs a chi-squared test to check whether the opponent behaves randomly | ||
or not. In case the opponent behaves randomly then Stein and Rapoport | ||
Defects until the next 15 round (where we check again), otherwise it | ||
still plays TitForTat.0 | ||
|
||
Names: | ||
|
||
- SteinAndRapoport [Axelrod1980]_ | ||
""" | ||
|
||
name = 'Stein and Rapoport' | ||
classifier = { | ||
'memory_depth': 15, | ||
'stochastic': False, | ||
'makes_use_of': {"length"}, | ||
'long_run_time': False, | ||
'inspects_source': False, | ||
'manipulates_source': False, | ||
'manipulates_state': False | ||
} | ||
|
||
def __init__(self, alpha: float=0.05) -> None: | ||
""" | ||
Parameters | ||
---------- | ||
alpha, float | ||
The significant level of pvalue from chi-squared test | ||
0.05 by default according to literature | ||
""" | ||
super().__init__() | ||
self.alpha = alpha | ||
self.opponent_is_random = None | ||
|
||
def strategy(self , opponent: Player) -> Action: | ||
round_number = len(self.history) + 1 | ||
|
||
# First 4 moves | ||
if round_number < 5: | ||
return C | ||
# For first 15 rounds tit for tat as we do not know opponents strategy | ||
elif round_number < 15: | ||
return opponent.history[-1] | ||
|
||
if round_number % 15 == 0: | ||
p_value = chisquare([opponent.cooperations, | ||
opponent.defections]).pvalue | ||
self.opponent_is_random = p_value >= self.alpha | ||
|
||
if self.opponent_is_random: | ||
# Defect if opponent plays randomly | ||
return D | ||
else: # TitForTatat if opponent plays not randomly | ||
return opponent.history[-1] | ||
|
||
def reset(self): | ||
super().reset() | ||
self.random_opponent = None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ matplotlib>=1.4.2 | |
hypothesis>=3.2 | ||
tqdm>=3.4.0 | ||
prompt-toolkit>=1.0.7 | ||
scipy>=0.19.0 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is a little clumsy and not as clear as it could be. How about:
"""Resets a player to its initial state
This method is called at the beginning of each match (between a pair of players) to reset a player's state to its initial starting point. It ensures that no 'memory' of previous matches is carried forward.
The default method resets a player's history, cooperations, defections and state_distribution. Players which have further attributes need to override this method and ensure those additional attributes are also reset.
"""
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry @MariosZoulias, this has turned up in this PR because I put a line break on line 244 (I shouldn't have done that), the text wasn't changed by either of us but if you could make the improvements suggested by @meatballs that would be helpful. This file is in
Axelrod/axelrod/player.py
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's sort this out separately. I'll merge this PR and put another one in for the docstring