markov1 - Sushi

This program has been disqualified.


AuthorSushisource
Submission date2011-06-12 00:55:30.805429
Rating2195
Matches played2362
Win rate20.15

Source code:

import operator, random, math
#RPS bot based on Markov Chains
def matmult(m, v):
    return map(lambda r: reduce(operator.add,map(operator.mul,r,v)),m)
def normz(v):
    lngth = math.sqrt(reduce(operator.add, map(lambda e: e**2,v)))
    return map(lambda e: e/lngth,v)
def ind(paperchoice):
    if paperchoice == 'R':
        return 0
    if paperchoice == 'P':
        return 1
    return 2
def piteration(m,v):
    step = matmult(m,v);
    for i in range(100):
        step = matmult(m, step)
    return step
def optimum(v):
    if v[0] == max(v):
        return 'P'
    if v[1] == max(v):
        return 'S'
    return 'R'
def setcol(matrix, col, newcol):
    for i,v in enumerate(newcol):
        matrix[i][col] = v
    return matrix

lastmove = None
if not input:
    #First round
    mkv = [[1.0/3,1.0/3,1.0/3], #R
           [1.0/3,1.0/3,1.0/3], #P
           [1.0/3,1.0/3,1.0/3]] #S
    tot = {'R':[0,0,0],'P':[0,0,0],'S':[0,0,0]}
    output = random.choice(['R','P','S'])
else:        
    if lastmove:
        tot[lastmove][ind(input)] += 1
        mkv = setcol(mkv, ind(lastmove), normz(tot[lastmove]))
    steady = piteration(mkv,[1.0/3,1.0/3,1.0/3])
    output = optimum(steady)
lastmove = input