Author | Marat |
Submission date | 2017-10-20 14:22:00.681910 |
Rating | 5169 |
Matches played | 335 |
Win rate | 51.94 |
Use rpsrunner.py to play unranked matches on your computer.
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
for c, w in choices:
if upto + w >= r:
return c
upto += w
return 'R'
def get_action(strategy):
return weighted_choice(zip(all_actions, strategy))
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:
my_action = get_action(get_strategy(regret_sum))
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