rm2

AuthorMarat
Submission date2017-10-21 00:01:10.628840
Rating4867
Matches played329
Win rate48.63

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

Source code:

import random

all_actions = ['R', 'P', 'S']

def get_strategy(regret_sum):
    strategy = [x if x > 0 else 0 for x in regret_sum]
    normalizing_sum = sum(strategy) + 0.0
    strategy = [x/normalizing_sum if normalizing_sum > 0 else 1/3.0 for x in strategy]
    return strategy

def weighted_choice(choices):
   total = sum(w for c, w in choices)
   r = random.uniform(0, total)
   upto = 0.0
   for c, w in choices:
      print(c, w, upto)
      if upto + w >= r:
         return c
      upto += w
   return 'R'

def get_action(strategy):
    zipped = list(zip(all_actions, strategy))
    return weighted_choice(zipped)

utility = {
        ('R','R') : 0,
        ('P','P') : 0,
        ('S','S'): 0,
        ('P','R'): 1,
        ('R','P'): -1,
        ('S','P'):1,
        ('P','S'):-1,
        ('R','S'):1,
        ('S','R'):-1
    }

regret_sum = [0, 0, 0]
if input == '':
    output = 'R'
    regret_sum = [0, 0, 0]
else:
    strategy = get_strategy(regret_sum)
    my_action = get_action(strategy)
    regret = [utility[(x, input)] - utility[(my_action, input)] for x in all_actions]
    regret_sum = [x + y for x, y in zip(regret_sum, regret)]
    output = my_action