-
Notifications
You must be signed in to change notification settings - Fork 0
/
trajax_refsol.py
225 lines (160 loc) · 6.92 KB
/
trajax_refsol.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
import functools
import gzip
import os
import pprint
import subprocess
import sys
import time
from operator import itemgetter
import diffrax
import equinox
import flax
import ipdb
import jax
import jax.numpy as np
import matplotlib
import matplotlib.pyplot as pl
import meshcat
import meshcat.geometry as geom
import meshcat.transformations as tf
import numpy as onp
import tqdm
import nn_utils
import plotting_utils
import pontryagin_utils
import visualiser
import wandb
from misc import *
import trajax
def refsol_homotopy(xs, sol0, v_nn, nn_params, problem_params, algo_params, dt=0.01, N=500):
# both of the other in one so we can loop efficiently with jax.lax.scan
# convert problem description {{{
K_lqr, P_lqr = pontryagin_utils.get_terminal_lqr(problem_params)
x_eq = problem_params['x_eq']
V_f = lambda x: 0.5 * (x - x_eq).T @ P_lqr @ (x - x_eq)
u_lqr_fct = lambda x: -K_lqr @ (x - problem_params['x_eq']) + problem_params['u_eq']
# discrete time dynamics & cost function
dynamics_cont = lambda x, u, t: problem_params['f'](x, u)
def dynamics_disc(x, u, k):
xn = trajax.integrators.rk4(dynamics_cont, dt=dt)(x, u, k)
return problem_params['project_M'](xn)
def cost_disc(x, u, k):
# implementation like https://github.com/google/trajax/blob/main/tests/optimizers_test.py#L495
stage_cost = problem_params['l'](x, u) * dt
terminal_cost = V_f(x) # add input cost too???
return np.where(k == N, terminal_cost, stage_cost)
# x0 = np.array([-1., 0., 0, 1., 5., 0, 0.])
# U0 = np.ones((N, problem_params['nu'])) * problem_params['u_eq'][0]
u_lower, u_upper = problem_params['U_interval']
# inspired by https://github.com/google/trajax/blob/main/tests/optimizers_test.py#L713
def control_constraint(x, u, k):
# functions that should be <= 0.
control_limits = np.concatenate([u_lower - u, u - u_upper])
return np.where(k == N, 0 * control_limits, control_limits)
# }}}
# get initial guess {{{
# simulate w continuous time approximate optimal control from V approx,
# but already with time discretisation & integration exactly matching
# trajax ilqr.
def v_mean(x, vmap_params):
# find (empirical) mean and std. dev of value function.
vs_ensemble = jax.vmap(v_nn, in_axes=(0, None))(vmap_params, x)
return vs_ensemble.mean()
vx_mean = jax.jacobian(v_mean, argnums=0)
def controller(x):
vx = vx_mean(x, nn_params)
return pontryagin_utils.u_star_general(x, vx, problem_params)
def body_rollout(carry, inp):
x = carry
u = controller(x)
next_x = dynamics_disc(x, u, 0)
carry = next_x
oup = (x, u)
return carry, oup
_, (X0, U0) = jax.lax.scan(body_rollout, xs[0], None, length=N)
# }}}
# define computation w scan {{{
def body(U, x0):
X, U, dual_equality, dual_inequality, penalty, equality_constraints, inequality_constraints, max_constraint_violation, obj, gradient, iteration_ilqr, iteration_al = trajax.optimizers.constrained_ilqr(
cost_disc, dynamics_disc,
x0, U,
inequality_constraint=control_constraint
)
return U, (X, obj)
# }}}
# do it :)
last_U, (Xs, objs) = jax.lax.scan(body, U0, xs)
return Xs, objs
def refsol(sol0, v_nn, nn_params, problem_params, algo_params, dt=0.05, N=100, plot=False):
# wrapper for trajax ilqr optimiser.
# - get initial guess from continuous-time solution (as given by diffrax) (not yet)
# - call other, lower level wrapper.
assert dt*N <= sol0.t1 - sol0.t0, 'solution too short to initialise'
# 1. get initial guess U {{{
def v_mean(x, vmap_params):
# find (empirical) mean and std. dev of value function.
vs_ensemble = jax.vmap(v_nn, in_axes=(0, None))(vmap_params, x)
return vs_ensemble.mean()
vx_mean = jax.jacobian(v_mean, argnums=0)
ts = np.arange(N) * dt
sol_ys = jax.vmap(sol0.evaluate)(ts)
sol_xs = sol_ys['x']
sol_vxs = jax.vmap(vx_mean, in_axes=(0, None))(sol_xs, nn_params)
# def u_star_general(x, costate, problem_params):
sol_us = jax.vmap(pontryagin_utils.u_star_general, in_axes=(0, 0, None))(
sol_xs, sol_vxs, problem_params
)
x0 = sol_xs[0]
return refsol_from_us(x0, sol_us, problem_params, algo_params, dt=dt, N=N, plot=plot)
def refsol_from_us(x0, U0, problem_params, algo_params, dt=0.05, N=100, plot=False):
# - discretise cost&dynamics
# - solve problem with constrained_ilqr
# - return only the objective.
# U0 an array of initial inputs, (N_t, nu)
# define & solve (discrete time) problem {{{
K_lqr, P_lqr = pontryagin_utils.get_terminal_lqr(problem_params)
x_eq = problem_params['x_eq']
V_f = lambda x: 0.5 * (x - x_eq).T @ P_lqr @ (x - x_eq)
u_lqr_fct = lambda x: -K_lqr @ (x - problem_params['x_eq']) + problem_params['u_eq']
# discrete time dynamics & cost function
dynamics_cont = lambda x, u, t: problem_params['f'](x, u)
def dynamics_disc(x, u, k):
xn = trajax.integrators.rk4(dynamics_cont, dt=dt)(x, u, k)
return problem_params['project_M'](xn)
def cost_disc(x, u, k):
# implementation like https://github.com/google/trajax/blob/main/tests/optimizers_test.py#L495
stage_cost = problem_params['l'](x, u) * dt
terminal_cost = V_f(x) # add input cost too???
return np.where(k == N, terminal_cost, stage_cost)
# x0 = np.array([-1., 0., 0, 1., 5., 0, 0.])
# U0 = np.ones((N, problem_params['nu'])) * problem_params['u_eq'][0]
u_lower, u_upper = problem_params['U_interval']
# inspired by https://github.com/google/trajax/blob/main/tests/optimizers_test.py#L713
def control_constraint(x, u, k):
# functions that should be <= 0.
control_limits = np.concatenate([u_lower - u, u - u_upper])
return np.where(k == N, 0 * control_limits, control_limits)
# cannot decide if trajax is beautiful or utterly deranged
X, U, dual_equality, dual_inequality, penalty, equality_constraints, inequality_constraints, max_constraint_violation, obj, gradient, iteration_ilqr, iteration_al = trajax.optimizers.constrained_ilqr(
cost_disc, dynamics_disc,
x0, U0,
inequality_constraint=control_constraint
)
# }}}
# basic unconstrained ilqr
# X, U, obj, gradient, adjoints, lqr, iteration = trajax.optimizers.ilqr(
# cost_disc, dynamics_disc,
# x0, U0,
# )
if plot:
pl.subplot(211)
ts = np.arange(N+1) * dt
pl.plot(ts, X, '.-', label=problem_params['state_names'])
pl.subplot(212)
ts = np.arange(N) * dt
pl.plot(ts, U, '.-', label=('u1', 'u2'))
ys = jax.vmap(lambda x: np.concatenate([x[0:2], np.array([np.arctan2(x[2], x[3])]), x[4:]]))(X)
visualiser.plot_trajectories_meshcat({'t': ts, 'x': ys})
pl.show()
# ipdb.set_trace()
return obj, U