-
Notifications
You must be signed in to change notification settings - Fork 0
/
ctrlrMinMax.py
208 lines (162 loc) · 5.95 KB
/
ctrlrMinMax.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
#! /usr/bin/python2
import time
import random
from ctrlr import Controller
from state import State
class ControllerMinMax(Controller):
"""docstring for ControllerMinMax"""
def __init__(self, name, player_ordinality, precalc_utivals_enabled=True, move_randomize=True):
super(ControllerMinMax, self).__init__(name, player_ordinality)
self.stats["d"] = 0
self.stats["n"] = 0
self.precalc_utivals_enabled = precalc_utivals_enabled
self.move_randomize = move_randomize
if self.precalc_utivals_enabled == True:
self.precalc_utivals_dict = {}
self.update_precalc_utivals_dict()
def output(self, state):
time_init = time.time()
move_positions = State.get_move_positions(state)
utivals = None
if self.precalc_utivals_enabled == True:
utivals = self.precalc_utivals_dict.get(state, None)
if utivals == None:
utivals = []
for pos in move_positions:
state_new = State.get_state_on_move(state, (self.player_ordinality, pos))
utival = self.minmax(state_new, 0)
utivals.append(utival)
move_positions_best = None
if self.player_ordinality == State.PLAYER_A:
utival_best = float("-inf")
for i,utival in enumerate(utivals):
if utival > utival_best:
utival_best = utival
move_positions_best = [move_positions[i]]
elif utival == utival_best:
move_positions_best.append(move_positions[i])
else:
utival_best = float("inf")
for i,utival in enumerate(utivals):
if utival < utival_best:
utival_best = utival
move_positions_best = [move_positions[i]]
elif utival == utival_best:
move_positions_best.append(move_positions[i])
if self.move_randomize == True:
pos = random.choice(move_positions_best)
else:
pos = move_positions_best[0]
time_end = time.time()
self.stats["t"] += time_end - time_init
return (self.player_ordinality, pos)
def minmax(self, state, stack_depth):
self.stats["n"] += 1
if stack_depth > self.stats["d"]:
self.stats["d"] = stack_depth
if State.is_state_terminal(state):
return State.utility_value(state)
else:
utival_best = None
player_cur = None
if state.player_last == State.PLAYER_A:
utival_best = float("inf")
player_cur = State.PLAYER_B
else:
utival_best = float("-inf")
player_cur = State.PLAYER_A
move_positions = State.get_move_positions(state)
for pos in move_positions:
state_new = State.get_state_on_move(state, (player_cur,pos))
utival = self.minmax(state_new, stack_depth+1)
if player_cur == State.PLAYER_A:
if utival > utival_best:
utival_best = utival
else:
if utival < utival_best:
utival_best = utival
return utival_best
def update_precalc_utivals_dict(self):
s = State((4,4),3)
s.player_last = State.PLAYER_B
self.precalc_utivals_dict[s] = (1,1,1,1)
grid = ((1, 0, 0, 0), (2, 0, 0, 0), (0, 0, 0, 0), (0, 0, 0, 0))
s = State.get_state_from_grid(grid)
s.min_length = 3
s.player_last = State.PLAYER_B
self.precalc_utivals_dict[s] = (1,1,1,-1)
grid = ((1, 2, 0, 0), (0, 0, 0, 0), (0, 0, 0, 0), (0, 0, 0, 0))
s = State.get_state_from_grid(grid)
s.min_length = 3
s.player_last = State.PLAYER_B
self.precalc_utivals_dict[s] = (1, 1, -1, -1)
grid = ((1, 0, 2, 0), (0, 0, 0, 0), (0, 0, 0, 0), (0, 0, 0, 0))
s = State.get_state_from_grid(grid)
s.min_length = 3
s.player_last = State.PLAYER_B
self.precalc_utivals_dict[s] = (-1, -1, 1, -1)
grid = ((1, 0, 0, 2), (0, 0, 0, 0), (0, 0, 0, 0), (0, 0, 0, 0))
s = State.get_state_from_grid(grid)
s.min_length = 3
s.player_last = State.PLAYER_B
self.precalc_utivals_dict[s] = (1, 1, 1, -1)
grid = ((2, 1, 0, 0), (0, 0, 0, 0), (0, 0, 0, 0), (0, 0, 0, 0))
s = State.get_state_from_grid(grid)
s.min_length = 3
s.player_last = State.PLAYER_B
self.precalc_utivals_dict[s] = (1, 1, 1, 1)
grid = ((0, 1, 0, 0), (0, 2, 0, 0), (0, 0, 0, 0), (0, 0, 0, 0))
s = State.get_state_from_grid(grid)
s.min_length = 3
s.player_last = State.PLAYER_B
self.precalc_utivals_dict[s] = (-1, 1, 1, -1)
grid = ((0, 1, 2, 0), (0, 0, 0, 0), (0, 0, 0, 0), (0, 0, 0, 0))
s = State.get_state_from_grid(grid)
s.min_length = 3
s.player_last = State.PLAYER_B
self.precalc_utivals_dict[s] = (-1, 1, 1, -1)
grid = ((0, 1, 0, 2), (0, 0, 0, 0), (0, 0, 0, 0), (0, 0, 0, 0))
s = State.get_state_from_grid(grid)
s.min_length = 3
s.player_last = State.PLAYER_B
self.precalc_utivals_dict[s] = (1, 1, 1, 1)
grid = ((2, 0, 1, 0), (0, 0, 0, 0), (0, 0, 0, 0), (0, 0, 0, 0))
s = State.get_state_from_grid(grid)
s.min_length = 3
s.player_last = State.PLAYER_B
self.precalc_utivals_dict[s] = (1, 1, 1, 1)
grid = ((0, 2, 1, 0), (0, 0, 0, 0), (0, 0, 0, 0), (0, 0, 0, 0))
s = State.get_state_from_grid(grid)
s.min_length = 3
s.player_last = State.PLAYER_B
self.precalc_utivals_dict[s] = (-1, 1, 1, -1)
grid = ((0, 0, 1, 0), (0, 0, 2, 0), (0, 0, 0, 0), (0, 0, 0, 0))
s = State.get_state_from_grid(grid)
s.min_length = 3
s.player_last = State.PLAYER_B
self.precalc_utivals_dict[s] = (-1, 1, 1, -1)
grid = ((0, 0, 1, 2), (0, 0, 0, 0), (0, 0, 0, 0), (0, 0, 0, 0))
s = State.get_state_from_grid(grid)
s.min_length = 3
s.player_last = State.PLAYER_B
self.precalc_utivals_dict[s] = (1, 1, 1, 1)
grid = ((2, 0, 0, 1), (0, 0, 0, 0), (0, 0, 0, 0), (0, 0, 0, 0))
s = State.get_state_from_grid(grid)
s.min_length = 3
s.player_last = State.PLAYER_B
self.precalc_utivals_dict[s] = (-1, 1, 1, 1)
grid = ((0, 2, 0, 1), (0, 0, 0, 0), (0, 0, 0, 0), (0, 0, 0, 0))
s = State.get_state_from_grid(grid)
s.min_length = 3
s.player_last = State.PLAYER_B
self.precalc_utivals_dict[s] = (-1, 1, -1, -1)
grid = ((0, 0, 2, 1), (0, 0, 0, 0), (0, 0, 0, 0), (0, 0, 0, 0))
s = State.get_state_from_grid(grid)
s.min_length = 3
s.player_last = State.PLAYER_B
self.precalc_utivals_dict[s] = (-1, -1, 1, 1)
grid = ((0, 0, 0, 1), (0, 0, 0, 2), (0, 0, 0, 0), (0, 0, 0, 0))
s = State.get_state_from_grid(grid)
s.min_length = 3
s.player_last = State.PLAYER_B
self.precalc_utivals_dict[s] = (-1, 1, 1, 1)