-
Notifications
You must be signed in to change notification settings - Fork 11
/
wrapped_goal_envs.py
234 lines (193 loc) · 7.84 KB
/
wrapped_goal_envs.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
'''
RLKIT and my extensions do not support OpenAI GoalEnv interface
Here we wrap any GoalEnv we want to use
'''
from gym.envs.robotics import FetchPickAndPlaceEnv
from gym.envs.robotics.fetch.rotated_fetch_anywhere_reach import RotatedFetchAnywhereReachEnv
from gym import spaces
from gym.spaces import Box
import numpy as np
# Sorry, this is very monkey-patchy
class WrappedFetchPickAndPlaceEnv(FetchPickAndPlaceEnv):
def __init__(self, *args, **kwargs):
super(WrappedFetchPickAndPlaceEnv, self).__init__(*args, **kwargs)
fetch_obs_space = self.observation_space
new_obs_space = spaces.Dict(
{
'obs': fetch_obs_space.spaces['observation'],
'obs_task_params': fetch_obs_space.spaces['desired_goal']
}
)
self.observation_space = new_obs_space
def reset(self, *args, **kwargs):
obs = super().reset(*args, **kwargs)
new_obs = {
'obs': obs['observation'],
'obs_task_params': obs['desired_goal']
}
return new_obs
def step(self, *args, **kwargs):
next_ob, raw_reward, terminal, env_info = super().step(*args, **kwargs)
new_next = {
'obs': next_ob['observation'],
'obs_task_params': next_ob['desired_goal']
}
env_info['achieved_goal'] = next_ob['achieved_goal']
return new_next, raw_reward, terminal, env_info
def log_statistics(self, test_paths):
rets = []
path_lens = []
for path in test_paths:
rets.append(np.sum(path['rewards']))
path_lens.append(path['rewards'].shape[0])
solved = [t[0] > -1.0*t[1] for t in zip(rets, path_lens)]
percent_solved = np.sum(solved) / float(len(solved))
return {'Percent_Solved': percent_solved}
class DebugReachFetchPickAndPlaceEnv(FetchPickAndPlaceEnv):
'''
This environment is made in a very monkey-patchy way.
The whole point of this environment is to make simpler
version of the pick and place where you just have to reach
for just above the block you have to pick up.
The desired goal / observed task params is the position just
above the cube.
Please try not to use this! It's very hacky. I'm just using
it for seeing why my models isn't working.
'''
def __init__(self, *args, **kwargs):
super(DebugReachFetchPickAndPlaceEnv, self).__init__(*args, **kwargs)
fetch_obs_space = self.observation_space
new_obs_space = spaces.Dict(
{
'obs': fetch_obs_space.spaces['observation'],
'obs_task_params': Box(-np.inf, np.inf, shape=(3,), dtype='float32')
}
)
self.observation_space = new_obs_space
def reset(self, *args, **kwargs):
obs = super().reset(*args, **kwargs)
goal = obs['observation'][3:6].copy()
goal[2] += 0.03
new_obs = {
'obs': obs['observation'],
'obs_task_params': goal
}
return new_obs
def step(self, *args, **kwargs):
next_ob, raw_reward, terminal, env_info = super().step(*args, **kwargs)
goal = next_ob['observation'][3:6].copy()
goal[2] += 0.03
new_next = {
'obs': next_ob['observation'],
'obs_task_params': goal
}
dist = np.linalg.norm(next_ob['observation'][:3] - goal)
if dist < 0.05:
raw_reward = -dist
else:
raw_reward = -1.0
return new_next, raw_reward, terminal, {}
def log_statistics(self, test_paths):
rets = []
path_lens = []
for path in test_paths:
rets.append(np.sum(path['rewards']))
path_lens.append(path['rewards'].shape[0])
solved = [t[0] > -1.0*t[1] for t in zip(rets, path_lens)]
percent_solved = np.sum(solved) / float(len(solved))
return {'Percent_Solved': percent_solved}
class DebugFetchReachAndLiftEnv(FetchPickAndPlaceEnv):
'''
This environment is made in a very monkey-patchy way.
The whole point of this environment is to make simpler
version of the pick and place where you just have to reach
for and lift the block straight up.kiKI
The desired goal / observed task params is the position 0.2
above the cube
Please try not to use this! It's very hacky. I'm just using
it for seeing why my models isn't working.
'''
def __init__(self, *args, **kwargs):
super(DebugFetchReachAndLiftEnv, self).__init__(*args, **kwargs)
fetch_obs_space = self.observation_space
new_obs_space = spaces.Dict(
{
'obs': fetch_obs_space.spaces['observation'],
'obs_task_params': Box(-np.inf, np.inf, shape=(3,), dtype='float32')
}
)
self.observation_space = new_obs_space
def reset(self, *args, **kwargs):
obs = super().reset(*args, **kwargs)
self.debug_goal = obs['observation'][3:6].copy()
self.debug_goal[2] += 0.2
new_obs = {
'obs': obs['observation'],
'obs_task_params': self.debug_goal
}
return new_obs
def step(self, *args, **kwargs):
next_ob, raw_reward, terminal, env_info = super().step(*args, **kwargs)
new_next = {
'obs': next_ob['observation'],
'obs_task_params': self.debug_goal
}
# distance of the cube from the debug goal
dist = np.linalg.norm(next_ob['observation'][3:6] - self.debug_goal)
if dist < 0.05:
raw_reward = -dist
else:
raw_reward = -1.0
return new_next, raw_reward, terminal, {}
def log_statistics(self, test_paths):
rets = []
path_lens = []
for path in test_paths:
rets.append(np.sum(path['rewards']))
path_lens.append(path['rewards'].shape[0])
solved = [t[0] > -1.0*t[1] for t in zip(rets, path_lens)]
percent_solved = np.sum(solved) / float(len(solved))
return {'Percent_Solved': percent_solved}
class WrappedRotatedFetchReachAnywhereEnv(RotatedFetchAnywhereReachEnv):
'''
Wrapped
Also changed the reward and shaped it reward function
'''
def __init__(self, *args, **kwargs):
super(WrappedRotatedFetchReachAnywhereEnv, self).__init__(*args, **kwargs)
fetch_obs_space = self.observation_space
new_obs_space = spaces.Dict(
{
'obs': fetch_obs_space.spaces['observation'],
'obs_task_params': fetch_obs_space.spaces['desired_goal']
}
)
self.observation_space = new_obs_space
def reset(self, *args, **kwargs):
obs = super().reset(*args, **kwargs)
new_obs = {
'obs': obs['observation'],
'obs_task_params': obs['desired_goal']
}
self.prev_dist = np.linalg.norm(obs['desired_goal'] - obs['achieved_goal'], axis=-1)
return new_obs
def step(self, *args, **kwargs):
next_ob, raw_reward, terminal, env_info = super().step(*args, **kwargs)
new_next = {
'obs': next_ob['observation'],
'obs_task_params': next_ob['desired_goal']
}
if env_info['is_success']:
raw_reward = 1.0
else:
raw_reward = 0.0
cur_dist = np.linalg.norm(next_ob['desired_goal'] - next_ob['achieved_goal'], axis=-1)
shaping = self.prev_dist - cur_dist
self.prev_dist = cur_dist
return new_next, raw_reward + 10*shaping, terminal, env_info
def log_statistics(self, test_paths):
successes = []
for path in test_paths:
successes.append(np.sum([e_info['is_success'] for e_info in path['env_infos']]) > 0)
percent_solved = np.sum(successes) / float(len(successes))
return {'Percent_Solved': percent_solved}