-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbandit-moving.py
More file actions
64 lines (54 loc) · 2.13 KB
/
bandit-moving.py
File metadata and controls
64 lines (54 loc) · 2.13 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
import numpy as np
import matplotlib.pyplot as plt
# Experiment Setup
np.random.seed(19680801)
num_actions = 10
num_trials = 2000
num_iter = 10000
epsilon = 1e-1
num_configs = 2
num_samples = num_configs * num_trials
q_star_a = np.repeat(np.random.normal(size=[num_actions, num_trials]), num_configs, axis=1)
optimal_action = np.argmax(q_star_a, axis=0)
optimal_actions = np.zeros([num_iter, num_samples], dtype=np.int32)
R_t_a = np.zeros([num_iter, num_actions, num_samples])
Q_a = np.zeros([num_actions, num_samples])
K_a = np.zeros([num_actions, num_samples], dtype=np.int32)
# The first action is always assumed to be the action at index 0
# Absent prior knowledge, this is equivalent to a random choice
for t in range(1, num_iter):
# Select Action
is_greedy = np.random.random(num_samples) < (1 - epsilon)
greedy_actions = np.argmax(Q_a, axis=0)
random_actions = np.random.randint(num_actions, size=num_samples)
actions = np.where(is_greedy, greedy_actions, random_actions)
action_idx = actions, np.arange(num_samples)
optimal_actions[t, actions == optimal_action] += 1
# Sample Environment
noise_term = np.random.normal(scale=1., size=num_samples)
R_t_a[t][action_idx] = q_star_a[action_idx] + noise_term
# Add random walk
shift = np.random.normal(scale=.1, size=[num_samples])
q_star_a += shift
# Update Estimate
K_a[action_idx] += 1
# Sample Average
step_size = 1 / K_a[action_idx]
# Exponential (set every other element)
step_size[::2] = 0.1
target = R_t_a[t][action_idx]
old_estimate = Q_a[action_idx]
Q_a[action_idx] = old_estimate + step_size * (target - old_estimate)
R_t = np.mean(np.sum(R_t_a, axis=1).reshape([num_iter, num_trials, -1]), axis=1)
plt.subplot(211)
plt.plot(R_t[:, 0], label='exponential average')
plt.plot(R_t[:, 1], label='sample average')
plt.xlabel('Steps')
plt.ylabel('Average reward')
plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3,
ncol=3, mode="expand", borderaxespad=0.)
plt.subplot(212)
plt.plot(np.mean(optimal_actions.reshape([num_iter, num_trials, -1]), axis=1))
plt.xlabel('Steps')
plt.ylabel('Optimal action')
plt.show()