-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
444 lines (380 loc) · 15.2 KB
/
bot.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
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
"""
bot.py. This module contains AI algorithms for taking action by bots.
They all get the current state of the board and return the calculated action
based on whom turn it was.
Algorithms are Minimax (with or without alpha-beta pruning), Expectimax and Deepimax.
Written by Ali Zandian (alizandian@outlook.com) for University project, researching a better way to gauge unlimited trees.
A project at the university of Ashrafi Esfahani.
"""
import entities as Entities
import datetime as Time
import queue
import math
# This var is for having a way out of the bot algorithms,
# Assigned in GUI by the user.
algorithmBreak = False
class Agent(Entities.Controller):
"""
An Controller controlled by Artificial Intelligence, Its main job is to
take the current state and return an action based on requested strategy.
"""
def __init__(self, strategy):
super().__init__()
self.strategy = strategy
self.title = strategy.name
self.depth = None
self.roa = None
self.doa = None
def Action(self, board, turn):
super().Action(board, turn)
# Simple raw minimax
if(self.strategy == Entities.Strategy.Minimax):
return Minimax(board = self.board, turn = self.turn, depth = self.depth)
# The minimax with alpha beta pruning
if(self.strategy == Entities.Strategy.MinimaxPAB):
isMax = True
if(turn == Entities.Turn.Sheeps):
isMax = False
theNode = MinimaxAlphaBeta(board = board, node = None, depth = 0, maxDebth = self.depth, isMax = isMax, a = -math.inf, b = +math.inf)
if(len(theNode.actions) != 0):
return theNode.actions.popleft(), theNode.GetTop()
# The expectimax
if(self.strategy == Entities.Strategy.Expectimax):
isMax = True
if(turn == Entities.Turn.Sheeps):
isMax = False
theNode = Expectimax(board = self.board, node = None, depth = 0, maxDebth = self.depth, isMax = isMax, isExpect = False)
if(len(theNode.actions) != 0):
return theNode.actions.popleft(), theNode.GetTop()
# The deepimax designed by me
if(self.strategy == Entities.Strategy.Deepimax):
isMax = True
if(turn == Entities.Turn.Sheeps):
isMax = False
theNode = Deepimax(board = self.board, node = None, depth = 0, maxDepth = self.depth, isMax = isMax, doa = self.doa, roa = self.roa)
if(len(theNode.actions) != 0):
return theNode.actions.popleft(), theNode.GetTop()
def Minimax(board, turn, depth):
"""
Returns and action (start, end) which start and end are 2D tuples representing
positions in the board.
board -> Gets the current board and calculate on it.
isFox -> Determining which side of the board (Sheep or fox) now taking a turn.
window -> Getting the main window in the graphics for updating purposes.
"""
"""
Minimax: Creates a table of decisions based on all available actions for each side of the
game. Tree is full and all parts should be there. We go as deep as we want (here is depth limited)
and the reason is we cant go all the way through the tree because game trees are mostly considered unlimited.
Then in any depth we want we stop and calculate Evaluation functions for tree leaves and then trace back
the tree and run Minimax algorithm which is in each state, we either max or min over children based on whom turn
it is.
"""
global algorithmBreak
algorithmBreak = False
isFox = True
if(turn == Entities.Turn.Sheeps):
isFox = False
currentDepth = 0
top = Entities.MinimaxTreeNode(currentDepth)
top.actions = queue.Queue().queue
# Going as deeply as we want, a depth limited. It can be break by minimaxBreak
while(algorithmBreak == False and currentDepth != depth):
# Getting all the noes in the current depth
states = top.GetDepthNodes(currentDepth)
if(states == None):
break
# Looping over all same depth nodes for updating board and make their children
for s in states:
# Creating a copy of the current board
currentBoard = Entities.Board()
currentBoard.Copy(board)
priviousActions = s.actions.copy()
# Loop over all actions respectably in this state and update the board
for i in range(s.actions.__len__()):
action = s.actions.popleft()
currentBoard.Action(action[0], action[1])
availableActions = []
# If this is fox turn
if(isFox == True):
availableActions = currentBoard.AvailableActionsFox()
# If this is sheeps turn
elif(isFox == False):
availableActions = currentBoard.AvailableActionsSheep()
# Loop over all available actions and for each make a child and assign to this state
for a in availableActions:
child = Entities.MinimaxTreeNode(currentDepth+1)
newActions = priviousActions.copy()
newActions.append(a)
child.actions = newActions
child.parent = s
s.AddChildren(child)
currentDepth += 1
isFox = not isFox
# Filling the tree is done, now we select the appropriate action
isFox = True
if(turn == Entities.Turn.Sheeps):
isFox = False
TheNode = MiniMaxValue(board, top, isFox)
# Returning the calculated action
if(algorithmBreak == False):
if(len(TheNode.actions) != 0):
return TheNode.actions.popleft(), top
else:
algorithmBreak = False
def MiniMaxValue(board, node, isMax):
"""
Gets the made tree top node and going through it recursively
to min or max over children
board -> Getting the current board
node -> top node of created miniMax tree
isMax -> Is this iteration of recursive for maxing over children or mining
"""
"""
MinimaxValue: Going from top to bottom of the tree and min and maxing over
nodes considering isMax variable and returning the min or max node in each parent.
In the end the nominated node returns with its evaluation function point.
"""
# If this node has no children
if(len(node.GetChildren()) == 0):
# Making a copy of the board
currentBoard = Entities.Board()
currentBoard.Copy(board)
currentActions = node.actions.copy()
# Looping over all actions in this node and updating the board
for i in range(currentActions.__len__()):
action = currentActions.popleft()
currentBoard.Action(action[0], action[1])
# Calculating the evaluation Function point
node.point = currentBoard.EvaluationFunction()
# returning this node
return node
# if this node has children
else:
# Recurse to children
subNodes = []
for child in node.GetChildren():
subNodes.append(MiniMaxValue(board, child, not isMax))
# If its the max turn, max over children nodes and return the max
if(isMax):
maxNode = max(subNodes, key = lambda x: x.point)
node.point = maxNode.point
return maxNode
# If its the min turn, min over children nodes and return the min
else:
minNode = min(subNodes, key = lambda x: x.point)
node.point = minNode.point
return minNode
def MinimaxAlphaBeta(board, node, depth, maxDebth, isMax, a, b):
"""
The minimax Method with alpha beta pruning.
Returns a node
"""
# Creating the node
if(depth == 0):
node = Entities.MinimaxTreeNode(0)
node.actions = queue.Queue().queue
# Creating a new instance of the board
currentBoard = Entities.Board()
currentBoard.Copy(board)
previousActions = node.actions.copy()
# Updating the board
for i in range(previousActions.__len__()):
action = previousActions.popleft()
currentBoard.Action(action[0], action[1])
# Get list of available actions
availableActions = []
if(isMax):
availableActions = currentBoard.AvailableActionsFox()
else:
availableActions = currentBoard.AvailableActionsSheep()
# Check the leave (We are in the last depth or we have no children)
if(depth == maxDebth or availableActions.__len__() == 0):
node.point = currentBoard.EvaluationFunction()
return node
# Make the children
for action in availableActions:
child = Entities.MinimaxTreeNode(depth + 1)
newActions = node.actions.copy()
newActions.append(action)
child.actions = newActions
child.parent = node
node.AddChildren(child)
# Actual minimax with alpha beta pruning code
if(isMax):
v = -math.inf
childNode = None
for child in node.children:
temp = MinimaxAlphaBeta(board, child, depth +1, maxDebth, False, a, b)
if(temp == None):
continue
if(temp.point > v):
childNode = temp
v = temp.point
if(v >= b):
return None
a = max(a, v)
node.point = v
return childNode
else:
v = math.inf
childNode = None
for child in node.children:
temp = MinimaxAlphaBeta(board, child, depth +1, maxDebth, True, a, b)
if(temp == None):
continue
if(temp.point < v):
childNode = temp
v = temp.point
if(v <= a):
return None
b = min(a, v)
node.point = v
return childNode
def Expectimax(board, node, depth, maxDebth, isMax, isExpect):
""" The expectimax algorithm """
# Creating the node
if(depth == 0):
node = Entities.MinimaxTreeNode(0)
node.actions = queue.Queue().queue
# Creating a new instance of the board
currentBoard = Entities.Board()
currentBoard.Copy(board)
previousActions = node.actions.copy()
# Updating the board
for i in range(previousActions.__len__()):
action = previousActions.popleft()
currentBoard.Action(action[0], action[1])
# Get list of available actions
availableActions = []
if(isMax):
availableActions = currentBoard.AvailableActionsFox()
else:
availableActions = currentBoard.AvailableActionsSheep()
# Check the leave (We are in the last depth or we have no children)
if(depth == maxDebth or availableActions.__len__() == 0):
node.point = currentBoard.EvaluationFunction()
return node
# Make the children
for action in availableActions:
child = Entities.MinimaxTreeNode(depth + 1)
newActions = node.actions.copy()
newActions.append(action)
child.actions = newActions
child.parent = node
node.AddChildren(child)
# Actual minimax with alpha beta pruning code
if(not isExpect): # Maximum
v = -math.inf
childNode = None
for child in node.children:
temp = Expectimax(board, child, depth +1, maxDebth, not isMax , True)
if(temp == None):
continue
if(temp.point > v):
childNode = temp
v = temp.point
node.point = v
return childNode
else: # Expecting
v = 0
childNode = None
for child in node.children:
temp = Expectimax(board, child, depth +1, maxDebth, not isMax , False)
if(temp == None):
continue
v = v + temp.point
node.point = v / node.children.__len__()
return node
def Deepimax(board, node, depth, maxDepth, isMax, doa, roa):
"""
This is the algorithm i made to go as deep as we can while maintaining good decisions.
"""
if(depth == 0 and node == None):
node = Entities.DeepimaxTreeNode(0)
node.actions = queue.Queue().queue
# Creating a new instance of the board
currentBoard = Entities.Board()
currentBoard.Copy(board)
previousActions = node.actions.copy()
# Updating the board
for i in range(previousActions.__len__()):
action = previousActions.popleft()
currentBoard.Action(action[0], action[1])
# Get list of available actions
availableActions = []
if(isMax):
availableActions = currentBoard.AvailableActionsFox()
else:
availableActions = currentBoard.AvailableActionsSheep()
# Check the leave (We are in the last depth or we have no children)
if(depth == maxDepth or availableActions.__len__() == 0 or(roa == 0 and doa == 0)):
node.point = currentBoard.EvaluationFunction()
return node
theNode = None
tempChildren = []
# Make the children
for action in availableActions:
child = Entities.DeepimaxTreeNode(node.depth + 1)
newActions = node.actions.copy()
newActions.append(action)
child.actions = newActions
if((depth % doa) == 0 and depth != 0):
tempChildren.append(child)
else:
child.parent = node
node.AddChildren(child)
# Actual Deepimax Algorithms
if((depth % doa) == 0 and depth != 0):
# Expecting
v = 0
for child in tempChildren:
temp = Deepimax(board, child, maxDepth, maxDepth, isMax, 0, 0)
if(temp == None):
continue
v = v + temp.point
node.point = v / tempChildren.__len__()
return node
else:
for child in node.children:
temp = Deepimax(board, child, depth +1, maxDepth, not isMax, doa, roa)
if(temp == None):
continue
if((depth % doa) == doa -1): # Collective
node.nominies.append(temp)
continue
else:
node.nominies += temp.nominies
theNode = node
if((depth % doa) == 0):
if(theNode.nominies.__len__() != 0): # We have nominees, so we re engage again
theNode.nominies = sorted(theNode.nominies, key=lambda x: x.point, reverse = isMax)
# Re-engage for 'range of accuracy' times
results = []
theMaxDepth = maxDepth - doa
if(doa > 1):
doa -= 1
currentRoa = roa
if(currentRoa > 1):
currentRoa -= 1
for i in range(roa):
if(i > theNode.nominies.__len__() - 1):
break
# Call
if(theMaxDepth == 0):
results.append(theNode.nominies[i])
else:
temp = Deepimax(board, theNode.nominies[i], 0, theMaxDepth, not isMax, doa, currentRoa)
if(temp != None):
results.append(temp)
result = None
if(isMax):
result = max(results, key = lambda x: x.point)
else:
result = min(results, key = lambda x: x.point)
theNode.point = result.point
return result
else:
return theNode
else:
return theNode