-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.py
182 lines (153 loc) · 5.45 KB
/
test.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
File: test.py
Desc: 测试
Cmd: nohup python3 test.py > log/test.log 2>&1 &
"""
from __future__ import print_function
import os
import sys
import logging
import time
import copy
import pickle
import zlib
# import chess
# import chess.uci
#import chess.enginedd
import random
import numpy as np
from collections import defaultdict, deque
CUR_PATH = os.path.dirname(os.path.abspath(__file__))
from dp import utils
from game import Game, Board
# from player import MCTS, MCTSPlayer, AIPlayer
# from train import Train
#from net.policy_value_net_keras import PolicyValueNet
#from net.policy_value_net_tensorflow import PolicyValueNet # Tensorflow
"""class Board2():
def __init__(self):
self.board = chess.Board()
print('xx')"""
class Test():
def __init__(self):
pass
if __name__ == '__main__':
utils.init_logging(log_file='test', log_path=CUR_PATH)
""""
#deepcopy test
board2 = Board2()
board3 = copy.deepcopy(board2)
board3.board.push(chess.Move.from_uci('e2e4'))
print(board3.board)
print(board2.board)
print('-----')
board = Board()
board.init_board()
board.graphic()
print(board.current_player)
print(board.availables)
print(board.book_variations['last'])
copy.deepcopy(board)
#构建一个纯mcts
mcts_player = MCTSPlayer(c_puct=5,n_playout=1)
print('------get_action------MCTSPlayer')
#走第一步
action = mcts_player.get_action(board)
print('------get_action res')
print(action)
print(board.action_ids[action])
#构建一个策略价值网络指导的mcts
board = Board()
board.init_board()
policy_value_net = PolicyValueNet(board.action_ids_size)
ai_player = AIPlayer(policy_value_net.policy_value_fn, c_puct=5, n_playout=2, is_selfplay=1)
print('------get_action------AIPlayer')
#走第一步
action = ai_player.get_action(board)
print('------get_action res')
print(action)
print(board.action_ids[action])
game = Game(board)
data_buffer = deque(maxlen=100)
batch_size = 10
logger.info("TRAIN Batch start i:{}".format(1), Test)
print('------start_self_play start')
winner, play_data = game.start_self_play(ai_player, temp=1.0)
print('------start_self_play res')
print(winner)
play_data = list(play_data)[:]
episode_len = len(play_data)
data_buffer.extend(play_data)
print(data_buffer)
print(len(data_buffer))
# net train test
state_batch = utils.pickle_load(CUR_PATH+'/log/state_batch.data')
mcts_probs_batch = utils.pickle_load(CUR_PATH+'/log/mcts_probs_batch.data')
winner_batch = utils.pickle_load(CUR_PATH+'/log/winner_batch.data')
print(state_batch)
print(mcts_probs_batch)
print(winner_batch)
#board = Board()
#policy_value_net = PolicyValueNet(board.action_ids_size)
#old_probs, old_v = policy_value_net.policy_value(state_batch)
#replay_databuffer
data_file = CUR_PATH + '/data/playdata/20190122155910-40-B-65352-bd33dbb3a0742b46fe9cef383630abe2.data'
game = Game()
game.replay_databuffer(data_file)
# png
log_file = 'pgn-' + str(os.getpid())
utils.init_logging(log_file=log_file, log_path=CUR_PATH)
logging.debug("log_file: {}".format(log_file))
# pgn->databuffer
game = Game()
for pgn_file in os.listdir(CUR_PATH+"/data/pgn/"):
if pgn_file[-4:] == '.pgn':
try:
game.pgn_to_databuffer(pgn_file=pgn_file)
except:
logging.warning(utils.get_trace())
for data_file in os.listdir(CUR_PATH + "/data/databuffer/"):
tmp = data_file.split('-')
if len(tmp) < 6:
continue
try:
step_num = int(tmp[5])
winner = tmp[2]
if step_num < 6:
logging.debug("RM\t"+str(step_num)+"\t"+winner+"\t"+data_file)
os.remove(CUR_PATH + "/data/databuffer/"+data_file)
if step_num < 30 and winner == 'Tie':
logging.debug("RM\t"+str(step_num)+"\t"+winner+"\t"+data_file)
os.remove(CUR_PATH + "/data/databuffer/"+data_file)
if step_num < 30:
logging.debug(str(step_num) + "\t" + winner + "\t" + data_file)
except:
logging.warning("name fail! \t" + data_file +"\n" + utils.get_trace())
board = Board()
print(board.action_ids_size)
"""
# 批量下载pgn棋谱
game = Game()
game.download_pgn(save_db=True, save_databuffer=False)
"""
model_path= CUR_PATH + '/model/'
#net_params = utils.pickle_load(model_path+'current_policy.90530-3.2909.model.bak')
#pickle.dump(net_params, open(model_path+'current_policy.90530-3.2909.model', 'wb'), protocol=4)
pickle.load(open(model_path+'current_policy.model', 'rb'), encoding='bytes')
engine = chess.engine.SimpleEngine.popen_uci("./engine/stockfish-10-linux/Linux/stockfish_10_x64")
board = chess.Board()
while not board.is_game_over():
print(board)
result = engine.play(board, chess.engine.Limit(time=0.100))
board.push(result.move)
engine.quit()
engine = chess.uci.popen_engine('./engine/stockfish-10-linux/Linux/stockfish_10_x64')
info_handler = chess.uci.InfoHandler()
engine.info_handlers.append(info_handler)
engine.position(chess.Board())
res = engine.go(movetime=2000)
print(res)
print(res.bestmove, res.ponder, info_handler.info['score'][1].cp)
"""