Skip to content

Add docstrings #1377

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 10 commits into from
Oct 7, 2020
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
2 changes: 1 addition & 1 deletion axelrod/strategies/_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class ExampleStrategy(Player):
def passes_in_list_filter(player, classifier_key, value):
"""
Tests whether a given list of values exist in the list returned from the
given players's classifier dict for the given classifier_key.
given player's classifier dict for the given classifier_key.

e.g.

Expand Down
1 change: 1 addition & 0 deletions axelrod/strategies/adaptive.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def score_last_round(self, opponent: Player):
self.scores[last_round[0]] += scores[0]

def strategy(self, opponent: Player) -> Action:
"""Actual strategy definition that determines player's action."""
# Update scores from the last play
self.score_last_round(opponent)
# Begin by playing the sequence C,C,C,C,C,C,D,D,D,D,D
Expand Down
1 change: 1 addition & 0 deletions axelrod/strategies/adaptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def __init__(
self.s = 0.0

def strategy(self, opponent: Player) -> Action:
"""Actual strategy definition that determines player's action."""
if self.history:
# Update internal state from the last play
last_round = (self.history[-1], opponent.history[-1])
Expand Down
1 change: 1 addition & 0 deletions axelrod/strategies/alternator.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Alternator(Player):
}

def strategy(self, opponent: Player) -> Action:
"""Actual strategy definition that determines player's action."""
if len(self.history) == 0:
return C
if self.history[-1] == C:
Expand Down
1 change: 1 addition & 0 deletions axelrod/strategies/ann.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ def _process_weights(self, weights, num_features, num_hidden):
self.bias_weights = np.array(bias)

def strategy(self, opponent: Player) -> Action:
"""Actual strategy definition that determines player's action."""
features = compute_features(self, opponent)
output = activate(
self.bias_weights,
Expand Down
2 changes: 2 additions & 0 deletions axelrod/strategies/apavlov.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def __init__(self) -> None:
self.opponent_class = None # type: Optional[str]

def strategy(self, opponent: Player) -> Action:
"""Actual strategy definition that determines player's action."""
# TFT for six rounds
if len(self.history) < 6:
return D if opponent.history[-1:] == [D] else C
Expand Down Expand Up @@ -96,6 +97,7 @@ def __init__(self) -> None:
self.opponent_class = None # type: Optional[str]

def strategy(self, opponent: Player) -> Action:
"""Actual strategy definition that determines player's action."""
# TFT for six rounds
if len(self.history) < 6:
return D if opponent.history[-1:] == [D] else C
Expand Down
1 change: 1 addition & 0 deletions axelrod/strategies/appeaser.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Appeaser(Player):
}

def strategy(self, opponent: Player) -> Action:
"""Actual strategy definition that determines player's action."""
if not len(opponent.history):
return C
else:
Expand Down
2 changes: 2 additions & 0 deletions axelrod/strategies/averagecopier.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class AverageCopier(Player):
}

def strategy(self, opponent: Player) -> Action:
"""Actual strategy definition that determines player's action."""
if len(opponent.history) == 0:
# Randomly picks a strategy (not affected by history).
return self._random.random_choice(0.5)
Expand Down Expand Up @@ -52,6 +53,7 @@ class NiceAverageCopier(Player):
}

def strategy(self, opponent: Player) -> Action:
"""Actual strategy definition that determines player's action."""
if len(opponent.history) == 0:
return C
p = opponent.cooperations / len(opponent.history)
Expand Down
9 changes: 9 additions & 0 deletions axelrod/strategies/axelrod_first.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ def __init__(self) -> None:
self.number_opponent_cooperations_in_response_to_D = 0

def strategy(self, opponent: Player) -> Action:
"""Actual strategy definition that determines player's action."""
round_number = len(self.history) + 1

if round_number == 1:
Expand Down Expand Up @@ -347,6 +348,7 @@ def _cooperation_probability(self) -> float:
return max(self._start_coop_prob + slope * rounds, self._end_coop_prob)

def strategy(self, opponent: Player) -> Action:
"""Actual strategy definition that determines player's action."""
if not opponent.history:
return C
if opponent.history[-1] == D:
Expand Down Expand Up @@ -491,6 +493,7 @@ class FirstByGrofman(Player):
}

