Author | david.werecat |
Submission date | 2018-08-01 20:25:07.386310 |
Rating | 7773 |
Matches played | 295 |
Win rate | 76.61 |
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 nextctx(self, val):
self.ctx = ((self.ctx + val + 1) << 2) & self.mask
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):
#self.preds[self.weigh.index(max(self.weigh))]
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.update(uval)
cUU.nextctx(uval)
cUT.update(tval)
cUT.nextctx(uval)
cTU.update(uval)
cTU.nextctx(tval)
cTT.update(tval)
cTT.nextctx(tval)
cBU.update(uval)
cBU.nextctx(uval)
#cBU.update(uval)
cBU.nextctx(tval)
cBT.update(tval)
cBT.nextctx(uval)
#cBT.update(tval)
cBT.nextctx(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]