cts-meta-7

This program has been disqualified.


AuthorSean
Submission date2016-09-01 22:46:51.960666
Rating7298
Matches played102
Win rate73.53

Source code:

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 to_probs(ps):
        p0 = min(ps)
        qs = [exp(p - p0) for p in ps]
        rt = 1.0 / sum(qs)
        for i, q in enumerate(qs):
            qs[i] = q * rt
        return qs

    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_sub(x, y):
        d = y - x
        return x + log(1.0 - exp(d))

    def log_mean(x, y):
        return log_half + log_add(x, y)

    def log_mean_3(x, y, z):
        return log_third + log_add(log_add(x, y), z)

    class ContextTree:
        def __init__(self):
            self.p = 0.0
            self.p_children = 0.0
            self.weights = [log_third for _ in xrange(3)]
            self.counts = [0, 0, 0]
            self.meta_counts = [0, 0, 0]
            self.cond = [third for _ in xrange(3)]
            self.meta = [0 for _ in xrange(3)]
            self.children = [None, None, None]
        def update(self, depth, alpha, beta, history, c, i=0):
            counts = self.counts
            meta_counts = self.meta_counts
            cond = self.cond
            t = sum(counts)
            rt = 1.0 / (t + 1.0)
            if t:
                p0 = min(cond)
                scores = [exp(cond[beaten[j]] - p0) - exp(cond[beat[j]] - p0) for j in xrange(3)]
                q = max(scores)
                n = scores.count(q)
                if n == 1:
                    k = scores.index(q)
                    d = (k + c) % 3
                    cond_p_meta = log((meta_counts[d] + third) * rt)
                    meta_counts[d] += 1
                else:
                    cond_p_meta = log_third
            else:
                cond_p_meta = log_third
            cond_p_self = log((counts[c] + third) * rt)
            counts[c] += 1
            if i >= min(len(history) - 1, depth):
                self.p += cond_p_self
                return
            x = history[i]
            if self.children[x] is None:
                self.children[x] = ContextTree()
            self.children[x].update(depth, alpha, beta, history, c, i + 1)
            p_children = 0.0
            for child in self.children:
                if child is not None:
                    p_children += child.p
            w0, w1, w2 = self.weights
            cond_p_children = p_children - self.p_children
            self.p_children = p_children
            self.p = log_add(log_add(w0 + cond_p_self, w1 + cond_p_meta), w2 + cond_p_children)
            probs = (cond_p_self, cond_p_meta, cond_p_children)
            base = alpha + self.p
            for i, (w, p) in enumerate(zip(self.weights, probs)):
                self.weights[i] = log_add(base, beta + w + p)
        def predict(self, depth, history, ps, i=0):
            counts = self.counts
            meta_counts = self.meta_counts
            scores = [0.0 for _ in xrange(3)]
            for j in xrange(3):
                scores[j] = counts[beaten[j]] - counts[beat[j]]
            q = max(scores)
            n = scores.count(q)
            rt = 1.0 / (sum(counts) + 1.0)
            if n == 1:
                k = scores.index(q)
                cond_p_meta = (log((meta_counts[(k + c) % 3] + third) * rt) for c in xrange(3))
            else:
                cond_p_meta = (log_third for c in xrange(3))
            cond_p_self = (log((counts[c] + third) * rt) for c in xrange(3))
            if i >= min(len(history) - 1, depth):
                for i, p0 in enumerate(cond_p_self):
                    ps[i] += p0 + self.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(depth, history, p_children, i + 1)
                    else:
                        factor += child.p
                elif y == x:
                    factor += log_third
            w0, w1, w2 = self.weights
            w3 = w2 + factor - self.p_children
            for i, (pse, pm, pc) in enumerate(zip(cond_p_self, cond_p_meta, p_children)):
                self.cond[i] = log_add(w0 + pse, log_add(w1 + pm, w3 + pc))
            for i in xrange(3):
                ps[i] += self.cond[i]

    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()
    my_model = ContextTree()
    their_model = ContextTree()
    history = collections.deque([])
    my_history = collections.deque([])
    their_history = collections.deque([])
    output = random.choice(name)
    rnd = 0
    log_p = 0.0
    weights = [log_third for _ in xrange(3)]
    cond_ps = [[third for _ in xrange(3)] for _ in xrange(3)]
else:
    inp = index[input]
    out = index[output]
    inp_ps = [w + log(p[inp]) for w, p in zip(weights, cond_ps)]
    log_p = float("-inf")
    for p in inp_ps:
        log_p = log_add(log_p, p)
    rnd += 1
    alpha = 1.0 / (rnd + 2)
    beta  = 1 - 2 * alpha
    alpha = log(alpha)
    beta = log(beta)

    base = alpha + log_p
    for i, p in enumerate(inp_ps):
        weights[i] = log_add(base, beta + p)

    model.update(9, alpha, beta, history, inp)
    my_model.update(3, alpha, beta, my_history, inp)
    their_model.update(3, alpha, beta, their_history, inp)

    history.appendleft(inp)
    history.appendleft(out)
    my_history.appendleft(out)
    their_history.appendleft(inp)
    ps = [0.0, 0.0, 0.0]
    my_ps = [0.0, 0.0, 0.0]
    their_ps = [0.0, 0.0, 0.0]
    model.predict(9, history, ps)
    my_model.predict(3, my_history, my_ps)
    their_model.predict(3, their_history, their_ps)
    cond_ps = [to_probs(p) for p in (ps, my_ps, their_ps)]
    def get_ps():
        for qs in zip(*cond_ps):
            p = float("-inf")
            for w, q in zip(weights, qs):
                p = log_add(p, q)
            yield p
    ps = list(get_ps())
    ps = to_probs(ps)
    scores = [0, 0, 0]
    for _ in xrange(3):
        r = random.uniform(0, 1)
        for k, p in enumerate(ps):
            r -= p
            if r <= 0:
                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])]