forked from pytorch/rl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmocking_classes.py
276 lines (220 loc) · 8.11 KB
/
mocking_classes.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
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
import torch
from torchrl.data.tensor_specs import (
NdUnboundedContinuousTensorSpec,
NdBoundedTensorSpec,
CompositeSpec,
MultOneHotDiscreteTensorSpec,
BinaryDiscreteTensorSpec,
BoundedTensorSpec,
UnboundedContinuousTensorSpec,
OneHotDiscreteTensorSpec,
)
from torchrl.data.tensordict.tensordict import _TensorDict
from torchrl.envs.common import _EnvClass
spec_dict = {
"bounded": BoundedTensorSpec,
"one_hot": OneHotDiscreteTensorSpec,
"unbounded": UnboundedContinuousTensorSpec,
"ndbounded": NdBoundedTensorSpec,
"ndunbounded": NdUnboundedContinuousTensorSpec,
"binary": BinaryDiscreteTensorSpec,
"mult_one_hot": MultOneHotDiscreteTensorSpec,
"composite": CompositeSpec,
}
default_spec_kwargs = {
BoundedTensorSpec: {"minimum": -1.0, "maximum": 1.0},
OneHotDiscreteTensorSpec: {"n": 7},
UnboundedContinuousTensorSpec: {},
NdBoundedTensorSpec: {"minimum": -torch.ones(4), "maxmimum": torch.ones(4)},
NdUnboundedContinuousTensorSpec: {
"shape": [
7,
]
},
BinaryDiscreteTensorSpec: {"n": 7},
MultOneHotDiscreteTensorSpec: {"nvec": [7, 3, 5]},
CompositeSpec: {},
}
def make_spec(spec_str):
target_class = spec_dict[spec_str]
return target_class(**default_spec_kwargs[target_class])
class _MockEnv(_EnvClass):
def __init__(self, seed: int = 100):
super().__init__(
device="cpu",
dtype=torch.float,
)
self.set_seed(seed)
@property
def maxstep(self):
return self.counter
def set_seed(self, seed: int) -> int:
self.seed = seed
self.counter = seed - 1
return seed
def custom_fun(self):
return 0
custom_attr = 1
@property
def custom_prop(self):
return 2
class DiscreteActionVecMockEnv(_MockEnv):
size = 7
observation_spec = CompositeSpec(
next_observation=NdUnboundedContinuousTensorSpec(shape=torch.Size([size]))
)
action_spec = OneHotDiscreteTensorSpec(7)
reward_spec = UnboundedContinuousTensorSpec()
from_pixels = False
out_key = "observation"
def _get_in_obs(self, obs):
return obs
def _get_out_obs(self, obs):
return obs
def _reset(self, tensordict: _TensorDict) -> _TensorDict:
self.counter += 1
state = torch.zeros(self.size) + self.counter
tensordict = tensordict.select().set(
"next_" + self.out_key, self._get_out_obs(state)
)
tensordict.set("done", torch.zeros(*tensordict.shape, 1, dtype=torch.bool))
return tensordict
def _step(
self,
tensordict: _TensorDict,
) -> _TensorDict:
tensordict = tensordict.to(self.device)
a = tensordict.get("action")
assert (a.sum(-1) == 1).all()
assert not self.is_done, "trying to execute step in done env"
obs = (
self._get_in_obs(self.current_tensordict.get(self.out_key))
+ a / self.maxstep
)
tensordict = tensordict.select() # empty tensordict
tensordict.set("next_" + self.out_key, self._get_out_obs(obs))
done = torch.isclose(obs, torch.ones_like(obs) * (self.counter + 1))
reward = done.any(-1).unsqueeze(-1)
done = done.all(-1).unsqueeze(-1)
tensordict.set("reward", reward.to(torch.float))
tensordict.set("done", done)
return tensordict
class ContinuousActionVecMockEnv(_MockEnv):
size = 7
observation_spec = CompositeSpec(
next_observation=NdUnboundedContinuousTensorSpec(shape=torch.Size([size]))
)
action_spec = NdBoundedTensorSpec(-1, 1, (7,))
reward_spec = UnboundedContinuousTensorSpec()
from_pixels = False
out_key = "observation"
def _get_in_obs(self, obs):
return obs
def _get_out_obs(self, obs):
return obs
def _reset(self, tensordict: _TensorDict) -> _TensorDict:
self.counter += 1
self.step_count = 0
state = torch.zeros(self.size) + self.counter
tensordict = tensordict.select().set(
"next_" + self.out_key, self._get_out_obs(state)
)
tensordict.set("done", torch.zeros(*tensordict.shape, 1, dtype=torch.bool))
return tensordict
def _step(
self,
tensordict: _TensorDict,
) -> _TensorDict:
self.step_count += 1
tensordict = tensordict.to(self.device)
a = tensordict.get("action")
assert not self.is_done, "trying to execute step in done env"
obs = self._obs_step(
self._get_in_obs(self.current_tensordict.get(self.out_key)), a
)
tensordict = tensordict.select() # empty tensordict
tensordict.set("next_" + self.out_key, self._get_out_obs(obs))
done = torch.isclose(obs, torch.ones_like(obs) * (self.counter + 1))
reward = done.any(-1).unsqueeze(-1)
done = done.all(-1).unsqueeze(-1)
tensordict.set("reward", reward.to(torch.float))
tensordict.set("done", done)
return tensordict
def _obs_step(self, obs, a):
return obs + a / self.maxstep
class DiscreteActionVecPolicy:
in_keys = ["observation"]
out_keys = ["action"]
def _get_in_obs(self, tensordict):
obs = tensordict.get(*self.in_keys)
return obs
def __call__(self, tensordict):
obs = self._get_in_obs(tensordict)
max_obs = (obs == obs.max(dim=-1, keepdim=True)[0]).cumsum(-1).argmax(-1)
k = tensordict.get(*self.in_keys).shape[-1]
max_obs = (max_obs + 1) % k
action = torch.nn.functional.one_hot(max_obs, k)
tensordict.set(*self.out_keys, action)
return tensordict
class DiscreteActionConvMockEnv(DiscreteActionVecMockEnv):
observation_spec = CompositeSpec(
next_pixels=NdUnboundedContinuousTensorSpec(shape=torch.Size([1, 7, 7]))
)
action_spec = OneHotDiscreteTensorSpec(7)
reward_spec = UnboundedContinuousTensorSpec()
from_pixels = True
out_key = "pixels"
def _get_out_obs(self, obs):
obs = torch.diag_embed(obs, 0, -2, -1).unsqueeze(0)
return obs
def _get_in_obs(self, obs):
return obs.diagonal(0, -1, -2).squeeze()
class DiscreteActionConvMockEnvNumpy(DiscreteActionConvMockEnv):
observation_spec = CompositeSpec(
next_pixels=NdUnboundedContinuousTensorSpec(shape=torch.Size([7, 7, 3]))
)
from_pixels = True
def _get_out_obs(self, obs):
obs = torch.diag_embed(obs, 0, -2, -1).unsqueeze(-1)
obs = obs.expand(*obs.shape[:-1], 3)
return obs
def _get_in_obs(self, obs):
return obs.diagonal(0, -2, -3)[..., 0]
def _obs_step(self, obs, a):
return obs + a.unsqueeze(-1) / self.maxstep
class ContinuousActionConvMockEnv(ContinuousActionVecMockEnv):
observation_spec = CompositeSpec(
next_pixels=NdUnboundedContinuousTensorSpec(shape=torch.Size([1, 7, 7]))
)
action_spec = NdBoundedTensorSpec(-1, 1, (7,))
reward_spec = UnboundedContinuousTensorSpec()
from_pixels = True
out_key = "pixels"
def _get_out_obs(self, obs):
obs = torch.diag_embed(obs, 0, -2, -1).unsqueeze(0)
return obs
def _get_in_obs(self, obs):
return obs.diagonal(0, -1, -2).squeeze()
class ContinuousActionConvMockEnvNumpy(ContinuousActionConvMockEnv):
observation_spec = CompositeSpec(
next_pixels=NdUnboundedContinuousTensorSpec(shape=torch.Size([7, 7, 3]))
)
from_pixels = True
def _get_out_obs(self, obs):
obs = torch.diag_embed(obs, 0, -2, -1).unsqueeze(-1)
obs = obs.expand(*obs.shape[:-1], 3)
return obs
def _get_in_obs(self, obs):
return obs.diagonal(0, -2, -3)[..., 0]
def _obs_step(self, obs, a):
return obs + a.unsqueeze(-1) / self.maxstep
class DiscreteActionConvPolicy(DiscreteActionVecPolicy):
in_keys = ["pixels"]
out_keys = ["action"]
def _get_in_obs(self, tensordict):
obs = tensordict.get(*self.in_keys).diagonal(0, -1, -2).squeeze()
return obs