forked from haroldsultan/MCTS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmancala.py
204 lines (188 loc) · 5.21 KB
/
mancala.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/env python
import random
import math
import hashlib
import logging
import argparse
from copy import deepcopy
from mcts import *
"""
Mancala using MCTS.
First to get >=25 points wins
Board starts as rows =[r1,r2] with ri = [4,4,4,4,4,4]
r1 = [0,1,2,3,4,5]
r2 = [0,1,2,3,4,5]
"""
NUM_TURNS = 3
class MancalaState():
def __init__(self,player1_points=0,player2_points=0,board=[[4,4,4,4,4,4],[4,4,4,4,4,4]],played_moves=[]):
self.player1_points = player1_points
self.player2_points = player2_points
self.board = board
self.num_moves = 6
self.played_moves=played_moves
def play2(self):
logger.info("PLAYING 2:: %s"%self)
moves2 = []
for ind,val in enumerate(self.board[1]):
if val>0:
moves2.append((ind,val))
if not moves2:
return
ind2,val2 = random.choice(moves2)
logger.info("Moving %d,%d"%(ind2,val2))
self.played_moves.append("PLAYER2: ind:%d,val:%d"%(ind2,val2))
lind = "NEGATIVE"
#pickup
self.board[1][ind2]=0
#play
while val2>0 and ind2>0:
ind2-=1
val2-=1
self.board[1][ind2]+=1
lind = ind2
if val2>0:
self.player2_points+=1
val2-=1
ind2=-1
lind = "HOME"
while val2>0 and ind2<5:
ind2+=1
val2-=1
self.board[0][ind2]+=1
lind = "NEGATIVE"
if val2>0:
ind2=6
while val2>0 and ind2>0:
ind2-=1
val2-=1
self.board[1][ind2]+=1
lind = ind2
if val2>0:
self.player2_points+=1
val2-=1
ind2=-1
lind = "HOME"
while val2>0 and ind2<5:
ind2+=1
val2-=1
self.board[0][ind2]+=1
lind = "NEGATIVE"
if lind == "HOME":
if self.check_for_remaining():
self.play2()
elif lind != "NEGATIVE":
if self.board[1][lind]==1:
captured = self.board[0][lind]
self.player2_points += captured + 1
self.board[0][lind] = 0
self.board[1][lind] = 0
self.check_for_remaining()
def play1(self):
logger.info("PLAYING 1:: %s"%self)
moves1 = []
for ind,val in enumerate(self.board[0]):
if val>0:
moves1.append((ind,val))
if not moves1:
return
ind1,val1 = random.choice(moves1)
logger.info("Moving %d,%d"%(ind1,val1))
self.played_moves.append("PLAYER1: ind:%d,val:%d"%(ind1,val1))
lind = "NEGATIVE"
#pickup
self.board[0][ind1]=0
#play
while val1>0 and ind1<5:
ind1+=1
val1-=1
self.board[0][ind1]+=1
lind = ind1
if val1>0:
self.player1_points+=1
val1-=1
ind1=6
lind = "HOME"
while val1>0 and ind1>0:
ind1-=1
val1-=1
self.board[1][ind1]+=1
lind = "NEGATIVE"
if val1>0:
ind1=-1
while val1>0 and ind1<5:
ind1+=1
val1-=1
self.board[0][ind1]+=1
lind = ind1
if val1>0:
self.player1_points+=1
val1-=1
ind1=6
lind = "HOME"
while val1>0 and ind1>0:
ind1-=1
val1-=1
self.board[1][ind1]+=1
lind = "NEGATIVE"
if lind == "HOME":
if self.check_for_remaining():
self.play1()
elif lind != "NEGATIVE":
if self.board[0][lind]==1:
captured = self.board[1][lind]
self.player1_points += captured + 1
self.board[0][lind] = 0
self.board[1][lind] = 0
self.check_for_remaining()
def check_for_remaining(self):
s1 = sum(self.board[0])
s2 = sum(self.board[1])
if s1==0 or s2 ==0:
self.player1_points+=s1
self.player2_points+=s2
self.board=[[0,0,0,0,0,0],[0,0,0,0,0,0]]
return False
return True
def next_state(self):
if self.check_for_remaining():
self.play1()
if self.check_for_remaining():
self.play2()
return MancalaState(self.player1_points,self.player2_points,deepcopy(self.board),deepcopy(self.played_moves))
def terminal(self):
self.check_for_remaining()
p1_wins = self.player1_points>=25
p2_wins = self.player2_points>=25
if p1_wins or p2_wins:
return True
if sum(self.board[0]+self.board[1])==0:
return True
return False
def reward(self):
if self.player1_points>=25:
return 1
elif self.player1_points==24:
return 0.5
else:
return 0
def __hash__(self):
return int(hashlib.md5(str(self.board).encode('utf-8')).hexdigest(),16)
def __eq__(self,other):
return hash(self)==hash(other)
def __repr__(self):
return "CurrentState: %s; points1: %d, points2: %d\nplayed_moves:\n%s"%(self.board,self.player1_points,self.player2_points,"\n".join(self.played_moves))
if __name__=="__main__":
parser = argparse.ArgumentParser(description='MCTS research code')
parser.add_argument('--num_sims', action="store", required=True, type=int, help="Number of simulations to run")
args=parser.parse_args()
current_node=Node(MancalaState())
num_moves_lambda = lambda node: len([x for x in node.state.board[0] if x>0])
for l in range(NUM_TURNS):
current_node=UCTSEARCH(args.num_sims/(l+1),current_node,num_moves_lambda)
print("level %d"%l)
print("Num Children: %d"%len(current_node.children))
for i,c in enumerate(current_node.children):
print(i,c,c.state.board)
print("Best Child: %s"%current_node.state)
print("--------------------------------")