# Hybrid

 Author Sean Submission date 2016-02-24 14:07:38.774768 Rating 7504 Matches played 401 Win rate 72.32

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

## Source code:

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

import collections
import random
import math

gamma = random.gammavariate
sqrt = math.sqrt
log =math.log
R, P, S = 0, 1, 2
index = {"R": R, "P": P, "S": S}
beat = (P, S, R)
name = ("R", "P", "S")
strategies = [("P", "S", "R"),
("S", "R", "P"),
("R", "P", "S")]

history = collections.deque([])

def ucb(s, n, t):
return (s / n) + sqrt(2 * log(t) / n)

def random_index(ps):
t = sum(ps)
r = random.uniform(0, t)
x = 0
for i, p in enumerate(ps):
x += p
if r <= x:
break
return i

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

def update(self, h, i, prediction=None):
stop = False
for d, k in enumerate(h):
self.counts[i] += 2
if stop or d >= 16:
return
if self.children is None:
self.children = [None for _ in xrange(3)]
if self.children[k] is None:
self.children[k] = MixTree()
stop = True
self = self.children[k]

def predict(self, h):
n0 = [1, 1, 1]
for d, k in enumerate(h):
for i, x in enumerate(self.counts):
n0[i] += x
if self.children is None:
break
child = self.children[k]
if child is None:
break
self = child
return n0

class SwitchTree:
def __init__(self, counts = None):
self.counts = [0.0 for _ in xrange(3)]
self.visits = [0.0 for _ in xrange(3)]
self.children = None

def score(self, t):
return ucb(self.pos_total, self.neg_total, self.total_visits, t)

def select_move(self, t):
n = sum(self.counts)
m = sum(1 for x in self.counts if x)
if m == 0:
a = 0.5
else:
a = m / (6 * log((n + 1.0) / m))
r = gamma(self.counts + a, 1)
p = gamma(self.counts + a, 1)
s = gamma(self.counts + a, 1)
scores = [s - p, r - s, p - r]
scores = [ucb(s, n + 0.5, t) for s, n in zip(scores, self.visits)]
best = max(scores)
return (best, scores.index(best))

def update(self, h, i):
for n in h:
self.counts[i] += 1
if self.children is None:
break
self = self.children[n]
if self is None:
break

def predict(self, h):
path = []
stop = False
path.append(self)
for d, n in enumerate(h):
if stop or d >= 16:
break
if self.children is None:
self.children = [None for _ in xrange(3)]
if self.children[n] is None:
self.children[n] = SwitchTree()
stop = True
child = self.children[n]
self = child
path.append(self)
t = sum(1.5 + sum(node.visits) for node in path)
best_score = float("-inf")
for i, n in enumerate(path):
score, move = n.select_move(t)
if score >= best_score:
j = i
best_score = score
best_node = n
best_move = move
return (best_node, best_move)
mix = MixTree()
switch = SwitchTree()
node = switch
scores = [[0, 0], [0, 0]]
visits = [0, 0]
else:

i = index[input]
j = index[output]
for k, p in enumerate([p0, p1]):
if p == beat[i]:
scores[k] += 1
elif i == beat[p]:
scores[k] += 1
mix.update(history, i)
node.visits[j] += 1
switch.update(history, i)
history.appendleft(i)
history.appendleft(j)

counts = mix.predict(history)
hypotheses = [random_index(counts) for _ in xrange(6)]
es = [0, 0, 0]
for i, _ in enumerate(es):
for h in hypotheses:
if i == beat[h]:
es[i] += 1
elif h == beat[i]:
es[i] -= 1
best = max(es)
p0 = random.choice([i for i in xrange(3) if es[i] == best])
node, p1 = switch.predict(history)
s0 = (scores - scores) / (sum(scores) + 1.0)
s1 = (scores - scores) / (sum(scores) + 1.0)
t = sum(visits)
m = 2 * log(t + 1.0)
if s0 + sqrt(m / (visits + 1.0)) > s1 + sqrt(m / (visits + 1.0)):
p = p0
visits += 1
else:
p = p1
visits += 1
output = name[p]``````
{"ft_lang_label":"__label__en","ft_lang_prob":0.524097,"math_prob":0.9994209,"size":3542,"snap":"2019-51-2020-05","text_gpt3_token_len":1257,"char_repetition_ratio":0.14810628,"word_repetition_ratio":0.06168831,"special_character_ratio":0.40711462,"punctuation_ratio":0.21158393,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99947757,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-12-15T07:51:05Z\",\"WARC-Record-ID\":\"<urn:uuid:4be70102-8dfe-4e3d-b731-413d5b60da5a>\",\"Content-Length\":\"8335\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:edf31785-5fde-4267-b589-814475cfa7ca>\",\"WARC-Concurrent-To\":\"<urn:uuid:464431e3-6294-430d-8b1a-3938ffe416bc>\",\"WARC-IP-Address\":\"172.217.164.179\",\"WARC-Target-URI\":\"http://www.rpscontest.com/entry/5663284820705280\",\"WARC-Payload-Digest\":\"sha1:BSXGS7DPTRP7675TUHLSGJPT573IGRSA\",\"WARC-Block-Digest\":\"sha1:FUM6TJNRQA6NWNBE5CDSJND7JN2RDBJM\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-51/CC-MAIN-2019-51_segments_1575541307797.77_warc_CC-MAIN-20191215070636-20191215094636-00054.warc.gz\"}"}