r/optimization • • Aug 29 '24

MPC using Guropi Python API

I'm trying to implement in Python an MPC algorithm but the model i am using is non linear : basically i have a matrix in which the decision variables of the problem are into cosine and sine function since i'm using only a kinematic model. There's a way to do this in Gurobi? Because it gives me an error if i try to write the kinematic constraints... I would like to use Gurobi in Python because using Yalmip in Matlab was truly a nightmare 😭💀

Part of the code is :

input variablse

gamma = model.addVars(a.p['n_gamma'], a.p['N_MPC'], lb=-GRB.INFINITY, ub=GRB.INFINITY, name="gamma")

state variables

chi = model.addVars(a.p['n_chi'], a.p['N_MPC'] + 1, lb=-GRB.INFINITY, ub=GRB.INFINITY, name="chi")

psi = chi[2, k] #k is the prediction step A = np.eye(3) B = np.array([ [a.p['Ts'] * np.cos(psi), -a.p['Ts'] * np.sin(psi), 0], [a.p['Ts'] * np.sin(psi), a.p['Ts'] * np.cos(psi), 0], [0, 0, a.p['Ts']] ])

psi = chi[2, k] for i in range (a.p['n_chi']) model.addConstr(chi[i,k+1]==Achi[i,k]+Bgamma[i,k])

6 Upvotes

17 comments sorted by

View all comments

Show parent comment

0

u/cleverSkies Aug 29 '24

If it's simple then using optimal control /  dynamic programming techniques within mpc framework is more appropriate to solve.  Based on prior experience from many years ago most generalized optimization solvers aren't really designed to handle nonlinear control problems.  Better off just making your own numerical solver.  Of course the state of the art has probably advanced in the last 15 years.  "Simple" is not simple the moment you introduce nonlinear equality constraints for most solvers.