Skip to main content

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

Answered

Comments

2 comments

  • Jonasz Staszek
    Community Moderator 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 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

Please sign in to leave a comment.