-
Notifications
You must be signed in to change notification settings - Fork 0
/
ucb.py
188 lines (156 loc) · 6.88 KB
/
ucb.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# ==========================================================================================
# file: ucb.py
# description: UCB (Upper Confidence Bounds)
# ==========================================================================================
from math import sqrt, log, e
from proto import Architecture
from proto import Controller, Proto, DataOwner, Comp, ProtoParameters
from seed_random import IsolatedBernoulliArm
from utils import StandardBanditsAlgorithm, permute_and_max
########################################################################################################################
# UCB Utils
########################################################################################################################
def ucb_function( turn, s_i, n_i ):
exploitation_term = s_i / n_i
exploration_term = sqrt((2 * log(turn, e)) / n_i)
return exploitation_term + exploration_term
class UCBParameters:
def __init__(self, sigma_seed, reward_seed):
self.sigma_seed = sigma_seed
self.reward_seed = reward_seed
########################################################################################################################
# Standard Algorithm
########################################################################################################################
class UCBBanditsAlgorithm(StandardBanditsAlgorithm):
"""
Implementation of the standard UCB-1 bandits algorithm.
It follows the standard defined in the paper "Algorithms for the multi-armed bandit problem"
accessible at url https://arxiv.org/pdf/1402.6028.pdf
"""
def __init__(self, arms_probs: [float], algo_parameters : UCBParameters):
super().__init__(arms_probs, reward_seed=algo_parameters.reward_seed)
self.sigma_seed = algo_parameters.sigma_seed
self.rewards_by_arm = { arm: 0 for arm in self.arms }
self.nb_pulls_by_arm = { arm: 0 for arm in self.arms }
def play(self, N, debug = False):
self.debug = debug
# start by playing each arm one time
t = 1
for arm in self.arms:
self.pull_and_update_arm(arm, t)
t += 1
# spending remaining budget
while t <= N:
# Print in the standard output s_i and n_i for each arm, useful to ensure the correctness.
if self.debug:
for arm in self.arms:
i = self.arms.index(arm)
print(f"STD Turn {t} R{i} si {self.rewards_by_arm[arm]} ni {self.nb_pulls_by_arm[arm]} vi {self.compute_value_by_arm(arm, t)}")
estimations = [
(
arm,
self.compute_value_by_arm(arm, t)
)
for arm in self.arms
]
arm, arm_estimation = permute_and_max(estimations, self.sigma_seed, t, key=lambda c: c[1])
self.pull_and_update_arm(arm, t)
t += 1
# once budget is spent, computes and returns total cumulative rewards
cumulative_rewards = sum(self.rewards_by_arm.values())
return cumulative_rewards
def pull_and_update_arm(self, arm : IsolatedBernoulliArm, t : int):
reward = arm.pull( t )
self.rewards_by_arm[arm] += reward
self.nb_pulls_by_arm[arm] += 1
def compute_value_by_arm(self, arm, turn):
s_i, n_i = self.rewards_by_arm[arm], self.nb_pulls_by_arm[arm]
return ucb_function( turn, s_i, n_i )
########################################################################################################################
# Specialisation of the algorithm
########################################################################################################################
class UCBDataOwner(DataOwner):
"""
Implementation of the DataOwner with a specialisation for the UCB algorithm.
"""
def compute_value(self, turn: int, iteration: int) -> float:
exploitation_term = self.s_i / self.n_i
exploration_term = sqrt( 2 * log(turn) / self.n_i )
return exploration_term + exploitation_term
def handle_select(self, turn: int, iteration: int, b_i: int):
if b_i == 1:
self.s_i += self.arm.pull(turn)
self.n_i += 1
class UCBComp(Comp):
def select_arm(self, turn: int, computation_round: int, values: [float]) -> int:
# performing an argmax on permuted data
max_index, max_value = 0, values[0]
for i in range(1, len(values)):
if max_value < values[i]:
max_index, max_value = i, values[i]
return max_index
class UCBProto(Proto):
"""
Implementation of the Proto, with a specialization for the standard UCB-1 bandits algorithm.
It follows the standard defined in the paper "Algorithms for the multi-armed bandit problem"
accessible at url https://arxiv.org/pdf/1402.6028.pdf
"""
def __init__(self, arms_probs: [float], proto_parameters: ProtoParameters, algo_parameters : UCBParameters):
super().__init__(arms_probs, proto_parameters)
def provide_do(self, **kwargs) -> DataOwner:
return UCBDataOwner(**kwargs)
def provide_controller(self, **kwargs) -> Controller:
return Controller(**kwargs)
def provide_comp(self, **kwargs) -> Comp:
return UCBComp(**kwargs)
def select_architecture(self, turn: int, computation_round: int):
return Architecture.INFORMED
###################################################
# Algorithms generation facility
###################################################
class UCBFacility:
def __init__(
self,
reward_seed,
sigma_seed,
alpha_seed,
sk,
pk,
cloud_key,
cd_key,
arms_probs
):
self.arms_probs = arms_probs
self.sk = sk
self.alpha_seed = alpha_seed
self.sigma_seed = sigma_seed
self.reward_seed = reward_seed
self.pk = pk
self.cd_key = cd_key
self.cloud_key = cloud_key
def create_standard(self) -> UCBBanditsAlgorithm:
return UCBBanditsAlgorithm(
arms_probs=self.arms_probs,
algo_parameters=self.__create_algo_parameters()
)
def create_generic(self, security: bool) -> UCBProto:
proto_parameters = ProtoParameters.new_from_keys(
cloud_key=self.cloud_key,
cd_key=self.cd_key,
pk=self.pk,
sk=self.sk,
alpha_seed=self.alpha_seed,
reward_seed=self.reward_seed,
sigma_seed=self.sigma_seed,
)
proto_parameters.security = security
return UCBProto(
arms_probs=self.arms_probs,
proto_parameters=proto_parameters,
algo_parameters=self.__create_algo_parameters()
)
def __create_algo_parameters(self) -> UCBParameters:
return UCBParameters(
reward_seed=self.reward_seed,
sigma_seed=self.sigma_seed,
)