def strategy(self, opponent: Player) -> Action:
"""Actual strategy definition that determines player's action."""
if len(self.history) == 0 or self.history[-1] == opponent.history[-1]:
return C
return self._random.random_choice(2 / 7)
Expand Down Expand Up @@ -630,6 +633,7 @@ def score_history(
return a

def strategy(self, opponent: Player) -> Action:
"""Actual strategy definition that determines player's action."""
if len(self.history) == 0:
return C
if len(self.history) == 1:
Expand Down Expand Up @@ -713,6 +717,7 @@ def _decrease_retaliation_counter(self):
self.is_retaliating = False

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

Expand Down Expand Up @@ -773,6 +778,7 @@ def __init__(self) -> None:
self._rounds_to_cooperate = 11

def strategy(self, opponent: Player) -> Action:
"""Actual strategy definition that determines player's action."""
if len(self.history) < self._rounds_to_cooperate:
return C
rounds = self._rounds_to_cooperate - 1
Expand Down Expand Up @@ -817,6 +823,7 @@ class FirstByAnonymous(Player):
}

def strategy(self, opponent: Player) -> Action:
"""Actual strategy definition that determines player's action."""
r = self._random.uniform(3, 7) / 10
return self._random.random_choice(r)

Expand Down Expand Up @@ -872,6 +879,7 @@ def __init__(self, alpha: float = 0.05) -> None:
self.opponent_is_random = False

def strategy(self, opponent: Player) -> Action:
"""Actual strategy definition that determines player's action."""
round_number = len(self.history) + 1

# First 4 moves
Expand Down Expand Up @@ -984,6 +992,7 @@ def _score_last_round(self, opponent: Player):
self.opponent_score += scores[1]

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

Expand Down
19 changes: 17 additions & 2 deletions axelrod/strategies/axelrod_second.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class SecondByChampion(Player):
}

def strategy(self, opponent: Player) -> Action:
"""Actual strategy definition that determines player's action."""
current_round = len(self.history)
# Cooperate for the first 10 turns
if current_round == 0:
Expand Down Expand Up @@ -84,6 +85,7 @@ class SecondByEatherley(Player):
}

def strategy(self, opponent: Player) -> Action:
"""Actual strategy definition that determines player's action."""
# Cooperate on the first move
if not len(opponent.history):
return C
Expand Down Expand Up @@ -127,6 +129,7 @@ def __init__(self) -> None:
self.is_TFT = False

def strategy(self, opponent: Player) -> Action:
"""Actual strategy definition that determines player's action."""
# Defect on the first move
if not opponent.history:
return D
Expand Down Expand Up @@ -180,6 +183,7 @@ def __init__(self) -> None:
self.patsy = True

def strategy(self, opponent: Player) -> Action:
"""Actual strategy definition that determines player's action."""
# Defect on the first move
if not self.history:
return D
Expand Down Expand Up @@ -382,6 +386,7 @@ def update_state(self, opponent):
self.one_turn_after_good_defection_ratio_count += 1

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

if not self.history:
return C
Expand Down Expand Up @@ -467,6 +472,7 @@ class SecondByGrofman(Player):
}

def strategy(self, opponent: Player) -> Action:
"""Actual strategy definition that determines player's action."""
# Cooperate on the first two moves
if len(self.history) < 2:
return C
Expand Down Expand Up @@ -539,6 +545,7 @@ def __init__(self):
)

def strategy(self, opponent: Player) -> Action:
"""Actual strategy definition that determines player's action."""
# First update the response matrix.
if len(self.history) >= 2:
if self.history[-2] == D:
Expand Down Expand Up @@ -670,6 +677,7 @@ def try_return(self, to_return):
return D

def strategy(self, opponent: Player) -> Action:
"""Actual strategy definition that determines player's action."""
turn = len(self.history) + 1

if turn == 1:
Expand Down Expand Up @@ -697,7 +705,7 @@ def strategy(self, opponent: Player) -> Action:
self.mode = "Defect"

# Check for a random strategy
if (coops >= 8 and coops <= 17) and self.cc_counts / coops < 0.7:
if (8 <= coops <= 17) and self.cc_counts / coops < 0.7:
self.mode = "Defect"

self.cd_counts, self.cc_counts = 0, 0
Expand Down Expand Up @@ -783,6 +791,7 @@ class SecondByCave(Player):
}

def strategy(self, opponent: Player) -> Action:
"""Actual strategy definition that determines player's action."""
turn = len(self.history) + 1
if turn == 1:
return C
Expand Down Expand Up @@ -833,6 +842,7 @@ class SecondByWmAdams(Player):
}

def strategy(self, opponent: Player) -> Action:
"""Actual strategy definition that determines player's action."""
if len(self.history) <= 1:
return C
number_defects = opponent.defections
Expand Down Expand Up @@ -889,6 +899,7 @@ def update_score(self, opponent: Player):
self.own_score += game.score(last_round)[0]

def strategy(self, opponent: Player) -> Action:
"""Actual strategy definition that determines player's action."""
if self.mode == "Defect":
return D

Expand Down Expand Up @@ -978,6 +989,7 @@ def try_return(self, to_return):
return to_return

