bb markov chain 2

Authorluis
Submission date2019-04-15 10:48:47.006972
Rating6468
Matches played254
Win rate66.54

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

Source code:

import random
import math

TEMPERATURE = 0.3
RPS = {"R": 0, "P": 1, "S": 2, 0: "R", 1: "P", 2: "S"}

if input == "":
    # initialize variables
    lastAction = None
    # uniform prior markovchain
    markovchain = [[[1.0, 1.0] for i in range(3)] for j in range(3)]
else:
    newAction = RPS[input]
    if (lastAction != None):
        for i in range(3):
            if (newAction == i):
                markovchain[lastAction][i][0] += 1.0 # update alpha
            else:
                markovchain[lastAction][i][1] += 1.0 # update beta
    lastAction = newAction

def rate(action):
    dist = markovchain[lastAction][action]
    return dist[0] / float(dist[0] + dist[1])

choice = 0
if (lastAction == None):
    choice = random.choice(range(3))
else:
    # sample boltzmann distribution, pick good action on average
    rates = [rate(i) for i in range(3)]
    probs = [math.exp((rates[(i - 1) % 3] - rates[(i + 1) % 3]) / TEMPERATURE) for i in range(3)]
    pick = random.random() * float(sum(probs))
    while (pick >= 0):
        pick -= probs[choice]
        if (pick <= 0):
            break
        if (choice == 2):
            break
        choice += 1

output = RPS[choice]