/
training.py
148 lines (124 loc) · 4.33 KB
/
training.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
import numpy as np
import board as B
import player as P
import AI as R
import Naive as N
import QLearning as Q
import pickle
import os
import matplotlib.pyplot as plt
NONE = 0
BLACK = 1
WHITE = 2
def turn(board, player, opponent_player, GUI):
# print('stone_nunm1: {}'.format(board.stone_num))
# 打てる場所のリストを作成
valid_pos = board.get_valid_position(board.get_color())
# 打つ場所がなかったらパスする
if board.is_pass(valid_pos):
if board.is_interruption():
return "game-set"
board.next_turn()
board._pass = True
return "pass"
# 打てる場所を出力
# print(valid_pos)
# ユーザからの入力
x, y = player.move(board, board.get_color(), 0)
#print('({}, {})'.format(x, y))
# player.getGameResult(board, opponent_player)
# x, y = random_ai.random_ai(valid_pos)
# ひっくり返す
board.reverse(board.get_color(), x, y)
# print('1--------------')
# board.show_board()
board.next_turn()
# print('--------------')
player.getGameResult(board, opponent_player, 0)
def game(player1, player2):
if type(player1) == Q.QLearning:
if os.path.exists('./data/first_win_rate.pickle'):
with open('./data/first_win_rate.pickle', 'rb') as f:
win_rate = pickle.load(f)
else:
win_rate = []
if type(player2) == Q.QLearning:
if os.path.exists('./data/first_win_rate.pickle'):
with open('./data/second_win_rate.pickle', 'rb') as f:
win_rate = pickle.load(f)
else:
win_rate = []
b = 0
w = 0
cnt = 1
board = B.Board()
player = P.Player()
random_ai = R.RandomAI()
color = BLACK
while True:
for i in range(100):
w = 0
b = 0
for j in range(100):
board.init()
while not board.is_end():
color = board.get_color()
if color == BLACK:
status = turn(board, player1, player2, 0)
else:
status = turn(board, player2, player1, 0)
if status == "game-set":
break
# 盤面の表示
# print('after -------------->')
# board.show_board()
win = board.who_won()
if win == BLACK:
# print('black win')
b += 1
elif win == WHITE:
# print('white win')
w += 1
if type(player1) == Q.QLearning:
win_rate.append(b / 100)
else:
win_rate.append(w / 100)
print('black {} win'.format(b))
print('white {} win'.format(w))
if type(player1) == Q.QLearning:
with open('./data/first_move_4x4.pickle'.format(cnt), 'wb') as f:
pickle.dump(player1, f)
with open('./data/first_win_rate.pickle', 'wb') as f:
pickle.dump(win_rate, f)
if type(player2) == Q.QLearning:
with open('./data/second_move_4x4.pickle', 'wb') as f:
pickle.dump(player2, f)
with open('./data/second_win_rate.pickle', 'wb') as f:
pickle.dump(win_rate, f)
cnt += 1
# plt.plot(win_rate)
# plt.show()
if __name__ == '__main__':
x = 3 #int(input('先手: ナイーブ=1, ランダム=2, QL=3 > '))
if x is 1:
player1 = N.NaiveAI()
elif x is 2:
player1 = R.RandomAI()
elif x is 3:
if os.path.exists('./data/first_move.pickle'):
with open('./data/first_move.pickle', 'rb') as f:
player1 = pickle.load(f)
else:
player1 = Q.QLearning(BLACK)
x = 2 #int(input('後手: ナイーブ=1, ランダム=2, QL=3 > '))
if x is 1:
player2 = N.NaiveAI()
elif x is 2:
player2 = R.RandomAI()
elif x is 3:
if os.path.exists('./data/second_move.pickle'):
with open('./data/second_move.pickle', 'rb') as f:
player2 = pickle.load(f)
else:
player2 = Q.QLearning(WHITE)
game(player1, player2)