π¨βπ©βπ¦βπ¦
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | |
""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, |