-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_controller.py
233 lines (215 loc) · 5.93 KB
/
train_controller.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
"""
Main file for configuring and training controllers.
"""
import math
import os
import multiprocessing
import numpy as np
import ray
import torch
from ray import tune
from ray.rllib.agents.ppo import PPOTrainer
from envs import FlexibleArmEnv, InvertedPendulumEnv, TimeDelayInvertedPendulumEnv, DiskMarginExampleEnv, FlexibleArmDiskMarginEnv
import lti_controllers
from models import (
RINN,
RNN,
DissipativeRINN,
DissipativeSimplestRINN,
FullyConnectedNetwork,
ImplicitModel,
LTIModel,
)
from trainers import ProjectedPPOTrainer
use_savio = False
if use_savio:
print("\n\n\nUsing Savio\n===========\n\n\n")
N_CPUS = int(os.getenv("SLURM_CPUS_ON_NODE"))
JOB_ID = os.getenv("SLURM_JOB_ID")
else:
# N_CPUS = 1 # test
# N_CPUS = 2 # test
N_CPUS = multiprocessing.cpu_count()
n_tasks = 1
n_workers_per_task = int(math.floor(N_CPUS / n_tasks)) - 1 - 1
seed = 1
# Same dt must be used in controller models
# dt = 0.01
# env = InvertedPendulumEnv
# env_config = {
# "observation": "partial",
# "normed": True,
# "dt": dt,
# "supply_rate": "l2_gain", # "stability",
# "disturbance_model": "occasional"
# }
# dt = 0.01
# env = TimeDelayInvertedPendulumEnv # 1.0
# env_config = {
# "observation": "partial",
# "normed": True,
# "dt": dt,
# "design_time_delay": 0.07,
# "time_delay_steps": 5,
# }
# dt = 0.001 # 0.0001
# env = FlexibleArmEnv
# env_config = {
# "observation": "full",
# "normed": True,
# "dt": dt,
# "rollout_length": int(2.5/dt)-1, # 10000,
# "supply_rate": "l2_gain",
# "disturbance_model": "none",
# "disturbance_design_model": "occasional",
# "design_model": "rigid",
# }
# dt = 0.001
# env = FlexibleArmEnv
# env_config = {
# "observation": "partial",
# "normed": True,
# "dt": dt,
# "rollout_length": int(2 / dt) - 1,
# "supply_rate": "l2_gain",
# "disturbance_model": "occasional",
# "disturbance_design_model": "occasional",
# "design_model": "rigidplus_integrator",
# "delta_alpha": 1.0,
# # "design_integrator_type": "utox2",
# # "supplyrate_scale": 0.5,
# # "lagrange_multiplier": 5,
# "design_integrator_type": "utoy",
# "supplyrate_scale": 1,
# "lagrange_multiplier": 1000,
# }
# dt = 0.001
# env = DiskMarginExampleEnv
# env_config = {
# "dt": dt,
# "seed": seed,
# }
dt = 0.001
env = FlexibleArmDiskMarginEnv
env_config = {
"dt": dt,
"seed": seed,
"normed": True,
"rollout_length": int(2 / dt) - 1,
"disturbance_model": "occasional",
"disk_margin_type": "12dB60deg", # "6dB36deg",
# "skew": 0,
# "alpha": 0,
}
# Configure the algorithm.
config = {
"env": env,
"env_config": env_config,
"model": {
# "custom_model": FullyConnectedNetwork,
# "custom_model_config": {
# "n_layers": 2,
# "size": 19
# }
# "custom_model": RINN,
# "custom_model_config": {
# "state_size": 2,
# "nonlin_size": 16,
# "log_std_init": np.log(1.0),
# "dt": dt,
# "plant": env,
# "plant_config": env_config,
# "eps": 1e-3,
# },
# "custom_model": DissipativeSimplestRINN,
# "custom_model_config": {
# "state_size": 2,
# "nonlin_size": 16,
# "log_std_init": np.log(1.0),
# "dt": dt,
# "plant": env,
# "plant_config": env_config,
# "eps": 1e-3,
# "mode": "thetahat",
# "trs_mode": "fixed",
# "min_trs": 1,
# "backoff_factor": 1.1,
# "lti_initializer": "dissipative_thetahat",
# "lti_initializer_kwargs": {
# "trs_mode": "fixed",
# "min_trs": 1,
# "backoff_factor": 1.1,
# },
# "fix_mdeltap": False
# },
"custom_model": LTIModel,
"custom_model_config": {
"dt": dt,
"plant": env,
"plant_config": env_config,
"learn": True,
"log_std_init": np.log(1.0),
"state_size": 2,
"trs_mode": "fixed",
"min_trs": 1, # 1.5, # 1.44,
"backoff_factor": 1.1,
"lti_controller": "dissipative_thetahat",
"lti_controller_kwargs": {
"trs_mode": "fixed",
"min_trs": 1, # 1.5 # 1.44
"backoff_factor": 1.1,
},
"fix_mdeltap": False,
},
},
## Custom Policy Parameters
# How often to do projection. n -> every n'th gradient step. E.g., 1 -> every gradient step.
"projection_period": 100,
## Testing changes to training parameters
"sgd_minibatch_size": 2048,
"train_batch_size": 20480,
"lr": 1e-4,
"num_envs_per_worker": 10,
## End test
"seed": seed,
"num_workers": n_workers_per_task,
"framework": "torch",
"num_gpus": 0, # 1,
"evaluation_num_workers": 1,
"evaluation_config": {"render_env": False, "explore": False},
"evaluation_interval": 1,
"evaluation_parallel_to_training": True,
}
print("==================================")
print("Number of workers per task: ", n_workers_per_task)
print("")
print("Config: ")
print(config)
print("")
test_env = env(env_config)
print(
f"Max reward per: step: {test_env.max_reward}, rollout: {test_env.max_reward*(test_env.time_max+1)}"
)
print("==================================")
def name_creator(trial):
config = trial.config
name = f"{config['env'].__name__}"
name += f"_{config['model']['custom_model'].__name__}"
if use_savio:
name += f"_{JOB_ID}"
return name
ray.init()
results = tune.run(
# PPOTrainer,
ProjectedPPOTrainer,
config=config,
stop={
"agent_timesteps_total": 1e7,
},
verbose=1,
trial_name_creator=name_creator,
name="scratch",
local_dir="ray_results",
checkpoint_at_end=True,
checkpoint_freq=1000,
)