Skip to content

Instantly share code, notes, and snippets.

from __future__ import division
from random import random
import numpy as np
import pandas as pd
@alasdairham
alasdairham / gist:fa2e660b7cc97b1003f081df3c25501b
Last active July 21, 2017 02:15
Regret Minimisation_Vers1
from __future__ import division
from random import random
import numpy as np
import pandas as pd
class RPS:
actions = ['ROCK', 'PAPER', 'SCISSORS']
n_actions = 3
utilities = pd.DataFrame([
# ROCK PAPER SCISSORS
[ 0, -1, 1], # ROCK
[ 1, 0, -1], # PAPER
[-1, 1, 0] # SCISSORS
], columns=actions, index=actions)
class Player:
def __init__(self, name):
self.strategy, self.avg_strategy,\
self.strategy_sum, self.regret_sum = np.zeros((4, RPS.n_actions))
self.name = name
def __repr__(self):
return self.name
def update_strategy(self):
class Game:
def __init__(self, max_game=10000):
self.p1 = Player('Alasdair')
self.p2 = Player('Calum')
self.max_game = max_game
def winner(self, a1, a2):
result = RPS.utilities.loc[a1, a2]
if result == 1: return self.p1
elif result == -1: return self.p2
class Player:
# ....
def regret(self, my_action, opp_action):
"""
We here define the regret of not having chosen an action as the difference between the utility of that action and the utility of the action we actually chose, with respect to the fixed choices of the other player. Compute the regret and add it to regret sum.
"""
result = RPS.utilities.loc[my_action, opp_action]
facts = RPS.utilities.loc[:, opp_action].values
regret = facts - result
self.regret_sum += regret
class Player:
# ....
def update_strategy(self):
"""
Set the preference (strategy) of choosing an action to be proportional to positive regrets. e.g, a strategy that prefers PAPER can be [0.2, 0.6, 0.2]
"""
self.strategy = np.copy(self.regret_sum)
self.strategy[self.strategy < 0] = 0 # reset negative regrets to zero
summation = sum(self.strategy)
if __name__ == '__main__':
game = Game()
print '==== Use simple regret-matching strategy === '
game.play()
print '==== Use averaged regret-matching strategy === '
game.conclude()
game.play(avg_regret_matching=True)
https://gist.github.com/namoshizun/7a3b820b013f8e367e84c70b45af7c34
/*
*
* Eg. SEND_INTERVAL = 3000; CAPTURE_INTERVAL = 50
*
* - Capture mousemove & mousedown event after every 50 ms
* - For each captured mouse event, save it into a local cache object
* - After every 3 seconds, sends the cached mouse event history to server,
* then clear cache. Exceptions are:
* 1. A click event triggers data submission to server (as described above) immediately
* 2. Pause sending data to server when there is no new movement,