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 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
import json
from os import makedirs
from os.path import exists, join
from bunch import bunchify
from datetime import datetime
class JsonMeta(object):
HOURS = 4
MINUTES = 59
SECONDS = 59
import unittest
import sys
from datetime import datetime
from os import listdir
from time import sleep
from tempfile import mkdtemp
from unittest.case import TestCase
# from json_parser import JsonParser
def output(self):
output = {'video_details': self.video_details}
result = list(self.frames.values())
# Every bbox in each frame has to have `top_k_labels` number of labels otherwise error raises.
for frame in result:
for bbox in frame.bboxes:
if not bbox.labels_full(self.top_k_labels):
raise ValueError(
"labels in frame_id: {}, bbox_id: {} is not fulled before outputting.".format(frame.frame_id,