Repository navigation
Expand file tree
/
Copy pathalgorithm.py
More file actions
366 lines (308 loc) · 15.3 KB
/
Copy pathalgorithm.py
File metadata and controls
366 lines (308 loc) · 15.3 KB
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
import jax
import jax.numpy as jnp
from functools import partial
from jaxtyping import PyTree, PRNGKeyArray
from typing import NamedTuple, Callable
from dataclasses import is_dataclass, fields
from hydrax.task_base import Task
from mujoco import mjx
from .interpolate import hermite_interp1d, Jet1, Jet2, Jet3
from .update import (
ess, dial_sigma, _check_update_shapes,
ps_update, sample_update, mppi_update, cem_update, dial_step, dial_update,
)
def get_field_info(x):
"""
Return:
- field_names: tuple of keys
- accessor(obj, key) -> leaf array (indexable by [tau, idx] for arrays with (horizon,particles,...))
- builder(vals) -> reconstructs the same container type as `x`
"""
# dataclass
if is_dataclass(x):
names = tuple(f.name for f in fields(x))
accessor = lambda obj, n: getattr(obj, n)
builder = lambda vals: type(x)(**{n: v for n, v in zip(names, vals)})
return names, accessor, builder
# namedtuple
if hasattr(x, "_fields") and isinstance(x._fields, tuple):
names = x._fields
accessor = lambda obj, n: getattr(obj, n)
# namedtuple accepts kwargs with field names
builder = lambda vals: type(x)(**{n: v for n, v in zip(names, vals)})
return names, accessor, builder
# jax.Array -> treat as ONE field: the whole array (horizon, particles, ...)
if jax.Array and isinstance(x, jax.Array):
names = ("__array__",)
accessor = lambda obj, _k: obj # shape (horizon, particles, ...)
builder = lambda vals: vals[0] # just return the array leaf
return names, accessor, builder
# plain tuple
if isinstance(x, tuple):
names = tuple(range(len(x)))
accessor = lambda obj, i: obj[i]
builder = lambda vals: tuple(vals)
return names, accessor, builder
raise TypeError(f"Unsupported state type: {type(x)}")
# ---------------------------------------------------------------------------
# The shared perturbation proposal
#
# Every baseline (PS/MPPI/CEM/DIAL) samples the same way: Gaussian noise around
# the nominal plan `theta.mean`, at a width read per-knot.
#
# The perturbation accepts action clips (joint limits).
#
# `theta` types using the shared perturbation require:
# (1) `mean` (K, *action),
# (2) `stdev` (scalar, or (K, *action)), and
# (3) `step_len` (scalar)
# ---------------------------------------------------------------------------
def sigma_at(theta: PyTree, idx: int) -> float:
"""Sampling noise for knot `idx`.
Handles both conventions: a scalar `stdev` (PS/MPPI/SIS/SIR, and DIAL, which
overwrites it per annealing level) and a per-knot array (CEM, which refits it
from the elites each replan). `jnp.ndim` is static, so this is free under jit.
"""
return theta.stdev if jnp.ndim(theta.stdev) == 0 else theta.stdev[idx]
def knot_index(theta: PyTree, t: float) -> int:
"""Knot whose interval contains time `t`."""
idx = jnp.floor(jnp.asarray(t) / theta.step_len).astype(jnp.int32) + 1
return jnp.clip(idx, 0, theta.mean.shape[0] - 1)
def make_perturbation(clip=None):
"""Build the (init, proposal) pair for a task, given its action clip."""
clip = clip or (lambda u, theta: u)
def at(key, theta, idx):
u = theta.mean[idx] + jax.random.normal(
key, theta.mean.shape[1:]) * sigma_at(theta, idx)
return clip(u, theta)
return (lambda key, x, theta: at(key, theta, 0),
lambda key, x, u, theta, t: at(key, theta, knot_index(theta, t)))
# Mujoco XLA helper functions
def mjx_hydrax_cost(task: Task):
running_cost = lambda x, j, th, _t: task.running_cost(x, j) * 0.01
terminal_cost = lambda x, th: task.terminal_cost(x)
return running_cost, terminal_cost
def mjx_linear_gaussian_proposal(task: Task, horizon: float, num_knots: int):
"""Legacy proposal, numerically identical to make_perturbation,
but closes the
"""
def init(key, x, th):
u = jax.random.normal(key, th.mjx_model.nu) * th.noise_level + th.mean[0]
u = jnp.clip(u, task.u_min, task.u_max)
return u
# Linear Proposal
def proposal(key, x: mjx.Data, u: jax.Array, th, t: float) -> jax.Array:
step_len = horizon / (num_knots - 1)
idx = (t / step_len).astype(int) + 1
u = jax.random.normal(key, th.mjx_model.nu) * th.noise_level + th.mean[idx]
u = jnp.clip(u, task.u_min, task.u_max)
return u
return init, proposal
def mjx_physics(x, j, th, _t):
if j.shape == ():
x = mjx.step(th.mjx_model, x.replace(ctrl=jnp.array([j])))
else:
x = mjx.step(th.mjx_model, x.replace(ctrl=j))
return x, None
class GenericMJXTaskParams(NamedTuple):
mjx_model: mjx.Model
mean: jax.Array
noise_level: float
actor_params: any = None
class PFMP(NamedTuple):
""" Particle Filtering Motion Planning (PFMP) """
mpc_dynamics: Callable[[PyTree, jax.Array, PyTree, float], tuple[PyTree, jax.Array]]
mpc_running_cost: Callable[[PyTree, jax.Array, PyTree, float], float]
mpc_terminal_cost: Callable[[PyTree, PyTree], float] # should be deterministic
mpc_init: Callable[[PRNGKeyArray, PyTree, PyTree], jax.Array]
mpc_proposal: Callable[[PRNGKeyArray, PyTree, jax.Array, PyTree, float], jax.Array]
horizon: float
num_knots: int
particles: int
ess_resample_thresh: float = 0.5
dt: float = 0.01
interp: str = "hermite" # "zoh" | "linear" | "hermite"
def tk(self, endpoint: bool = True):
if endpoint:
return jnp.linspace(0., self.horizon, self.num_knots)
else:
return jnp.linspace(0., self.horizon, self.num_knots)[:-1]
def _interpolate(self, x0: PyTree, u0: jax.Array, u1: jax.Array,
t0: float, theta: PyTree, return_traj: bool = False):
"""Integrate the dynamics over one knot interval [t0, t0+step_len], with
the control spline-interpolated from u0 to u1. Returns (x_end, cost), or
(x_end, cost, xs) with the per-substep states when `return_traj`. Shared
by `transition_model` (one proposed interval, endpoint only) and `predict`
(a fixed sequence, dense)."""
step_len = self.horizon / (self.num_knots - 1)
steps_per_knot = int(step_len / self.dt)
t1 = t0 + step_len
# Note: interp beyond linear is currently unused,
# was used in early experiments before we realized
# linear is generally more stable for inference.
if isinstance(u0, (Jet1, jax.Array)):
nout = 1
elif isinstance(u0, Jet2):
nout = 2
elif isinstance(u0, Jet3):
nout = 3
if self.interp == "zoh":
tq = jnp.linspace(t0, t1, steps_per_knot + 1)
us = jax.tree.map(
lambda v: jnp.broadcast_to(v, (steps_per_knot + 1,) + jnp.shape(v)), u1)
else:
tq, us = hermite_interp1d(t0, t1, u0, u1, num=steps_per_knot + 1, nout=nout)
def interpolate_step(carry, inps):
x, c = carry
u, s = inps
(x_next, _y) = self.mpc_dynamics(x, u, theta, s)
c_next = self.mpc_running_cost(x_next, u, theta, s)
return (x_next, c + c_next), (x_next if return_traj else None)
(x1, cost), xs = jax.lax.scan(
interpolate_step, (x0, 0.0), (jax.tree.map(lambda u: u[:-1], us), tq[:-1]))
return (x1, cost, xs) if return_traj else (x1, cost)
@partial(jax.jit, static_argnames=['self'])
def transition_model(self, key, x0: PyTree, u0: jax.Array, theta: PyTree, t0: float) -> PyTree:
# Propose a new spline knot, then integrate the interval that reaches it.
u1 = self.mpc_proposal(key, x0, u0, theta, t0)
x1, cost = self._interpolate(x0, u0, u1, t0, theta)
return (x1, u1), (u1, cost)
@partial(jax.jit, static_argnames=['self'])
def predict(self, x0: PyTree, us: jax.Array, theta: PyTree) -> PyTree:
"""Deterministically roll a fixed knot sequence through the dynamics.
Unlike `transition_model`/`unroll`, this proposes nothing and samples
nothing: it is the forward model of an already-chosen plan (e.g.
`theta.mean`). Returns the *dense* trajectory. Every integration substep,
starting at x0. The leaves have shape (num_intervals*steps_per_knot + 1, ...).
Use for scoring, analysis, or visualising what a plan is predicted to do.
us: (num_knots, action_dim) control knots.
"""
def interval_step(x, inp):
u0, u1, t0 = inp
x_end, _cost, xs = self._interpolate(x, u0, u1, t0, theta, return_traj=True)
return x_end, xs # xs: (steps_per_knot, ...) states within the interval
_, seg = jax.lax.scan(
interval_step, x0, (us[:-1], us[1:], self.tk(endpoint=False)))
# (num_intervals, steps_per_knot, ...) -> (num_intervals*steps_per_knot, ...).
# Merge the two leading axes explicitly: an inferred -1 divides by zero on
# mjx.Data leaves that have a zero-sized dim (e.g. empty contact buffers).
dense = jax.tree.map(lambda a: a.reshape((a.shape[0] * a.shape[1],) + a.shape[2:]), seg)
return jax.tree.map(lambda a, b: jnp.concatenate([a[None], b], axis=0), x0, dense)
@partial(jax.jit, static_argnames=['self'])
def unroll(
self,
key: PRNGKeyArray,
x0: PyTree,
theta: PyTree,
) -> tuple[jax.Array, jax.Array, PyTree]:
"""Training primitive. Returns (us, costs, xs) per particle per knot.
us: (particles, num_knots, action_dim)
costs: (particles, num_knots)
xs: per-knot states, shape (particles, num_knots-1, ...) — state at end of each interval
"""
key1, key2 = jax.random.split(key)
keys1 = jax.random.split(key1, (self.particles,))
u0s = jax.vmap(self.mpc_init, in_axes=(0, None, None))(keys1, x0, theta)
keys2 = jax.random.split(key2, (self.particles, self.num_knots - 1))
def scan_one(particle_keys, u0):
def step(carry, inp):
(x1, u1), (u, cost) = self.transition_model(inp[0], carry[0], carry[1], theta, inp[1])
return (x1, u1), (u, cost, x1)
(final_x, _), (us, costs, xs) = jax.lax.scan(
step, (x0, u0), (particle_keys, self.tk(endpoint=False))
)
return final_x, us, costs, xs
final_xs, us, costs, xs = jax.vmap(scan_one)(keys2, u0s)
final_costs = jax.vmap(self.mpc_terminal_cost, in_axes=(0, None))(final_xs, theta)
us = jax.tree.map(lambda head, tail: jnp.concat([head[:, None], tail], axis=1), u0s, us)
costs = jnp.concatenate((costs, final_costs[:, None]), axis=-1)
# xs: (particles, num_knots-1, ...) — state at end of each knot interval
return us, costs, xs
@partial(jax.jit, static_argnames=['self'])
def run_sis(
self,
key,
x0: PyTree,
theta: PyTree
) -> tuple[jax.Array, jax.Array]:
# SIS is `unroll` without the per-knot physical states.
trajs, costs, _xs = self.unroll(key, x0, theta)
return trajs, costs
@partial(jax.jit, static_argnames=["self"])
def _sir(
self,
key: PRNGKeyArray,
x0: PyTree, theta: PyTree
) -> tuple[PyTree[jax.Array], jax.Array]:
key1, key2 = jax.random.split(key)
keys = jax.random.split(key1, (self.particles,))
num_intervals = self.num_knots - 1
step_keys = jax.random.split(key2, num_intervals * self.particles).reshape(num_intervals, self.particles)
resample_keys = jax.random.split(key2, num_intervals)
x0s = jax.tree.map(lambda x: jnp.repeat(x[None], self.particles, axis=0), x0)
u0s = jax.vmap(self.mpc_init, in_axes=(0, None, None))(keys, x0, theta)
def step(carry, inp):
log_ws, xs, us = carry
step_keys, resample_key, t = inp
(new_xs, new_us), (_, costs) = jax.vmap(
lambda k, x, u: self.transition_model(k, x, u, theta, t))(step_keys, xs, us)
# Reweight, and resample only when the effective sample size falls below the threshold.
log_ws = log_ws - costs
w = jnp.exp(log_ws - jnp.max(log_ws))
ps = w / jnp.sum(w)
should_resample = 1 / jnp.sum(ps ** 2) < self.ess_resample_thresh * self.particles
def do_resample(_):
parents = jax.random.choice(resample_key, self.particles, shape=(self.particles,), p=ps, replace=True)
return parents, jnp.zeros(self.particles)
def dont_resample(_):
return jnp.arange(self.particles), log_ws
parents, log_ws = jax.lax.cond(should_resample, do_resample, dont_resample, operand=None)
new_xs = jax.tree.map(lambda a: a[parents], new_xs)
new_us = jax.tree.map(lambda a: a[parents], new_us)
return (log_ws, new_xs, new_us), (new_us, parents, costs)
(_, final_xs, _), (us, ancestors, costs) = jax.lax.scan(
step, (jnp.zeros(self.particles), x0s, u0s),
(step_keys, resample_keys, self.tk(endpoint=False)))
states = jax.tree.map(lambda u0, u: jnp.concat([u0[None], u], axis=0), u0s, us)
final_costs = jax.vmap(self.mpc_terminal_cost, in_axes=(0, None))(final_xs, theta)
costs = jnp.concatenate((costs.T, final_costs[:, None]), axis=-1)
return states, ancestors, costs, final_xs
@partial(jax.jit, static_argnames=['self'])
def run_sir(
self,
key: PRNGKeyArray,
x0: PyTree, theta: PyTree
) -> tuple[PyTree[jax.Array], jax.Array]:
states, ancestors, costs, _ = self._sir(key, x0, theta)
trajs = self.choose_paths(states, ancestors, jnp.arange(self.particles))
return trajs, costs
@staticmethod
@jax.jit
def choose_path(states, ancestors, index):
final_idx = ancestors.shape[0]
# names + accessor + builder from the *instance*
field_names, accessor, builder = get_field_info(states)
def body(idx, tau):
vals = [accessor(states, name)[tau, idx] for name in field_names]
state = builder(vals) # rebuild in the original container type
return ancestors[tau-1, idx], state
back_indices = jnp.arange(final_idx, -1, -1)
_, traj_back = jax.lax.scan(body, index, back_indices)
return jax.tree.map(lambda x: x[::-1], traj_back)
@staticmethod
@jax.jit
def choose_paths(
states: PyTree[jax.Array], # (num_knots,particles,...)
ancestors: jax.Array, # (num_knots,particles)
indices: jax.Array # (N,) where self.particles >= N
) -> PyTree[jax.Array]: # (N,horizon,...)
"""
Trace back the trajectories for N self.particles among self.particles, over a finite horizon.
Args:
states: PyTree with leaves of shape (horizon, self.particles, ...) representing the full simulation state at each time step.
ancestors: (horizon, self.particles) int32 array specifying indices for `states` that were resampled.
indices: jax.Array, array of indices for the final N particles at time horizon.
Returns:
NamedTuple: containing fields with shape (N, horizon, ...), representing N reconstructed trajectories.
"""
return jax.vmap(PFMP.choose_path, in_axes=(None, None, 0))(states, ancestors, indices)