-
Notifications
You must be signed in to change notification settings - Fork 268
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
Jodoyle29 patch 1 #1417
base: dev
Are you sure you want to change the base?
Jodoyle29 patch 1 #1417
Conversation
Add probabilistic_hill_climb to the strategies folder
Added ProbabilisticHillClimb to the list of all strategies and added the import for probabilistic_hill_climb
Added probabilistic_hill_climb
""" | ||
Defects with probability of 50%. | ||
Every time the oppenent deffects, probability becomes '(100 + 1)/100', increasing by 1%. | ||
Every time the opponent confesses, probability becomes '(100 - 1)/100', decreasing by 1%. |
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.
The C
in the IPD stands for cooperate (confessing is technically the defection).
Every time the opponent confesses, probability becomes '(100 - 1)/100', decreasing by 1%. | |
Every time the opponent cooperates, probability becomes '(100 - 1)/100', decreasing by 1%. |
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.
(This will need to be modified throughout.)
|
||
class ProbabilisticHillClimb(Player): | ||
""" | ||
Defects with probability of 50%. |
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.
This strategy defects with a given probability that changes over time. It's initial probability of defection is 0.5.
|
||
def __init__(self) -> None: | ||
super().__init__() | ||
self.probability = 0.5 |
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.
self.probability = 0.5 | |
self.probability_of_defection = 0.5 |
def strategy(self, opponent: Player) -> Action: | ||
"""Actual strategy definition that determines player's action.""" | ||
MAX = 100 | ||
if not len(opponent.history): # if opponent has no previous moves, confess on first move |
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.
if not len(opponent.history): # if opponent has no previous moves, confess on first move | |
if len(opponent.history) == 0: # if opponent has no previous moves, cooperate on first move |
|
||
def strategy(self, opponent: Player) -> Action: | ||
"""Actual strategy definition that determines player's action.""" | ||
MAX = 100 |
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.
MAX = 100 |
Based on the doctoring and that this is not a usable parameter I'd suggest just removing it.
If you did want to include it then you'd need to change it to match PEP8.
MAX = 100 | |
denominator = 100 |
|
||
else: | ||
if opponent.history[-1] == D: | ||
self.probability += 1/MAX |
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.
self.probability += 1/MAX | |
self.probability += 1 / MAX |
if opponent.history[-1] == D: | ||
self.probability += 1/MAX | ||
if(self.probability >= 1): | ||
self.probability = 0.5 # to excape local maxima |
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'm not sure the comment is precise here. Could you describe the behaviour in the doctoring please.
else: | ||
print("There has been an error") | ||
return self.history[-1] |
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.
else: | |
print("There has been an error") | |
return self.history[-1] | |
return self.history[-1] |
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.
Interesting strategy.
I've left some documentation suggestions and minor PEP8 requirements.
There are no tests for your strategy which will be needed to merge any PR. There is some information on this here: https://axelrod.readthedocs.io/en/stable/how-to/contributing/strategy/writing_test_for_the_new_strategy.html
If you need any guidance let us know.
Thank you for the feedback. I will make these modifications and work on some tests within the next few days. |
Fixed documentation suggestions and PEP8 requirements
I believe I will need some guidance for writing the tests. How can I test for expected behaviour if the behaviour is probability based (I don't know exactly what my strategy will do at each move, I only know what it will probably do)? |
You can fix a random seed (or search for seeds) that trigger the expected behaviors. We strive for 100% coverage of the possible behaviors, if possible. |
You can see an example of this here: https://github.com/Axelrod-Python/Axelrod/blob/dev/axelrod/tests/strategies/test_memorytwo.py#L99 (there are a number of other stochastic strategies throughout the library). |
See also the code snippet in #1353 |
Gosh I'd forgotten about that! Yes!!! |
Adding a probabilistic hill climbing algorithm.
This strategy is based on the following assumption: if the opponent is going to defect, it is better to defect. If the opponent is going to confess, it is better to confess. Using a simple probabilistic approach, we can predict the opponents next move and chose to confess/defect accordingly.