Skip to content
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

Draft
wants to merge 5 commits into
base: dev
Choose a base branch
from
Draft

Conversation

jodoyle29
Copy link

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.

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%.
Copy link
Member

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).

Suggested change
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%.

Copy link
Member

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%.
Copy link
Member

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

Suggested change
MAX = 100
denominator = 100


else:
if opponent.history[-1] == D:
self.probability += 1/MAX
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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
Copy link
Member

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.

Comment on lines 53 to 55
else:
print("There has been an error")
return self.history[-1]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
else:
print("There has been an error")
return self.history[-1]
return self.history[-1]

Copy link
Member

@drvinceknight drvinceknight left a 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.

@jodoyle29
Copy link
Author

Thank you for the feedback. I will make these modifications and work on some tests within the next few days.

@jodoyle29 jodoyle29 marked this pull request as draft April 27, 2023 13:09
Fixed documentation suggestions and PEP8 requirements
@jodoyle29
Copy link
Author

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)?

@marcharper
Copy link
Member

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.

@drvinceknight
Copy link
Member

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).

@marcharper
Copy link
Member

See also the code snippet in #1353

@drvinceknight
Copy link
Member

See also the code snippet in #1353

Gosh I'd forgotten about that! Yes!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants