markov_v8_mem10

AuthorPiotrekG
Submission date2018-08-28 19:48:48.122370
Rating6191
Matches played272
Win rate59.19

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

Source code:

from __future__ import division
import random
import bisect
import itertools

beat = {'R': 'P', 'P': 'S', 'S': 'R'}


class Markov_model():
    def __init__(self, matrix):
        self.matrix = matrix

    def update_matrix(self, olag2, ilag1, lim):

        self.matrix[olag2][ilag1]['N'] = self.matrix[olag2][ilag1]['N'] + 1

        sum_n = 0
        for i in self.matrix[olag2]:
            sum_n += self.matrix[olag2][i]['N']

        for i in self.matrix[olag2]:
            self.matrix[olag2][i]['Pr'] = self.matrix[olag2][i]['N'] / sum_n

        for i in self.matrix[olag2]:
            self.matrix[olag2][i]['N'] = self.matrix[olag2][i]['N'] * lim / sum_n

    @staticmethod
    def weighted_choice(choices):
        values, weights = zip(*choices)
        total = 0
        cum_weights = []
        for w in weights:
            total += w
            cum_weights.append(total)
        x = random.random() * total
        i = bisect.bisect(cum_weights, x)
        return values[i]

    def predict(self, olag1):

        prs = []
        for i in self.matrix[olag1]:
            prs.append((i, self.matrix[olag1][i]['Pr']))

        prediction = self.weighted_choice(prs)

        return prediction


lim = 10

if input == '':
    output = random.choice(list(beat.keys()))
    olag1 = ''
    olag2 = ''
    olag3 = ''
    ilag1 = ''
    ilag2 = ''
    # fist level is my output lag(2) and second level is input lag(1)

    keys = []
    for i in itertools.product(''.join(beat), ''.join(beat), ''.join(beat)):
        keys.append(''.join(i))

    matrix = {}
    for key in keys:
        matrix[key] = {'R': {'Pr': 1 / 3, 'N': lim / 3},
                       'P': {'Pr': 1 / 3, 'N': lim / 3},
                       'S': {'Pr': 1 / 3, 'N': lim / 3}}

    model = Markov_model(matrix)

elif len(olag3) > 0:

    key_mat2 = olag3 + ilag2 + olag2
    key_mat1 = olag2 + ilag1 + olag1
    model.update_matrix(key_mat2, ilag1, lim)
    predicted_input = model.predict(key_mat1)
    output = beat[predicted_input]

else:
    output = random.choice(list(beat.keys()))


olag3 = olag2
olag2 = olag1
olag1 = output
ilag2 = ilag1
ilag1 = input