-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
96 lines (90 loc) · 4.02 KB
/
main.py
File metadata and controls
96 lines (90 loc) · 4.02 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
from ultimate_tic_tac_toe import game_manager
from ultimate_tic_tac_toe.mcts import MCT
import configparser
import argparse
import os
def lets_play(config_parser):
print("Welcome!")
while True:
try:
rule = int(input("Select rule set! \n1: Standard \n2: Bizarre \n"))
except Exception: # might be fixed
print("Invalid input for rule set. ")
else:
if rule in (1, 2):
break
else:
continue
model_name_mapping = (config_parser.get("model_config", "model_name_normal_rule"),
config_parser.get("model_config", "model_name_bizarre_rule"))
model_path = os.path.normpath(os.path.join(config_parser.get("model_config", "model_dir"), model_name_mapping[rule - 1]))
print("Loading model ({} KiB)... ".format("%.2f" % (os.path.getsize(model_path)/1024)))
game = game_manager.GameManager(model_path)
print("Done! ")
time_to_say_goodbye = False
while not time_to_say_goodbye:
while True:
result = input(r"Do you want to play as initiator?[Y/N]").strip().lower()
if result == "y":
as_initiator = True
break
elif result == "n":
as_initiator = False
break
else:
print('The expected input is enter either Y or N, got "{}"'.format(result))
while True:
result = input(r"Which side do you want to play as?[O/X]").strip().lower()
if result == "o":
side = "O"
break
elif result == "x":
side = "X"
break
else:
print('The expected input is enter either O or X, got "{}"'.format(result))
try:
game.play_in_terminal(side, as_initiator, config_parser.getint("game_config", "computational_cost"),
config_parser.getboolean("game_config", "update_model_after_each_round"))
except KeyboardInterrupt:
print("Game aborted. ")
while True:
restart_request = input("Start another round?[Y/n]").strip().lower()
if restart_request == "y":
print("Let's try again! ")
break
elif restart_request == "n":
time_to_say_goodbye = True
print("Bye! ")
break
else:
print("Unrecognized input, please try again. ")
def train(args, config_parser):
model_name_mapping = {"normal": config_parser.get("model_config", "model_name_normal_rule"),
"bizarre": config_parser.get("model_config", "model_name_bizarre_rule")}
model_path = os.path.normpath(os.path.join(config_parser.get("model_config", "model_dir"), model_name_mapping[args.rule]))
tree = MCT.load_model(model_path)
result = MCT.offline_learning(tree, args.epoch, args.verbose)
print("#Exploitation: {}\n#Exploration: {}\n".format(*result))
print(tree)
MCT.save_model(tree, model_path)
if __name__ == "__main__":
config_parser = configparser.ConfigParser()
config_parser.read("config")
arg_parser = argparse.ArgumentParser(description="Illustrations for parameters. ")
arg_parser.add_argument("--train", action="store_true", default=False, help="Specify this to train the model. ")
arg_parser.add_argument("--rule", action="store", type=str, help='Could be either "normal" or "bizarre". ')
arg_parser.add_argument("--epoch", action="store", type=int, help="Specify the number of epochs in training. ")
arg_parser.add_argument("--verbose", action="store", type=int, default=1,
help="Specify the verbosity level in training. ")
args = arg_parser.parse_args()
if not args.train:
try:
lets_play(config_parser)
except KeyboardInterrupt:
print("Shutting down program. ")
else:
try:
train(args, config_parser)
except KeyboardInterrupt:
print("Aborting... ")