v8_kNN

Authorbhyung
Submission date2018-08-29 07:26:36.119591
Rating5189
Matches played275
Win rate50.91

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

Source code:

import random
import math
r = "R"
p = "P"
s = "S"
mem = []

# list containing probability + probability list + num reps + repetition number list
rr = rp = rs = pr = pp = ps = sr = sp = ss = 0
rrl = rpl = rsl = prl = ppl = psl = srl = spl = ssl = []
rep = 0
repl = []

### features = probability of rps next move given previous hand + number of repetitions

# Euclidean distance fx
def distance(x0,y0,x1,y1):
    return math.sqrt((x0-y0)**2+(x1-y1)**2)

# kNN function
def kNN(ppoint,rpoint,k,hist):
    index = 0
    min_index = []
    for j in range(k):
        for i in range(len(ppoint)-1):
            dtemp = 1000
            d = distance(ppoint[0],rpoint[0],ppoint[i+1],rpoint[i+1])

            if dtemp > d:
                dtemp = d
                index = [i+1]
                continue

        min_index.append(index)

    for n in range(len(min_index)):
        rcount = pcount = scount = 0
        if hist[min_index[n]] == r:
            rcount += 1
        elif hist[min_index[n]] == p:
            pcount += 1
        else:
            scount += 1
    
    if rcount > max(pcount,scount):
        return r
    elif pcount > scount:
        return p
    else:
        return s

def update():
   # probability + num reps
    rrl.insert(0,rr/len(mem)) 
    rpl.insert(0,rp/len(mem)) 
    rsl.insert(0,rs/len(mem)) 
    prl.insert(0,pr/len(mem)) 
    ppl.insert(0,pp/len(mem)) 
    psl.insert(0,ps/len(mem)) 
    srl.insert(0,sr/len(mem)) 
    spl.insert(0,sp/len(mem)) 
    ssl.insert(0,ss/len(mem)) 
    repl.insert(0,rep)
    

# start
if len(mem) > 0:
    if mem[0] == mem[1]:
        rep += 1
    else:
        rep = 0
    
if input == "" or len(repl) < 4:
    output = random.choice([r,p,s])
    mem.insert(0,input)
    update()
else:
    mem.insert(0,input)
    for i in range(len(mem)-1):
        if mem[i] == r and mem[i+1] == r:
            rr += 1 
        elif mem[i] == r and mem[i+1] == p:
            rp += 1
        elif mem[i] == r and mem[i+1] == s:
            rs += 1
        elif mem[i] == p and mem[i+1] == r:
            pr += 1
        elif mem[i] == p and mem[i+1] == p:
            pp += 1
        elif mem[i] == p and mem[i+1] == s:
            ps += 1
        elif mem[i] == s and mem[i+1] == r:
            sr += 1
        elif mem[i] == s and mem[i+1] == p:
            sp += 1
        elif mem[i] == s and mem[i+1] == s:
            ss += 1
    
    update()
   

    if input == r:
        ans = kNN(max(rrl[0],rpl[0],rsl[0]),repl,3,mem)
        if ans == r:
            output = p
        elif ans == p:
            output = s
        else:
            output = r
        
    elif input == p:
        ans = kNN(max(prl[0],ppl[0],psl[0]),repl,3,mem)
        if ans == r:
            output = p
        elif ans == p:
            output = s
        else:
            output = r

    elif input == s:
        ans = kNN(max(srl[0],spl[0],ssl[0]),repl,3,mem)
        if ans == r:
            output = p
        elif ans == p:
            output = s
        else:
            output = r