-
Notifications
You must be signed in to change notification settings - Fork 0
/
minimax.py
145 lines (120 loc) · 4.2 KB
/
minimax.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
import defaults as defu
import movement as mov
import numpy as np
import copy
def get_children_max(postion):
X = postion[0]
Y = postion[1]
# up Y--, down Y++, left X--, right X++
up = [X, Y-1]
down = [X, Y+1]
left = [X-1, Y]
right = [X+1, Y]
children_temp = [up, down, left, right]
children = []
for child in children_temp:
if mov.safe_state(child[0], child[1]):
children.append(child)
return children
def get_children_min():
listaya = []
for i in range(-defu.R_NUM_RANGE, defu.R_NUM_RANGE+1):
listaya.append(i)
return listaya
def catch_flag(postion, grid):
X = postion[0]
Y = postion[1]
# up Y--, down Y++, left X--, right X++
up = [X, Y - 1]
down = [X, Y + 1]
left = [X - 1, Y]
right = [X + 1, Y]
children_temp = [up, down, left, right]
for child in children_temp:
if mov.safe_state(child[0],child[1]) and grid[str(child[0])+str(child[1])] == defu.FLAG:
return child[0], child[1]
return None, None
def print_grid(grid):
print(str(grid['00']) + " " + str(grid['10']) + " " + str(grid['20']))
print(str(grid['01']) + " " + str(grid['11']) + " " + str(grid['21']))
print(str(grid['02']) + " " + str(grid['12']) + " " + str(grid['22']))
def minimax(postion, gridy, depth, alpha, beta, maximizingPlayer):
if depth == 0:
return gridy[str(postion[0])+str(postion[1])], None
if maximizingPlayer:
maxEval = -defu.INFINTY
children = get_children_max(postion)
retPos = None
for child in children:
grid = copy.deepcopy(gridy)
val1 = grid[str(child[0])+str(child[1])]
val2 = grid[str(postion[0])+str(postion[1])]
grid[str(child[0])+str(child[1])] = val1 + val2
grid[str(postion[0]) + str(postion[1])] = defu.FLAG
eval, _ = minimax(child, grid, depth-1, alpha, beta, False)
if eval > maxEval:
retPos = child
maxEval = max(maxEval, eval)
alpha = max(alpha, eval)
if beta <= alpha:
break
return maxEval, retPos
else:
minEval = defu.INFINTY
children = get_children_min()
for child in children:
grid = copy.deepcopy(gridy)
xFlag, yFlag = catch_flag(postion, grid)
if xFlag is None:
break
grid[str(xFlag)+str(yFlag)] = child
eval, _ = minimax(postion, grid, depth-1, alpha, beta, True)
minEval = min(minEval, eval)
beta = min(beta, eval)
if beta <= alpha:
break
return minEval, None
def one_step_minimax(postion, grid, depth):
alpha = -defu.INFINTY
beta = defu.INFINTY
result, pos = minimax(postion, grid, depth, alpha, beta, True)
rnad = np.random.randint(-defu.R_NUM_RANGE, defu.R_NUM_RANGE+1)
oldVal = grid[str(postion[0]) + str(postion[1])]
grid[str(pos[0]) + str(pos[1])] += oldVal
new_score = grid[str(pos[0]) + str(pos[1])]
grid[str(postion[0]) + str(postion[1])] = rnad
return new_score, pos
"""
if __name__ == '__main__':
postion = [0, GRID_Y-1]
#grid = {"00":1, "01":1, "02":0, "10":-1, "11":0, "12":1, "20":0, "21":1, "22":-1}
#grid = {"00":1, "01":0, "02":-1, "10":-1, "11":0, "12":5, "20":0, "21":1, "22":-1}
#postion = [1, 2]
#depth = 5
#goal = 10
#grid = random_numbers()
grid = {"00": -1, "01": 1, "02": 1, "10": -1, "11": 1, "12": -1, "20": 1, "21": -1, "22": 1}
print_grid(grid)
print("###################")
depth = 5
goal = 75
score = 0
#allPos = [[]]
#np.random.seed(5)
for i in range(10000):
alpha = -INFINTY
beta = INFINTY
print_grid(grid)
result, pos = minimax(postion, grid, depth, alpha, beta, True, goal, score)
rnad = np.random.randint(-1, 2)
oldVal = grid[str(postion[0])+str(postion[1])]
grid[str(pos[0])+str(pos[1])] += oldVal
score = grid[str(pos[0])+str(pos[1])]
grid[str(postion[0]) + str(postion[1])] = rnad
postion = pos
print(score, pos)
print("")
if score >= goal:
print(i)
break
"""