-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolicy.py
328 lines (273 loc) · 12 KB
/
policy.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
327
328
from typing import Callable, Dict, List, Optional, Tuple, Type, Union, Any
from gymnasium import spaces
import torch as th
from torch import nn
import numpy as np
from functools import partial
import time
from stable_baselines3 import PPO
from stable_baselines3.common.policies import ActorCriticPolicy
from stable_baselines3.common.torch_layers import BaseFeaturesExtractor
from stable_baselines3.common.distributions import (
BernoulliDistribution,
CategoricalDistribution,
DiagGaussianDistribution,
MultiCategoricalDistribution,
StateDependentNoiseDistribution,
)
from stable_baselines3.common.distributions import Distribution
from stable_baselines3.common.type_aliases import Schedule, MaybeCallback
from mpc import ModelPredictiveControlWithoutOptimizer
class ActorCriticModelPredictiveControlFeatureExtractor(BaseFeaturesExtractor):
"""
Custom features extractor for policy and value function.
It receives as input the observations and returns a tuple containing the features extracted for the policy
and the features extracted for the value function.
:param observation_space: (gym.Space)
:param features_dim: (int) Number of features extracted.
This corresponds to the number of unit for the last layer of the network.
"""
def __init__(
self,
observation_space: spaces.Space,
input_dim: int = 4,
features_dim: int = 64,
):
super().__init__(observation_space, features_dim)
assert isinstance(observation_space, spaces.Box)
self.input_dim = input_dim
# We assume MlpPolicy
# Extract features from input
# Note: If you want to use images as input,
# you will need to define a new CNN feature extractor.
# self.feature_extractor = nn.Sequential(
# nn.Linear(input_dim, features_dim),
# nn.ReLU(),
# nn.Linear(features_dim, features_dim),
# nn.ReLU(),
# nn.Linear(features_dim, features_dim),
# )
# Flatten feature extractor
self.feature_extractor = nn.Sequential(
nn.Flatten(),
)
def forward(self, observations: th.Tensor) -> th.Tensor:
return self.feature_extractor(observations[..., : self.input_dim])
class ActorCriticModelPredictiveControlPolicy(ActorCriticPolicy):
def __init__(
self,
observation_space: spaces.Space,
action_space: spaces.Space,
lr_schedule: Callable[[float], float],
mpc_class: Type[
ModelPredictiveControlWithoutOptimizer
] = ModelPredictiveControlWithoutOptimizer,
mpc_kwargs: Optional[Dict[str, Any]] = None,
predict_action: bool = False,
predict_cost: bool = False,
num_cost_terms: int = 1,
obs_to_state_target: Optional[Callable] = None,
*args,
**kwargs,
):
self.action_dim = mpc_kwargs["action_size"]
self.prediction_horizon = mpc_kwargs["prediction_horizon"]
self.predict_action = predict_action
self.predict_cost = predict_cost
self.num_cost_terms = num_cost_terms
self.obs_to_state_target = obs_to_state_target
assert (
self.predict_action != self.predict_cost
), "Either predict action or predict cost must be True"
if "net_arch" not in kwargs:
kwargs["net_arch"] = dict(pi=[64, 64], vf=[64, 64])
kwargs["net_arch"]["pi"].append(
self.prediction_horizon * self.action_dim
if self.predict_action
else self.prediction_horizon * self.num_cost_terms
)
# Disable orthogonal initialization
kwargs["ortho_init"] = False
super().__init__(
observation_space,
action_space,
lr_schedule,
# Pass remaining arguments to base class
*args,
**kwargs,
)
self.mpc = mpc_class(**mpc_kwargs)
def _build(self, lr_schedule: Schedule) -> None:
"""
Create the networks and the optimizer.
:param lr_schedule: Learning rate schedule
lr_schedule(1) is the initial learning rate
"""
self._build_mlp_extractor()
latent_dim_pi = self.mlp_extractor.latent_dim_pi
if isinstance(self.action_dist, DiagGaussianDistribution):
_, self.log_std = self.action_dist.proba_distribution_net(
latent_dim=latent_dim_pi, log_std_init=self.log_std_init
)
elif isinstance(self.action_dist, StateDependentNoiseDistribution):
_, self.log_std = self.action_dist.proba_distribution_net(
latent_dim=latent_dim_pi,
latent_sde_dim=latent_dim_pi,
log_std_init=self.log_std_init,
)
else:
raise NotImplementedError(f"Unsupported distribution '{self.action_dist}'.")
self.value_net = nn.Linear(self.mlp_extractor.latent_dim_vf, 1)
# Init weights: use orthogonal initialization
# with small initial weight for the output
if self.ortho_init:
# TODO: check for features_extractor
# Values from stable-baselines.
# features_extractor/mlp values are
# originally from openai/baselines (default gains/init_scales).
module_gains = {
self.features_extractor: np.sqrt(2),
self.mlp_extractor: np.sqrt(2),
self.action_net: 0.01,
self.value_net: 1,
}
if not self.share_features_extractor:
# Note(antonin): this is to keep SB3 results
# consistent, see GH#1148
del module_gains[self.features_extractor]
module_gains[self.pi_features_extractor] = np.sqrt(2)
module_gains[self.vf_features_extractor] = np.sqrt(2)
for module, gain in module_gains.items():
module.apply(partial(self.init_weights, gain=gain))
# Setup optimizer with initial learning rate
self.optimizer = self.optimizer_class(
self.parameters(), lr=lr_schedule(1), **self.optimizer_kwargs
)
def forward(
self, obs: th.Tensor, deterministic: bool = False
) -> Tuple[th.Tensor, th.Tensor, th.Tensor]:
"""
Forward pass in all the networks (actor and critic)
:param obs: Observation
:param deterministic: Whether to sample or use deterministic actions
:return: action, value and log probability of the action
"""
# Preprocess the observation if needed
features = self.extract_features(obs)
if self.share_features_extractor:
latent_pi, latent_vf = self.mlp_extractor(features)
else:
pi_features, vf_features = features
latent_pi = self.mlp_extractor.forward_actor(pi_features)
latent_vf = self.mlp_extractor.forward_critic(vf_features)
# Evaluate the values for the given observations
values = self.value_net(latent_vf)
state, target = self.obs_to_state_target(obs)
batch_size = obs.shape[0]
with th.enable_grad():
if self.predict_action:
action_initial = latent_pi.view(
batch_size, self.prediction_horizon, self.action_dim
)
action_initial.requires_grad = True
mean_actions, _ = self.mpc(state, target, action_initial, None)
else:
cost_dict = latent_pi.view(
batch_size, self.prediction_horizon, self.num_cost_terms
)
mean_actions, _ = self.mpc(state, target, None, cost_dict)
mean_actions = mean_actions[:, 0]
if isinstance(self.action_dist, DiagGaussianDistribution):
distribution = self.action_dist.proba_distribution(
mean_actions, self.log_std
)
elif isinstance(self.action_dist, StateDependentNoiseDistribution):
distribution = self.action_dist.proba_distribution(
mean_actions, self.log_std, latent_pi
)
else:
raise ValueError("Invalid action distribution")
actions = distribution.get_actions(deterministic=deterministic)
log_prob = distribution.log_prob(actions)
actions = actions.reshape((-1, *self.action_space.shape))
return actions, values, log_prob
def evaluate_actions(
self, obs: th.Tensor, actions: th.Tensor
) -> Tuple[th.Tensor, th.Tensor, th.Tensor]:
"""
Evaluate the values, log probability and entropy for given actions given the current policy
:param obs: Observation
:param actions: Actions
:return: values, log probability and entropy
"""
# Preprocess the observation if needed
features = self.extract_features(obs)
if self.share_features_extractor:
latent_pi, latent_vf = self.mlp_extractor(features)
else:
pi_features, vf_features = features
latent_pi = self.mlp_extractor.forward_actor(pi_features)
latent_vf = self.mlp_extractor.forward_critic(vf_features)
state, target = self.obs_to_state_target(obs)
batch_size = obs.shape[0]
if self.predict_action:
action_initial = latent_pi.view(
batch_size, self.prediction_horizon, self.action_dim
)
mean_actions, _ = self.mpc(state, target, action_initial, None)
else:
cost_dict = latent_pi.view(
batch_size, self.prediction_horizon, self.num_cost_terms
)
mean_actions, _ = self.mpc(state, target, None, cost_dict)
mean_actions = mean_actions[:, 0]
if isinstance(self.action_dist, DiagGaussianDistribution):
distribution = self.action_dist.proba_distribution(
mean_actions, self.log_std
)
elif isinstance(self.action_dist, StateDependentNoiseDistribution):
distribution = self.action_dist.proba_distribution(
mean_actions, self.log_std, latent_pi
)
else:
raise ValueError("Invalid action distribution")
log_prob = distribution.log_prob(actions)
dist_entropy = distribution.entropy()
values = self.value_net(latent_vf).flatten()
return values, log_prob, dist_entropy
def get_distribution(self, obs: th.Tensor) -> Distribution:
"""
Get the current policy distribution given the observations.
:param obs:
:return: the action distribution.
"""
features = self.extract_features(obs)
if self.share_features_extractor:
latent_pi, _ = self.mlp_extractor(features)
else:
pi_features, _ = features
latent_pi = self.mlp_extractor.forward_actor(pi_features)
state, target = self.obs_to_state_target(obs)
batch_size = obs.shape[0]
with th.enable_grad():
if self.predict_action:
action_initial = latent_pi.view(
batch_size, self.prediction_horizon, self.action_dim
)
action_initial.requires_grad = True
mean_actions, _ = self.mpc(state, target, action_initial, None)
else:
cost_dict = latent_pi.view(
batch_size, self.prediction_horizon, self.num_cost_terms
)
cost_dict = th.clamp(cost_dict, min=10.0, max=10.0)
mean_actions, _ = self.mpc(state, target, None, cost_dict)
mean_actions = mean_actions[:, 0]
if isinstance(self.action_dist, DiagGaussianDistribution):
return self.action_dist.proba_distribution(mean_actions, self.log_std)
elif isinstance(self.action_dist, StateDependentNoiseDistribution):
return self.action_dist.proba_distribution(
mean_actions, self.log_std, latent_pi
)
else:
raise ValueError("Invalid action distribution")