-
Notifications
You must be signed in to change notification settings - Fork 0
/
custEnvsCombined.py
327 lines (257 loc) · 11.3 KB
/
custEnvsCombined.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
import numpy as np
import random
from gym import Env, spaces, Wrapper
from gym.wrappers.time_limit import TimeLimit
from stable_baselines3 import PPO
glob_lambda=0.5
random.seed(42)
class HeirarchyEnvMeta(Env):
def __init__(self,env,numModels=0,baseEnvID="",envStep=10):
super(HeirarchyEnvMeta,self)
self.baseEnv = env
self.baseEnv = TimeLimit(self.baseEnv, max_episode_steps=self.baseEnv.max_path_length)
self.envStep = envStep
self.action_space = spaces.Discrete(numModels)
self.observation_space = self.baseEnv.observation_space
self.prev_actions = []
self.model = [None]*numModels
for i in range(numModels):
self.model[i] = PPO.load(f"randomWindow/metaworld_{baseEnvID}/metaworld_{baseEnvID}_{i}")
def get_obs(self):
# position = env.sim.data.qpos.flat.copy()
# velocity = env.sim.data.qvel.flat.copy()
# observation = np.concatenate((position, velocity)).ravel()
# return observation
# do frame stacking
pos_goal = self.baseEnv._get_pos_goal()
if self.baseEnv._partially_observable:
pos_goal = np.zeros_like(pos_goal)
curr_obs = self.baseEnv._get_curr_obs_combined_no_goal()
# do frame stacking
if self.baseEnv.isV2:
obs = np.hstack((curr_obs, self.baseEnv._prev_obs, pos_goal))
else:
obs = np.hstack((curr_obs, pos_goal))
self.baseEnv._prev_obs = curr_obs
return obs
def step(self, action):
self.prev_actions.append(action)
baseEnv_obs = self.get_obs()
# self.do_simulation(action)
# select the model used to predict next action
# use the model to predict the action for base_env
reward = 0
for _ in range(self.envStep):
baseEnv_action, _states = self.model[action].predict(baseEnv_obs)
baseEnv_obs, re, done, info = self.baseEnv.step(baseEnv_action)
reward += re
# envDone = done or getattr(self.baseEnv, 'curr_path_length', 0) > self.baseEnv.max_path_length
if done:
break
# done = self.done
# reward = np.linalg.norm(observation[start_dim:start_dim+num_of_dim]-prev_observation[start_dim:start_dim+num_of_dim])
return baseEnv_obs, reward, done, info
def reset(self):
return self.baseEnv.reset()
class HeirarchyEnvAnt(Env):
def __init__(self,env,numModels=0):
super(HeirarchyEnvAnt,self)
self.baseEnv = env
self.action_space = spaces.Discrete(numModels)
self.observation_space = self.baseEnv.observation_space
self.prev_actions = []
self.model = [None]*numModels
for i in range(numModels):
self.model[i] = PPO.load(f"randomWindow/ant/ant_{i}")
def get_obs(self, env):
position = env.sim.data.qpos.flat.copy()[2:]
velocity = env.sim.data.qvel.flat.copy()
contact_force = env.contact_forces.flat.copy()
observations = np.concatenate((position, velocity, contact_force))
return observations
def step(self, action):
self.prev_actions.append(action)
baseEnv_obs = self.get_obs(self.baseEnv)
# self.do_simulation(action)
# select the model used to predict next action
# use the model to predict the action for base_env
reward = 0
for _ in range(10):
baseEnv_action, _states = self.model[action].predict(baseEnv_obs)
baseEnv_obs, re, done, info = self.baseEnv.step(baseEnv_action)
reward += re
# done = self.done
# reward = np.linalg.norm(observation[start_dim:start_dim+num_of_dim]-prev_observation[start_dim:start_dim+num_of_dim])
return baseEnv_obs, reward, done, info
def reset(self):
return self.baseEnv.reset()
class AntWrapper(Wrapper):
def __init__(self,
env,
k_obs,
act_sanction=1,
lambda_act=glob_lambda
):
super().__init__(env)
self.env._k_obs = k_obs
self.env._act = act_sanction
self.env._lambda = lambda_act
self.env.prev_act = np.zeros(8)
def step(self, action):
prev_observation = self.get_obs()
observation, reward, done, info = self.env.step(action)
new_observation = self.get_obs()
new_reward = np.linalg.norm(new_observation[self.env._k_obs==1] - prev_observation[self.env._k_obs==1], ord=2) - \
np.linalg.norm(new_observation[self.env._k_obs==0] - prev_observation[self.env._k_obs==0], ord=2) + \
self.env._lambda*(np.linalg.norm(action - self.env.prev_act, ord=2))*self.env._act
self.env.prev_act = action.copy()
return observation, new_reward, done, info
def get_obs(self):
position = self.env.sim.data.qpos.flat.copy()[2:]
velocity = self.env.sim.data.qvel.flat.copy()
contact_force = self.env.contact_forces.flat.copy()
observations = np.concatenate((position, velocity, contact_force))
return observations
class CheetahWrapper(Wrapper):
def __init__(self,
env,
k_obs,
act_sanction=1,
lambda_act=glob_lambda
):
super().__init__(env)
self.env._k_obs = k_obs
self.env._act = act_sanction
self.env._lambda = lambda_act
self.env.prev_act = np.zeros(6)
def step(self, action):
prev_observation = self.get_obs()
observation, reward, done, info = self.env.step(action)
new_observation = self.get_obs()
new_reward = np.linalg.norm(new_observation[self.env._k_obs==1] - prev_observation[self.env._k_obs==1], ord=2) - \
np.linalg.norm(new_observation[self.env._k_obs==0] - prev_observation[self.env._k_obs==0], ord=2) + \
self.env._lambda*(np.linalg.norm(action - self.env.prev_act, ord=2))*self.env._act
self.env.prev_act = action.copy()
return observation, new_reward, done, info
def get_obs(self):
position = self.sim.data.qpos.flat.copy()
velocity = self.sim.data.qvel.flat.copy()
observation = np.concatenate((position, velocity)).ravel()
return observation
class WalkerWrapper(Wrapper):
def __init__(self,
env,
k_obs,
act_sanction=1,
lambda_act=glob_lambda
):
super().__init__(env)
self.env._k_obs = k_obs
self.env._act = act_sanction
self.env._lambda = lambda_act
self.env.prev_act = np.zeros(6)
def step(self, action):
prev_observation = self.get_obs()
observation, reward, done, info = self.env.step(action)
new_observation = self.get_obs()
new_reward = np.linalg.norm(new_observation[self.env._k_obs==1] - prev_observation[self.env._k_obs==1], ord=2) - \
np.linalg.norm(new_observation[self.env._k_obs==0] - prev_observation[self.env._k_obs==0], ord=2) + \
self.env._lambda*(np.linalg.norm(action - self.env.prev_act, ord=2))*self.env._act
self.env.prev_act = action.copy()
return observation, new_reward, done, info
def get_obs(self):
position = self.sim.data.qpos.flat.copy()
velocity = np.clip(self.sim.data.qvel.flat.copy(), -10, 10)
observation = np.concatenate((position, velocity)).ravel()
return observation
class HumanoidWrapper(Wrapper):
def __init__(self,
env,
k_obs,
act_sanction=1,
lambda_act=glob_lambda
):
super().__init__(env)
self.env._k_obs = k_obs
self.env._act = act_sanction
self.env._lambda = lambda_act
self.env.prev_act = np.zeros(17)
def step(self, action):
prev_observation = self.get_obs()
observation, reward, done, info = self.env.step(action)
new_observation = self.get_obs()
new_reward = np.linalg.norm(new_observation[self.env._k_obs==1] - prev_observation[self.env._k_obs==1], ord=2) - \
np.linalg.norm(new_observation[self.env._k_obs==0] - prev_observation[self.env._k_obs==0], ord=2) + \
self.env._lambda*(np.linalg.norm(action - self.env.prev_act, ord=2))*self.env._act
self.env.prev_act = action.copy()
return observation, new_reward, done, info
def get_obs(self):
position = self.sim.data.qpos.flat.copy()
velocity = self.sim.data.qvel.flat.copy()
com_inertia = self.sim.data.cinert.flat.copy()
com_velocity = self.sim.data.cvel.flat.copy()
actuator_forces = self.sim.data.qfrc_actuator.flat.copy()
external_contact_forces = self.sim.data.cfrc_ext.flat.copy()
return np.concatenate(
(
position,
velocity,
com_inertia,
com_velocity,
actuator_forces,
external_contact_forces,
)
)
class HopperWrapper(Wrapper):
def __init__(self,
env,
k_obs,
act_sanction=1,
lambda_act=glob_lambda
):
super().__init__(env)
self.env._k_obs = k_obs
self.env._act = act_sanction
self.env._lambda = lambda_act
self.env.prev_act = np.zeros(3)
def step(self, action):
prev_observation = self.get_obs()
observation, reward, done, info = self.env.step(action)
new_observation = self.get_obs()
new_reward = np.linalg.norm(new_observation[self.env._k_obs==1] - prev_observation[self.env._k_obs==1], ord=2) - \
np.linalg.norm(new_observation[self.env._k_obs==0] - prev_observation[self.env._k_obs==0], ord=2) + \
self.env._lambda*(np.linalg.norm(action - self.env.prev_act, ord=2))*self.env._act
self.env.prev_act = action.copy()
return observation, new_reward, done, info
def get_obs(self):
position = self.sim.data.qpos.flat.copy()
velocity = np.clip(self.sim.data.qvel.flat.copy(), -10, 10)
observation = np.concatenate((position, velocity)).ravel()
return observation
class MetaworldWrapper(Wrapper):
def __init__(self,
env,
k_obs,
act_sanction=1,
lambda_act=glob_lambda
):
super().__init__(env)
self.env._k_obs = k_obs
self.env._act = act_sanction
self.env._lambda = lambda_act
self.env.prev_act = np.zeros(4)
def step(self, action):
prev_observation = self.get_obs()
observation, reward, done, info = self.env.step(action)
new_observation = self.get_obs()
new_reward = np.linalg.norm(new_observation[self.env._k_obs==1] - prev_observation[self.env._k_obs==1], ord=2) - \
np.linalg.norm(new_observation[self.env._k_obs==0] - prev_observation[self.env._k_obs==0], ord=2) + \
self.env._lambda*(np.linalg.norm(action - self.env.prev_act, ord=2))*self.env._act
envDone = done or getattr(self.env, 'curr_path_length', 0) > self.env.max_path_length
self.env.prev_act = action.copy()
return observation, reward, envDone, info
def get_obs(self):
position = self.sim.data.qpos.flat.copy()
velocity = self.sim.data.qvel.flat.copy()
observation = np.concatenate((position, velocity)).ravel()
return observation