-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchopsticksolvedia.py
392 lines (349 loc) · 12.9 KB
/
chopsticksolvedia.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
from collections import deque
from copy import deepcopy
import random
from statistics import mean
from sys import setrecursionlimit
import numpy as np
import scipy.linalg as la
class Position:
def __init__(self, position, turn):
self.position = list(position)
self.turn = turn
self.won = None
self.parents = []
self.children = []
self.initialized = False
self.distance = 100 # This variable denotes the distance from an absolutely won position (where one player is dead) to prevent looping
if sum(self.position[0:2]) == 0:
self.won = False
self.distance = 0
if sum(self.position[2:4]) == 0:
self.won = True
self.distance = 0
def getValidPositions(self, cache):
# Moves using the first pair of hands if the first player, or the 2nd pair of hands if otherwise
offset = 0 if self.turn else 2
# Using a set automatically handles duplicates
possibilities = set()
# Simulates each hand hitting each other possible hand
for i in range(2):
for j in range(2):
newpos = self.position.copy()
if newpos[2 - offset + j] == 0:
continue
newpos[2 - offset + j] = (
newpos[2 - offset + j] + newpos[offset + i]
) % 5
# Ensure ordering
if newpos[2 - offset + 1] < newpos[2 - offset]:
newpos[2 - offset + 1], newpos[2 - offset] = (
newpos[2 - offset],
newpos[2 - offset + 1],
)
if newpos != self.position:
possibilities.add(tuple(newpos))
# merges
for i in range(self.position[offset + 1] + 1):
newpos = self.position.copy()
newpos[1 + offset] -= i
newpos[0 + offset] += i
if newpos[1 + offset] < newpos[0 + offset]:
newpos[0 + offset], newpos[1 + offset] = (
newpos[1 + offset],
newpos[0 + offset],
)
if newpos != self.position and max(newpos) < 5 and min(newpos) >= 0:
possibilities.add(tuple(newpos))
# Gets every child object (for recursion)
possibility_objects = []
for i in possibilities:
pobject = Position(i, not self.turn)
pobject_string = str(pobject)
if pobject_string not in cache:
cache[pobject_string] = pobject
else:
pobject = cache[pobject_string]
possibility_objects.append(pobject)
return possibility_objects
def recursivelyGenerateChildren(self, cache):
if self.won:
return
self.initialized = True
# Get kids
# Update parent with kid info
self.children = self.getValidPositions(cache)
# Update kids with parent info
for child in self.children:
child.parents.append(self)
for child in self.children:
if child.initialized:
pass
else:
child.recursivelyGenerateChildren(cache)
def __str__(self):
string = ""
for i in self.position:
string += str(i)
string += "1" if self.turn else "2"
return string
setrecursionlimit(1000000)
def gengraph(damping, skip=True):
cache = {}
initial = Position([1, 1, 1, 1], True)
cache[str(initial)] = initial
initial.recursivelyGenerateChildren(cache)
wonqueue = deque()
for i in cache.values():
if i.won == True or i.won == False:
wonqueue.append(i)
# Calculate which positions are guranteed to be won by a side, and how many moves it takes to win from such a position
while len(wonqueue) > 0:
item = wonqueue.popleft()
for parent in item.parents:
allarelost = True6.7710394
veteranparent = parent.won is not None
for child in parent.children:
if child.won == parent.turn:
parent.won = parent.turn
allarelost = False
if child.won == None:
allarelost = False
if allarelost:
parent.won = not parent.turn
if not veteranparent and parent.won is not None:
wonqueue.append(parent)
parent.distance = item.distance + 1
else:
if item.distance < parent.distance:
parent.distance = item.distance + 1
positionlist = list(cache.values())
# Remove any position where True (the human player) is winning. The AI plays optimally, which means the human will never be winning. Thus, considering that as a possibility in the PageRank calculation will throw off the results.
for position in positionlist:
position.children = [x for x in position.children if not x.won == True]
positionlist = [x for x in positionlist if not x.won == True]
# Build a sympy matrix that is then used to rref the pagerank
matrixA = []
matrixC = []
for position in positionlist:
matrixrow = [0] * (len(positionlist))
matrixrow[positionlist.index(position)] = 1
matrixCval = [0]
if position.won == False:
matrixCval[0] = 1
else:
children = position.children
childind = []
for child in children:
childind.append(positionlist.index(child))
for ind in childind:
matrixrow[ind] = -1 / len(position.children) * damping
matrixCval = [0]
matrixC.append(matrixCval)
matrixA.append(matrixrow)
A = np.array(matrixA, dtype="float")
C = np.array(matrixC, dtype="float")
sol = np.linalg.solve(A, C)
oldsol = sol
for (i, position) in enumerate(positionlist):
position.blunderscore = sol[i][0]
if skip:
newpositionlist = deepcopy(positionlist)
for position in newpositionlist:
if position.turn == False:
maxblunder = 0
for child in position.children:
maxblunder = max(maxblunder, child.blunderscore)
position.children = [
x for x in position.children if x.blunderscore == maxblunder
]
if len(position.children) != 1 and position.won != False:
print("ALARM")
matrixA = []
matrixC = []
for position in newpositionlist:
matrixrow = [0] * (len(newpositionlist))
matrixrow[newpositionlist.index(position)] = 1
matrixCval = [0]
if position.won == False:
matrixCval[0] = 1
else:
children = position.children
childind = []
for child in children:
childind.append(newpositionlist.index(child))
for ind in childind:
matrixrow[ind] = -1 / len(position.children) * damping
matrixCval = [0]
matrixC.append(matrixCval)
matrixA.append(matrixrow)
A = np.array(matrixA, dtype="float")
C = np.array(matrixC, dtype="float")
sol = np.linalg.solve(A, C)
for (i, position) in enumerate(newpositionlist):
cache[str(position)].blunderscore = sol[i][0]
return cache
# player class - given a position, makes a move
class ComputerPlayer:
def move(self, position):
wonmoves = []
mindepth = 1000
for child in position.children:
if child.won == False:
mindepth = min(mindepth, child.distance)
wonmoves.append(child)
for move in wonmoves:
if move.distance == mindepth:
return move
return random.choice(position.children)
# player class - given a position, makes a move
class EfficientComputerPlayer:
def move(self, position):
wonmoves = []
mindepth = 1000
maxblunder = 0
for child in position.children:
maxblunder = max(maxblunder, child.blunderscore)
if child.won == False:
mindepth = min(mindepth, child.distance)
wonmoves.append(child)
for move in wonmoves:
if move.distance == mindepth:
return move
for child in position.children:
if child.blunderscore == maxblunder:
return child
class RandomPlayer:
def move(self, position):
return random.choice(position.children)
class HumanPlayer:
def move(self, position):
return random.choice(
list(x for x in position.children if (x.distance > 6 or x.won != False))
)
def runtests(turn, iters, cache):
resarr = []
initial = cache["11112"] if not turn else cache["11111"]
initialcounter = 1 if not turn else 0
iters = iters
counters = []
for i in range(iters):
pos = initial
counter = initialcounter
while pos.won != False:
if counter % 2 == 0:
pos = RandomPlayer().move(pos)
else:
pos = ComputerPlayer().move(pos)
counter += 1
counters.append(counter)
print(mean(counters))
resarr.append(mean(counters))
counters = []
for i in range(iters):
pos = initial
counter = initialcounter
while pos.won != False:
if counter % 2 == 0:
pos = RandomPlayer().move(pos)
else:
pos = EfficientComputerPlayer().move(pos)
counter += 1
counters.append(counter)
print(mean(counters))
resarr.append(mean(counters))
counters = []
for i in range(iters):
pos = initial
counter = initialcounter
while pos.won != False:
if counter % 2 == 0:
pos = HumanPlayer().move(pos)
else:
pos = ComputerPlayer().move(pos)
counter += 1
counters.append(counter)
print(mean(counters))
resarr.append(mean(counters))
counters = []
for i in range(iters):
pos = initial
counter = initialcounter
while pos.won != False:
if counter % 2 == 0:
pos = HumanPlayer().move(pos)
else:
pos = EfficientComputerPlayer().move(pos)
counter += 1
counters.append(counter)
print(mean(counters))
resarr.append(mean(counters))
return resarr
resarff = []
for test in [0.1, 0.25, 0.5, 0.75, 0.85, 0.95, 0.99, 0.999]:
print(test)
cache = gengraph(test)
resarr1 = runtests(0, 10000000, cache)
cache = gengraph(test)
resarr2 = runtests(1, 10000000, cache)
resarrf = []
for i in range(4):
resarrf.append(resarr1[i] / 2 + resarr2[i] / 2)
resarff.append(resarrf)
print(test)
for (i, r) in enumerate(resarff):
print([0.1, 0.25, 0.5, 0.75, 0.85, 0.95, 0.99, 0.999][i], ": ", r)
pos = cache["11112"]
# currentpos = initial
while False:
print("Available moves are:")
for ind, child in enumerate(currentpos.children):
print(
ind,
" ",
child,
" ",
child.won,
" ",
child.blunderscore,
" ",
child.distance,
)
while True:
move = input("Enter your move (number)")
if move == "explore":
for i in cache[input("Enter cache compatible position string: ")].children:
print(i, i.won, i.blunderscore, sep=" ")
elif move == "help":
print("Set - sets position using cache compatible position string")
print("Explore - outputs children of a move")
for ind, child in enumerate(currentpos.children):
print(ind, " ", child, " ", child.won, " ", child.blunderscore)
elif move == "set":
newpos = cache[input("Enter cache compatible position string: ")]
break
else:
try:
newpos = currentpos.children[int(move)]
break
except:
pass
bestwinpos = None
bestwindistance = 1000
bestblunderscore = 0
for child in newpos.children:
bestblunderscore = max(bestblunderscore, child.blunderscore)
print(child, " ", child.won, " ", child.blunderscore, " ", child.distance)
if child.won == False:
if bestwindistance > child.distance:
bestwindistance = child.distance
bestwinpos = child
if bestwinpos is not None:
currentpos = bestwinpos
else:
for child in newpos.children:
if child.blunderscore == bestblunderscore:
currentpos = child
print("AI PLAYED ", currentpos, " ", currentpos.won)
if sum(currentpos.position[0:2]) == 0:
print("YOU LOST")
break