-
Notifications
You must be signed in to change notification settings - Fork 2
/
etc.py
45 lines (39 loc) · 1.34 KB
/
etc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from .randmax import randmax
import numpy as np
from .base_mab import BaseMAB
class ETC(BaseMAB):
"""Explore-Then-Commit strategy for two arms
Parameters
----------
nbArms :int,
Number of arms of bandit
Horizon : int,
Parameter that scale when the best arm is chosen before commiting
"""
def __init__(self, nbArms,Horizon,c=1/2):
self.nbArms = nbArms
self.T = Horizon
self.clear()
self.Explore = True # are we still exploring?
self.Best = 0
self.c = c
def clear(self):
self.nbDraws = np.zeros(self.nbArms)
self.cumRewards = np.zeros(self.nbArms)
self.t = 0
self.Explore = True
def chooseArmToPlay(self):
if self.Explore : # Exploring
return self.t % self.nbArms
else : # Commit
return self.Best
def receiveReward(self, arm, reward):
self.t += 1
self.cumRewards[arm] = self.cumRewards[arm]+reward
self.nbDraws[arm] = self.nbDraws[arm] +1
if self.Explore :
arms_mean = self.cumRewards / self.nbDraws
sorted_means = np.sort(arms_mean)
if np.abs(arms_mean[0]-arms_mean[1]) > np.sqrt( self.c * np.log(self.T/self.t) / self.t):
self.Best = randmax(arms_mean)
self.Explore = False