Bog

This program has been disqualified.


AuthorSean
Submission date2016-02-25 10:05:50.714374
Rating6468
Matches played66
Win rate65.15

Source code:

if input == "":

    import collections
    import math
    import random

    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")
    third = 1.0 / 3.0
    plus1 = [1, 2, 0]
    plus2 = [2, 0, 1]

    class MarkovTree:
        def __init__(self, parent = None):
            self.counts = [0.0 for _ in xrange(3)]
            self.meta = [0.0 for _ in xrange(3)]
            self.visits = [1.0 for _ in xrange(3)]
            self.children = None
            self.prev = None
            self.parent = parent

        def select_move(self, k):
            r = gamma(self.counts[0] + 1, 1)
            p = gamma(self.counts[1] + 1, 1)
            s = gamma(self.counts[2] + 1, 1)
            u = 1.0 / (r + p + s)
            scores = [(s - p) * u, (r - s) * u, (p - r) * u]
            m = scores.index(max(scores))
            self.prev = m
            meta = [gamma(n + 1.0, 1) for n in self.meta]
            v = 1.0 / sum(meta)
            meta = [n * v for n in meta]
            meta_scores = [0, 0, 0]
            meta_scores[m] = meta[0] - meta[2]
            meta_scores[plus2[m]] = meta[1] - meta[0]
            meta_scores[plus1[m]] = meta[2] - meta[1]
            bounds = [sqrt(max(log(1000 / (3 * v)), 0) / v) for v in self.visits]
            for i, (s, bound) in enumerate(zip(scores, bounds)):
                scores[i] = s + bound
            for i, (s, v) in enumerate(zip(meta_scores, bounds)):
                meta_scores[i] = s + bound
            best = max(scores)
            meta_best = max(meta_scores)
            if best >= 0.7 * meta_best:
                return (best, scores.index(best))
            return (meta_best, meta_scores.index(meta_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] = MarkovTree(self)
                    stop = True
                child = self.children[n]
                self = child
                path.append(self)
            best_score = float("-inf")
            best_node = None
            k = 3 * len(path) + 1
            leaf = self
            for n in path:
                score, move = n.select_move(k)
                if score >= best_score:
                    best_score = score
                    best_node = n
                    best_move = move
            return (leaf, best_move, best_score)

    both = MarkovTree()
    us = MarkovTree()
    them = MarkovTree()
    history = collections.deque([])
    our_history = collections.deque([])
    their_history = collections.deque([])
    a, b, c = None, None, None

else:

    i = index[input]
    j = index[output]

    bt = beat[i]
    for node in (a, b, c):
        while node is not None:
            node.visits[j] += 1
            if node.prev == bt:
                node.meta[0] += 1
            elif plus2[node.prev] == bt:
                node.meta[1] += 1
            else:
                node.meta[2] += 1
            node = node.parent
    both.update(history, i)
    us.update(our_history, i)
    them.update(their_history, i)

    history.appendleft(i)
    history.appendleft(j)
    our_history.appendleft(j)
    their_history.appendleft(i)

a, m0, s0 = both.predict(history)
b, m1, s1 = them.predict(their_history)
c, m2, s2 = us.predict(our_history)
m = m0
s = s0
if s1 > s:
    m = m1
    s = s1
if s2 > s:
    m = m2
    s = s2
output = name[m]