-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
169 lines (153 loc) · 5.02 KB
/
run.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
# Standard libraries
from pathlib import Path
from time import time
# PyPI packages
from matplotlib import pyplot as plt
from matplotlib.ticker import MaxNLocator
import numpy as np
# Local package
from bandit.examples import reward_function
from bandit.experiment import (
create_folders,
generate_arms,
generate_domain,
run
)
# Global settings
np.random.seed(1)
plt.rcParams.update({'figure.figsize': (8, 6)})
plt.rcParams.update({'font.size': 16})
# Global variables
DATA_PATH = Path('./figures')
# Static environment
# Simulation parameters
arm_count = 3 # arm space, 3 actions
dimension = 1 # 1-dimensional context values
bounds = (0, 1) # context space bounded between 0 and 1
reward_variance = 0.1 # variance of reward random variables
window_length = 0 # number of observations to train GP model
horizon = 1000 # number of time steps to run simulation
replications = 30 # number of repeated experiment runs
change_points = [] # locations of change-point events
# Run simulation
create_folders(DATA_PATH)
domain = generate_domain(bounds)
arms_initial = generate_arms(
true_reward_function=reward_function,
arm_count=arm_count,
dimension=dimension,
bounds=bounds,
reward_variance=reward_variance,
window_length=window_length
)
start_time = time()
arms, cumulative_regret, estimated_best_actions, true_best_actions = run(
arms=arms_initial,
domain=domain,
dimension=dimension,
bounds=bounds,
horizon=horizon,
replications=replications,
change_points=change_points
)
end_time = time()
print(f"Simulation took {round(end_time-start_time, 3)} s.")
# Regret
plt.figure()
plt.plot(cumulative_regret, label='Cumulative regret, Thompson sampling')
for index, change_point in enumerate(change_points):
if index == 0:
plt.plot(change_point, cumulative_regret[change_point], 'o', c='k', label='Change-point in reward function')
else:
plt.plot(change_point, cumulative_regret[change_point], 'o', c='k')
plt.vlines(
x=change_points,
ymin=[0]*len(change_points),
ymax=[cumulative_regret[change_point] for change_point in change_points],
colors='k',
linestyles='dashed'
)
plt.xlabel('Time step')
plt.ylabel('Cumulative regret')
plt.legend()
plt.tight_layout()
plt.savefig(DATA_PATH / 'regret' / 'static.pdf')
# Optimal arm identification
plt.figure()
plt.plot(domain, estimated_best_actions, label='Estimated best action')
plt.plot(domain, true_best_actions, label='True best action')
plt.xlabel('Context')
plt.ylabel('Action')
plt.gca().yaxis.set_major_locator(MaxNLocator(integer=True))
plt.legend()
plt.tight_layout()
plt.savefig(DATA_PATH / 'actions' / 'static.pdf')
# Reward estimation
for arm in arms:
arm.reward.plot(file_name=DATA_PATH / 'reward' / f'static_{arm.index}.pdf')
# Dynamic environment (multiple changepoints)
# Simulation parameters
arm_count = 3 # arm space, 3 actions
dimension = 1 # 1-dimensional context values
bounds = (0, 1) # context space bounded between 0 and 1
reward_variance = 0.1 # variance of reward random variables
window_length = 200 # number of observations to train GP model
horizon = 3000 # number of time steps to run simulation
replications = 30 # number of repeated experiment runs
change_points = [999, 1999] # locations of change-point events
# Run simulation
create_folders(DATA_PATH)
domain = generate_domain(bounds)
arms_initial = generate_arms(
true_reward_function=reward_function,
arm_count=arm_count,
dimension=dimension,
bounds=bounds,
reward_variance=reward_variance,
window_length=window_length
)
start_time = time()
arms, cumulative_regret, estimated_best_actions, true_best_actions = run(
arms=arms_initial,
domain=domain,
dimension=dimension,
bounds=bounds,
horizon=horizon,
replications=replications,
change_points=change_points
)
end_time = time()
print(f"Simulation took {round(end_time-start_time, 3)} s.")
# Regret
plt.figure()
plt.plot(cumulative_regret, label='Cumulative regret, Thompson sampling')
for index, change_point in enumerate(change_points):
if index == 0:
plt.plot(change_point, cumulative_regret[change_point], 'o', c='k', label='Change-point in reward function')
else:
plt.plot(change_point, cumulative_regret[change_point], 'o', c='k')
plt.vlines(
x=change_points,
ymin=[0]*len(change_points),
ymax=[cumulative_regret[change_point] for change_point in change_points],
colors='k',
linestyles='dashed'
)
plt.xlabel('Time step')
plt.ylabel('Cumulative regret')
plt.legend()
plt.tight_layout()
plt.savefig(DATA_PATH / 'regret' / 'dynamic.pdf')
# Optimal arm identification
plt.figure()
plt.plot(domain, estimated_best_actions, label='Estimated best action')
plt.plot(domain, true_best_actions, label='True best action')
plt.xlabel('Context')
plt.ylabel('Action')
plt.gca().yaxis.set_major_locator(MaxNLocator(integer=True))
plt.legend()
plt.tight_layout()
plt.savefig(DATA_PATH / 'actions' / 'dynamic.pdf')
# Reward estimation
for arm in arms:
arm.reward.plot(file_name=DATA_PATH / 'reward' / f'dynamic_{arm.index}.pdf')