Skip to main content

Cannot set objective function. minimize max(var.values())

Answered

Comments

3 comments

  • Official comment
    Simranjit Kaur
    • 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 try Gurobot, our chatbot interface offering instant, expert-level support.
  • Jonasz Staszek
    • Community Moderator
    • Gurobi-versary
    • Thought Leader
    • First Question

    It seems to me that you would need to reformulate your model somehow. Perhaps this other thread will be of help.

    0
  • Jaromił Najman
    • Gurobi Staff

    Jonasz is right. If you want to use Gurobi's max_ function, you have to introduce an auxiliary variable and an equality constraint to model it, e.g.,

    import gurobipy as gp
    from gurobipy import GRB

    m = gp.Model("test")
    x = m.addVars(5, vtype=GRB.CONTINUOUS, lb=0, ub=100, name="X")
    z = m.addVar(vtype=GRB.CONTINUOUS, name="Z")
    # Add constraint z = max(x1,x2,x3,x4,x5)
    m.addConstr(z == gp.max_(x), name="max_contraint")
    m.setObjective(z, sense=GRB.MINIMIZE)

    Best regards,
    Jaromił

    1

Post is closed for comments.