reorder decision variable vector opti

160 views
Skip to first unread message

rskr...@gmail.com

unread,
May 21, 2021, 5:26:10 PM5/21/21
to CasADi
Hi all,

I solving an optimization problem using multiple shooting. Usually, it is convenient to declare the discretized states and controls as
X = opti.variable(Nx,N);
U = opti.variable(Nu,N);
where Nx,Nu are dimensions of the state and control vector.
The decision variable vector is 
W= [X1 X2 ... XN U1 U2...UN].
However, I want to structure the W such that
W=[X1 U1 X2 ....XN UN ] to obtain a block diagonal sparsity pattern for the matrices.
Is there a straightforward way to approach this?

Regards
Sandeep
 

gabf...@gmail.com

unread,
Jun 16, 2021, 10:28:55 AM6/16/21
to CasADi
Hi,

I'm having a similar issue also when just retrieving the derivatives, which I'd like to obtain for some further calculations, from the opti class.
In this case it seems to me that there's a bug or maybe I don't understand the logic behind and there's a smarter way to perform this operation.
A piece of code that shows the problem:

X = opti.variable(nx, N + 1)
U = opti.variable(nu, N)
... 
H = casadi.hessian(opti.f, X) # this works, type is casadi.casadi.MX

z_vars = []
for i in range(N):
    z_vars.append(U[:,i])
    z_vars.append(X[:,i+1])
Z = casadi.vertcat(z_vars)
H = casadi.hessian(opti.f, Z) # DOESN'T work, throws:

RuntimeError                              Traceback (most recent call last)
<ipython-input-24-6b85bbe7f899> in <module>
----> 1 c.hessian(opti.f, Z)

/usr/local/python/casadi/casadi.py in hessian(*args)
  12649     hessian(SX ex, SX arg) -> SX
  12650     """
> 12651     return _casadi.hessian(*args)
  12652
  12653 def quadratic_coeff(*args) -> "casadi::Matrix< casadi::SXElem > &, casadi::Matrix< casadi::SXElem > &, casadi::Matrix< casadi::SXElem > &":

RuntimeError: Error in MX::hessian at .../casadi/core/mx.cpp:1679:
Error in MX::gradient at .../casadi/core/generic_matrix.hpp:1203:
Error in MX::jtimes at .../casadi/core/generic_matrix.hpp:1191:
Error in MX::reverse at .../casadi/core/mx.cpp:1741:
Error in Function::Function for 'reverse_temp' [MXFunction] at .../casadi/core/function.cpp:235:
.../casadi/core/function_internal.cpp:144: Error calling MXFunction::init for 'reverse_temp':
.../casadi/core/x_function.hpp:286: For reverse_temp: Xfunction input arguments must be purely symbolic.
Argument 0(i0) is not symbolic.

Now the type is the same, I'm not sure why reordering the vector with the derivatives should not work, I don't get the purely symbolic error, can someone help me out with this?

Joris Gillis

unread,
Jul 22, 2021, 7:41:11 AM7/22/21
to CasADi
Dear Sandeep,

One popular technique is to do opti.variable(Nx) and opti.variable(Nu) in a loop, and collect results in a cell (Matlab) or list (Python), possibly followed by a horzcat.

Best regards,
  Joris

Joris Gillis

unread,
Jul 22, 2021, 7:44:44 AM7/22/21
to CasADi
We do not currently support sliced MX symbols as Hessian arguments (or more generally, as Function inputs).
The loop technique mentioned below can give you the desired structure

Best regards,
  Joris

sandeep kumar r

unread,
Jul 22, 2021, 8:38:34 AM7/22/21
to Joris Gillis, CasADi
Thanks Joris. I'll try that out

--
Sent from CasADi's user forum at http://forum.casadi.org.
---
You received this message because you are subscribed to the Google Groups "CasADi" group.
To unsubscribe from this group and stop receiving emails from it, send an email to casadi-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/casadi-users/cb01052c-4e18-46a0-99e6-cd30b8c90625n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages