-
Notifications
You must be signed in to change notification settings - Fork 1
/
ea_optimize.py
326 lines (280 loc) · 9.47 KB
/
ea_optimize.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
from concurrent.futures import ThreadPoolExecutor
from datetime import datetime
# import dummypydoocs as pydoocs
import gym
import numpy as np
from gym.wrappers import (
FilterObservation,
FlattenObservation,
FrameStack,
RecordVideo,
RescaleAction,
TimeLimit,
)
from stable_baselines3 import PPO, TD3
from backend import EADOOCSBackend
from environment import EATransverseTuning
from utils import (
ARESEAeLog,
FilterAction,
NotVecNormalize,
PolishedDonkeyCompatibility,
RecordEpisode,
TQDMWrapper,
load_config,
)
def optimize(
target_mu_x,
target_sigma_x,
target_mu_y,
target_sigma_y,
target_mu_x_threshold=3.3198e-6,
target_mu_y_threshold=3.3198e-6,
target_sigma_x_threshold=3.3198e-6,
target_sigma_y_threshold=3.3198e-6,
max_steps=50,
model_name="chocolate-totem-247",
logbook=False,
data_log_dir=None,
progress_bar=False,
callback=None,
):
"""
Optimise beam in ARES EA using a reinforcement learning agent.
"""
config = load_config(f"models/{model_name}/config")
# Load the model
model = PPO.load(f"models/{model_name}/model")
callback = setup_callback(callback)
# Create the environment
env = EADOOCSBackend(
action_mode=config["action_mode"],
magnet_init_mode=config["magnet_init_mode"],
magnet_init_values=config["magnet_init_values"],
max_quad_delta=config["max_quad_delta"],
max_steerer_delta=config["max_steerer_delta"],
reward_mode=config["reward_mode"],
target_beam_mode=config["target_beam_mode"],
target_beam_values=np.array(
[target_mu_x, target_sigma_x, target_mu_y, target_sigma_y]
),
target_mu_x_threshold=target_mu_x_threshold,
target_mu_y_threshold=target_mu_y_threshold,
target_sigma_x_threshold=target_sigma_x_threshold,
target_sigma_y_threshold=target_sigma_y_threshold,
threshold_hold=1,
w_done=config["w_done"],
w_mu_x=config["w_mu_x"],
w_mu_x_in_threshold=config["w_mu_x_in_threshold"],
w_mu_y=config["w_mu_y"],
w_mu_y_in_threshold=config["w_mu_y_in_threshold"],
w_on_screen=config["w_on_screen"],
w_sigma_x=config["w_sigma_x"],
w_sigma_x_in_threshold=config["w_sigma_x_in_threshold"],
w_sigma_y=config["w_sigma_y"],
w_sigma_y_in_threshold=config["w_sigma_y_in_threshold"],
w_time=config["w_time"],
)
if max_steps is not None:
env = TimeLimit(env, max_steps)
if progress_bar:
env = TQDMWrapper(env)
if callback is not None:
env = OptimizeFunctionCallback(env, callback)
if data_log_dir is not None:
env = RecordEpisode(env, save_dir=data_log_dir)
if logbook:
env = ARESEAeLog(env, model_name=model_name)
if config["filter_observation"] is not None:
env = FilterObservation(env, config["filter_observation"])
if config["filter_action"] is not None:
env = FilterAction(env, config["filter_action"], replace=0)
env = FlattenObservation(env)
if config["frame_stack"] is not None:
env = FrameStack(env, config["frame_stack"])
if config["rescale_action"] is not None:
env = RescaleAction(
env, config["rescale_action"][0], config["rescale_action"][1]
)
env = RecordVideo(env, video_folder=f"recordings_real/{datetime.now():%Y%m%d%H%M}")
env = NotVecNormalize(env, f"models/{model_name}/vec_normalize.pkl")
callback.env = env
# Actual optimisation
observation = env.reset()
done = False
while not done:
action, _ = model.predict(observation, deterministic=True)
observation, reward, done, info = env.step(action)
env.close()
def optimize_donkey(
target_mu_x,
target_sigma_x,
target_mu_y,
target_sigma_y,
target_mu_x_threshold=3.3198e-6,
target_mu_y_threshold=3.3198e-6,
target_sigma_x_threshold=3.3198e-6,
target_sigma_y_threshold=3.3198e-6,
max_steps=50,
model_name="polished-donkey-996",
logbook=False,
data_log_dir=None,
progress_bar=False,
callback=None,
):
"""
Function used for optimisation during operation.
Note: Current version only works for polished-donkey-996.
"""
# config = read_from_yaml(f"models/{model}/config")
assert (
model_name == "polished-donkey-996"
), "Current version only works for polished-donkey-996."
# Load the model
model = TD3.load(f"models/{model_name}/model")
callback = setup_callback(callback)
# Create the environment
env = EATransverseTuning(
backend=EADOOCSBackend(),
action_mode="delta",
magnet_init_mode="constant",
magnet_init_values=np.array([10, -10, 0, 10, 0]),
max_quad_delta=30 * 0.1,
max_steerer_delta=6e-3 * 0.1,
reward_mode="differential",
target_beam_mode="constant",
target_beam_values=np.array(
[target_mu_x, target_sigma_x, target_mu_y, target_sigma_y]
),
target_mu_x_threshold=target_mu_x_threshold,
target_mu_y_threshold=target_mu_y_threshold,
target_sigma_x_threshold=target_sigma_x_threshold,
target_sigma_y_threshold=target_sigma_y_threshold,
w_beam=1.0,
w_mu_x=1.0,
w_mu_y=1.0,
w_sigma_x=1.0,
w_sigma_y=1.0,
)
if max_steps is not None:
env = TimeLimit(env, max_episode_steps=max_steps)
if progress_bar:
env = TQDMWrapper(env)
if callback is not None:
env = OptimizeFunctionCallback(env, callback)
if data_log_dir is not None:
env = RecordEpisode(env, save_dir=data_log_dir)
if logbook:
env = ARESEAeLog(env, model_name=model_name)
env = RecordVideo(env, f"recordings_real/{datetime.now():%Y%m%d%H%M}")
env = FlattenObservation(env)
env = PolishedDonkeyCompatibility(env)
env = NotVecNormalize(env, f"models/{model_name}/vec_normalize.pkl")
env = RescaleAction(env, -1, 1)
callback.env = env
# Actual optimisation
observation = env.reset()
done = False
while not done:
action, _ = model.predict(observation, deterministic=True)
observation, reward, done, info = env.step(action)
env.close()
def optimize_async(*args, **kwargs):
"""Run `optimize without blocking."""
executor = ThreadPoolExecutor(max_workers=1)
# executor.submit(optimize, *args, **kwargs)
kwargs["model_name"] = "polished-donkey-996"
executor.submit(optimize_donkey, *args, **kwargs)
def setup_callback(callback):
"""
Prepare the callback for the actual optimisation run and return a callback that
works exactly as expected.
"""
if callback is None:
callback = BaseCallback()
elif isinstance(callback, list):
callback = CallbackList(callback)
return callback
class BaseCallback:
"""
Base for callbacks to pass into `optimize` function and get information at different
points of the optimisation.
Provides access to the environment via `self.env`.
"""
def __init__(self):
self.env = None
def environment_reset(self, obs):
"""Called after the environment's `reset` method has been called."""
pass
def environment_step(self, obs, reward, done, info):
"""
Called after every call to the environment's `step` function.
Return `True` tostop optimisation.
"""
return False
def environment_close(self):
"""Called after the optimization was finished."""
pass
class CallbackList(BaseCallback):
"""Combines multiple callbacks into one."""
def __init__(self, callbacks):
super().__init__()
self.callbacks = callbacks
@property
def env(self):
return self._env
@env.setter
def env(self, value):
self._env = value
for callback in self.callbacks:
callback.env = self._env
def environment_reset(self, obs):
for callback in self.callbacks:
callback.environment_reset(obs)
def environment_step(self, obs, reward, done, info):
return any(
[
callback.environment_step(obs, reward, done, info)
for callback in self.callbacks
]
)
def environment_close(self):
for callback in self.callbacks:
callback.environment_close()
class TestCallback(BaseCallback):
"""
Very simple callback for testing. Prints method name and arguments whenever callback
is called.
"""
def environment_reset(self, obs):
print(
f"""environment_reset
-> {obs = }"""
)
def environment_step(self, obs, reward, done, info):
print(
f"""environment_step
-> {obs = }
-> {reward = }
-> {done = }
-> {info = }"""
)
return False
def environment_close(self):
print("""environment_close""")
class OptimizeFunctionCallback(gym.Wrapper):
"""Wrapper to send screen image, beam parameters and optimisation end to GUI."""
def __init__(self, env, callback):
super().__init__(env)
self.callback = callback
def reset(self):
obs = super().reset()
self.callback.environment_reset(obs)
return obs
def step(self, action):
obs, reward, done, info = super().step(action)
done = done or self.callback.environment_step(obs, reward, done, info)
return obs, reward, done, info
def close(self):
super().close()
self.callback.environment_close()