proxsuite-nlp  0.10.0
A primal-dual augmented Lagrangian-type solver for nonlinear programming on manifolds.
Loading...
Searching...
No Matches
casadi_utils.py
Go to the documentation of this file.
1"""
2Copyright (C) 2022 LAAS-CNRS, INRIA
3"""
4
5import casadi
6import proxsuite_nlp
7import numpy as np
8
9
10class CasadiFunction(proxsuite_nlp.C2Function):
12 self,
13 nx: int,
14 ndx: int,
15 expression: casadi.SX,
16 cx: casadi.SX,
17 use_hessian: bool = True,
18 ):
19 nres = expression.shape[0]
20 super().__init__(nx, ndx, nres)
21 assert nx == cx.shape[0]
22 dx = casadi.SX.sym("dx", ndx)
23 # TODO: replace this using manifold operation
24 xplus = cx + dx
25 self.clam = casadi.SX.sym("lam", nres)
26 self.expr = casadi.substitute(expression, cx, xplus)
27 self.Jexpr = casadi.jacobian(self.expr, dx)
28 self.use_hessian = use_hessian
29 if use_hessian:
30 self.Hexpr = casadi.jacobian(self.clam.T @ self.Jexpr, dx)
31 else:
32 self.Hexpr = casadi.SX.zeros(ndx, ndx)
33
34 self.fun = casadi.Function("f", [cx, dx], [self.expr])
35 self.Jfun = casadi.Function("Jf", [cx, dx], [self.Jexpr])
36 self.Hfun = casadi.Function("Hf", [cx, dx, self.clam], [self.Hexpr])
37 self._zero = np.zeros(ndx)
38
39 def __call__(self, x):
40 return np.asarray(self.fun(x, self._zero)).flatten()
41
42 def computeJacobian(self, x, J):
43 J[:] = np.asarray(self.Jfun(x, self._zero))
44
45 def vectorHessianProduct(self, x, v, H):
46 H[:, :] = np.asarray(self.Hfun(x, self._zero, v))
__init__(self, int nx, int ndx, casadi.SX expression, casadi.SX cx, bool use_hessian=True)