Skip to main content

Formulating Model Predictive Control in Gurobi (Python)

Answered

Comments

2 comments

  • Official comment
    Simranjit Kaur
    Gurobi Staff Gurobi Staff
    This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum. Or why not try our AI Gurobot?.
  • 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

Post is closed for comments.