bb markov chain defense

Authorluis
Submission date2019-04-15 12:43:21.635723
Rating6448
Matches played225
Win rate64.0

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

Source code:

from __future__ import print_function
import random
import math

TEMPERATURE = 0.5
ADAPT = 1.1
RPS = {"R": 0, "P": 1, "S": 2, 0: "R", 1: "P", 2: "S"}

if input == "":
    # initialize variables
    lastAction = None
    temperature = TEMPERATURE
    # uniform prior markov chain
    markovchain = [[[1.0, 1.0] for i in range(3)] for j in range(3)]
else:
    newAction = RPS[input]
    # update temperature
    delta_score = (newAction - choice + 1) % 3 - 1
    # clamp temperature so we don't get stuck
    temperature = min(max(temperature * math.pow(ADAPT, delta_score), 0.1), 500)
    # print(temperature)

    if (lastAction != None):
        # update markov chain
        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.randint(0, 2)
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]