π¨βπ©βπ¦βπ¦
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 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 |
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
from tkinter import (Frame, Canvas, ttk, HORIZONTAL, VERTICAL, IntVar, Scale, Button, Label, PhotoImage, BOTH, LEFT, Y, | |
X, TOP, messagebox) | |
from numpy import int_ | |
from gamestate import GameState | |
from meta import GameMeta | |
from uct_mcstsagent import UctMctsAgent | |
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 MCTSMeta: | |
EXPLORATION = 0.5 | |
RAVE_CONST = 300 | |
RANDOMNESS = 0.5 | |
POOLRAVE_CAPACITY = 10 | |
K_CONST = 10 | |
A_CONST = 0.25 | |
WARMUP_ROLLOUTS = 7 |
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 tkinter import Tk | |
from gui import Gui | |
def main(): | |
root = Tk() | |
interface = Gui(root) | |
root.mainloop() |
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 math import sqrt, log | |
from copy import deepcopy | |
from random import choice, random | |
from time import clock | |
from gamestate import GameState | |
from uct_mcstsagent import Node, UctMctsAgent | |
from meta import * | |
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 RaveMctsAgent(UctMctsAgent): | |
def __init__(self, state: GameState = GameState(8)): | |
self.root_state = deepcopy(state) | |
self.root = RaveNode() # Replace Node with RaveNode | |
self.run_time = 0 | |
self.node_count = 0 | |
self.num_rollouts = 0 | |
def set_gamestate(self, state: GameState) -> None: | |
""" |
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 tkinter import (Frame, Canvas, ttk, HORIZONTAL, VERTICAL, IntVar, Scale, Button, Label, PhotoImage, BOTH, LEFT, Y, | |
X, TOP, messagebox) | |
from numpy import int_ | |
from gamestate import GameState | |
from meta import GameMeta | |
from rave_mctsagent import (RaveMctsAgent, LGRMctsAgent, PoolRaveMctsAgent, DecisiveMoveMctsAgent) | |
from ucb1_tuned_mctsagent import UCB1TunedMctsAgent | |
from uct_mcstsagent import UctMctsAgent |
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 would_lose(self, cell: tuple, color: int) -> bool: | |
""" | |
Return True is the move indicated by cell and color would lose the game, | |
False otherwise. | |
""" | |
connect1 = False | |
connect2 = False | |
if color == GameMeta.PLAYERS['black']: | |
if cell[1] == 0: | |
connect1 = True |
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 DecisiveMoveMctsAgent(RaveMctsAgent): | |
def roll_out(self, state: GameState) -> tuple: | |
""" | |
Simulate a random game except that we play all known critical cells | |
first, return the winning player and record critical cells at the end. | |
""" | |
moves = state.moves() | |
good_moves = moves.copy() | |
good_opponent_moves = moves.copy() | |
to_play = state.turn() |