bb markov chain 2

 Author luis Submission date 2019-04-15 10:48:47.006972 Rating 6552 Matches played 233 Win rate 66.95

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

def rate(action):
dist = markovchain[lastAction][action]
return dist / float(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() * float(sum(probs))
while (pick >= 0):
pick -= probs[choice]
if (pick <= 0):
break
if (choice == 2):
break
choice += 1

output = RPS[choice]