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
-
Official comment
This post is more than two 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. -
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
Post is closed for comments.
Comments
3 comments