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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions axelrod/strategies/_strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@
from .mutual import Desperate, Hopeless, Willing
from .negation import Negation
from .oncebitten import FoolMeOnce, ForgetfulFoolMeOnce, OnceBitten
from .probabilistic_hill_climb import ProbabilisticHillClimb
from .prober import (
CollectiveStrategy,
Detective,
Expand Down Expand Up @@ -423,6 +424,7 @@
PSOGamblerMem1,
Pi,
Predator,
ProbabilisticHillClimb,
Prober,
Prober2,
Prober3,
Expand Down
59 changes: 59 additions & 0 deletions axelrod/strategies/probabilistic_hill_climb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from axelrod.action import Action
from axelrod.player import Player

C, D = Action.C, Action.D

class ProbabilisticHillClimb(Player):
"""
Defects with initial probability of 50%.
Every time the oppenent deffects, probability becomes '(100 + 1)/100', increasing by 1%.
Every time the opponent cooperates, probability becomes '(100 - 1)/100', decreasing by 1%.
In case of error (conditions aren't triggered, repeat last move)

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 cooperate, it is better to cooperate.
Using a simple probabilistic approach, we can predict the opponents next move and chose
to cooperate/defect accordingly.

Hill climbing algorithms can be prone to being 'stuck' in local minima. To avoid this we
introduce some randomness, that is, if the probability of defection becomes equal to or
greater than 1 ( >= 100%), the probability of defection will be reset to 50% to avoid this.
For example: Think of this strategy playing against another strategy such as Tit-For-Tat.
If the probability of defection becomes too high, then (without adding radomness) both
ProbabilistciHillClimbing and Tit-For-Tat will infinitely defect, leading to a worse outcome.
"""

name = "Hill Climb"
classifier = {
"memory_depth": float("inf"), # Long memory
"stochastic": True,
"long_run_time": False,
"inspects_source": False,
"manipulates_source": False,
"manipulates_state": False,
}

def __init__(self) -> None:
super().__init__()
self.probability_of_defection = 0.5

def strategy(self, opponent: Player) -> Action:
"""Actual strategy definition that determines player's action."""

if len(opponent.history) == 0:
return C

else:
if opponent.history[-1] == D:
self.probability_of_defection += 1 / 100
if(self.probability_of_defection >= 1):
self.probability_of_defection = 0.5
return self._random.random_choice(self.probability_of_defection)

if opponent.history[-1] == C:
self.probability_of_defection -= 1 / 100
if(self.probability_of_defection <= 0):
self.probability_of_defection = 0.1 # avoid crash
return self._random.random_choice(self.probability_of_defection)

return self.history[-1]
2 changes: 2 additions & 0 deletions docs/reference/strategy_index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ Here are the docstrings of all the strategies in the library.
:members:
.. automodule:: axelrod.strategies.oncebitten
:members:
.. automodule:: axelrod.strategies.probabilistic_hill_climb
:members:
.. automodule:: axelrod.strategies.prober
:members:
.. automodule:: axelrod.strategies.punisher
Expand Down