-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodules.py
596 lines (485 loc) · 14.5 KB
/
modules.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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
"""
NAME: Naveen Venkat
ID: 2015A7PS0078P
"""
import turtle as t
from random import randrange
from time import time
from sys import getsizeof
import config
# utility function
def uf_zeroList(n): #Creates a list of zeroes of size n
if config.verbose:
print "uf_zeroList: ", n
tempList = []
for i in range(n):
tempList.append(0)
pass
return tempList
def uf_neatScreen():
for i in range(50):
print
# =========
# TREE NODE
# =========
class TreeNode(object):
#static methods
@staticmethod
def initial_state(dim):
tempList = []
for i in range(dim):
tempList.append(0)
return [tempList, config.initial]
#constructor
def __init__(self, state=None, action=None, utilityValue=None):
if state==None:
state = TreeNode.initial_state(16)
self.state = state
self.action = action
self.utilityValue = utilityValue
# STATE REPRESENTATION
# state = [ [board_setting], player_turn ]
# where player can be either config.minplayer or config.maxplayer
# and board_setting is a vector of size 16
def is_ith_column_free(board_setting, i):
if config.verbose:
print "is_ith_column_free: ", i
for row in range(4):
if board_setting[row*4 + i] == 0:
return True
return False
def get_ith_column_free_cell_index(board_setting, i):
if config.verbose:
print "get_ith_column_free_cell_index: ", i
for row in range(4):
if board_setting[4*row + i] == 0:
return 4*row + i
if not(config.suppressGuiClicks):
print "FATAL ERROR: get_ith_column_free_cell_index | ", i, "col is not empty. The board_setting is: "
return None
def actions(state):
if config.verbose:
print "actions: ", state
board_setting = state[0]
possible_actions = []
for i in range(4):
if is_ith_column_free(board_setting, i):
possible_actions.append(i+1)
if config.verbose:
print "possible_actions: ", possible_actions
return possible_actions
def successor_function(state, action):
if config.verbose:
print "successor_function: ", state, ": ", action
if action==None:
return None
board_setting = state[0]
player_turn = state[1]
if action == 1:
i = get_ith_column_free_cell_index(board_setting, 0)
if(i==None):
return None
new_board_setting = list(board_setting)
new_board_setting[i] = player_turn
elif action == 2:
i = get_ith_column_free_cell_index(board_setting, 1)
if(i==None):
return None
new_board_setting = list(board_setting)
new_board_setting[i] = player_turn
elif action == 3:
i = get_ith_column_free_cell_index(board_setting, 2)
if(i==None):
return None
new_board_setting = list(board_setting)
new_board_setting[i] = player_turn
elif action == 4:
i = get_ith_column_free_cell_index(board_setting, 3)
if(i==None):
return None
new_board_setting = list(board_setting)
new_board_setting[i] = player_turn
else:
print "FATAL ERROR: Successor_Function got an invalid action =", action
return None
return [new_board_setting, -player_turn]
def terminal_test(state):
if config.verbose:
print "terminal_test: ", state
board_setting = state[0]
player_turn = state[1]
for triplet in config.triplets:
if is_uniform(board_setting, triplet):
return True
for i in range(16):
if board_setting[i]==0:
return False
return True
def is_uniform(board_setting, triplet):
if (board_setting[triplet[0]] == board_setting[triplet[1]] == board_setting[triplet[2]]) & (board_setting[triplet[0]]!=0):
return True
return False
# =============
# UTILITY VALUE
# =============
def utility_value(state):
if config.verbose:
print "utility_value: ", state
board_setting = state[0]
player_turn = state[1]
for triplet in config.triplets:
if is_uniform(board_setting, triplet):
if config.verbose:
print "Winning situation ", triplet
return board_setting[triplet[0]]
if config.verbose:
print "FATAL ERROR: utility_value | board_setting is not uniform for any triplet"
print board_setting
return 0
# =================
# MINIMAX ALGORITHM
# =================
def min_value(state):
if terminal_test(state):
return utility_value(state)
else:
succ = None
v = config.maxint
for a in actions(state):
succ = successor_function(state,a)
if succ!=None:
childnode = TreeNode(succ, a)
config.R1 += 1
config.tempStackSize += 1
if config.tempStackSize > config.maxStackSize:
config.maxStackSize = config.tempStackSize
childnode.utilityValue = max_value(childnode.state)
v = min(v, childnode.utilityValue)
config.tempStackSize -= 1
return v
def max_value(state):
if terminal_test(state):
return utility_value(state)
else:
succ = None
v = config.minint
for a in actions(state):
succ = successor_function(state,a)
if succ!=None:
childnode = TreeNode(succ, a)
config.R1 += 1
config.tempStackSize += 1
if config.tempStackSize > config.maxStackSize:
config.maxStackSize = config.tempStackSize
childnode.utilityValue = min_value(childnode.state)
v = max(v, childnode.utilityValue)
config.tempStackSize -= 1
return v
def minimax_algorithm(state):
if config.verbose:
print state
board_setting = state[0]
player_turn = state[1]
action = None
succ = None
t1 = time()
if player_turn == config.maxplayer:
v = config.minint
for a in actions(state):
succ = successor_function(state, a)
if succ!=None:
childnode = TreeNode(succ, a)
config.R1 += 1
config.tempStackSize += 1
if config.tempStackSize > config.maxStackSize:
config.maxStackSize = config.tempStackSize
childnode.utilityValue = min_value(childnode.state)
if childnode.utilityValue > v:
v = childnode.utilityValue
action = childnode.action
config.guiAction = childnode.action
config.tempStackSize -= 1
elif player_turn == config.minplayer:
v = config.maxint
for a in actions(state):
succ = successor_function(state, a)
if succ!=None:
childnode = TreeNode(succ, a)
config.R1 += 1
config.tempStackSize += 1
if config.tempStackSize > config.maxStackSize:
config.maxStackSize = config.tempStackSize
childnode.utilityValue = max_value(childnode.state)
if childnode.utilityValue < v:
v = childnode.utilityValue
action = a
config.guiAction = childnode.action
config.tempStackSize -= 1
config.minimaxAgentTime += time() - t1
return successor_function(state,action)
# ===================
# ALPHABETA ALGORITHM
# ===================
def alphabeta_min_value(state, alpha, beta):
if terminal_test(state):
#print utility_value(state)
return utility_value(state)
else:
succ = None
v = config.maxint
for a in actions(state):
succ = successor_function(state, a)
if succ!=None:
childnode = TreeNode(succ, a)
config.R6 += 1
config.tempStackSize += 1
if config.tempStackSize > config.maxStackSize:
config.maxStackSize = config.tempStackSize
childnode.utilityValue = alphabeta_max_value(childnode.state, alpha, beta)
v = min(v, childnode.utilityValue)
if v <= alpha:
return v
beta = min(beta,v)
config.tempStackSize -= 1
return v
def alphabeta_max_value(state, alpha, beta):
if terminal_test(state):
#print utility_value(state)
return utility_value(state)
else:
succ = None
v = config.minint
for a in actions(state):
succ = successor_function(state,a)
if succ!=None:
childnode = TreeNode(succ, a)
config.R6 += 1
config.tempStackSize += 1
if config.tempStackSize > config.maxStackSize:
config.maxStackSize = config.tempStackSize
childnode.utilityValue = alphabeta_min_value(childnode.state, alpha, beta)
v = max(v, childnode.utilityValue)
if v >= beta:
return v
alpha = max(alpha, v)
config.tempStackSize -= 1
return v
def alphabeta_algorithm(state):
if config.verbose:
print state
board_setting = state[0]
player_turn = state[1]
action = None
alpha = config.minint
beta = config.maxint
t1 = time()
if player_turn == config.maxplayer:
v = config.minint
for a in actions(state):
succ = successor_function(state,a)
if succ!=None:
childnode = TreeNode(succ, a)
config.R6 += 1
config.tempStackSize += 1
if config.tempStackSize > config.maxStackSize:
config.maxStackSize = config.tempStackSize
childnode.utilityValue = alphabeta_min_value(childnode.state, alpha, beta)
if childnode.utilityValue > v:
v = childnode.utilityValue
action = a
config.guiAction = a
config.tempStackSize -= 1
if player_turn == config.minplayer:
v = config.maxint
for a in actions(state):
succ = successor_function(state,a)
if succ!=None:
childnode = TreeNode(succ, a)
config.R6 += 1
config.tempStackSize += 1
if config.tempStackSize > config.maxStackSize:
config.maxStackSize = config.tempStackSize
childnode.utilityValue = alphabeta_max_value(childnode.state, alpha, beta)
if childnode.utilityValue < v:
v = childnode.utilityValue
action = a
config.guiAction = a
config.tempStackSize -= 1
config.alphaBetaAgentTime += time() - t1
return successor_function(state,action)
# =======================
# CONSOLE FUNCTIONALITIES
# =======================
def print_board(state):
if config.neat:
uf_neatScreen()
print
for row in range(4):
for col in range(4):
if state[0][row*4+col] == 1:
print 'o',
elif state[0][row*4+col] == -1:
print 'x',
else:
print '.',
print
if state[1] == config.maxplayer:
print "Human moved"
elif state[1] == config.minplayer:
print "Machine moved"
elif state[1] == config.initial:
print "Initial state"
print
if config.neat:
raw_input("press enter to continue...")
def menu():
if config.neat:
uf_neatScreen()
print
print "Menu"
print "----"
#print "========================================="
print "1. Display empty board"
print "2. Play the game using Minimax algorithm"
print "3. Play the game using Alpha Beta pruning"
print "4. Show all results (R1 - R12)"
print "5. Exit"
#print "========================================="
print
def publish_minimax_stats():
print
print "Minimax based analysis"
print "R1 (number of nodes generated) = ", config.R1
print "R2 (memory allocated to one node) = ", config.R2
print "R3 (maximum growth of the implicit stack) = ", config.R3
print "R4 (total time to play the game) = ", config.R4
print "R5 (number of nodes created in one micro second) = ", config.R5
#print "minimaxAgentTime = ", config.minimaxAgentTime
def publish_alphabeta_stats():
print
print "Alpha Beta pruning based analysis"
print "R6 (number of nodes generated) = ", config.R6
print "R7 (saving using pruning) = ", config.R7
print "R8 (total time to play a game) = ", config.R8
#print "alphaBetaAgentTime = ", config.alphaBetaAgentTime
def publish_comparative_stats():
if config.R6==0:
print "Please play minimax game first, then alpha beta game, and then run the analysis"
else:
config.R9 = config.R1 / config.R6
print
print "Comparative Analysis"
print "R9 (minimax/alphabeta) = ", config.R9
print "R10 (average time to play the game) = ", config.R10
print "R11 (number of times player M wins) = ", config.R11
print "R12 (average number of times player M wins) = ", config.R12
print
def reset_all_stats():
# Minimax based analysis
config.R1 = 0 # number of nodes generated till the problem is solved
config.R2 = 0 # amount of memory allocated to one node
config.R3 = 0 # the maximum growth of the implicit stack
config.R4 = 0 # the total time to play the game
config.R5 = 0 # the number of nodes created in one micro second
# Alpha Beta pruning based analysis
config.R6 = 0 # the number of nodes generated till the problem is solved
config.R7 = 0 # (R1 - R6)/R1 : saving using pruning
config.R8 = 0 # the total time to play a game
# Comparative analysis
config.R9 = 0 # the memory used in both the techniques (Minimax and Alpha Beta pruning)
config.R10 = 0 # average time to play the game ( 10 times )
config.R11 = 0 # the number of times player M wins
config.R12 = 0 # average number of times player M wins ( repeating the step in R10 for 20 times)
# ==================
# CONSOLE BASED GAME
# ==================
def play_console_minimax_game():
config.R1 = 0 # number of nodes generated till the problem is solved
config.R2 = 0 # amount of memory allocated to one node
config.R3 = 0 # the maximum growth of the implicit stack
config.R4 = 0 # the total time to play the game
config.R5 = 0 # the number of nodes created in one micro second
t1 = time()
config.R2 = getsizeof(TreeNode)
initstate = [uf_zeroList(16),config.maxplayer]
"""first move hard coded"""
if config.verbose:
print "first move hard coded"
ns = initstate
ns[0][0] = config.maxplayer
ns[1] = config.minplayer
print_board(ns)
"""first move calculated"""
# if config.verbose:
# print "first move calculated"
# print "Computer is calculating the first move. This may take upto 90 seconds (on i7 7700HQ under moderate load). Please wait..."
# ns = minimax_algorithm(initstate)
# print_board(ns)
while(True):
inp = int(raw_input("move: "))
#if inp not in range(1,5):
ns = successor_function(ns, inp)
print_board(ns)
if(terminal_test(ns)):
print "YOU WIN"
break
if config.neat:
uf_neatScreen()
print "thinking..."
ns = minimax_algorithm( ns )
print_board(ns)
if(terminal_test(ns)):
config.R11 += 1
print "YOU LOSE"
break
t2 = time()
config.R4 = t2-t1
config.R3 = config.maxStackSize
config.maxStackSize = 0
config.tempStackSize = 0
config.R5 = (float(config.R1) / float(config.minimaxAgentTime)) / 100000
config.minimaxAgentTime = 0
if config.neat:
raw_input("press enter to continue ...")
def play_console_alphabeta_game():
config.R6 = 0 # the number of nodes generated till the problem is solved
config.R7 = 0 # (R1 - R6)/R1 : saving using pruning
config.R8 = 0 # the total time to play a game
config.R8 = 0
t1 = time()
config.R2 = getsizeof(TreeNode)
initstate = [uf_zeroList(16),config.maxplayer]
"""first move hard coded"""
# if config.verbose:
# print "first move hard coded"
# ns = initstate
# ns[0][0] = config.maxplayer
# ns[1] = config.minplayer
# print_board(ns)
"""first move calculated"""
if config.verbose:
print "first move calculated"
ns = alphabeta_algorithm(initstate)
print_board(ns)
while(True):
inp = int(raw_input("move: "))
ns = successor_function(ns, inp)
print_board(ns)
if(terminal_test(ns)):
print "YOU WIN"
break
if config.neat:
uf_neatScreen()
print "thinking..."
ns = alphabeta_algorithm( ns )
print_board(ns)
if(terminal_test(ns)):
config.R11 += 1
print "YOU LOSE"
break
t2 = time()
config.R8 = t2-t1
config.R7 = float(float((config.R1 - config.R6))/float(config.R1))
config.alphaBetaAgentTime = 0
if config.neat:
raw_input("press enter to continue...")