Skip to content

Instantly share code, notes, and snippets.

@yuitest
Created May 20, 2014 05:12
Show Gist options
  • Save yuitest/d21f1bea1ddda56fc0d3 to your computer and use it in GitHub Desktop.
Save yuitest/d21f1bea1ddda56fc0d3 to your computer and use it in GitHub Desktop.
いぷしろんぐりーでぃー
# coding: utf8
from __future__ import division, print_function, unicode_literals
import random
class BiasedCoin(object):
def __init__(self, bias=.5):
self.bias = bias
def __repr__(self):
return '<Coin {}>'.format(self.bias)
def challenge(self):
return random.random() >= self.bias
def create_random_coins(amount):
x = [BiasedCoin(random.random()) for _ in xrange(amount)]
random.shuffle(x)
return x
class Game(object):
def __init__(self, sessions=100, coins=5):
self.max_sessions = sessions
self.coins = create_random_coins(coins)
self.session_count = 0
self.point = 0
def challenge(self, i):
if self.is_end():
raise Exception
result = self.coins[i].challenge()
point = 0 + result
self.point += point
self.session_count += 1
return point
def is_end(self):
if self.session_count >= self.max_sessions:
return True
return False
class EpsilonGreedy(object):
def __init__(self, e, n_arms):
self.epsilon = e
self.counts = [0 for _ in xrange(n_arms)]
self.values = [.0 for _ in xrange(n_arms)]
def select_arm(self):
values = self.values
if random.random() > self.epsilon:
return values.index(max(values))
return values.index(random.choice(values))
def update(self, chosen_arm, reward):
values = self.values
counts = self.counts
counts[chosen_arm] += 1
n = counts[chosen_arm]
if n == 0:
values[chosen_arm] = reward
else:
new_value = ((n - 1) / n) * values[chosen_arm] + (1 / n) * reward
values[chosen_arm] = new_value
if __name__ == '__main__':
# こうやってあそぶ
sessions = 1000
coins = 25
game = Game(sessions, coins)
eg = EpsilonGreedy(0.1, coins)
for _ in xrange(sessions):
arm_num = eg.select_arm()
p = game.challenge(arm_num)
eg.update(arm_num, p)
print(eg.counts)
print(game.point)
@yuitest
Copy link
Author

yuitest commented May 20, 2014

この URL の Ruby のコードを参考にして書いた。
http://lxyuma.hatenablog.com/entry/2013/09/18/002613

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment