-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.py
182 lines (155 loc) · 7.31 KB
/
config.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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import json
import numpy as np
def create_uci_labels():
labels = []
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
nums = ['1', '2', '3', '4', '5', '6', '7', '8']
promoted_to = ['q', 'r', 'b', 'n']
# UCI labels magic xd
for l1 in range(8):
for n1 in range(8):
destinations = [(t, n1) for t in range(8)] + \
[(l1, t) for t in range(8)] + \
[(l1 + t, n1 + t) for t in range(-7, 8)] + \
[(l1 + t, n1 - t) for t in range(-7, 8)] + \
[(l1 + a, n1 + b) for (a, b) in
[(-2, -1), (-1, -2), (-2, 1), (1, -2), (2, -1), (-1, 2), (2, 1), (1, 2)]]
for (l2, n2) in destinations:
if (l1, n1) != (l2, n2) and l2 in range(8) and n2 in range(8):
move = letters[l1] + nums[n1] + letters[l2] + nums[n2]
labels.append(move)
for l1 in range(8):
l = letters[l1]
for p in promoted_to:
labels.append(l + '2' + l + '1' + p)
labels.append(l + '7' + l + '8' + p)
if l1 > 0:
l_s1 = letters[l1 - 1]
labels.append(l + '2' + l_s1 + '1' + p)
labels.append(l + '7' + l_s1 + '8' + p)
if l1 < 7:
l_p1 = letters[l1 + 1]
labels.append(l + '2' + l_p1 + '1' + p)
labels.append(l + '7' + l_p1 + '8' + p)
return labels
def flipped_uci_labels():
def repl(x):
return "".join([(str(9 - int(a)) if a.isdigit() else a) for a in x])
return [repl(x) for x in create_uci_labels()]
class ModelConfig(object):
def __init__(self):
# These are the default value
self.cnn_filter_num = 256
self.cnn_first_filter_size = 5
self.cnn_filter_size = 3
self.input_depth = 18
self.l2_reg = 1e-4
self.res_layer_num = 7
self.value_fc_size = 256
class PlayConfig(object):
def __init__(self):
self.c_puct = 1.5
self.dirichlet_alpha = 0.3
self.max_game_length = 1000
self.max_game_per_file = 32
self.max_processes = 1
self.min_resign_turn = 5
self.noise_eps = 0.25
self.resign_threshold = -0.8
self.search_threads = 16
self.simulation_num_per_move = 100
self.tau_decay_rate = 0.99
self.virtual_loss = 3
class SupervisedLearning(object):
def __init__(self):
self.max_processes = 4
self.min_elo_policy = 1200
self.max_elo_policy = 2800
class TrainingConfig(object):
def __init__(self):
self.batch_size = 128
self.epoch_to_checkpoint = 1
self.loss_weight = [1.25, 1.0]
class EvaluateConfig(object):
def __init__(self):
self.c_puct = 1
self.game_num = 50
self.max_processes = 1
self.noise_eps = 0
self.replace_rate = 0.55
self.search_threads = 16
self.simulation_num_per_move = 200
self.tau_decay_rate = 0.6
class Config(object):
labels = create_uci_labels()
n_labels = len(labels)
flipped_labels = flipped_uci_labels()
unflipped_labels = None
def __init__(self, path: str = None):
if path is not None:
with open(path, 'r') as fp:
_json_config = json.load(fp)
self.model_path = _json_config["model_path"]
self.play_path = _json_config["play_path"]
self.pgn_path = _json_config["pgn_path"]
self.tfr_path = _json_config["tfr_path"]
# Config for the Chess AI Model
self.model = ModelConfig()
self.model.cnn_filter_num = _json_config["model"]["cnn_filter_num"]
self.model.cnn_first_filter_size = _json_config["model"]["cnn_first_filter_size"]
self.model.cnn_filter_size = _json_config["model"]["cnn_filter_size"]
self.model.input_depth = _json_config["model"]["input_depth"]
self.model.l2_reg = _json_config["model"]["l2_reg"]
self.model.res_layer_num = _json_config["model"]["res_layer_num"]
self.model.value_fc_size = _json_config["model"]["value_fc_size"]
# Config for supervised learning
self.supervised_learning = SupervisedLearning()
self.supervised_learning.max_processes = _json_config["supervised_learning"]["max_processes"]
self.supervised_learning.min_elo_policy = _json_config["supervised_learning"]["min_elo_policy"]
self.supervised_learning.max_elo_policy = _json_config["supervised_learning"]["max_elo_policy"]
# Config for the Player
self.play = PlayConfig()
self.play.c_puct = _json_config["play"]["c_puct"]
self.play.dirichlet_alpha = _json_config["play"]["dirichlet_alpha"]
self.play.max_game_length = _json_config["play"]["max_game_length"]
self.play.max_game_per_file = _json_config["play"]["max_game_per_file"]
self.play.max_processes = _json_config["play"]["max_processes"]
self.play.min_resign_turn = _json_config["play"]["min_resign_turn"]
self.play.noise_eps = _json_config["play"]["noise_eps"]
self.play.resign_threshold = _json_config["play"]["resign_threshold"]
self.play.search_threads = _json_config["play"]["search_threads"]
self.play.simulation_num_per_move = _json_config["play"]["simulation_num_per_move"]
self.play.tau_decay_rate = _json_config["play"]["tau_decay_rate"]
self.play.virtual_loss = _json_config["play"]["virtual_loss"]
# Config for training
self.training = TrainingConfig()
self.training.batch_size = _json_config["training"]["batch_size"]
self.training.epoch_to_checkpoint = _json_config["training"]["epoch_to_checkpoint"]
self.training.loss_weight = _json_config["training"]["loss_weight"]
# Config for evaluator
self.evaluate = EvaluateConfig()
self.evaluate.c_puct = _json_config["evaluate"]["c_puct"]
self.evaluate.game_num = _json_config["evaluate"]["game_num"]
self.evaluate.max_processes = _json_config["evaluate"]["max_processes"]
self.evaluate.noise_eps = _json_config["evaluate"]["noise_eps"]
self.evaluate.replace_rate = _json_config["evaluate"]["replace_rate"]
self.evaluate.search_threads = _json_config["evaluate"]["search_threads"]
self.evaluate.simulation_num_per_move = _json_config["evaluate"]["simulation_num_per_move"]
self.evaluate.tau_decay_rate = _json_config["evaluate"]["tau_decay_rate"]
else:
self.model_path = ""
self.play_path = ""
self.pgn_path = ""
self.tfr_path = ""
self.model = ModelConfig()
self.supervised_learning = SupervisedLearning()
self.play = PlayConfig()
self.training = TrainingConfig()
self.evaluate = EvaluateConfig()
@staticmethod
def flip_policy(policy):
return np.asarray([policy[i] for i in Config.unflipped_index])
def save_config(self, path: str):
with open(path, "w+") as fp:
fp.write(json.dumps(self, default=lambda o: o.__dict__, indent=2))
Config.unflipped_index = [Config.labels.index(x) for x in Config.flipped_labels]