This program has been disqualified.
Author | Sean |
Submission date | 2016-08-22 10:38:56.028915 |
Rating | 6111 |
Matches played | 67 |
Win rate | 62.69 |
if input == "":
import math
log = math.log
exp = math.exp
log_half = log(0.5)
third = 1.0 / 3.0
log_third = log(1.0/3.0)
log_two_thirds = log(2.0/3.0)
def log_add(x, y):
if y > x:
x, y = y, x
d = y - x
if d < -60:
return x
return x + log(1.0 + exp(d))
def log_mean(x, y):
return log_half + log_add(x, y)
def meta(k, c):
if k == c:
d = 0
elif k == beat[c]:
d = 1
else:
d = 2
return d
class ContextTree:
def __init__(self):
self.p_self = 0.0
self.p = 0.0
self.counts = [0, 0, 0]
self.children = [None, None, None]
def update(self, history, c, i=0):
counts = self.counts
scores = [0.0 for _ in xrange(3)]
for j in xrange(3):
scores[j] = counts[beaten[j]] - counts[beat[j]]
k = scores.index(max(scores))
d = meta(k, c)
t = sum(counts) + 1.5
self.p_self += log((counts[d] + 0.5) / t)
if i >= min(len(history) - 1, 32):
self.p = self.p_self
return
x = history[i]
counts[d] += 1
if self.children[x] is None:
self.children[x] = ContextTree()
self.children[x].update(history, c, i + 1)
p_children = 0.0
for child in self.children:
if child is not None:
p_children += child.p
self.p = log_mean(self.p_self, p_children)
def predict(self, history, ps, i=0):
counts = self.counts
scores = [0.0 for _ in xrange(3)]
for j in xrange(3):
scores[j] = counts[beaten[j]] - counts[beat[j]]
k = scores.index(max(scores))
t = sum(counts) + 1.5
p_self = (self.p_self + log((counts[meta(k, c)] + 0.5) / t) for c in xrange(3))
if i >= min(len(history) - 1, 32):
for i, p in enumerate(p_self):
ps[i] += p
return
x = history[i]
p_children = [0.0 for _ in self.children]
factor = 0.0
for y, child in enumerate(self.children):
if child is not None:
if y == x:
child.predict(history, p_children, i + 1)
else:
factor += child.p
elif y == x:
factor += log_third
for j, p in enumerate(p_children):
p_children[j] = p + factor
p_below = (log_mean(ps, pc) for ps, pc in zip(p_self, p_children))
for i, p in enumerate(p_below):
ps[i] += p
import collections
import random
R, P, S = range(3)
index = {"R": R, "P": P, "S": S}
name = ("R", "P", "S")
beat = (P, S, R)
beaten = (S, R, P)
model = ContextTree()
history = collections.deque([])
output = random.choice(name)
else:
i = index[input]
j = index[output]
model.update(history, i)
history.appendleft(i)
history.appendleft(j)
ps = [0.0, 0.0, 0.0]
model.predict(history, ps)
p0 = min(ps)
for i, p in enumerate(ps):
ps[i] = exp(p - p0)
scores = [0, 0, 0]
t = sum(ps)
for _ in xrange(3):
x = 0
r = random.uniform(0, t)
for k, p in enumerate(ps):
x += p
if x >= r:
break
scores[beat[k]] += 1
scores[beaten[k]] -= 1
m = max(scores)
output = name[random.choice([k for k, x in enumerate(scores) if x == m])]