-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame_handle.pyx
executable file
·317 lines (279 loc) · 9.55 KB
/
game_handle.pyx
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
from random import shuffle
from time import time
import numpy as np
#from libcpp cimport bool
cimport numpy as np
# We now need to fix a datatype for our arrays. I've used the variable
# DTYPE for this, which is assigned to the usual NumPy runtime
# type info object.
DTYPE = np.int
# "ctypedef" assigns a corresponding compile-time type to DTYPE_t. For
# every type in the numpy module there's a corresponding compile-time
# type with a _t-suffix.
ctypedef np.int_t DTYPE_t
cdef class Game:
cdef public int moves[6*7]
cdef public np.int_t[:,:] grid
cdef public int nb_moves
cdef public int winner
cdef public int fini
cdef public int [7] hauteurs
def __init__(self):
self.grid = np.zeros((6,7), dtype = DTYPE)
self.fini = 0
self.winner = 0
self.nb_moves = 0
cdef int i
for i in range(7):
self.hauteurs[i]= 5
cpdef Game copy(self):
cdef Game other = Game()
other.grid = self.grid.copy()
other.fini = self.fini
other.winner = self.winner
other.nb_moves = self.nb_moves
other.hauteurs = self.hauteurs.copy()
other.moves = self.moves.copy()
return other
cpdef int turn(self):
return 1 + (self.nb_moves%2)
cpdef undo(self):
colonne = self.moves[self.nb_moves -1]
row = self.row(colonne) + 1
self.fini = False
self.winner = 0
self.grid[row, colonne] = 0
self.hauteurs[colonne] +=1
self.nb_moves -= 1
cpdef int check_win(self, int i, int j ):
cdef DTYPE_t joueur = self.grid[i, j]
cdef int xi, xj, count, offset
cdef int idx
for idx in range(4):
if idx == 0:
xi, xj = 0, 1
elif idx == 1:
xi, xj = 1, 1
elif idx == 2:
xi, xj = 1, 0
else:
xi, xj =-1, 1
count = 1
offset = 1
while are_valid(i + offset * xi, j + offset * xj) and self.grid[i + offset * xi, j + offset * xj] == joueur:
count +=1
offset +=1
offset = -1
while are_valid(i + offset * xi, j + offset * xj) and self.grid[i + offset * xi, j + offset * xj] == joueur:
count +=1
offset -=1
if count >=4:
self.fini = True
self.winner = joueur
return True
self.fini = True
for idx in range(7):
if self.grid[0,idx] == 0:
self.fini = False
return False
return True
cpdef int is_move_possible(self, int colonne):
return self.grid[0,colonne] == 0
cpdef int row(self, int colonne):
return self.hauteurs[colonne]
cpdef play(self, int colonne):
"assuming the move is possible"
if self.fini:
return
cdef int i = self.row(colonne)
cdef int turn = self.turn()
self.grid[i][colonne] = turn
self.moves[self.nb_moves] = colonne
self.check_win(i,colonne)
self.hauteurs[colonne] -=1
self.nb_moves += 1
def from_grid(grid, turn = 1):
game = Game()
game.grid = grid
if turn ==2:
game.nb_moves = 1
for i in range(7):
for j in range(5,-1,-1):
if grid[j, i] == 0:
game.hauteurs[i]=j
break
return game
cdef int* possible_moves(Game game):
cdef int[8] moves
cdef int index = 0, i
for i in range(7):
if game.is_move_possible(i):
index +=1
moves[index] = i
moves[0] = index
return moves
cdef int are_valid(int i, int j):
return 0 <= i <6 and 0<= j <7
cdef double alpha_beta(Game jeu, Evaluation evaluation, int depth, float alpha, float beta):
cdef int joueur = jeu.turn()
cdef float best_score = float('inf') * (-1 if joueur ==1 else 1)
if depth == 0 or jeu.fini:
return evaluation.evaluate(jeu)
cdef int colonne
cdef float score
for colonne in range(7):
if jeu.is_move_possible(colonne):
jeu.play(colonne)
score = alpha_beta(jeu, evaluation, depth - 1 , alpha, beta)
jeu.undo()
if joueur == 1 and score > best_score or joueur == 2 and score < best_score:
best_score = score
if joueur ==1:
if score > beta :
return score
alpha = max(alpha, score)
if joueur ==2:
if score < alpha:
return score
beta = min(beta, score)
return best_score
cpdef int min_max(jeu, Evaluation evaluation, depth = 3):
cdef float alpha = float('-inf')
cdef float beta = float('inf')
cdef int joueur = jeu.turn()
cdef int best = -1
cdef float best_score = float('inf') * (-1 if joueur ==1 else 1)
cdef int colonne
cdef float score
for colonne in np.random.permutation(7):
if jeu.is_move_possible(colonne):
jeu.play(colonne)
score = alpha_beta(jeu, evaluation, depth - 1 , alpha, beta)
jeu.undo()
if joueur == 1 and score > best_score or joueur == 2 and score < best_score:
best = colonne
best_score = score
if joueur ==1:
alpha = max(alpha, score)
if joueur ==2:
beta = min(beta, score)
return best
cdef class Node:
cdef public int expended
cdef public int parent_move
cdef public int nb_children
cdef public Node[:] children
cdef public Node parent
cdef public Game game
cdef public int win
cdef public int visited
def __init__(self, Game game, int parent_move = -1,):
self.expended = False
self.game = game
self.parent_move = parent_move
self.win = 0
self.visited = 0
self.children = np.zeros(7, dtype = np.object)
cpdef expend(self):
cdef int i
cdef int index = 0
cdef Game otherGame
for i in range(7):
if self.game.is_move_possible(i):
otherGame = self.game.copy()
otherGame.play(i)
self.children[index] = Node(otherGame, i)
self.children[index].parent = self
index += 1
self.nb_children = index
self.expended = True
cpdef Node random_child(self):
return self.children[np.random.randint(self.nb_children)]
cpdef update_stats(self, winner):
self.visited += 1
if winner != self.game.turn():
self.win +=1
cdef float eval_score(Node self):
cdef int visits
if self.visited == 0:
visits = 1
else:
visits = self.visited
cdef float res = self.win / visits
res += np.sqrt(2*np.log(self.parent.visited) / visits) #assert self.parent.visited >= 1, sinon c'est que l'algo marche pas comme prévu
return res
def win_rate(self):
if self.visited == 0:
return 0
else:
return self.win / self.visited
cdef class Evaluation:
cpdef int evaluate(self, Game jeu) except *:
return 0
cdef class Evaluation_simple(Evaluation):
cpdef int evaluate(self, Game jeu) except *:
if jeu.winner == 1:
return 1
elif jeu.winner == 2:
return -1
else:
return 0
def monte_carlo_tree_search(game, time_allocated):
d = time()
current_time = 0
root = Node(game.copy())
cdef int iterations = 0
while current_time < time_allocated:
leaf = traverse(root)
simulation_result = rollout(leaf)
backpropagate(leaf, simulation_result)
iterations += 1
current_time = time() - d
cdef float best_score =-1
cdef float score
cdef int best = -1
for i in range(root.nb_children):
score = win_rate(root.children[i])
if score > best_score:
best_score = score
best = root.children[i].parent_move
return best
cdef Node traverse(node):
while node.expended:
if node.nb_children == 0:
return node
node = best_child(node)
node.expend()
if node.nb_children == 0:
return node
else:
return node.random_child()
# function for the result of the simulation
cdef int rollout(Node node):
cdef Game game = node.game.copy()
cdef int* coups_possibles
cdef int coup_choisi
while not game.fini:
coups_possibles = possible_moves(game)
coup_choisi = coups_possibles[1 + np.random.randint(coups_possibles[0])]
game.play(coup_choisi)
return game.winner
# function for backpropagation
cdef backpropagate(Node node, int result):
node.update_stats(result)
if not node.parent:
return
backpropagate(node.parent, result)
# function for selecting the best child
# node with highest number of visits
cdef Node best_child(Node node):
cdef Node best = node
cdef float best_score = -1
cdef float score
cdef int i = 0
for i in range(node.nb_children):
score = eval_score(node.children[i])
if score > best_score:
best_score = score
best = node.children[i]
return best