-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuccessive_elimination_agent.py
53 lines (47 loc) · 1.98 KB
/
successive_elimination_agent.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
46
47
48
49
50
51
52
53
from agent import Agent
from util import *
import numpy as np
import math
class SuccessiveEliminationAgent(Agent):
best_arm = 0
last_arm = 0
#
def __init__(self,time_horizon,number_of_arms):
self.time_horizon = time_horizon
self.number_of_arms = number_of_arms
self.reset()
self.name = "successive-elimination"
#
def reset(self):
self.mu_estimators = [0]*self.number_of_arms
self.best_arm = 0
self.last_arm = 0
self.arms_visited = [0]*self.number_of_arms
self.ubc = [0]*self.number_of_arms
self.lbc = [0]*self.number_of_arms
self.active_arms = np.arange(self.number_of_arms)
self.active_arms_snapshot = np.arange(self.number_of_arms)
print('successive-elimination agent started to decide.')
#
def decide(self,time,time_horizon,number_of_arms):
arm = 0
if self.last_arm == len(self.active_arms_snapshot):
self.last_arm = 0
self.active_arms_snapshot = np.copy(self.active_arms)
arm = self.active_arms_snapshot[self.last_arm]
self.last_arm += 1
self.arms_visited[arm] += 1
return arm
#
def observe(self,time,arm,reward):
self.mu_estimators[arm] = incrementalAvg(self.arms_visited[arm],self.mu_estimators[arm],reward)
self.ubc[arm] = self.mu_estimators[arm] + math.sqrt(2*math.log(self.time_horizon)/self.arms_visited[arm])
self.lbc[arm] = self.mu_estimators[arm] - math.sqrt(2*math.log(self.time_horizon)/self.arms_visited[arm])
updated_actives = np.copy(self.active_arms)
if len(self.active_arms) > 0:
for a in self.active_arms:
if np.max(self.lbc) > self.ubc[a] and self.arms_visited[a] > 0:
#print(np.max(self.lbc))
#print(self.lbc[a],self.ubc[a])
updated_actives = updated_actives[updated_actives != a]
self.active_arms = updated_actives