This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## Data Structure | |
| Example of HTTP payload sent to server | |
| ```json | |
| { | |
| "ip": "user ip", | |
| "user_agent": "OS, Browser, etc..", | |
| "data":[ | |
| { | |
| "x": 50, | |
| "y": 100, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * | |
| * 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, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| https://gist.github.com/namoshizun/7a3b820b013f8e367e84c70b45af7c34 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from __future__ import division | |
| from random import random | |
| import numpy as np | |
| import pandas as pd |
NewerOlder