Skip to content

Change namespace and add adapter #1335

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

Closed
wants to merge 7 commits into from
Closed
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
13 changes: 9 additions & 4 deletions axelrod/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,23 @@
from axelrod.action import Action
from axelrod.random_ import random_choice, random_flip, seed, Pdf
from axelrod.plot import Plot
from axelrod.game import DefaultGame, Game
from axelrod.base_game import BaseGame
from axelrod.game import DefaultGame, IpdGame
from axelrod.history import History, LimitedHistory
from axelrod.player import Player
from axelrod.base_player import BasePlayer
from axelrod.player import IpdPlayer
from axelrod.classifier import Classifiers
from axelrod.evolvable_player import EvolvablePlayer
from axelrod.mock_player import MockPlayer
from axelrod.match import Match
from axelrod.base_match import BaseMatch
from axelrod.match import IpdMatch
from axelrod.moran import MoranProcess, ApproximateMoranProcess
from axelrod.strategies import *
from axelrod.deterministic_cache import DeterministicCache
from axelrod.match_generator import *
from axelrod.tournament import Tournament
from axelrod.base_tournament import BaseTournament
from axelrod.tournament import IpdTournament
from axelrod.result_set import ResultSet
from axelrod.ecosystem import Ecosystem
from axelrod.fingerprint import AshlockFingerprint, TransitiveFingerprint
from axelrod.ipd_adapter import Player, Game, Match, Tournament
28 changes: 14 additions & 14 deletions axelrod/_strategy_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ def inspect_strategy(inspector, opponent):

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

Returns
Expand All @@ -82,9 +82,9 @@ def _limited_simulate_play(player_1, player_2, h1):

Parameters
----------
player_1: Player
player_1: IpdPlayer
The player whose move is already known.
player_2: Player
player_2: IpdPlayer
The player the we want to inspect.
h1: Action
The next action for first player.
Expand All @@ -99,9 +99,9 @@ def simulate_match(player_1, player_2, strategy, rounds=10):

Parameters
----------
player_1: Player
player_1: IpdPlayer
The player that will have a constant strategy.
player_2: Player
player_2: IpdPlayer
The player we want to simulate.
strategy: Action
The constant strategy to use for first player.
Expand All @@ -117,12 +117,12 @@ def _calculate_scores(p1, p2, game):

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

Returns
-------
Expand All @@ -142,12 +142,12 @@ def look_ahead(player_1, player_2, game, rounds=10):

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

Expand Down
28 changes: 28 additions & 0 deletions axelrod/base_game.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from typing import Tuple, Union

import axelrod as axl

Score = Union[int, float]


class BaseGame(object):
"""Container for the scoring logic."""

def __init__(self) -> None:
"""Create a new game object."""
pass

def score(self, pair: Tuple[axl.Action, axl.Action]) -> Tuple[Score, Score]:
"""Returns the appropriate score for a decision pair.

Parameters
----------
pair: tuple(Action, Action)
A pair actions for two players, for example (C, C).

Returns
-------
tuple of int or float
Scores for two player resulting from their actions.
"""
raise NotImplementedError()
73 changes: 73 additions & 0 deletions axelrod/base_match.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
from typing import Dict, List, Tuple, Union

import axelrod as axl
from axelrod.base_game import BaseGame
from axelrod.base_player import BasePlayer

Score = Union[int, float]


class BaseMatch(object):
"""The BaseMatch class conducts matches between two players."""

def __init__(
self,
players: Tuple[BasePlayer],
turns: int = None,
prob_end: float = None,
game: BaseGame = None,
noise: float = 0,
match_attributes: Dict = None,
reset: bool = True,
):
"""
Needs to be overwritten in derived class.

Parameters
----------
players : tuple
A pair of axelrodPlayer objects
turns : integer
The number of turns per match
prob_end : float
The probability of a given turn ending a match
game : axelrod.BaseGame
The game object used to score the match
noise : float
The probability that a player's intended action should be flipped
match_attributes : dict
Mapping attribute names to values which should be passed to players.
The default is to use the correct values for turns, game and noise
but these can be overridden if desired.
reset : bool
Whether to reset players or not
"""
pass

