boltzmann bayes markov chain

 Author luis Submission date 2019-04-15 10:30:02.112664 Rating 5172 Matches played 226 Win rate 53.1

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, 1] 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] += 1  # update alpha
else:
markovchain[lastAction][i] += 1  # update beta
lastAction = newAction

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

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() * sum(probs)
while (pick >= 0):
pick -= probs[choice]
if (pick <= 0):
break
if (choice == 2):
break
choice += 1

output = RPS[choice]