-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
112 lines (95 loc) · 3.57 KB
/
main.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
File: main.py
Desc: 国际象棋强化学习+MCTS策略价值网络模型训练-控制台
Author:yanjingang([email protected])
Date: 2019/1/21 22:46
Cmd:
生成训练数据:
nohup python3 main.py selfplay 40 > log/selfplay.log 2>&1 &
使用png生成训练数据:
nohup python3 main.py pgn Bu.pgn >>log/pgn.log 2>&1 &
训练模型:
nohup python3 main.py train 1 > log/train.log 2>&1 &
与模型对战:
python3 main.py infer ai-vs-human
评估模型:
nohup python3 main.py evaluate 40 > log/evaluate.log 2>&1 &
重放某次对战过程:
python3 main.py replay 20150216-KorchnoiUhlmannRapid-B-KorchnoiV-UhlmannW-48-1.data
"""
import os
import sys
import getopt
import logging
CUR_PATH = os.path.dirname(os.path.abspath(__file__))
from dp import utils
from game import Game
from train import Train
from evaluate import Evaluate
class Main():
"""控制台"""
@staticmethod
def selfplay(params=None):
"""生成对战数据"""
os.environ["CUDA_VISIBLE_DEVICES"]="-1"
n_playout = 400 if params is None else int(params)
game = Game()
game.start_selfplay(n_playout=n_playout, best_model=Train.BEST_MODEL)
@staticmethod
def train(params=None):
"""训练模型"""
n_train = 10000 if params is None else int(params)
train = Train()
train.start_train(n_train=n_train, curr_model=Train.CURR_MODEL)
@staticmethod
def evaluate(params=None):
"""评估模型胜率"""
n_playout_mcts = 400 if params is None else int(params)
evaluate = Evaluate()
evaluate.start_evaluate(n_playout_ai=400, n_playout_mcts=n_playout_mcts, curr_model=Train.CURR_MODEL)
@staticmethod
def infer(params=None):
"""与模型对战"""
vs_type = 'human-vs-stockfish' if params is None else params
game = Game()
game.start_infer(vs_type=vs_type, n_playout=50, best_model=Train.CURR_MODEL)
@staticmethod
def replay(params=None):
"""重放某次对战数据"""
os.environ["CUDA_VISIBLE_DEVICES"]="-1"
data_file = '20190122155910-40-B-65352-bd33dbb3a0742b46fe9cef383630abe2.data' if params is None else params
game = Game()
game.replay_databuffer(data_file=data_file, replay_step=1000)
@staticmethod
def pgn(params=None):
"""使用png生成训练数据"""
os.environ["CUDA_VISIBLE_DEVICES"]="-1"
pgn_file = 'Bu.pgn' if params is None else params
game = Game()
game.pgn_to_databuffer(pgn_file=pgn_file)
if __name__ == '__main__':
opts, args = getopt.getopt(sys.argv[1:], "p:", ["type="])
if len(args) > 0 and args[0] in ['selfplay', 'train', 'evaluate', 'infer', 'replay', 'pgn']:
type = args[0]
else:
exit("usage: python main.py [selfplay|train|evaluate|infer|replay|pgn] ")
params = args[1] if len(args) > 1 else None
# log init
log_file = type + '-' + str(os.getpid())
utils.init_logging(log_file=log_file, log_path=CUR_PATH)
print("log_file: {}".format(log_file))
# do main
if type == 'selfplay': # 生成对战数据
Main.selfplay(params)
elif type == 'train': # 训练模型
Main.train(params)
elif type == 'evaluate': # 评估模型
Main.evaluate(params)
elif type == 'infer': # 与模型对战
Main.infer(params)
elif type == 'replay': # 重放某次对战数据
Main.replay(params)
elif type == 'pgn': # 使用png生成训练数据
Main.pgn(params)