Use min_ to get element wise minimum of a MVar and 1
AnsweredI have the following code and trying to get the elementwise minimum working, however I am getting an error "gurobipy.GurobiError: Invalid data in vars array". Can anyone tell me where I might be doing wrong?
selection = model.addMVar((1, n1), vtype=gp.GRB.BINARY)
product = selection @ condition # condition is a sparse matrix of shape n1xn2
binary = model.addMVar((1, n2), vtype=gp.GRB.BINARY)
for i in range(n):
model.addConstr(binary[0, i] == gp.min_(product[0, i], 1))
0
-
Hi Weihang,
The elements in your "product array" are gurobipy.MLinExpr. The initial arguments to gurobipy.min_ need to be variables. You can fix this by introducing auxiliary variables like so:
# could be INTEGER if numbers in "condition matrix" are integer
product_aux = m.addMVar(n2, vtype=gp.GRB.CONTINUOUS)
for i in range(n2):
m.addConstr(product_aux[i] == product[0, i])
m.addConstr(binary[0, i] == gp.min_(product_aux[i] , 1))- Riley
0 -
Hi Riley,
Thanks very much for the answer. It is clear now why it was not working previously. Really appreciate the help!
0
Please sign in to leave a comment.
Comments
2 comments