forked from facebookresearch/mbrl-lib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrajectory_opt.py
749 lines (646 loc) · 30.7 KB
/
trajectory_opt.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
import time
from typing import Callable, List, Optional, Sequence, Tuple, cast
import hydra
import numpy as np
import omegaconf
import torch
import torch.distributions
import mbrl.models
import mbrl.types
import mbrl.util.math
from .core import Agent, complete_agent_cfg
class Optimizer:
def __init__(self):
pass
def optimize(
self,
obj_fun: Callable[[torch.Tensor], torch.Tensor],
x0: Optional[torch.Tensor] = None,
**kwargs,
) -> torch.Tensor:
"""Runs optimization.
Args:
obj_fun (callable(tensor) -> tensor): objective function to maximize.
x0 (tensor, optional): initial solution, if necessary.
Returns:
(torch.Tensor): the best solution found.
"""
pass
class CEMOptimizer(Optimizer):
"""Implements the Cross-Entropy Method optimization algorithm.
A good description of CEM [1] can be found at https://arxiv.org/pdf/2008.06389.pdf. This
code implements the version described in Section 2.1, labeled CEM_PETS
(but note that the shift-initialization between planning time steps is handled outside of
this class by TrajectoryOptimizer).
This implementation also returns the best solution found as opposed
to the mean of the last generation.
Args:
num_iterations (int): the number of iterations (generations) to perform.
elite_ratio (float): the proportion of the population that will be kept as
elite (rounds up).
population_size (int): the size of the population.
lower_bound (sequence of floats): the lower bound for the optimization variables.
upper_bound (sequence of floats): the upper bound for the optimization variables.
alpha (float): momentum term.
device (torch.device): device where computations will be performed.
return_mean_elites (bool): if ``True`` returns the mean of the elites of the last
iteration. Otherwise, it returns the max solution found over all iterations.
clipped_normal (bool); if ``True`` samples are drawn from a normal distribution
and clipped to the bounds. If ``False``, sampling uses a truncated normal
distribution up to the bounds. Defaults to ``False``.
[1] R. Rubinstein and W. Davidson. "The cross-entropy method for combinatorial and continuous
optimization". Methodology and Computing in Applied Probability, 1999.
"""
def __init__(
self,
num_iterations: int,
elite_ratio: float,
population_size: int,
lower_bound: Sequence[Sequence[float]],
upper_bound: Sequence[Sequence[float]],
alpha: float,
device: torch.device,
return_mean_elites: bool = False,
clipped_normal: bool = False,
):
super().__init__()
self.num_iterations = num_iterations
self.elite_ratio = elite_ratio
self.population_size = population_size
self.elite_num = np.ceil(self.population_size * self.elite_ratio).astype(
np.int32
)
self.lower_bound = torch.tensor(lower_bound, device=device, dtype=torch.float32)
self.upper_bound = torch.tensor(upper_bound, device=device, dtype=torch.float32)
self.alpha = alpha
self.return_mean_elites = return_mean_elites
self.device = device
self._clipped_normal = clipped_normal
def _init_population_params(
self, x0: torch.Tensor
) -> Tuple[torch.Tensor, torch.Tensor]:
mean = x0.clone()
if self._clipped_normal:
dispersion = torch.ones_like(mean)
else:
dispersion = ((self.upper_bound - self.lower_bound) ** 2) / 16
return mean, dispersion
def _sample_population(
self, mean: torch.Tensor, dispersion: torch.Tensor, population: torch.Tensor
) -> torch.Tensor:
# fills population with random samples
# for truncated normal, dispersion should be the variance
# for clipped normal, dispersion should be the standard deviation
if self._clipped_normal:
pop = mean + dispersion * torch.randn_like(population)
pop = torch.where(pop > self.lower_bound, pop, self.lower_bound)
population = torch.where(pop < self.upper_bound, pop, self.upper_bound)
return population
else:
lb_dist = mean - self.lower_bound
ub_dist = self.upper_bound - mean
mv = torch.min(torch.square(lb_dist / 2), torch.square(ub_dist / 2))
constrained_var = torch.min(mv, dispersion)
population = mbrl.util.math.truncated_normal_(population)
return population * torch.sqrt(constrained_var) + mean
def _update_population_params(
self, elite: torch.Tensor, mu: torch.Tensor, dispersion: torch.Tensor
) -> Tuple[torch.Tensor, torch.Tensor]:
new_mu = torch.mean(elite, dim=0)
if self._clipped_normal:
new_dispersion = torch.std(elite, dim=0)
else:
new_dispersion = torch.var(elite, dim=0)
mu = self.alpha * mu + (1 - self.alpha) * new_mu
dispersion = self.alpha * dispersion + (1 - self.alpha) * new_dispersion
return mu, dispersion
def optimize(
self,
obj_fun: Callable[[torch.Tensor], torch.Tensor],
x0: Optional[torch.Tensor] = None,
callback: Optional[Callable[[torch.Tensor, torch.Tensor, int], None]] = None,
**kwargs,
) -> torch.Tensor:
"""Runs the optimization using CEM.
Args:
obj_fun (callable(tensor) -> tensor): objective function to maximize.
x0 (tensor, optional): initial mean for the population. Must
be consistent with lower/upper bounds.
callback (callable(tensor, tensor, int) -> any, optional): if given, this
function will be called after every iteration, passing it as input the full
population tensor, its corresponding objective function values, and
the index of the current iteration. This can be used for logging and plotting
purposes.
Returns:
(torch.Tensor): the best solution found.
"""
mu, dispersion = self._init_population_params(x0)
best_solution = torch.empty_like(mu)
best_value = -np.inf
population = torch.zeros((self.population_size,) + x0.shape).to(
device=self.device
)
for i in range(self.num_iterations):
population = self._sample_population(mu, dispersion, population)
values = obj_fun(population)
if callback is not None:
callback(population, values, i)
# filter out NaN values
values[values.isnan()] = -1e-10
best_values, elite_idx = values.topk(self.elite_num)
elite = population[elite_idx]
mu, dispersion = self._update_population_params(elite, mu, dispersion)
if best_values[0] > best_value:
best_value = best_values[0]
best_solution = population[elite_idx[0]].clone()
return mu if self.return_mean_elites else best_solution
class MPPIOptimizer(Optimizer):
"""Implements the Model Predictive Path Integral optimization algorithm.
A derivation of MPPI can be found at https://arxiv.org/abs/2102.09027
This version is closely related to the original TF implementation used in PDDM with
some noise sampling modifications and the addition of refinement steps.
Args:
num_iterations (int): the number of iterations (generations) to perform.
population_size (int): the size of the population.
gamma (float): reward scaling term.
sigma (float): noise scaling term used in action sampling.
beta (float): correlation term between time steps.
lower_bound (sequence of floats): the lower bound for the optimization variables.
upper_bound (sequence of floats): the upper bound for the optimization variables.
device (torch.device): device where computations will be performed.
"""
def __init__(
self,
num_iterations: int,
population_size: int,
gamma: float,
sigma: float,
beta: float,
lower_bound: Sequence[Sequence[float]],
upper_bound: Sequence[Sequence[float]],
device: torch.device,
):
super().__init__()
self.planning_horizon = len(lower_bound)
self.population_size = population_size
self.action_dimension = len(lower_bound[0])
self.mean = torch.zeros(
(self.planning_horizon, self.action_dimension),
device=device,
dtype=torch.float32,
)
self.lower_bound = torch.tensor(lower_bound, device=device, dtype=torch.float32)
self.upper_bound = torch.tensor(upper_bound, device=device, dtype=torch.float32)
self.var = sigma**2 * torch.ones_like(self.lower_bound)
self.beta = beta
self.gamma = gamma
self.refinements = num_iterations
self.device = device
def optimize(
self,
obj_fun: Callable[[torch.Tensor], torch.Tensor],
x0: Optional[torch.Tensor] = None,
callback: Optional[Callable[[torch.Tensor, torch.Tensor, int], None]] = None,
**kwargs,
) -> torch.Tensor:
"""Implementation of MPPI planner.
Args:
obj_fun (callable(tensor) -> tensor): objective function to maximize.
x0 (tensor, optional): Not required
callback (callable(tensor, tensor, int) -> any, optional): if given, this
function will be called after every iteration, passing it as input the full
population tensor, its corresponding objective function values, and
the index of the current iteration. This can be used for logging and plotting
purposes.
Returns:
(torch.Tensor): the best solution found.
"""
past_action = self.mean[0]
self.mean[:-1] = self.mean[1:].clone()
for k in range(self.refinements):
# sample noise and update constrained variances
noise = torch.empty(
size=(
self.population_size,
self.planning_horizon,
self.action_dimension,
),
device=self.device,
)
noise = mbrl.util.math.truncated_normal_(noise)
lb_dist = self.mean - self.lower_bound
ub_dist = self.upper_bound - self.mean
mv = torch.minimum(torch.square(lb_dist / 2), torch.square(ub_dist / 2))
constrained_var = torch.minimum(mv, self.var)
population = noise.clone() * torch.sqrt(constrained_var)
# smoothed actions with noise
population[:, 0, :] = (
self.beta * (self.mean[0, :] + noise[:, 0, :])
+ (1 - self.beta) * past_action
)
for i in range(max(self.planning_horizon - 1, 0)):
population[:, i + 1, :] = (
self.beta * (self.mean[i + 1] + noise[:, i + 1, :])
+ (1 - self.beta) * population[:, i, :]
)
# clipping actions
# This should still work if the bounds between dimensions are different.
population = torch.where(
population > self.upper_bound, self.upper_bound, population
)
population = torch.where(
population < self.lower_bound, self.lower_bound, population
)
values = obj_fun(population)
values[values.isnan()] = -1e-10
if callback is not None:
callback(population, values, k)
# weight actions
weights = torch.reshape(
torch.exp(self.gamma * (values - values.max())),
(self.population_size, 1, 1),
)
norm = torch.sum(weights) + 1e-10
weighted_actions = population * weights
self.mean = torch.sum(weighted_actions, dim=0) / norm
return self.mean.clone()
class ICEMOptimizer(Optimizer):
"""Implements the Improved Cross-Entropy Method (iCEM) optimization algorithm.
iCEM improves the sample efficiency over standard CEM and was introduced by
[2] for real-time planning.
Args:
num_iterations (int): the number of iterations (generations) to perform.
elite_ratio (float): the proportion of the population that will be kept as
elite (rounds up).
population_size (int): the size of the population.
population_decay_factor (float): fixed factor for exponential decrease in population size
colored_noise_exponent (float): colored-noise scaling exponent for generating correlated
action sequences.
lower_bound (sequence of floats): the lower bound for the optimization variables.
upper_bound (sequence of floats): the upper bound for the optimization variables.
keep_elite_frac (float): the fraction of elites to keep (or shift) during CEM iterations
alpha (float): momentum term.
device (torch.device): device where computations will be performed.
return_mean_elites (bool): if ``True`` returns the mean of the elites of the last
iteration. Otherwise, it returns the max solution found over all iterations.
population_size_module (int, optional): if specified, the population is rounded to be
a multiple of this number. Defaults to ``None``.
[2] C. Pinneri, S. Sawant, S. Blaes, J. Achterhold, J. Stueckler, M. Rolinek and
G, Martius, Georg. "Sample-efficient Cross-Entropy Method for Real-time Planning".
Conference on Robot Learning, 2020.
"""
def __init__(
self,
num_iterations: int,
elite_ratio: float,
population_size: int,
population_decay_factor: float,
colored_noise_exponent: float,
lower_bound: Sequence[Sequence[float]],
upper_bound: Sequence[Sequence[float]],
keep_elite_frac: float,
alpha: float,
device: torch.device,
return_mean_elites: bool = False,
population_size_module: Optional[int] = None,
):
super().__init__()
self.num_iterations = num_iterations
self.elite_ratio = elite_ratio
self.population_size = population_size
self.population_decay_factor = population_decay_factor
self.elite_num = np.ceil(self.population_size * self.elite_ratio).astype(
np.int32
)
self.colored_noise_exponent = colored_noise_exponent
self.lower_bound = torch.tensor(lower_bound, device=device, dtype=torch.float32)
self.upper_bound = torch.tensor(upper_bound, device=device, dtype=torch.float32)
self.initial_var = ((self.upper_bound - self.lower_bound) ** 2) / 16
self.keep_elite_frac = keep_elite_frac
self.keep_elite_size = np.ceil(keep_elite_frac * self.elite_num).astype(
np.int32
)
self.elite = None
self.alpha = alpha
self.return_mean_elites = return_mean_elites
self.population_size_module = population_size_module
self.device = device
if self.population_size_module:
self.keep_elite_size = self._round_up_to_module(
self.keep_elite_size, self.population_size_module
)
@staticmethod
def _round_up_to_module(value: int, module: int) -> int:
if value % module == 0:
return value
return value + (module - value % module)
def optimize(
self,
obj_fun: Callable[[torch.Tensor], torch.Tensor],
x0: Optional[torch.Tensor] = None,
callback: Optional[Callable[[torch.Tensor, torch.Tensor, int], None]] = None,
**kwargs,
) -> torch.Tensor:
"""Runs the optimization using iCEM.
Args:
obj_fun (callable(tensor) -> tensor): objective function to maximize.
x0 (tensor, optional): initial mean for the population. Must
be consistent with lower/upper bounds.
callback (callable(tensor, tensor, int) -> any, optional): if given, this
function will be called after every iteration, passing it as input the full
population tensor, its corresponding objective function values, and
the index of the current iteration. This can be used for logging and plotting
purposes.
Returns:
(torch.Tensor): the best solution found.
"""
mu = x0.clone()
var = self.initial_var.clone()
best_solution = torch.empty_like(mu)
best_value = -np.inf
for i in range(self.num_iterations):
decay_population_size = np.ceil(
np.max(
(
self.population_size * self.population_decay_factor**-i,
2 * self.elite_num,
)
)
).astype(np.int32)
if self.population_size_module:
decay_population_size = self._round_up_to_module(
decay_population_size, self.population_size_module
)
# the last dimension is used for temporal correlations
population = mbrl.util.math.powerlaw_psd_gaussian(
self.colored_noise_exponent,
size=(decay_population_size, x0.shape[1], x0.shape[0]),
device=self.device,
).transpose(1, 2)
population2 = torch.minimum(
population * torch.sqrt(var) + mu, self.upper_bound
)
population2 = torch.maximum(population2, self.lower_bound)
if self.elite is not None:
kept_elites = torch.index_select(
self.elite,
dim=0,
index=torch.randperm(self.elite_num, device=self.device)[
: self.keep_elite_size
],
)
if i == 0:
end_action = (
torch.normal(
mu[-1, :].repeat(kept_elites.shape[0], 1),
torch.sqrt(var[-1, :]).repeat(kept_elites.shape[0], 1),
)
.unsqueeze(1)
.to(self.device)
)
kept_elites_shifted = torch.cat(
(kept_elites[:, 1:, :], end_action), dim=1
)
population = torch.cat((population, kept_elites_shifted), dim=0)
elif i == self.num_iterations - 1:
population = torch.cat((population, mu.unsqueeze(dim=0)), dim=0)
else:
population = torch.cat((population, kept_elites), dim=0)
values = obj_fun(population)
if callback is not None:
callback(population, values, i)
# filter out NaN values
values[values.isnan()] = -1e-10
best_values, elite_idx = values.topk(self.elite_num)
self.elite = population[elite_idx]
new_mu = torch.mean(self.elite, dim=0)
new_var = torch.var(self.elite, unbiased=False, dim=0)
mu = self.alpha * mu + (1 - self.alpha) * new_mu
var = self.alpha * var + (1 - self.alpha) * new_var
if best_values[0] > best_value:
best_value = best_values[0]
best_solution = population[elite_idx[0]].clone()
return mu if self.return_mean_elites else best_solution
class TrajectoryOptimizer:
"""Class for using generic optimizers on trajectory optimization problems.
This is a convenience class that sets up optimization problem for trajectories, given only
action bounds and the length of the horizon. Using this class, the concern of handling
appropriate tensor shapes for the optimization problem is hidden from the users, which only
need to provide a function that is capable of evaluating trajectories of actions. It also
takes care of shifting previous solution for the next optimization call, if the user desires.
The optimization variables for the problem will have shape ``H x A``, where ``H`` and ``A``
represent planning horizon and action dimension, respectively. The initial solution for the
optimizer will be computed as (action_ub - action_lb) / 2, for each time step.
Args:
optimizer_cfg (omegaconf.DictConfig): the configuration of the optimizer to use.
action_lb (np.ndarray): the lower bound for actions.
action_ub (np.ndarray): the upper bound for actions.
planning_horizon (int): the length of the trajectories that will be optimized.
replan_freq (int): the frequency of re-planning. This is used for shifting the previous
solution for the next time step, when ``keep_last_solution == True``. Defaults to 1.
keep_last_solution (bool): if ``True``, the last solution found by a call to
:meth:`optimize` is kept as the initial solution for the next step. This solution is
shifted ``replan_freq`` time steps, and the new entries are filled using the initial
solution. Defaults to ``True``.
"""
def __init__(
self,
optimizer_cfg: omegaconf.DictConfig,
action_lb: np.ndarray,
action_ub: np.ndarray,
planning_horizon: int,
replan_freq: int = 1,
keep_last_solution: bool = True,
):
optimizer_cfg.lower_bound = np.tile(action_lb, (planning_horizon, 1)).tolist()
optimizer_cfg.upper_bound = np.tile(action_ub, (planning_horizon, 1)).tolist()
self.optimizer: Optimizer = hydra.utils.instantiate(optimizer_cfg)
self.initial_solution = (
((torch.tensor(action_lb) + torch.tensor(action_ub)) / 2)
.float()
.to(optimizer_cfg.device)
)
self.initial_solution = self.initial_solution.repeat((planning_horizon, 1))
self.previous_solution = self.initial_solution.clone()
self.replan_freq = replan_freq
self.keep_last_solution = keep_last_solution
self.horizon = planning_horizon
def optimize(
self,
trajectory_eval_fn: Callable[[torch.Tensor], torch.Tensor],
callback: Optional[Callable] = None,
) -> np.ndarray:
"""Runs the trajectory optimization.
Args:
trajectory_eval_fn (callable(tensor) -> tensor): A function that receives a batch
of action sequences and returns a batch of objective function values (e.g.,
accumulated reward for each sequence). The shape of the action sequence tensor
will be ``B x H x A``, where ``B``, ``H``, and ``A`` represent batch size,
planning horizon, and action dimension, respectively.
callback (callable, optional): a callback function
to pass to the optimizer.
Returns:
(tuple of np.ndarray and float): the best action sequence.
"""
best_solution = self.optimizer.optimize(
trajectory_eval_fn,
x0=self.previous_solution,
callback=callback,
)
if self.keep_last_solution:
self.previous_solution = best_solution.roll(-self.replan_freq, dims=0)
# Note that initial_solution[i] is the same for all values of [i],
# so just pick i = 0
self.previous_solution[-self.replan_freq :] = self.initial_solution[0]
return best_solution.cpu().numpy()
def reset(self):
"""Resets the previous solution cache to the initial solution."""
self.previous_solution = self.initial_solution.clone()
class TrajectoryOptimizerAgent(Agent):
"""Agent that performs trajectory optimization on a given objective function for each action.
This class uses an internal :class:`TrajectoryOptimizer` object to generate
sequence of actions, given a user-defined trajectory optimization function.
Args:
optimizer_cfg (omegaconf.DictConfig): the configuration of the base optimizer to pass to
the trajectory optimizer.
action_lb (sequence of floats): the lower bound of the action space.
action_ub (sequence of floats): the upper bound of the action space.
planning_horizon (int): the length of action sequences to evaluate. Defaults to 1.
replan_freq (int): the frequency of re-planning. The agent will keep a cache of the
generated sequences an use it for ``replan_freq`` number of :meth:`act` calls.
Defaults to 1.
verbose (bool): if ``True``, prints the planning time on the console.
keep_last_solution (bool): if ``True``, the last solution found by a call to
:meth:`optimize` is kept as the initial solution for the next step. This solution is
shifted ``replan_freq`` time steps, and the new entries are filled using the initial
solution. Defaults to ``True``.
Note:
After constructing an agent of this type, the user must call
:meth:`set_trajectory_eval_fn`. This is not passed to the constructor so that the agent can
be automatically instantiated with Hydra (which in turn makes it easy to replace this
agent with an agent of another type via config-only changes).
"""
def __init__(
self,
optimizer_cfg: omegaconf.DictConfig,
action_lb: Sequence[float],
action_ub: Sequence[float],
planning_horizon: int = 1,
replan_freq: int = 1,
verbose: bool = False,
keep_last_solution: bool = True,
):
self.optimizer = TrajectoryOptimizer(
optimizer_cfg,
np.array(action_lb),
np.array(action_ub),
planning_horizon=planning_horizon,
replan_freq=replan_freq,
keep_last_solution=keep_last_solution,
)
self.optimizer_args = {
"optimizer_cfg": optimizer_cfg,
"action_lb": np.array(action_lb),
"action_ub": np.array(action_ub),
}
self.trajectory_eval_fn: mbrl.types.TrajectoryEvalFnType = None
self.actions_to_use: List[np.ndarray] = []
self.replan_freq = replan_freq
self.verbose = verbose
def set_trajectory_eval_fn(
self, trajectory_eval_fn: mbrl.types.TrajectoryEvalFnType
):
"""Sets the trajectory evaluation function.
Args:
trajectory_eval_fn (callable): a trajectory evaluation function, as described in
:class:`TrajectoryOptimizer`.
"""
self.trajectory_eval_fn = trajectory_eval_fn
def reset(self, planning_horizon: Optional[int] = None):
"""Resets the underlying trajectory optimizer."""
if planning_horizon:
self.optimizer = TrajectoryOptimizer(
cast(omegaconf.DictConfig, self.optimizer_args["optimizer_cfg"]),
cast(np.ndarray, self.optimizer_args["action_lb"]),
cast(np.ndarray, self.optimizer_args["action_ub"]),
planning_horizon=planning_horizon,
replan_freq=self.replan_freq,
)
self.optimizer.reset()
def act(
self, obs: np.ndarray, optimizer_callback: Optional[Callable] = None, **_kwargs
) -> np.ndarray:
"""Issues an action given an observation.
This method optimizes a full sequence of length ``self.planning_horizon`` and returns
the first action in the sequence. If ``self.replan_freq > 1``, future calls will use
subsequent actions in the sequence, for ``self.replan_freq`` number of steps.
After that, the method will plan again, and repeat this process.
Args:
obs (np.ndarray): the observation for which the action is needed.
optimizer_callback (callable, optional): a callback function
to pass to the optimizer.
Returns:
(np.ndarray): the action.
"""
if self.trajectory_eval_fn is None:
raise RuntimeError(
"Please call `set_trajectory_eval_fn()` before using TrajectoryOptimizerAgent"
)
plan_time = 0.0
if not self.actions_to_use: # re-plan is necessary
def trajectory_eval_fn(action_sequences):
return self.trajectory_eval_fn(obs, action_sequences)
start_time = time.time()
plan = self.optimizer.optimize(
trajectory_eval_fn, callback=optimizer_callback
)
plan_time = time.time() - start_time
self.actions_to_use.extend([a for a in plan[: self.replan_freq]])
action = self.actions_to_use.pop(0)
if self.verbose:
print(f"Planning time: {plan_time:.3f}")
return action
def plan(self, obs: np.ndarray, **_kwargs) -> np.ndarray:
"""Issues a sequence of actions given an observation.
Returns s sequence of length self.planning_horizon.
Args:
obs (np.ndarray): the observation for which the sequence is needed.
Returns:
(np.ndarray): a sequence of actions.
"""
if self.trajectory_eval_fn is None:
raise RuntimeError(
"Please call `set_trajectory_eval_fn()` before using TrajectoryOptimizerAgent"
)
def trajectory_eval_fn(action_sequences):
return self.trajectory_eval_fn(obs, action_sequences)
plan = self.optimizer.optimize(trajectory_eval_fn)
return plan
def create_trajectory_optim_agent_for_model(
model_env: mbrl.models.ModelEnv,
agent_cfg: omegaconf.DictConfig,
num_particles: int = 1,
) -> TrajectoryOptimizerAgent:
"""Utility function for creating a trajectory optimizer agent for a model environment.
This is a convenience function for creating a :class:`TrajectoryOptimizerAgent`,
using :meth:`mbrl.models.ModelEnv.evaluate_action_sequences` as its objective function.
Args:
model_env (mbrl.models.ModelEnv): the model environment.
agent_cfg (omegaconf.DictConfig): the agent's configuration.
num_particles (int): the number of particles for taking averages of action sequences'
total rewards.
Returns:
(:class:`TrajectoryOptimizerAgent`): the agent.
"""
complete_agent_cfg(model_env, agent_cfg)
agent = hydra.utils.instantiate(agent_cfg)
def trajectory_eval_fn(initial_state, action_sequences):
return model_env.evaluate_action_sequences(
action_sequences, initial_state=initial_state, num_particles=num_particles
)
agent.set_trajectory_eval_fn(trajectory_eval_fn)
return agent