ttt.models package¶
Submodules¶
ttt.models.action module¶
ttt.models.agent module¶
-
class
ttt.models.agent.Agent[source]¶ Bases:
objectClass modeling a player
-
grid¶ Representation of the board as an array of integers.
- Type
numpy.ndarray
-
current_state¶ Current game state
- Type
ttt.models.State
-
-
class
ttt.models.agent.CPUAgent[source]¶ Bases:
ttt.models.agent.AgentClass modeling a CPU player.
-
states¶ List of states. Representation of the game as a Markov Decision Process.
- Type
list
-
add_state(state: ttt.models.state.State) → None[source]¶ Adds the given state to the MDP.
- Parameters
state (ttt.models.State) – State to be added.
-
deserialize(json_data: dict) → None[source]¶ Dumps specified json_data to the agent.
- Parameters
json_data (dict) – Dictionary with the following format: {mdp: dict}
-
get_best_move() → ttt.models.action.Action[source]¶ Generates a new move using the previous knowledge.
- Returns
move
- Return type
ttt.models.Action
-
get_random_move() → ttt.models.action.Action[source]¶ Generates a random move.
- Returns
move
- Return type
ttt.models.Action
-
get_state(grid: numpy.ndarray) → ttt.models.state.State[source]¶ Gets the state with the given grid.
- Parameters
grid (numpy.ndarray) – Representation of the board.
- Returns
state – State containing the given grid.
- Return type
ttt.models.State
-
has_state(state: ttt.models.state.State) → bool[source]¶ Checks if MDP has registered the given state :param state: State to be checked :type state: ttt.models.State
- Returns
is_registered
- Return type
bool
-
load(path: str) → None[source]¶ Loads a JSON file containing agent’s params.
- Parameters
path (str) – Path of the JSON file.
-
save(path: str) → None[source]¶ Saves model’s params as a JSON file.
- Parameters
path (str) – Where to save agent params.
-
serialize() → dict[source]¶ Serializes this Agent into a dict.
- Returns
serializable_dict
- Return type
dict
-
ttt.models.game module¶
-
class
ttt.models.game.Game(player_1: ttt.models.agent.Agent, player_2: ttt.models.agent.Agent)[source]¶ Bases:
objectClass modeling.
-
grid¶ - Type
numpy.ndarray
-
player_1¶ - Type
ttt.models.Agent
-
player_2¶ - Type
ttt.models.Agent
-
result¶ - Type
int
-
grid_is_full¶ - Type
bool
-
game_sequence¶ - Type
list
- Parameters
player_1 (ttt.models.Agent) –
player_2 (ttt.models.Agent) –
-
apply_move(move: ttt.models.action.Action, player_id: int) → None[source]¶ Commits the move over the board.
- Parameters
move (ttt.models.Action) – Move to be performed.
player_id (int) – Value to apply.
-
check_diagonal_win() → None[source]¶ Checks if there is 3 consecutives marks of the same player diagonally
-
check_horizontal_win() → None[source]¶ Checks if there is 3 consecutives marks of the same player horizontally.
-
ttt.models.reward module¶
-
class
ttt.models.reward.Reward[source]¶ Bases:
objectModels all possible rewards.
-
static
defeat(i, lr)[source]¶ Computes the reward for a given state when the result was a defeat.
- Parameters
i (int) – Position in which the state was reached
lr (float) – Learning rate
- Returns
reward
- Return type
float
-
static
ttt.models.state module¶
-
class
ttt.models.state.State(grid: numpy.ndarray = None)[source]¶ Bases:
objectClass modelling the states of the game
- Parameters
grid (numpy.ndarray) – Representation of the board.
-
grid¶ Representation of the board.
- Type
numpy.ndarray
-
next_states_values¶ The weight of the transition to a state.
- Type
numpy.ndarray
-
next_states_transitions¶ The next move to move to the next state.
- Type
numpy.ndarray
-
deserialize(json_data: dict) → None[source]¶ Dumps specified json_data to the state.
- Parameters
json_data (dict) – Dictionary with the following format: {grid: list, next_state_values: list, next_state_transitions: list}
-
get_best_move() → ttt.models.action.Action[source]¶ Decides on the best available move based on the transition weights.