why

Authormomo
Submission date2012-09-03 18:46:10.121999
Rating7332
Matches played790
Win rate76.33

Use rpsrunner.py to play unranked matches on your computer.

Source code:

import random, math



def highest(v):

    return random.choice([i for i in range(len(v)) if max(v) == v[i]])



def lowest(v):

    return random.choice([i for i in range(len(v)) if min(v) == v[i]])



def best(c):

    return highest([c[1]-c[2], c[2]-c[0], c[0]-c[1]])



if(1):

    if (input == ""):

        N = 1

        AR1 = 0.86

        states = ["R","S","P"]

        st = [0,1,2]

        sdic = {"R":0, "S":1, "P":2}

        table = {}

        cutoff = 400

        last1 = 1



        hennies = 5

        res = [[0, 1, -1], [-1, 0, 1], [1, -1, 0]]

        r=0

        MEM2 = [3,4]

        M = len(MEM2)*2 + 1
        ALL = M*3

        if(last1 == 1):
                M = len(MEM2)*2 
                ALL = M*3+1
        

        models = [0.3,0.9,0.6,0.4,0.3,1]*(len(MEM2))+ [1]

        state = [0] * (ALL)

        yo = random.choice(st)

        tu = random.choice(st)



        pa = (yo, tu)

        hi = [pa]

        hit = [tu]
        prognosis = [random.choice(st) for i in range(ALL)]

        choices = []



    else:

          tu = sdic[input]

          pa = (yo,tu)

          hi += [pa]

          hit += [tu]
          state = [ AR1 * state[i] + res[prognosis[i]][tu] * models[i] for i in range(ALL)]



          r = res[yo][tu]







    prognosis = [random.choice(st) for j in range(ALL)]

    prop =  [random.choice(st) for j in range(len(MEM2)*2)]

    i = 0

 

    #are our last moves repeating history? if yes, pick a random occurence - but prefer newer matches.

    for m in MEM2:

      if(N + 1> m):

        key = tuple(hi[-m-1:-1])

        if (key in table): 

                table[key] += [pa]

        else: 

                table[key] = [pa]

      if(N > m):

        key = tuple(hi[-m:])

        if (key in table): 

                ch = table[key]

                k = len(ch)

                k = max(random.choice(range(0,k)),random.choice(range(0,k)))

                prop[i] = ch[k][0]



                prop[i+1] = ch[k][1]

  

      i += 2

     

    #fallback: this is the predictor used in reflex, an improvement over a couple of hennies, (henny from see http://webdocs.cs.ualberta.ca/~darse/rsbpc.html

    p = [0,0,0]
    p[hit[random.randint(0, N-1)]] += 1
    for k in [300,100]:
        if N > k: p[hit[random.randint(N-k-1, N-1)]] += 1
    

    j = random.randint(0, N-1)
    p[(3 + tu + hit[j]- hit[j-1])%3] += 1

   
    if N > 5:
     j = random.randint(5, N-1)
     p[(3+ hit[N-4] + hit[j]- hit[j-5])%3] += 1
    
    if N > 5:
     j = random.randint(5, N-1)
     p[(16+ sum(hit[N-5:N]) - sum(hit[j-5:j]))%3] += 1


    i = -3

    for m in range(len(MEM2)):

       i += 3; prognosis[i] = (prop[2*m])

       i += 3; prognosis[i] = (prop[2*m+1])

    i += 3; prognosis[i]  =  best(p)
    i += 3

    if (last1 == 1):
        i -= 2


    assert(i==ALL)

   

    for j in range(M):

        prognosis[j*3 + 1] = (prognosis[j*3] + 1) % 3

        prognosis[j*3 + 2] = (prognosis[j*3+1] + 1) % 3





    best = highest(state)

    yo = prognosis[best]

    output = states[yo]  

    N = N + 1