Question about converting a matrix to indicator matrix in gurobi
AnsweredHi, suppose I have a matrix/vector containing decision variable x, such as v = [x_1, 2*x_2, x_3+x_4,...], now I want to convert it into a binary matrix such as k_i = 0 if v_i = 0, k_i = 1 if v_i >0,... May I ask if there's any way to do it?
FYI, I use Python API, so may I ask if I can use numpy here like: https://stackoverflow.com/questions/26551825/converting-an-array-to-positive-indicator? If not, then how to write it in Gurobi? Thanks a lot!
-
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. -
It's not possible to do this via numpy's type casting functions; you need to construct constraints to specify the relationship between v and k. I'm assuming here that k_i are binary variables in the model, as opposed to a post-processing step where the values of v_i are already known.
You can express the necessary constraints using either a big-M formulation:
model.addConstr(v[i] <= M*k[i])
where M is an upper bound on the value of v_i, or using indicator constraints:
model.addConstr((k[i] == 0) >> (v[i] == 0))
0
Post is closed for comments.
Comments
2 comments