Creating diagonal matrix from binary variables
回答済みI have defined a binary variable:
z = m.addVars(arcs, vtype=GRB.BINARY,name="z") where arcs are (i,j). z_{i,j} = 1 means there exists a line between i and j.
I want to create a matrix such that z_{i,j} are in the diagonal elements.
Z = [ z_{1,2} 0 0;
0 z_{1,3} 0;
0 0 z_{2,3}]
How do I formulate this Z matrix in Gurobi? I further plan to use Z matrix in my objective functions.
-
正式なコメント
This post is more than three 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 why not try our AI Gurobot?. -
Which API are you using? (Your first line looks like Python, but the definition of Z does not.)
Note that until version 8, the Gurobi Python API does not directly support matrix-oriented modeling, but this is going to change with Gurobi 9 (due later this year).
1 -
Hi Silke, I am using Python. The definition of Z was just an example.
Do you think there is any indirect way I can create a matrix with binary variables in the diagonals?
0 -
You can certainly create a numpy matrix with your variables on the diagonals; e.g.
Z = np.diag([z[i] for i in arcs])
but the question is what you want to do with this matrix once you have it...
0 -
Thank you Silke, I was able to define the binary matrix with your help.
Z = np.diag([z[i,j] for i,j in arcs])
print(Z)
[[<gurobi.Var z[163,111]> 0 0 ..., 0 0 0]
[0 <gurobi.Var z[47,54]> 0 ..., 0 0 0]
[0 0 <gurobi.Var z[150,88]> ..., 0 0 0]...]What I want to do with the Z matrix is this: I have another matrix H that is multiplied to Z like this: Z*H. I want to maximize the rank of Z*H in the objective function.
Do you think setting the objective function like maximize rank(Z*H) is allowed in Gurobi? I am not sure if Z*H is counted as matrix-oriented modeling?
0 -
I am glad you got the matrix definition to work.
The objective function needs to be a linear or quadratic expression, which, unfortunately, the rank of a matrix is not. (In fact, it is not even continuous.) I would expect that numpy cannot compute the rank of (Z*H) since it does not know what Gurobi variables are...
So this is not going to work (because the math involved is more complicated -- this is not a short-coming of numpy or Gurobi).
0
投稿コメントは受け付けていません。
コメント
6件のコメント