-
Notifications
You must be signed in to change notification settings - Fork 4
/
casados_integrator.py
517 lines (410 loc) · 16 KB
/
casados_integrator.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
# Copyright Jonathan Frey, Jochem De Schutter, Moritz Diehl
# The 2-Clause BSD License
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
from casadi import Callback, Sparsity, Function, CasadiMeta
import casadi
from acados_template import AcadosSimSolver, AcadosSim, casadi_length
import numpy as np
def check_casadi_version():
casadi_version = CasadiMeta.version()
major, minor, patch = casadi_version.split(".")
if int(major) < 3:
raise Exception(
f"casadi version {casadi_version} is too old. Need at least 3.0.0"
)
elif int(major) == 3:
if int(minor) < 6:
raise Exception(
f"This version of CasadosIntegrator supports CasADi version >= 3.6.0. CasADi version {casadi_version} was found. Please look for an older version of CasadosIntegrator at https://github.com/FreyJo/casados-integrators or upgrade your CasADi version."
)
else:
print(f"Warning: This version of CasadosIntegrator supports CasADi version >= 3.6.0. CasADi version {casadi_version} was found and is not tested.")
class CasadosIntegrator(Callback):
"""
This class is a wrapper of the acados integrator (AcadosSimSolver) into a CasADi Callback.
It offers:
- first order forward sensitivities (via get_jacobian())
- first order adjoint sensitivities (via get_reverse())
- second order sensitivities (hessians) with adjoint seed (via get_reverse() + get_jacobian()) (for acados integrators that offer second order senitivities)
This makes it fully functional within CasADi NLPs
"""
def __init__(self, acados_sim: AcadosSim, use_cython=True, code_reuse=False):
check_casadi_version()
if use_cython:
json_file = f"acados_sim_{acados_sim.model.name}.json"
if not code_reuse:
AcadosSimSolver.generate(acados_sim, json_file=json_file)
AcadosSimSolver.build(
acados_sim.code_export_directory, with_cython=True
)
self.acados_integrator = AcadosSimSolver.create_cython_solver(json_file)
else:
self.acados_integrator = AcadosSimSolver(acados_sim)
self.nx = casadi_length(acados_sim.model.x)
self.nu = casadi_length(acados_sim.model.u)
self.model_name = acados_sim.model.name
self.print_level = 0
# self.print_level = 1
self.x0 = None
self.u0 = None
# needed to keep the callback alive
self.jac_callback = None
self.adj_callback = None
self.hess_callback = None
self.reset_timings()
Callback.__init__(self)
self.construct("CasadosIntegrator")
def set_z_guess(self, z0):
'''
Set initial guess of the algebraic variables
Parameters
----------
z0 : np.ndarray
value of the algebraic variables guess
'''
# set the value in the integrator to be used in the first call
self.acados_integrator.set("z", z0)
def set_xdot_guess(self, xdot0):
'''
Set initial guess of xdot
Parameters
----------
xdot0 : np.ndarray
value of xdot guess
'''
# set the value in the integrator to be used in the first call
self.acados_integrator.set("xdot", xdot0)
def get_sparsity_in(self, i):
if i == 0:
out = Sparsity.dense(self.nx)
elif i == 1:
out = Sparsity.dense(self.nu)
return out
def get_sparsity_out(self, i):
out = Sparsity.dense(self.nx)
return out
def get_name_in(self, i):
if i == 0:
out = "x0"
elif i == 1:
out = "p"
return out
def get_n_in(self):
return 2
def get_n_out(self):
return 1
def get_name_out(self, i):
return "xf"
def eval(self, arg):
# extract inputs
x0 = np.array(arg[0])
u0 = np.array(arg[1])
if self.print_level:
print(f"CasadosIntegrator: x0 {x0} u0 {u0}")
self.acados_integrator.options_set("sens_forw", False)
self.acados_integrator.options_set("sens_adj", False)
self.acados_integrator.options_set("sens_hess", False)
# set
self.acados_integrator.set("x", x0)
self.acados_integrator.set("u", u0)
# solve
status = self.acados_integrator.solve()
# output
x_next = self.acados_integrator.get("x")
self.time_sim += self.acados_integrator.get("time_tot")
return [x_next]
def has_jacobian(self, *args) -> bool:
return True
def get_jacobian(self, *args):
if self.jac_callback is None:
self.jac_callback = CasadosIntegratorSensForw(self)
return self.jac_callback
def has_reverse(self, nadj) -> bool:
if nadj == 1:
return True
else:
return False
def get_reverse(self, *args) -> "casadi::Function":
if self.adj_callback is None:
self.adj_callback = CasadosIntegratorSensAdj(self)
return self.adj_callback
def reset_timings(self):
self.time_sim = 0.0
self.time_forw = 0.0
self.time_adj = 0.0
self.time_hess = 0.0
# NOTE: doesnt even get called -> dead end -> see https://github.com/casadi/casadi/issues/2019
# def uses_output(self, *args) -> bool:
# r"""
# uses_output(Function self) -> bool
# Do the derivative functions need nondifferentiated outputs?
# """
# print("in uses_output()\n\n")
# return False
# JACOBIAN
class CasadosIntegratorSensForw(Callback):
def __init__(self, casados_integrator):
self.acados_integrator = casados_integrator.acados_integrator
self.casados_integrator = casados_integrator
self.nx = self.casados_integrator.nx
self.nu = self.casados_integrator.nu
self.print_level = casados_integrator.print_level
Callback.__init__(self)
self.construct("CasadosIntegratorSensForw")
# casados_integrator.jac_callback = self
def get_sparsity_in(self, i):
if i == 0:
out = Sparsity.dense(self.nx)
elif i == 1:
out = Sparsity.dense(self.nu)
elif i == 2:
out = Sparsity.dense(self.nx)
return out
def get_sparsity_out(self, i):
if i == 0:
out = Sparsity.dense(self.nx, self.nx)
if i == 1:
out = Sparsity.dense(self.nx, self.nu)
return out
def get_name_in(self, i):
if i == 0:
out = "x0"
elif i == 1:
out = "u0"
elif i == 2:
out = "xf"
return out
def get_n_in(self):
return 3
def get_n_out(self):
return 2
def get_name_out(self, i):
if i == 0:
out = "jac_xf_x0"
if i == 1:
out = "jac_xf_p"
return out
def eval(self, arg):
# extract inputs
x0 = np.array(arg[0])
u0 = np.array(arg[1])
if self.print_level:
print(f"CasadosIntegratorSensForw: x0 {x0} u0 {u0}")
# set
self.acados_integrator.set("x", x0)
self.acados_integrator.set("u", u0)
self.acados_integrator.options_set("sens_forw", True)
self.acados_integrator.options_set("sens_adj", False)
self.acados_integrator.options_set("sens_hess", False)
# solve
status = self.acados_integrator.solve()
# output
S_forw = self.acados_integrator.get("S_forw")
# S_forw = np.ascontiguousarray(S_forw.reshape(S_forw.shape, order="F"))
self.casados_integrator.time_forw += self.acados_integrator.get("time_tot")
return [S_forw[:, :self.nx], S_forw[:, self.nx:]]
def has_jacobian(self, *args) -> bool:
return False
def has_reverse(self, nadj) -> bool:
# print(f"CasadosIntegratorSensForw: has_reverse, nadj: {nadj}\n")
return False
# Citing casadi docstrings:
# Get a function that calculates nadj adjoint derivatives.
# Returns a function with n_in + n_out + n_out inputs and n_in outputs.
# The first n_in inputs correspond to nondifferentiated inputs.
# The next n_out inputs correspond to nondifferentiated outputs.
# The last n_out inputs correspond to adjoint seeds, stacked horizontally
# The n_in outputs correspond to adjoint sensitivities, stacked horizontally.
# (n_in = n_in(),
# n_out = n_out())
# (n_in = n_in(), n_out = n_out())
# ADJOINT
class CasadosIntegratorSensAdj(Callback):
def __init__(self, casados_integrator):
self.acados_integrator = casados_integrator.acados_integrator
self.casados_integrator = casados_integrator
self.nx = casados_integrator.nx
self.nu = casados_integrator.nu
self.print_level = casados_integrator.print_level
Callback.__init__(self)
self.construct("CasadosIntegratorSensAdj")
def get_sparsity_in(self, i):
if i == 0:
out = Sparsity.dense(self.nx, 1)
elif i == 1:
out = Sparsity.dense(self.nu, 1)
elif i == 2:
out = Sparsity(self.nx, 1)
elif i == 3:
out = Sparsity.dense(self.nx, 1)
return out
def get_sparsity_out(self, i):
if i == 0:
out = Sparsity.dense(self.nx)
elif i == 1:
out = Sparsity.dense(self.nu)
return out
def get_name_in(self, i):
if i == 0:
out = "x0"
elif i == 1:
out = "u0"
elif i == 2:
out = "nominal_out"
elif i == 3:
out = "adj_seed"
return out
def get_n_in(self):
return 4
def get_n_out(self):
return 2
def get_name_out(self, i):
if i == 0:
out = "S_adj_x0"
elif i == 1:
out = "S_adj_p"
return out
def eval(self, arg):
# extract inputs
x0 = np.array(arg[0])
u0 = np.array(arg[1])
seed = np.array(arg[3])
if self.print_level:
print(f"CasadosIntegratorSensAdj: x0 {x0} u0 {u0} seed {seed}")
# set adj seed:
self.acados_integrator.set("seed_adj", seed)
# set input
self.acados_integrator.set("x", x0)
self.acados_integrator.set("u", u0)
# solve
self.acados_integrator.options_set("sens_adj", True)
self.acados_integrator.options_set("sens_forw", False)
self.acados_integrator.options_set("sens_hess", False)
status = self.acados_integrator.solve()
# output
S_adj = self.acados_integrator.get("S_adj")
if self.print_level > 1:
print(f"\nevaluated acados integrator in callback, got S_adj: {S_adj}\n")
S_adj_x = S_adj[: self.nx]
S_adj_u = S_adj[self.nx :]
self.casados_integrator.time_adj += self.acados_integrator.get("time_tot")
return [S_adj_x, S_adj_u]
def has_jacobian(self, *args) -> bool:
return True
def get_jacobian(self, *args):
if self.casados_integrator.hess_callback is None:
self.casados_integrator.hess_callback = CasadosIntegratorSensHess(
self.casados_integrator
)
return self.casados_integrator.hess_callback
# HESSIAN
class CasadosIntegratorSensHess(Callback):
def __init__(self, casados_integrator):
self.acados_integrator = casados_integrator.acados_integrator
self.casados_integrator = casados_integrator
self.nx = casados_integrator.nx
self.nu = casados_integrator.nu
self.print_level = casados_integrator.print_level
Callback.__init__(self)
self.construct("CasadosIntegratorSensHess")
def get_sparsity_in(self, i):
if i == 0:
out = Sparsity.dense(self.nx, 1)
elif i == 1:
out = Sparsity.dense(self.nu, 1)
elif i == 2:
out = Sparsity.dense(self.nx, 1)
elif i == 3:
out = Sparsity.dense(self.nx, 1)
elif i == 4:
out = Sparsity.dense(self.nx, 1)
elif i == 5:
out = Sparsity.dense(self.nu, 1)
return out
def get_sparsity_out(self, i):
if i == 0:
out = Sparsity.dense(self.nx, self.nx)
elif i == 1:
out = Sparsity.dense(self.nx, self.nu)
elif i == 2:
out = Sparsity.dense(self.nx, self.nx)
elif i == 3:
out = Sparsity.dense(self.nx, self.nx)
elif i == 4:
out = Sparsity.dense(self.nu, self.nx)
elif i == 5:
out = Sparsity.dense(self.nu, self.nu)
elif i == 6:
out = Sparsity.dense(self.nu, self.nx)
elif i == 7:
out = Sparsity.dense(self.nu, self.nx)
return out
def get_name_in(self, i):
if i == 0:
out = "x0"
elif i == 1:
out = "u0"
elif i == 2:
out = "nominal_out"
elif i == 3:
out = "adj_seed"
elif i == 4:
out = "S_adj_out_x"
elif i == 5:
out = "S_adj_out_u"
return out
def get_n_in(self):
return 6
def get_n_out(self):
return 8
def eval(self, arg):
# extract inputs
x0 = np.array(arg[0])
seed = np.array(arg[3])
u0 = np.array(arg[1])
if self.print_level:
print(f"CasadosIntegratorSensHess: x0 {x0} u0 {u0} seed {seed}")
# set adj seed:
self.acados_integrator.set("seed_adj", seed)
# set input
self.acados_integrator.set("x", x0)
self.acados_integrator.set("u", u0)
# solve
self.acados_integrator.options_set("sens_hess", True)
self.acados_integrator.options_set("sens_forw", True)
self.acados_integrator.options_set("sens_adj", True)
status = self.acados_integrator.solve()
# output
S_hess = self.acados_integrator.get("S_hess")
S_forw = self.acados_integrator.get("S_forw")
# NOTE: old casadi (<3.6) expects jacobian(S_adj, [x, u, nominal_out, seed_adj])
# = [S_hess(for x,u), zeros(nx+nu, nx), S_forw ]
# out = np.concatenate(
# [S_hess, np.zeros((self.nx + self.nu, self.nx)), S_forw.T], axis=1
# )
self.casados_integrator.time_hess += self.acados_integrator.get("time_tot")
# new casadi (>=3.6) jacobian api wants:
# in: ['x0', 'u0', 'nominal_out', 'adj_seed', 'out_S_adj_x0', 'out_S_adj_p']
# out: ['jac_S_adj_x0_x0', 'jac_S_adj_x0_u0', 'jac_S_adj_x0_nominal_out', 'jac_S_adj_x0_adj_seed',
# 'jac_S_adj_p_x0', 'jac_S_adj_p_u0', 'jac_S_adj_p_nominal_out', 'jac_S_adj_p_adj_seed']
return [S_hess[:self.nx, :self.nx], S_hess[:self.nx, self.nx:], np.zeros((self.nx, self.nx)), (S_forw.T)[:self.nx, :],
S_hess[self.nx:, :self.nx], S_hess[self.nx:, self.nx:], np.zeros((self.nu, self.nx)), (S_forw.T)[self.nx:, :]]
def has_jacobian(self, *args) -> bool:
return False