Skip to main content

Formulating Model Predictive Control in Gurobi (Python)

Answered

Comments

1 comment

  • Eli Towle
    Gurobi Staff Gurobi Staff

    By default, variables added to a model using Model.addMVar() have a lower bound of \( 0 \). When I pass \( \texttt{lb=-GRB.INFINITY} \) as a keyword argument to your calls to Model.addMVar(), Gurobi finds an optimal solution.

    You can write the code more concisely by (i) using 2-D MVar objects and (ii) directly passing the variable bounds to Model.addMVar(). For example:

    xmin = np.tile(xmin, (N+1,1))
    xmax = np.tile(xmax, (N+1,1))
    umin = np.tile(umin, (N,1))
    umax = np.tile(umax, (N,1))

    x = m.addMVar(shape=(N+1,12), lb=xmin, ub=xmax, name='x')
    z = m.addMVar(shape=(N+1,12), lb=-GRB.INFINITY, name='z')
    u = m.addMVar(shape=(N,4), lb=umin, ub=umax, name='u')

    m.addConstr(x[0, :] == np.zeros(12))
    for k in range(N):
        m.addConstr(z[k, :] == x[k, :] - xr)
        m.addConstr(x[k+1, :] == A @ x[k, :] + B @ u[k, :])
    m.addConstr(z[N, :] == x[N, :] - xr)

    obj1 = sum(z[k, :] @ Q @ z[k, :] for k in range(N+1))
    obj2 = sum(u[k, :] @ R @ u[k, :] for k in range(N))
    m.setObjective(obj1 + obj2, GRB.MINIMIZE)
    0

Please sign in to leave a comment.