-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbandit.py
More file actions
169 lines (140 loc) · 4.89 KB
/
bandit.py
File metadata and controls
169 lines (140 loc) · 4.89 KB
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
import numpy as np
import matplotlib.pyplot as plt
class BernoulliBandit:
def __init__(self, k):
self.k = k
self.probs = np.random.uniform(size=k)
self.best_idx = np.argmax(self.probs)
self.best_prob = self.probs[ self.best_idx ]
def step(self, i):
# 拉动第 i 个拉杆, 随机数比较, 中奖得奖励 1, 否则得 0
if np.random.rand() <= self.probs[ i ]:
return 1
else:
return 0
class Solver:
def __init__(self, bandit: BernoulliBandit):
self.bandit = bandit
self.counts = np.zeros_like(bandit.probs)
self.regret = 0
self.regret_history = [ ]
self.action_history = [ ]
def update_regret(self, i):
self.regret += self.bandit.best_prob - self.bandit.probs[ i ]
self.regret_history.append(self.regret)
def step(self):
raise NotImplementedError
def run(self, epoches):
for _ in range(epoches):
i = self.step()
self.counts[ i ] += 1
self.action_history.append(i)
self.update_regret(i)
class MyAgent(Solver):
def __init__(self, bandit):
super().__init__(bandit)
self.choose_me = np.zeros_like(self.bandit.probs)
def step(self):
if np.random.rand() <= 1 / (1 + len(self.action_history)):
i = np.random.choice(self.bandit.k)
else:
if np.random.random() <= 0.5:
i = np.random.choice(self.bandit.k)
else:
i = self.choose_me.argmax()
r = self.bandit.step(i)
if r == 1:
self.choose_me[ i ] += 0.5
else:
self.choose_me[ i ] -= 2
return i
class EpsilonGreedy(Solver):
def __init__(self, bandit, eps=1e-2, init_prob=1.):
super().__init__(bandit)
self.eps = eps
self.estimate = np.ones_like(self.bandit.probs) * init_prob
def step(self):
if np.random.rand() <= self.eps:
i = np.random.choice(self.bandit.k)
else:
i = self.estimate.argmax()
r = self.bandit.step(i)
self.estimate[ i ] += 1 / (self.counts[ i ] + 1) * (r - self.estimate[ i ])
return i
class DecayingEpsilonGreedy(Solver):
def __init__(self, bandit, init_prob=1.):
super().__init__(bandit)
self.estimate = np.ones_like(self.bandit.probs) * init_prob
def step(self):
if np.random.rand() <= 1 / (1 + len(self.action_history)):
i = np.random.choice(self.bandit.k)
else:
i = self.estimate.argmax()
r = self.bandit.step(i)
self.estimate[ i ] += 1 / (self.counts[ i ] + 1) * (r - self.estimate[ i ])
return i
class UCB1(Solver):
def __init__(self, bandit, init_prob=1., coef=1):
super().__init__(bandit)
self.coef = coef
self.estimate = np.ones_like(self.bandit.probs) * init_prob
def step(self):
ucb = self.estimate + self.coef * (np.log(self.counts.sum() + 1) / (2 * (self.counts + 1))) ** 0.5
i = np.argmax(ucb)
r = self.bandit.step(i)
self.estimate[ i ] += 1. / (self.counts[ i ] + 1) * (r - self.estimate[ i ])
return i
class ThompsonSampling(Solver):
def __init__(self, bandit):
super().__init__(bandit)
self.a = np.ones_like(self.bandit.probs)
self.b = np.ones_like(self.bandit.probs)
def step(self):
samples = np.random.beta(self.a, self.b)
i = samples.argmax()
r = self.bandit.step(i)
if r:
self.a[ i ] += 1
else:
self.b[ i ] += 1
return i
if __name__ == '__main__':
k = 10
epoches = 1000
bandit = BernoulliBandit(k)
my_agent = MyAgent(bandit)
eps_greedy = EpsilonGreedy(bandit)
decay_eps_greedy = DecayingEpsilonGreedy(bandit)
ucb1 = UCB1(bandit)
thomson_sampler = ThompsonSampling(bandit)
my_agent.run(epoches)
eps_greedy.run(epoches)
decay_eps_greedy.run(epoches)
ucb1.run(epoches)
thomson_sampler.run(epoches)
print(
f"累计懊悔:\nmy_agent: {my_agent.regret}"
f"\t|| eps_greedy: {eps_greedy.regret}"
f"\ndecay_eps_greedy: {decay_eps_greedy.regret}"
f"\t|| ucb1: {ucb1.regret}"
f"\nthomson_sampler: {thomson_sampler.regret}"
)
plt.figure()
plt.subplot(3, 2, 1)
plt.plot(my_agent.regret_history)
plt.title('MyAgent Regret History')
plt.subplot(3, 2, 2)
plt.plot(eps_greedy.regret_history)
plt.title('EpsilonGreedy Regret History')
plt.subplot(3, 2, 3)
plt.plot(decay_eps_greedy.regret_history)
plt.title('Decaying EpsilonGreedy Regret History')
plt.subplot(3, 2, 4)
plt.plot(ucb1.regret_history)
plt.title('UCB1 Regret History')
plt.subplot(3, 2, 5)
plt.plot(thomson_sampler.regret_history)
plt.title('ThomsonSampling Regret History')
plt.suptitle('Regret History')
plt.tight_layout()
plt.show()