# SkipTree9/3Hedged

 Author Sean Submission date 2016-02-20 21:03:55.359170 Rating 5787 Matches played 417 Win rate 58.03

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

## Source code:

``````if input == "":

import collections
import random
import math

log = math.log
sqrt = math.sqrt

class MarkovTree:
def __init__(self, counts = None):
self.counts = [0 for _ in xrange(3)]
self.children = None
self.total = 0

def update_helper(self, h, i, p, d, skips):
stop = False
for j in xrange(p, len(h)):
k = h[j]
self.counts[i] += 2
self.total += 2
if stop or d >= 9 or skips >= 3:
return
d += 1
if self.children is None:
self.children = [None for _ in xrange(4)]
self.children[3] = MarkovTree()
if self.children[k] is None:
self.children[k] = MarkovTree()
stop = True
self.children[3].update_helper(h, i, j + 1, d, skips + 1)
self = self.children[k]

def update(self, h, i):
self.update_helper(h, i, 0, 0, 0)

def predict_helper(self, h, p, n0):
for j in xrange(p, len(h)):
k = h[j]
for i, x in enumerate(self.counts):
n0[i] += x
if self.children is None:
return
self.children[3].predict_helper(h, j + 1, n0)
child = self.children[k]
if child is None:
return
self = child

def predict(self, h):
n0 = [1, 1, 1]
self.predict_helper(h, 0, n0)
return n0

R, P, S = 0, 1, 2
index = {"R": R, "P": P, "S": S}
beat = ("P", "S", "R")

tree = MarkovTree()

history = collections.deque([])
output = random.choice(beat)
random_plays = 1
model_plays = 1
score = 0
random_play = 0
model_play = 1
play_type = random_play
else:
m = 2.0 * (random_plays + model_plays)
a = sqrt(m / random_plays)
b = (score / float(model_plays)) + sqrt(m / model_plays)
if a > b:
play_type = random_play
output = random.choice(beat)
random_plays += 1
else:
model_plays += 1
i = index[input]
j = index[output]
if play_type == model_play:
if output == beat[i]:
score += 1
elif input == beat[j]:
score -= 1
play_type = model_play
tree.update(history, i)
history.appendleft(i)
history.appendleft(j)

counts = tree.predict(history)
t = sum(counts)
r = random.uniform(0, t)
x = 0
for i, p in enumerate(counts):
x += p
if r <= x:
break
output = beat[i]

random_plays *= 0.99
model_plays *= 0.99
score *= 0.99``````