MPC using casadi.Opti()

1,421 views
Skip to first unread message

JX Yu

unread,
Jun 7, 2020, 3:08:42 PM6/7/20
to CasADi
Hello everyone,
I use casadi.Opti() in matlab to solve quadratic form of MPC sum(x'Qx+u'*R*u) with input constraints in the control horizon is subject to -u_max<=u<=u_max. x is 4*1 and u is 1*1. p is the initial state of x.

I tried qpoases, nlpsol and qrqp. But simliar erros show that it cannot load shared library. I have already added casadi path to matlab. Do I need to download these solvers files?

opti=casadi.Opti();
Np=30;Nc=20;
x=opti.variable(4,Np);
u=opti.variable(1,Nc);
p=opti.parameter(4,1);
sumx=0;sumu=0;
for kp=1:Np-1
    if kp<=Nc
        opti.subject_to(x(:,kp+1)==AT*x(:,kp)+B2*u(1,kp)+D0);
        sumu=u(1,kp)'*R*u(1,kp)+sumu;
    else
        opti.subject_to(x(:,kp+1)==AT*x(:,kp)+B2*u(1,Nc)+D0);
    end
    sumx=sumx+x(:,kp+1)'*Q*x(:,kp+1);
end
opti.subject_to(norm(u,inf)<=u_max);
sum=sumx+sumu;
opti.subject_to(x(:,1)==p);
opti.set_value(p,[ey;vy+vx*ephi;ephi;yawrate-vx*rho]);
opti.minimize(sum);
opti.solver('nlpsol');
sol=opti.solve();

CasADi -  WARNING(".../casadi/core/plugin_interface.hpp:322: Assertion "handle!=nullptr" failed:
PluginInterface::load_plugin: Cannot load shared library 'libcasadi_nlpsol_nlpsol.dll': 
   (
    Searched directories: 1. casadipath from GlobalOptions
                          2. CASADIPATH env var
                          3. PATH env var (Windows)
                          4. LD_LIBRARY_PATH env var (Linux)
                          5. DYLD_LIBRARY_PATH env var (osx)
    A library may be 'not found' even if the file exists:
          * library is not compatible (different compiler/bitness)
          * the dependencies are not found
   )
  Tried 'E:\casadi-windows-matlabR2016a-v3.5.1' :
    Error code (WIN32): 126
  Tried '' :
    Error code (WIN32): 126
  Tried '.' :
    Error code (WIN32): 126") [.../casadi/core/plugin_interface.hpp:171]
Error using casadi.Opti/solve (line 253)
Error in Opti::solve [OptiNode] at .../casadi/core/optistack.cpp:159:
.../casadi/core/plugin_interface.hpp:417: Plugin 'nlpsol' is not found.

Joris Gillis

unread,
Jun 9, 2020, 3:35:16 AM6/9/20
to CasADi
Hi there.

For conic problems, you need to initialize Opti with 'conic' argument:

opti=casadi.Opti('conic');
x
=opti.variable(2);

opti
.subject_to([1 2]*x<=-2);
opti
.minimize(sumsqr(x));
opti
.solver('qpoases');
sol
=opti.solve();

sol
.value(x)



Note that the norm(u,inf) formulation will not give satisfactory results.
Change to the equivalent:

opti.subject_to(u<=u_max);
Best regards,
  Joris

JX Yu

unread,
Jun 10, 2020, 11:34:28 AM6/10/20
to CasADi
Hi Gillis,
Thanks for your reply. It works, But the solution is much different from other solver, "cvxgen" while I guess my code is right.

And could I speed up the solver to 0.01s while it takes 0.13s to obtain a solution now?

Best,
Jiaxing
Reply all
Reply to author
Forward
0 new messages