-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
55 lines (44 loc) · 1.27 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import logging
import yaml
import torch
from environment.game import Hex
from agent.actor import Actor
from agent.models import ANET
def init_logger():
"""
Initialize logger settings
:return: None
"""
logging.basicConfig(
level=logging.INFO, format="%(asctime)s [%(levelname)-5.5s] %(message)s",
handlers=[
logging.FileHandler("app.log", mode="w"),
logging.StreamHandler()
])
def load_config(path):
"""
Load the configuration from task_2_table.yaml.
"""
return yaml.load(open(path, 'r'), Loader=yaml.SafeLoader)
def load_model(file_path):
config = load_config("./configs/config.yaml")
actor = Actor(None, load_actor=True)
model = ANET(config["anet_config"])
model.load_state_dict(torch.load(file_path))
model.eval()
actor.load_anet(file_path, model)
return actor
def get_next_player(player):
return 2 if player == 1 else 1
def get_new_game(game_config):
"""
Initialize a new Hex game, and return it
:param game_config: dict
:return: Game
"""
_type = game_config["game_type"]
if _type == "hex":
game = Hex(game_config["hex"], verbose=game_config["verbose"])
else:
raise ValueError("Game type is not supported")
return game