-
Notifications
You must be signed in to change notification settings - Fork 0
/
chapter02.py
149 lines (112 loc) · 4.11 KB
/
chapter02.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
import numpy as np
class Bandit:
def __init__(self, μ, σ, seed=0):
self.k = len(μ)
self.μ = μ
self.σ = [σ] * self.k if np.isscalar(σ) else σ
self.rng = np.random.default_rng(seed)
def __str__(self):
return str(self.__class__) + ": " + str(self.__dict__)
def pulled(self, arm):
return self.rng.normal(self.μ[arm], self.σ[arm])
class Greedy:
def __init__(self, k, ε=0, reward=None, count=None, seed=0):
self.k = k
self.ε = ε
self.reward = reward or np.zeros(k)
self.count = count or np.zeros(k)
self.rng = np.random.default_rng(seed)
def __str__(self):
return str(self.__class__) + ": " + str(self.__dict__)
def pull(self):
if self.rng.random() < self.ε:
return self.rng.integers(0, self.k)
else:
return np.argmax(self.reward / self.count) # nan comes first
def update(self, reward, arm):
self.reward[arm] += reward
self.count[arm] += 1
class Optimistic:
def __init__(self, k, ε=0., α=0.1, q0=0., seed=0):
self.k = k
self.ε = ε
self.α = α
self.q = np.full(k, q0, dtype=np.float_)
self.rng = np.random.default_rng(seed)
def __str__(self):
return str(self.__class__) + ": " + str(self.__dict__)
def pull(self):
if self.rng.random() < self.ε:
return self.rng.integers(0, self.k)
else:
return np.argmax(self.q)
def update(self, reward, arm):
self.q[arm] += self.α * (reward - self.q[arm])
class NonStationaryGreedy:
def __init__(self, k, ε=0.1, α=0.5, denom=0., initial=0., seed=0):
self.k = k
self.ε = ε
self.α = α
self.denom = np.full(k, denom, dtype=np.float_)
self.q = np.full(k, initial, dtype=np.float_)
self.rng = np.random.default_rng(seed)
def __str__(self):
return str(self.__class__) + ": " + str(self.__dict__)
def pull(self):
if self.rng.random() < self.ε:
return self.rng.integers(0, self.k)
else:
return np.argmax(self.q)
def update(self, reward, arm):
self.denom[arm] += self.α * (1 - self.denom[arm])
self.q[arm] += self.α / self.denom[arm] * (reward - self.q[arm])
class UCB:
def __init__(self, k, c=1, t=1, q=None, n=None):
self.k = k
self.c = c
self.t = t
self.q = q or np.zeros(k)
self.n = n or np.zeros(k)
def __str__(self):
return str(self.__class__) + ": " + str(self.__dict__)
def pull(self):
return np.argmax(self.q + self.c*np.sqrt(np.log(self.t)/self.n)) # nan comes first
def update(self, reward, arm):
self.t += 1
self.n[arm] += 1
self.q[arm] += (reward - self.q[arm]) / self.n[arm]
class Gradient:
def __init__(self, k, α=0.1, h=None, t=0, baseline=None, seed=0):
self.k = k
self.α = α
self.h = h or np.zeros(k)
self.t = t
self.baseline = baseline
self.objective = False if np.isscalar(baseline) else True
self.rng = np.random.default_rng(seed)
def __str__(self):
return str(self.__class__) + ": " + str(self.__dict__)
def pull(self):
π = np.exp(self.h)
π /= np.sum(π)
return self.rng.choice(self.k, p=π)
def update(self, reward, arm):
self.t += 1
π = np.exp(self.h)
π /= -np.sum(π)
π[arm] += 1
if self.objective and self.t == 1:
self.baseline = reward
self.h += self.α * (reward - self.baseline) * π
if self.objective and self.t > 1:
self.baseline += (reward - self.baseline) / self.t
def play(bandit, player, t=1):
reward = np.zeros(t)
hit = np.zeros(t)
champion = np.argmax(bandit.μ)
for i in range(t):
arm = player.pull()
hit[i] = arm == champion
reward[i] = bandit.pulled(arm)
player.update(reward[i], arm)
return reward, hit