@property
def players(self) -> Tuple[BasePlayer]:
raise NotImplementedError()

def play(self) -> List[Tuple[axl.Action]]:
"""The resulting list of actions from a match between two players."""
raise NotImplementedError()

def scores(self) -> List[Score]:
"""Returns the scores of the previous BaseMatch plays."""
raise NotImplementedError()

def final_score(self) -> Score:
"""Returns the final score for a BaseMatch."""
raise NotImplementedError()

def final_score_per_turn(self) -> Score:
"""Returns the mean score per round for a BaseMatch."""
raise NotImplementedError()

def winner(self) -> BasePlayer:
"""Returns the winner of the IpdMatch."""
raise NotImplementedError()

def __len__(self) -> int:
"""Number of turns in the match"""
raise NotImplementedError()
39 changes: 39 additions & 0 deletions axelrod/base_player.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import inspect
from typing import Optional, Tuple

import axelrod as axl


class BasePlayer(object):

def __init__(self):
pass

def strategy(self, opponent: "BasePlayer") -> axl.Action:
"""Calculates the action of this player against the provided
opponent."""
raise NotImplementedError()

def play(
self,
opponent: "BasePlayer",
noise: float = 0,
strategy_holder: Optional["BasePlayer"] = None,
) -> Tuple[axl.Action, axl.Action]:
"""This pits two players against each other, using the passed strategy
holder, if provided."""
raise NotImplementedError()

def clone(self) -> "BasePlayer":
"""Clones the player without history, reapplying configuration
parameters as necessary."""
raise NotImplementedError()

def reset(self):
"""Resets a player to its initial state

This method is called at the beginning of each match (between a pair
of players) to reset a player's state to its initial starting point.
It ensures that no 'memory' of previous matches is carried forward.
"""
raise NotImplementedError()
79 changes: 79 additions & 0 deletions axelrod/base_tournament.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
from typing import List, Tuple

import axelrod as axl
from axelrod.base_player import BasePlayer
from axelrod.base_game import BaseGame


class BaseTournament(object):
def __init__(
self,
players: List[BasePlayer],
name: str = "axelrod",
game: BaseGame = None,
turns: int = None,
prob_end: float = None,
repetitions: int = 10,
noise: float = 0,
edges: List[Tuple] = None,
match_attributes: dict = None,
) -> None:
"""
Parameters
----------
players : list
A list of axelrodPlayer objects
name : string
A name for the tournament
game : axelrod.IpdGame
The game object used to score the tournament
turns : integer
The number of turns per match
prob_end : float
The probability of a given turn ending a match
repetitions : integer
The number of times the round robin should be repeated
noise : float
The probability that a player's intended action should be flipped
prob_end : float
The probability of a given turn ending a match
edges : list
A list of edges between players
match_attributes : dict
Mapping attribute names to values which should be passed to players.
The default is to use the correct values for turns, game and noise
but these can be overridden if desired.
"""
pass

def setup_output(self, filename=None):
"""assign/create `filename` to `self`. If file should be deleted once
`play` is finished, assign a file descriptor. """
raise NotImplementedError()

def play(
self,
build_results: bool = True,
filename: str = None,
processes: int = None,
progress_bar: bool = True,
) -> 'ResultSet':
"""
Plays the tournament and passes the results to the ResultSet class

Parameters
----------
build_results : bool
whether or not to build a results set
filename : string
name of output file
processes : integer
The number of processes to be used for parallel processing
progress_bar : bool
Whether or not to create a progress bar which will be updated

Returns
-------
axelrod.ResultSet
"""
raise NotImplementedError()
Loading