Skip to content

Instantly share code, notes, and snippets.

View masouduut94's full-sized avatar
👨‍👩‍👦‍👦
Family comes before Work.

Masoud Masoumi Moghadam masouduut94

👨‍👩‍👦‍👦
Family comes before Work.
View GitHub Profile
@masouduut94
masouduut94 / mctsagent.py
Created October 23, 2020 17:19
Wrap up the whole code.
class UctMctsAgent:
"""
Basic no frills implementation of an agent that preforms MCTS for hex.
Attributes:
root_state (GameState): Game simulator that helps us to understand the game situation
root (Node): Root of the tree search
run_time (int): time per each run
node_count (int): the whole nodes in tree
num_rollouts (int): The number of rollouts for each search
def search(self, time_budget: int) -> None:
"""
Search and update the search tree for a
specified amount of time in seconds.
"""
start_time = clock()
num_rollouts = 0
# do until we exceed our time budget
@masouduut94
masouduut94 / mctsagent.py
Created October 23, 2020 16:01
code for backpropagation part.
@staticmethod
def backup(node: Node, turn: int, outcome: int) -> None:
"""
Update the node statistics on the path from the passed node to root to reflect
the outcome of a randomly simulated playout.
Args:
node:
turn: winner turn
outcome: outcome of the rollout
@masouduut94
masouduut94 / mctsagent.py
Created October 23, 2020 15:28
simulation code of mcts agent.
@staticmethod
def roll_out(state: GameState) -> int:
"""
Simulate an entirely random game from the passed state and return the winning
player.
Args:
state: game state
Returns:
@masouduut94
masouduut94 / mctsagent.py
Created October 23, 2020 15:16
selection phase of mcts on the game.
def select_node(self) -> tuple:
"""
Select a node in the tree to preform a single simulation from.
"""
node = self.root
state = deepcopy(self.root_state)
# stop if we find reach a leaf node
while len(node.children) != 0:
class UctMctsAgent:
"""
Basic no frills implementation of an agent that preforms MCTS for hex.
Attributes:
root_state (GameState): Game simulator that helps us to
understand the game situation.
root (Node): Root of the tree search.
run_time (int): time per each run.
node_count (int): the whole nodes in tree.
num_rollouts (int): The number of rollouts for each search.
class Node:
"""
Node for the MCTS. Stores the move applied to reach this node from its parent,
stats for the associated game position, children, parent and outcome
(outcome==none unless the position ends the game).
Args:
move:
parent:
N (int): times this position was visited.
Q (int): average reward (wins-losses) from this position.
class GameMeta:
PLAYERS = {'none': 0, 'white': 1, 'black': 2}
INF = float('inf')
GAME_OVER = -1
EDGE1 = 1
EDGE2 = 2
NEIGHBOR_PATTERNS = ((-1, 0), (0, -1), (-1, 1), (0, 1), (1, 0), (1, -1))
from numpy import zeros, int_
from unionfind import UnionFind
from meta import GameMeta
class GameState:
"""
Stores information representing the current state of a game of hex, namely
the board and the current turn. Also provides functions for playing game.
"""
@masouduut94
masouduut94 / union_find.py
Created October 23, 2020 09:10
disjoint sets data structure adapted to game of hex.
class UnionFind:
"""
Notes:
unionfind data structure specialized for finding hex connections.
Implementation inspired by UAlberta CMPUT 275 2015 class notes.
Attributes:
parent (dict): Each group parent
rank (dict): Each group rank
groups (dict): Stores the groups and chain of cells