Skip to content

Remove cheating strategies #1388

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 14 commits into from
May 24, 2021
Merged
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
128 changes: 0 additions & 128 deletions axelrod/_strategy_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,134 +45,6 @@ def detect_cycle(history, min_size=1, max_size=12, offset=0):
return None


def inspect_strategy(inspector, opponent):
"""Inspects the strategy of an opponent.

Simulate one round of play with an opponent, unless the opponent has
an inspection countermeasure.

Parameters
----------
inspector: Player
The player doing the inspecting
opponent: Player
The player being inspected

Returns
-------
Action
The action that would be taken by the opponent.
"""
if hasattr(opponent, "foil_strategy_inspection"):
return opponent.foil_strategy_inspection()
else:
return opponent.strategy(inspector)


def _limited_simulate_play(player_1, player_2, h1):
"""Simulates a player's move.

After inspecting player_2's next move (allowing player_2's strategy
method to set any internal variables as needed), update histories
for both players. Note that player_1's move is an argument.

If you need a more complete simulation, see `simulate_play` in
player.py. This function is specifically designed for the needs
of MindReader.

Parameters
----------
player_1: Player
The player whose move is already known.
player_2: Player
The player the we want to inspect.
h1: Action
The next action for first player.
"""
h2 = inspect_strategy(player_1, player_2)
player_1.update_history(h1, h2)
player_2.update_history(h2, h1)


def simulate_match(player_1, player_2, strategy, rounds=10):
"""Simulates a number of rounds with a constant strategy.

Parameters
----------
player_1: Player
The player that will have a constant strategy.
player_2: Player
The player we want to simulate.
strategy: Action
The constant strategy to use for first player.
rounds: int
The number of rounds to play.
"""
for match in range(rounds):
_limited_simulate_play(player_1, player_2, strategy)


def _calculate_scores(p1, p2, game):
"""Calculates the scores for two players based their history.

Parameters
----------
p1: Player
The first player.
p2: Player
The second player.
game: Game
Game object used to score rounds in the players' histories.

Returns
-------
int, int
The scores for the two input players.
"""
s1, s2 = 0, 0
for pair in zip(p1.history, p2.history):
score = game.score(pair)
s1 += score[0]
s2 += score[1]
return s1, s2


def look_ahead(player1, player2, game, rounds=10):
"""Returns a constant action that maximizes score by looking ahead.

Parameters
----------
player_1: Player
The player that will look ahead.
player_2: Player
The opponent that will be inspected.
game: Game
The Game object used to score rounds.
rounds: int
The number of rounds to look ahead.

Returns
-------
Action
The action that maximized score if it is played constantly.
"""
results = {}
possible_strategies = {C: Cooperator(), D: Defector()}
for action, player in possible_strategies.items():
# Instead of a deepcopy, create a new opponent and replay the history to it.
opponent_ = player2.clone()
if opponent_.classifier["stochastic"]:
opponent_.set_seed(player2._seed)
for h in player1.history:
_limited_simulate_play(player, opponent_, h)

# Now play forward with the constant strategy.
simulate_match(player, opponent_, action, rounds)
results[action] = _calculate_scores(player, opponent_, game)

return C if results[C] > results[D] else D


@lru_cache()
def recursive_thue_morse(n):
"""The recursive definition of the Thue-Morse sequence.
Expand Down
12 changes: 0 additions & 12 deletions axelrod/strategies/_strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@
ZDMem2,
)
from .gambler import EvolvableGambler, Gambler # pylint: disable=unused-import
from .geller import Geller, GellerCooperator, GellerDefector
from .gobymajority import (
GoByMajority,
GoByMajority5,
Expand Down Expand Up @@ -190,8 +189,6 @@
from .memorytwo import AON2, MEM2, DelayedAON1
from .memorytwo import MemoryTwoPlayer # pylint: disable=unused-import

from .mindcontrol import MindBender, MindController, MindWarper
from .mindreader import MindReader, MirrorMindReader, ProtectedMindReader
from .mutual import Desperate, Hopeless, Willing
from .negation import Negation
from .oncebitten import FoolMeOnce, ForgetfulFoolMeOnce, OnceBitten
Expand Down Expand Up @@ -363,9 +360,6 @@
Fortress3,
Fortress4,
GTFT,
Geller,
GellerCooperator,
GellerDefector,
GeneralSoftGrudger,
GoByMajority,
GoByMajority10,
Expand Down Expand Up @@ -399,11 +393,6 @@
MEM2,
MathConstantHunter,
Michaelos,
MindBender,
MindController,
MindReader,
MindWarper,
MirrorMindReader,
NTitsForMTats,
NaiveProber,
Negation,
Expand All @@ -422,7 +411,6 @@
Prober2,
Prober3,
Prober4,
ProtectedMindReader,
Pun1,
Punisher,
Raider,
Expand Down
9 changes: 2 additions & 7 deletions axelrod/strategies/darwin.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,6 @@ def __init__(self) -> None:
def receive_match_attributes(self):
self.outcomes = self.match_attributes["game"].scores

@staticmethod
def foil_strategy_inspection() -> Action:
"""Foils _strategy_utils.inspect_strategy and _strategy_utils.look_ahead"""
return C

def strategy(self, opponent: Player) -> Action:
"""Actual strategy definition that determines player's action."""
trial = len(self.history)
Expand All @@ -81,12 +76,12 @@ def strategy(self, opponent: Player) -> Action:
return current

def reset(self):
""" Reset instance properties. """
"""Reset instance properties."""
super().reset()
Darwin.genome[0] = C # Ensure initial Cooperate

def mutate(self, outcome: tuple, trial: int) -> None:
""" Select response according to outcome. """
"""Select response according to outcome."""
if outcome[0] < 3 and (len(Darwin.genome) >= trial):
self.response = D if Darwin.genome[trial - 1] == C else C

Expand Down
113 changes: 0 additions & 113 deletions axelrod/strategies/geller.py

This file was deleted.

2 changes: 1 addition & 1 deletion axelrod/strategies/grudger.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ class SpitefulCC(Player):
@staticmethod
def strategy(opponent: Player) -> Action:
"""
Cooperates until the oponent defects, then defects forever.
Cooperates until the opponent defects. Then defects forever.
Always cooperates twice at the start.
"""
if len(opponent.history) < 2:
Expand Down
Loading