Author | david.werecat |
Submission date | 2018-08-02 20:54:51.621230 |
Rating | 6839 |
Matches played | 302 |
Win rate | 69.21 |
Use rpsrunner.py to play unranked matches on your computer.
import random
class MatchModel:
def __init__(self, order):
self.order = order
self.mask = 1 << (order << 1)
self.ctx = 0
self.hist = [3 for i in range(0, self.mask)]
self.mask -= 1
def predict(self):
tmp = self.ctx
for i in range(0, self.order):
fnd = self.hist[tmp]
if fnd != 3: return fnd
tmp >>= 2
return random.randint(0, 2)
def update(self, val):
tmp = self.ctx
for i in range(0, self.order):
self.hist[tmp] = val
tmp >>= 2
def next(self, val): self.ctx = (self.ctx << 2) + val + 1
def done(self): self.ctx &= self.mask
def upSgl(self, cval, hval):
self.update(hval)
self.next(cval)
self.done()
def upDbl(self, cv0, cv1, hval):
self.update(hval)
self.next(cv0)
self.next(cv1)
self.done()
class ModelPicker:
def __init__(self, count, decay):
self.count = count
self.preds = [0 for i in range(0, count)]
self.weigh = [0.0 for i in range(0, count)]
self.decay = decay
def setpred(self, idx, val): self.preds[idx] = val
def setpreds(self, idx, vals):
vl = len(vals)
for vi in range(0, vl):
self.preds[idx] = vals[vi]
idx += 1
def predict(self):
idx = 0
mxv = -10000.0
tmv = 0
for i in range(0, self.count):
tmv = self.weigh[i]
if tmv > mxv:
mxv = tmv
idx = i
return self.preds[idx]
def update(self, val):
val *= 3
for i in range(0, self.count):
self.weigh[i] = (self.weigh[i] * self.decay) + VALUE[val + self.preds[i]]
if input == "":
VALUE = [1,-1,0,0,1,-1,-1,0,1]
WINS = [1,2,0]
LOSS = [2,0,1]
NVAL = {"R":0,"P":1,"S":2,"":0}
CVAL = ["R", "P", "S"]
cUU = MatchModel(8)
cUT = MatchModel(8)
cTU = MatchModel(8)
cTT = MatchModel(8)
cBU = MatchModel(8)
cBT = MatchModel(8)
sse = ModelPicker(28, 0.93)
tval = random.randint(0, 2)
uval = random.randint(0, 2)
else:
tval = NVAL[input]
cUU.upSgl(uval, uval)
cUT.upSgl(uval, tval)
cTU.upSgl(tval, uval)
cTT.upSgl(tval, tval)
cBU.upDbl(uval, tval, uval)
cBT.upDbl(uval, tval, tval)
sse.update(tval)
sse.preds[0] = cUU.predict()
sse.preds[1] = cUT.predict()
sse.preds[2] = cTU.predict()
sse.preds[3] = cTT.predict()
sse.preds[4] = cBU.predict()
sse.preds[5] = cBT.predict()
sse.preds[6] = tval
sse.preds[7] = uval
for i in range(0, 8):
sse.preds[i + 8] = WINS[sse.preds[i]]
sse.preds[i + 16] = WINS[sse.preds[i + 8]]
sse.setpreds(24, [0, 1, 2, random.randint(0, 2)])
uval = WINS[sse.predict()]
output = CVAL[uval]