Integration of objective function error

160 views
Skip to first unread message

Rugved katole

unread,
Sep 14, 2022, 11:03:08 AM9/14/22
to CasADi
Hi,
I am trying to solve an MPC problem using OPTI stack in python, The objective function is a integral function. I tried to accommodate it as follows.

opti = Opti()

z = opti.variable(2,N+1)
u = opti.variable(2,N)
z_initial = opti.parameter(2,1)
u_prev = opti.parameter(2,1)

z_ref = opti.parameter(2,N+2)

obj = 0

for i in range(N-1):
obj += (z[:,i+1]-z_ref[:,i]).T @ Q @ (z[:,i+1]-z_ref[:,i])
obj += ((u[:,i+1]-u[:,i])/X_D).T @ S @ ((u[:,i+1]-u[:,i])/X_D) # change in control variables w.r.t distance
obj += u[:,i].T @ R @ u[:,i] # control variables

obj = Function("objective",[z,u],[obj])
print(obj)
DAE = {"x": z, "p": u, "ode":obj(z,u)}

intg = integrator("intg","rk", DAE)

res = intg(z, u, [],[],[],[])
states_nxt = res[0]

Objective_fun = Function("Estimator",[z,u],[obj]).

On running this it shows following error. 
I couldn't understand what exactly it means.
RuntimeError: .../casadi/core/integrator.cpp:1430: Assertion "e.numel()==nr*nrhs" failed:
Inconsistent number of rhs

My objective function takes opti stack variables as input

objective:(i0[2x181],i1[2x180])->(o0[2x181]) MXFunction

Any kind of help is appreciated,
Thank you

Regards, 
Rugved



Bruno M L

unread,
Sep 19, 2022, 7:53:06 AM9/19/22
to CasADi
Hi!

In DAE, you are puting 'z' (2x1) as your states, and obj (1x1) as your ode. They have different dimensions, that is why it is not working.

The integral objective is better implemented with a quadrature function. Take a look at the documentation.

Also, look at the direct_multiple_shooting.py example. It uses the quadrature function to implement a Lagrange term.

Rugved katole

unread,
Sep 19, 2022, 2:52:17 PM9/19/22
to CasADi
Okay Thank you, I will look into it.
Reply all
Reply to author
Forward
0 new messages