markov_v3

AuthorPiotrekG
Submission date2018-08-23 09:51:52.168361
Rating5538
Matches played277
Win rate54.15

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

Source code:

import random

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

# 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}}
}


def update_matrix(matrix, olag2, ilag1):
    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

    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 = ''

elif olag2 != '':

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

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

olag2 = olag1
olag1 = output
ilag1 = input

print olag1, olag2, ilag1