Cannot set objective function. minimize max(var.values())
AnsweredI have an objective I am trying to cast as follows.
m.setObjective(max(X.values(), sense=GRB.MINIMIZE))
But I am getting a type error.
TypeError: '>' not supported between instances of 'Var' and 'Var'
X = m.addVars(range(len(n)), vtype=GRB.CONTINUOUS, lb=0, ub=100, name="X")
The rest of my model is somewhat complex. X depends on a sum of smaller values, and these smaller values depend on two binary decision variables. I'm curious if my problem is more surface level though. Please let me know if you see anything clearly wrong with these definitions, or if you need more detail about constraints relating to X.
For what it is worth, I do not have any strict lessthan comparisons in any of my constraints.
Thank you.

It seems to me that you would need to reformulate your model somehow. Perhaps this other thread will be of help.
0 
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.
Comments
2 comments