forked from edwardchu-studio/alpha-zero-general-with-Go-game
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInterGame.py
143 lines (129 loc) · 5.11 KB
/
InterGame.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
try:
from GoMCTS import MCTS
from go.GoGame import display
from go.GoGame import GoGame as game
from go.GoPlayers import *
from go.pytorch.NNet import NNetWrapper as nn
import numpy as np
from utils import *
except:
from .GoMCTS import MCTS
from .go.GoGame import display
from .go.GoGame import GoGame as game
from .go.GoPlayers import *
from .go.pytorch.NNet import NNetWrapper as nn
import numpy as np
from .utils import *
"""
use this script to play any two agents against each other, or play manually with
any agent.
"""
BoardSize=7
HUMAN_FIRST=-1
HUMAN_SECOND=1
class InterGame(object):
def __init__(self,NetType='ResNet'):
self.game=game(BoardSize)
self.board=self.game.getInitBoard()
self.n=self.game.getBoardSize()[0]
self.players=[self.AlphaPlay,None,self.HumanPlay]
self.curPlayer=1
self.gameStatus=0
if NetType=='ResNet':
self.AlphaNet=nn(self.game,t='RES')
self.AlphaNet.load_checkpoint('/home/zc1213/course/alphabackend/alphabrain/HistoryLog/Go/R_Ver2_checkpoint/{}/'.format(BoardSize),'best.pth.tar')
self.AlphaArgs = dotdict({'numMCTSSims': 2000, 'cpuct':21.3})
self.AlphaMCTS = MCTS(self.game, self.AlphaNet,self.AlphaArgs)
self.Alpha= lambda x: np.argmax(self.AlphaMCTS.getActionProb(x, temp=0))
else:
self.AlphaNet=nn(self.game,t='CNN')
self.AlphaNet.load_checkpoint('/home/zc1213/course/alphabackend/alphabrain/HistoryLog/Go/C_checkpoint/{}/'.format(BoardSize),'best.pth.tar')
self.AlphaArgs = dotdict({'numMCTSSims': 2000, 'cpuct':17.3})
self.AlphaMCTS = MCTS(self.game, self.AlphaNet,self.AlphaArgs)
self.Alpha= lambda x: np.argmax(self.AlphaMCTS.getActionProb(x, temp=0))
self.alphaMoveCache={}
def initialize(self):
self.board=self.game.getInitBoard()
self.alphaMoveCache={}
return True
def getScore(self):
return self.game.getScore(self.board)
def judgeGame(self):
self.gameStatus=self.game.getGameEnded(self.board,self.curPlayer)
if self.gameStatus==-1:
print("player 1 lost.")
return -1
elif self.gameStatus==1:
print("player 1 won.")
return 1
else:
print("game continues.")
return 0
def getAlphaPlayFromCache(self,humanMove):
if humanMove in list(self.alphaMoveCache.keys()):
print("have cached, get from cache")
return self.alphaMoveCache[humanMove]
else:
print("new request,come back later")
self.alphaMoveCache={}
self.alphaMoveCache.update({
humanMove:self.AlphaPlay()
})
return self.alphaMoveCache[humanMove]
def AlphaPlay(self,*move):
assert(self.judgeGame()==0)
action = self.Alpha(self.game.getCanonicalForm(self.board,self.curPlayer ))
valids = self.game.getValidMoves(self.game.getCanonicalForm(self.board, self.curPlayer),1)
if valids[action]==0:
print(action)
assert valids[action] >0
self.board, self.curPlayer = self.game.getNextState(self.board, self.curPlayer, action)
alphaMove= (int(action / self.n), int(action % self.n))
return alphaMove
def HumanPlay(self,move):
assert(self.judgeGame()==0)
x,y = [int(x) for x in move]
valids = self.game.getValidMoves(self.game.getCanonicalForm(self.board, self.curPlayer),1)
action= self.game.n * x + y if x!= -1 else self.game.n ** 2
if valids[action]==0:
print("Invalid Move!")
return
self.board, self.curPlayer = self.game.getNextState(self.board, self.curPlayer, action)
return
def showBoard(self):
display(self.board)
class InterGameTest(object):
def __init__(self,NetType='ResNet'):
self.game=game(BoardSize)
self.board=self.game.getInitBoard()
self.n=self.game.getBoardSize()[0]
self.players=[self.AlphaPlay,None,self.HumanPlay]
self.curPlayer=1
self.gameStatus=0
def initialize(self):
self.board=self.game.getInitBoard()
def judgeGame(self):
self.gameStatus=self.game.getGameEnded(self.board,self.curPlayer)
if self.gameStatus==-1:
print("player 1 lost.")
return -1
elif self.gameStatus==1:
print("player 1 won.")
return 1
else:
print("game continues.")
return 0
def AlphaPlay(self,*move):
return (3,3)
def HumanPlay(self,move):
assert(self.judgeGame()==0)
x,y = [int(x) for x in move]
valids = self.game.getValidMoves(self.game.getCanonicalForm(self.board, self.curPlayer),1)
action= self.game.n * x + y if x!= -1 else self.game.n ** 2
if valids[action]==0:
print("Invalid Move!")
return
self.board, self.curPlayer = self.game.getNextState(self.board, self.curPlayer, action)
return
def showBoard(self):
display(self.board)