forked from giomgit/potion-fork
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactiveis_learn.py
321 lines (285 loc) · 15.6 KB
/
activeis_learn.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
import os
import logging
import copy
import torch
import gymnasium as gym
from potion.common.debug_logger import setup_debug_logger
from expsuite import PyExperimentSuite
from potion.envs.lq import LQ
from potion.common.misc_utils import clip, seed_all_agent
from potion.meta.steppers import ConstantStepper, Adam
from potion.actors.continuous_policies import ShallowGaussianPolicy, DeepGaussianPolicy
from potion.algorithms.reinforce_offpolicy_g import reinforce_offpolicy_step_g
from potion.algorithms.reinforce import reinforce_step
from potion.simulation.trajectory_generators import generate_batch
from gymnasium.spaces import Box
import numpy as np
class ClipActionOwn(gym.ActionWrapper, gym.utils.RecordConstructorArgs):
"""Clip the continuous action within the valid :class:`Box` observation space bound.
Example:
>>> import gymnasium as gym
>>> from gymnasium.wrappers import ClipAction
>>> env = gym.make("Hopper-v4")
>>> env = ClipAction(env)
>>> env.action_space
Box(-1.0, 1.0, (3,), float32)
>>> _ = env.reset(seed=42)
>>> _ = env.step(np.array([5.0, -2.0, 0.0]))
... # Executes the action np.array([1.0, -1.0, 0]) in the base environment
"""
def __init__(self, env: gym.Env):
"""A wrapper for clipping continuous actions within the valid bound.
Args:
env: The environment to apply the wrapper
"""
assert isinstance(env.action_space, Box)
gym.utils.RecordConstructorArgs.__init__(self)
gym.ActionWrapper.__init__(self, env)
def action(self, action):
"""Clips the action within the valid bounds.
Args:
action: The action to clip
Returns:
The clipped action
"""
return np.clip(action, -1, 1)
class MySuite(PyExperimentSuite):
def reset(self, params, rep):
self.seed = params['seed']*rep
seed_all_agent(self.seed)
# Environment
# ===========
if params['environment'] == 'lq':
self.env = LQ(params['state_dim'],1,max_pos=10, max_action = float('inf'), sigma_noise=params['sigma_noise'], horizon=params["horizon"])
self.env.horizon = params['horizon']
self.env.seed(self.seed)
elif params['environment'] == 'cartpole':
self.env = gym.wrappers.ClipAction(gym.make('ContCartPole-v0'))
self.env.gamma = 1
self.env.horizon = params['horizon']
self.env.seed(self.seed)
elif params['environment'] == 'swimmer':
self.env = gym.wrappers.ClipAction(gym.make('Swimmer-v4'))
self.env.horizon = params['horizon']
self.env.gamma = params['gamma']
elif params['environment'] == 'halfcheetah':
self.env = ClipActionOwn(gym.make('HalfCheetah-v4'))
self.env.horizon = params['horizon']
self.env.gamma = params['gamma']
elif params['environment'] == 'ant':
self.env = gym.wrappers.ClipAction(gym.make('Ant-v4'))
self.env.horizon = params['horizon']
self.env.gamma = params['gamma']
elif params['environment'] == 'invertedpendulum':
self.env = gym.wrappers.ClipAction(gym.make('InvertedPendulum-v4'))
self.env.horizon = params['horizon']
self.env.gamma = params['gamma']
elif params['environment'] == 'pusher':
self.env = gym.wrappers.ClipAction(gym.make('Pusher-v4'))
self.env.horizon = params['horizon']
self.env.gamma = params['gamma']
else:
raise NotImplementedError
state_dim = sum(self.env.observation_space.shape)
action_dim = sum(self.env.action_space.shape)
# Policy
# ======
if params['environment'] == 'lq' or params['environment'] == 'cartpole' or params['environment'] == "halfcheetah":
self.policy = ShallowGaussianPolicy(
state_dim, # input size
action_dim, # output size
mu_init = params["mu_init"]*torch.ones([state_dim, action_dim]),
logstd_init = params["logstd_init"]*torch.ones(action_dim),
learn_std = params["learn_std"]
)
elif params['environment'] == 'swimmer' or params['environment'] == "ant":
self.policy = DeepGaussianPolicy(
state_dim,
action_dim,
hidden_neurons = [32,32],
mu_init = None,
logstd_init = params["logstd_init"]*torch.ones(action_dim),
learn_std = params["learn_std"]
)
elif params['environment'] == 'invertedpendulum' or params['environment'] == 'pusher':
self.policy = DeepGaussianPolicy(
state_dim,
action_dim,
hidden_neurons = [8,8],
mu_init = None,
logstd_init = params["logstd_init"]*torch.ones(action_dim),
learn_std = params["learn_std"]
)
self.stepper = eval(params["stepper"])
# Offpolicy data
# ==============
# Initial data for first offline CE estimation
if params['ce_use_offline_data']:
self.offline_policies = [self.policy]
self.offline_batches = [generate_batch(self.env, self.policy, self.env.horizon, params['batchsize'], seed=self.seed)]
self.njt_batches = [generate_batch(self.env, self.policy, self.env.horizon, params['batchsize'], seed=self.seed)]
else:
self.offline_policies = None
self.offline_batches = None
self.njt_batches = None
# Prepare behavioural policies
if isinstance(params['ce_batchsizes'], str):
self.ce_batchsizes = eval(params['ce_batchsizes'])
else:
self.ce_batchsizes = params['ce_batchsizes']
if self.ce_batchsizes is None:
self.behavioural_policies = [copy.deepcopy(self.policy)]
else:
print(self.ce_batchsizes)
self.behavioural_policies = [copy.deepcopy(self.policy) for _ in range(len(self.ce_batchsizes)+1)]
# Logger
# ======
self.debug_logger = setup_debug_logger(
name = str(rep),
log_file = os.path.join(params['path'],params['name'],'') + str(rep) + '_DEBUG' + '.log',
level = logging.DEBUG,
stream ='stderr'
)
def iterate(self, params, rep, n):
if 'debug_target' == params['name']:
log, self.offline_policies, self.offline_batches, self.njt_batches = reinforce_offpolicy_step_g(
self.env, self.policy, self.env.horizon, self.behavioural_policies, self.offline_policies, self.offline_batches, self.njt_batches,
batchsize = params['batchsize'],
baseline = params['baseline'],
biased_offpolicy = params['biased_offpolicy'],
ce_batchsizes = self.ce_batchsizes,
disc = self.env.gamma,
defensive_batch = params['defensive_batch'],
debug_logger = self.debug_logger,
estimator = params['estimator'],
ce_tol_grad=params['ce_tol_grad'],
ce_lr = params['ce_lr'],
ce_initialize_behavioural_policies = params['ce_initialize_behavioural_policies'],
ce_max_iter = params['ce_max_iter'],
ce_weight_decay = params['ce_weight_decay'],
ce_mis_normalize = params['ce_mis_normalize'],
ce_mis_clip = params['ce_mis_clip'],
ce_optimizer = params['ce_optimizer'],
seed = params['seed']+n,
shallow = isinstance(self.policy, ShallowGaussianPolicy),
stepper = self.stepper,
test_batchsize = params['batchsize'],
log_grad = False,
log_ce_params_norms = True,
log_params_norms = True,
njt_batchsize = params['njt_batchsize'],
window=params['window'],)
# Uso la target corrente per la stima della CE
self.offline_policies = [self.policy]*params["batchsize"]
self.offline_batches = [generate_batch(self.env, self.policy, self.env.horizon, params['batchsize'],
action_filter=None,
seed=params['seed']+n,
n_jobs=False)]
self.njt_batches = [generate_batch(self.env, self.policy, self.env.horizon, params['batchsize'],
action_filter=None,
seed=params['seed']+n,
n_jobs=False)]
else:
#self.offline_policies = self.offline_policies[-params["window"] * (params["batchsize"]+params["defensive_batch"]):]
#self.offline_batches = self.offline_batches[-params["window"] * (params["batchsize"]+params["defensive_batch"]):]
#self.njt_batches = self.njt_batches[-params["window"] * (params["batchsize"]+params["defensive_batch"]):]
if params['ce_use_offline_data']:
self.offline_policies = self.offline_policies[-params["window"]:]
self.offline_batches = self.offline_batches[-params["window"]:]
self.njt_batches = self.njt_batches[-params["window"]:]
if params["ce_update_frequency"] is not None:
if params["ce_update_frequency"] == 0 or n % params["ce_update_frequency"] == 0:
log, self.offline_policies, self.offline_batches, self.njt_batches = reinforce_offpolicy_step_g(
self.env, self.policy, self.env.horizon, self.behavioural_policies, self.offline_policies, self.offline_batches, self.njt_batches,
batchsize = params['batchsize'],
baseline = params['baseline'],
biased_offpolicy = params['biased_offpolicy'],
ce_batchsizes = self.ce_batchsizes,
disc = self.env.gamma,
defensive_batch = params['defensive_batch'],
debug_logger = self.debug_logger,
estimator = params['estimator'],
ce_tol_grad=params['ce_tol_grad'],
ce_lr = params['ce_lr'],
ce_initialize_behavioural_policies = params['ce_initialize_behavioural_policies'],
ce_max_iter = params['ce_max_iter'],
ce_weight_decay = params['ce_weight_decay'],
ce_mis_normalize = params['ce_mis_normalize'],
ce_mis_clip = params['ce_mis_clip'],
ce_optimizer = params['ce_optimizer'],
ce_optimize_mean = params['ce_optimize_mean'],
ce_optimize_variance = params['ce_optimize_variance'],
seed = params['seed']+n,
shallow = isinstance(self.policy, ShallowGaussianPolicy),
stepper = self.stepper,
test_batchsize = params['batchsize'],
log_grad = False,
log_ce_params_norms = True,
log_params_norms = True,
njt_batchsize = params['njt_batchsize'],
)
else:
all_ce_batchsizes = sum(self.ce_batchsizes) if self.ce_batchsizes is not None else 0
log, self.offline_policies, self.offline_batches, self.njt_batches = reinforce_offpolicy_step_g(
self.env, self.policy, self.env.horizon, self.behavioural_policies, self.offline_policies, self.offline_batches, self.njt_batches,
batchsize = params['batchsize'],# + params["njt_batchsize"] + all_ce_batchsizes,
baseline = params['baseline'],
biased_offpolicy = params['biased_offpolicy'],
ce_batchsizes = None,
disc = self.env.gamma,
defensive_batch = params['defensive_batch'],
debug_logger = self.debug_logger,
estimator = params['estimator'],
ce_tol_grad=params['ce_tol_grad'],
ce_lr = params['ce_lr'],
ce_initialize_behavioural_policies = params['ce_initialize_behavioural_policies'],
ce_max_iter = params['ce_max_iter'],
ce_weight_decay = params['ce_weight_decay'],
ce_mis_normalize = params['ce_mis_normalize'],
ce_mis_clip = params['ce_mis_clip'],
ce_optimizer = params['ce_optimizer'],
ce_optimize_mean = params['ce_optimize_mean'],
ce_optimize_variance = params['ce_optimize_variance'],
seed = params['seed']+n,
shallow = isinstance(self.policy, ShallowGaussianPolicy),
stepper = self.stepper,
test_batchsize = params['batchsize'], #+ params["njt_batchsize"] + all_ce_batchsizes,
log_grad = False,
log_ce_params_norms = True,
log_params_norms = True,
njt_batchsize = params['njt_batchsize'],
)
else:
log, self.offline_policies, self.offline_batches, self.njt_batches = reinforce_offpolicy_step_g(
self.env, self.policy, self.env.horizon, self.behavioural_policies, self.offline_policies, self.offline_batches, self.njt_batches,
batchsize = params['batchsize'],
baseline = params['baseline'],
biased_offpolicy = params['biased_offpolicy'],
ce_batchsizes = self.ce_batchsizes,
disc = self.env.gamma,
defensive_batch = params['defensive_batch'],
debug_logger = self.debug_logger,
estimator = params['estimator'],
ce_tol_grad=params['ce_tol_grad'],
ce_lr = params['ce_lr'],
ce_initialize_behavioural_policies = params['ce_initialize_behavioural_policies'],
ce_max_iter = params['ce_max_iter'],
ce_weight_decay = params['ce_weight_decay'],
ce_mis_normalize = params['ce_mis_normalize'],
ce_mis_clip = params['ce_mis_clip'],
ce_optimizer = params['ce_optimizer'],
ce_optimize_mean = params['ce_optimize_mean'],
ce_optimize_variance = params['ce_optimize_variance'],
seed = params['seed']+n,
shallow = isinstance(self.policy, ShallowGaussianPolicy),
stepper = self.stepper,
test_batchsize = params['batchsize'],
log_grad = False,
log_ce_params_norms = True,
log_params_norms = True,
njt_batchsize = params['njt_batchsize'],
)
return log
if __name__ == "__main__":
mysuite = MySuite()
mysuite.start()