BothCM_CH9R

This program has been disqualified.


Authordavid.werecat
Submission date2014-12-13 01:22:15.186492
Rating5000
Matches played0
Win rate0

Source code:

#Created by David Catt on December 12, 2014
import random

class CtxMap:
    def __init__(self, order):
        self.order = order
        self.mask = 3 ** order
        self.prob = [random.randint(0, 2) for a in range(0, self.mask)]
        self.ctx = 0
    def predict(self):
        return self.prob[self.ctx]
    def update(self, cvl, val):
        self.prob[self.ctx] = val
        self.ctx = ((self.ctx * 3) + cvl) % self.mask
    def nxtctx(self, cvl):
        self.ctx = ((self.ctx * 3) + cvl) % self.mask

class CtxModel:
    def __init__(self, order, decay):
        self.order = order
        self.decay = decay
        self.mask = 3 ** (order + 1)
        self.prob = [0.0] * self.mask
        self.ctx = 0
    def predict(self):
        tvl = 0
        tpr = 0.0
        prb = 0.0
        for i in range(0, 3):
            prb = self.prob[self.ctx + i]
            if prb > tpr:
                tvl = i
                tpr = prb
        return tvl
    def rndpredict(self):
        p0 = self.prob[self.ctx]
        p1 = self.prob[self.ctx + 1]
        p2 = self.prob[self.ctx + 2]
        rv = random.random() * (p0 + p1 + p2)
        if rv <= p0:
            return 0
        elif rv <= p1:
            return 1
        else:
            return 2
    def update(self, cvl, val):
        for i in range(0, 3):
            self.prob[self.ctx + i] *= self.decay
        self.prob[self.ctx + val] += 1.0
        self.ctx = ((self.ctx + cvl) * 3) % self.mask
    def nxtctx(self, cvl):
        self.ctx = ((self.ctx + cvl) * 3) % self.mask

class ModelSwitch:
    def __init__(self, count, decay):
        self.value = [1,0,-1,-1,1,0,0,-1,1]
        self.count = count
        self.decay = decay
        self.offset = [0.0] * count
        self.weigh = [0.0] * count
        self.pred = [0] * count
    def setoffset(self, idx, val):
        self.offset[idx] = val
    def setvalue(self, idx, val):
        self.pred[idx] = val
    def predict(self):
        tvl = 0
        tpr = -1.0
        prb = 0.0
        for i in range(0, self.count):
            prb = self.weigh[i] + self.offset[i]
            if prb > tpr:
                tvl = self.pred[i]
                tpr = prb
        return tvl
    def update(self, val):
        for i in range(0, self.count):
            self.weigh[i] = (self.weigh[i] * self.decay) + self.value[(val * 3) + self.pred[i]]

if input == "":
    winner = [1,2,0]
    nval = {"R":0,"P":1,"S":2,"":0}
    cval = ["R","P","S"]
    amdl = [CtxMap((i % 6) + 1) if i < 24 else CtxMap((((i - 24) % 4) + 1) * 2) for i in range(0, 32)]
    omdl = [CtxModel((i % 6) + 1, 0.93) if i < 24 else CtxModel((((i - 24) % 4) + 1) * 2, 0.93) for i in range(0, 32)]
    zmdl0 = CtxModel(0, 0.93)
    zmdl1 = CtxModel(0, 0.93)
    msse = ModelSwitch(304, 0.93)
    for i in range(192, 288):
        msse.setoffset(i, -0.05)
    msse.setoffset(300, -0.1)
    msse.setvalue(301, 0)
    msse.setvalue(302, 1)
    msse.setvalue(303, 2)
    lval = 0
else:
    val = nval[input]
    for i in range(0, 6):
        amdl[i].update(val, val)
        amdl[i+6].update(lval, val)
        amdl[i+12].update(val, lval)
        amdl[i+18].update(lval, lval)
        omdl[i].update(val, val)
        omdl[i+6].update(lval, val)
        omdl[i+12].update(val, lval)
        omdl[i+18].update(lval, lval)
    for i in range(24, 28):
        amdl[i].nxtctx(val)
        amdl[i].update(lval, val)
        amdl[i+4].nxtctx(val)
        amdl[i+4].update(lval, lval)
        omdl[i].nxtctx(val)
        omdl[i].update(lval, val)
        omdl[i+4].nxtctx(val)
        omdl[i+4].update(lval, lval)
    zmdl0.update(val, val)
    zmdl1.update(val, lval)
    msse.update(val)
pv = 0
for i in range(0, 32):
    pv = amdl[i].predict()
    msse.setvalue(i, pv)
    pv = winner[pv]
    msse.setvalue(i + 32, pv)
    pv = winner[pv]
    msse.setvalue(i + 64, pv)
    pv = omdl[i].predict()
    msse.setvalue(i + 96, pv)
    pv = winner[pv]
    msse.setvalue(i + 128, pv)
    pv = winner[pv]
    msse.setvalue(i + 160, pv)
    pv = omdl[i].rndpredict()
    msse.setvalue(i + 192, pv)
    pv = winner[pv]
    msse.setvalue(i + 224, pv)
    pv = winner[pv]
    msse.setvalue(i + 256, pv)
pv = zmdl0.predict()
msse.setvalue(288, pv)
pv = winner[pv]
msse.setvalue(289, pv)
pv = winner[pv]
msse.setvalue(290, pv)
pv = zmdl1.predict()
msse.setvalue(291, pv)
pv = winner[pv]
msse.setvalue(292, pv)
pv = winner[pv]
msse.setvalue(293, pv)
pv = nval[input]
msse.setvalue(294, pv)
pv = winner[pv]
msse.setvalue(295, pv)
pv = winner[pv]
msse.setvalue(296, pv)
pv = lval
msse.setvalue(297, pv)
pv = winner[pv]
msse.setvalue(298, pv)
pv = winner[pv]
msse.setvalue(299, pv)
msse.setvalue(300, random.randint(0, 2))
lval = winner[msse.predict()]
output = cval[lval]