markov_v6_limited_mem10

AuthorPiotrekG
Submission date2018-08-23 11:39:28.424556
Rating4329
Matches played291
Win rate40.89

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

Source code:

from __future__ import division
import random

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

def update_matrix(matrix, olag2, ilag1, lim):
    matrix[olag2][ilag1]['N'] = matrix[olag2][ilag1]['N'] + 1

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

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

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

    return matrix


def select_output(matrix, olag1, olag2):
    best_prob_val = 0
    predicted_input = ''
    for i in matrix[olag1]:
        if matrix[olag2][i]['Pr'] > best_prob_val:
            best_prob_val = matrix[olag2][i]['Pr']
            predicted_input = i

    if best_prob_val <= 0.34:
        predicted_input = random.choice(list(beat.keys()))

    return predicted_input

if input == '':
    output = random.choice(list(beat.keys()))
    olag1 = ''
    olag2 = ''
    ilag1 = ''
    # fist level is my output lag(2) and second level is input lag(1)
    matrix = {
        'R': {'R': {'Pr': 1 / 3, 'N': 1},
              'P': {'Pr': 1 / 3, 'N': 1},
              'S': {'Pr': 1 / 3, 'N': 1}},
        'S': {'R': {'Pr': 1 / 3, 'N': 1},
              'P': {'Pr': 1 / 3, 'N': 1},
              'S': {'Pr': 1 / 3, 'N': 1}},
        'P': {'R': {'Pr': 1 / 3, 'N': 1},
              'P': {'Pr': 1 / 3, 'N': 1},
              'S': {'Pr': 1 / 3, 'N': 1}}
    }

elif olag2 != '':

    matrix = update_matrix(matrix, olag2, ilag1, 10)
    predicted_input = select_output(matrix, olag1, olag2)
    output = beat[predicted_input]
    print(matrix)

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

olag2 = olag1
olag1 = output
ilag1 = input