def strategy(self, opponent: Player) -> Action:
"""Actual strategy definition that determines player's action."""
if len(opponent.history) == 0:
return C

Expand Down Expand Up @@ -1262,6 +1274,7 @@ def detect_parity_streak(self, last_move):
return True

def strategy(self, opponent: Player) -> Action:
"""Actual strategy definition that determines player's action."""
turn = len(self.history) + 1

if turn == 1:
Expand Down Expand Up @@ -1433,6 +1446,7 @@ def _score_last_round(self, opponent: Player):
self.opponent_score += scores[1]

def strategy(self, opponent: Player) -> Action:
"""Actual strategy definition that determines player's action."""
current_round = len(self.history) + 1

if current_round == 1:
Expand Down Expand Up @@ -1507,6 +1521,7 @@ def __init__(self) -> None:
self.flack = 0.0 # The relative untrustworthiness of opponent

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

Expand Down Expand Up @@ -1833,7 +1848,7 @@ def try_return(self, to_return, opp_def):
# If so, then override
if turn > 40:
portion_defect = (opp_def + 0.5) / turn
if 0.45 < portion_defect and portion_defect < 0.55:
if 0.45 < portion_defect < 0.55:
return D

return to_return
Expand Down
2 changes: 2 additions & 0 deletions axelrod/strategies/backstabber.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class BackStabber(Player):
}

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


Expand Down Expand Up @@ -56,6 +57,7 @@ class DoubleCrosser(Player):
}

def strategy(self, opponent: Player) -> Action:
"""Actual strategy definition that determines player's action."""
if _opponent_triggers_alt_strategy(opponent):
return _alt_strategy(opponent)
return _backstabber_strategy(opponent)
Expand Down
1 change: 1 addition & 0 deletions axelrod/strategies/better_and_better.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class BetterAndBetter(Player):
}

def strategy(self, opponent: Player) -> Action:
"""Actual strategy definition that determines player's action."""
current_round = len(self.history) + 1
probability = current_round / 1000
return self._random.random_choice(probability)
1 change: 1 addition & 0 deletions axelrod/strategies/bush_mosteller.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ def stimulus_update(self, opponent: Player):
)

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

# First turn
if len(self.history) == 0:
Expand Down
1 change: 1 addition & 0 deletions axelrod/strategies/calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def set_seed(self, seed: int = None):
self.joss_instance.set_seed(seed)

def strategy(self, opponent: Player) -> Action:
"""Actual strategy definition that determines player's action."""
turn = len(self.history)
if turn > 0:
self.joss_instance.history.append(
Expand Down
1 change: 1 addition & 0 deletions axelrod/strategies/cooperator.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Cooperator(Player):

@staticmethod
def strategy(opponent: Player) -> Action:
"""Actual strategy definition that determines player's action."""
return C


Expand Down
2 changes: 2 additions & 0 deletions axelrod/strategies/cycler.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def _get_first_three() -> List[Action]:
return [C, D, D]

def strategy(self, opponent: Player) -> Action:
"""Actual strategy definition that determines player's action."""
while self.first_three:
return self.first_three.pop(0)
if self.cycle_counter < self.cycle_length:
Expand Down Expand Up @@ -91,6 +92,7 @@ def __init__(self, cycle: str = "CCD") -> None:
self.set_cycle(cycle=cycle)

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

def set_cycle(self, cycle: str):
Expand Down
1 change: 1 addition & 0 deletions axelrod/strategies/darwin.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def foil_strategy_inspection() -> Action:
return C

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

if trial > 0:
Expand Down
1 change: 1 addition & 0 deletions axelrod/strategies/dbs.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ def compute_prob_rule(self, outcome, alpha=1):
return p_cond

def strategy(self, opponent: Player) -> Action:
"""Actual strategy definition that determines player's action."""
# First move
if not self.history:
return C
Expand Down
1 change: 1 addition & 0 deletions axelrod/strategies/defector.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Defector(Player):

@staticmethod
def strategy(opponent: Player) -> Action:
"""Actual strategy definition that determines player's action."""
return D


Expand Down
1 change: 1 addition & 0 deletions axelrod/strategies/doubler.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Doubler(Player):
}

def strategy(self, opponent: Player) -> Action:
"""Actual strategy definition that determines player's action."""
if not self.history:
return C
if (
Expand Down
1 change: 1 addition & 0 deletions axelrod/strategies/finite_state_machines.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ def __init__(
self.fsm = SimpleFSM(transitions, initial_state)

def strategy(self, opponent: Player) -> Action:
"""Actual strategy definition that determines player's action."""
if len(self.history) == 0:
return self.initial_action
else:
Expand Down
Loading