Implementing inequality constraints h(x) <= 0

124 views
Skip to first unread message

Vittorio Mattei

unread,
May 26, 2022, 7:53:11 AM5/26/22
to CasADi
Hello,

I'm trying to implement the following optimization problem in CasADi using Python:

problem.png

I have some problems on the implementation of the 2 inequality constraints.
The code that I wrote is the following:

# Structure holding decision variables
V = struct_symMX([
      (
       entry("X",repeat=[M+1], struct=states),
       entry("U",repeat=[M], struct=control),
       entry("S",repeat=[M], struct=output),
      )
    ])

# Lower/upper bounds of the decision variables
vars_lb = V(-np.inf)
vars_ub = V(+np.inf)

# Initial guess
vars_init = V(0)

# Structure holding parameters
P = struct_symMX([
      (
       entry("r",repeat=[M],struct=output),
      )
    ])

# Start with an empty NLP
J = 0
dyn = []
s = []

# Formulate the NLP
for k in range(M):
    J += ( V['S',k] ).T @ ( V['S',k] )
    s += [ - V['S',k] + P['r',k] - H( V['X',k] ) ]
    s += [ - V['S',k] - P['r',k] + H( V['X',k] ) ]
    dyn += [ V['X',k+1] - F( x0=V['X',k], p=V['U',k] )['xf'] ]
   
# Structure holding constraints
g = struct_MX([
            entry("dyn", expr=dyn),
            entry("s", expr=s),
        ])

# Equality constraints (lb = up)
lbg = g(0)          
ubg = g(0)          

# Inequality constraints ( -np.inf <= h(x) <= 0 )
lbg["s"] = - np.inf
ubg["s"] = 0


# Create an NLP solver
prob = {'f': J, 'x': V, 'g': g, 'p':P}
options = {'ipopt': {}}
solver = nlpsol('solver_target', 'ipopt', prob, options)

I have some perplexity on the red section, since the solver is not able to find an Optimal Solution and it always reaches the maximum iteration number.
I tried writing the same problem in MATLAB using Opti Stack and it worked, so I'm making some mistake in Python but I'm not able to find it.
Is someone able to help me?

Thank you in advance and have a nice day.
Vittorio
Reply all
Reply to author
Forward
0 new messages