Drawing Relation between two set of variables
AnsweredGreetings,
In my modeling, I have two set of variables. X[0..n] and Y[0..n]. X is independent of Y. But I want Y to be dependent on X. Using some optimality constraints, X [0..n] is getting populated. Now, i need to populate Y based on additional constrains, only for those cells, for which X[i] is not equal to zero.
Is the following right way ?
-
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 Sesha,
Do you like to implement the constraint below?
\[\mbox{if} ~~ X_i \neq 0, \mbox{then}~~ X_i Y_i \geq 0.001, ~~ i=0, \cdots, n\]Assuming \(X\) and \(Y\) are continuous variables, you would need to introduce a binary variable \(Z\) to model the following:
- \[\mbox{if} ~~ X_i \neq 0, \mbox{then}~~Z_i = 1, ~~ i=0, \cdots, n\]
- \[\mbox{if} ~~ Z_i = 1, \mbox{then}~~ X_i Y_i \geq 0.001, ~~ i=0, \cdots, n\]
The first indicator constraint can be modelled using the following linear constraints where \(M\) is a sufficiently large value representing the domain of the \(X\) variables.
\(-M Z_i \leq X_i \leq M Z_i, ~~ i=0, \cdots, n\): This gurantees that if \(X_i \neq 0\), then \(Z_i\) has to be 1.
The second indicator constraint can then be modelled as below:
\( X_i Y_i \geq 0.001 - M (1 -Z_i), ~~ i=0, \cdots, n\): This gurantees that if \(Z_i = 1\) (i.e., \(X_i \neq 0\)), then \(X_i Y_i \geq 0.001\).
Best regards,
Maliheh
0 -
Thank you Maliheh for you reply. Unfortunately, that is not the constraint that I want to implemt.
I have variables X[0..n] which are getting populated based on some constraints. Some of those can be zero. Now, I want another set of variables Y[0..n] that should be populated with optimized values again based on some other constraints. I want only those variables Y[i] to be populated with NZ values if X[i] is NZ.
Thanks,
Shesha.
0 -
Unfortunately, that is not the constraint that I want to implemt.
Could you please write the mathematical formulation of the constraint you would like to implement? The previous post implements the constraint below:
\[\mbox{if} ~~ X_i \neq 0, \mbox{then}~~ X_i Y_i \geq 0.001, ~~ i=0, \cdots, n\]How does the constraint you intend to implement differ from the above constraint?
Maliheh
0 -
Hi Maliheh ,
I see your point. I am new to gurobi. How do I program indicator constraints in Python? In my example X is a continuous variable in the range of {0..1}.
Regards,
Shesha.0 -
This is my attempt to model it.
M = 1
Z = [[None for j in range(NCONTAINERS)] for i in range (NVARS)]
EPSILON = 0.001
for i in range (NVARS):
for j in range (NCONTAINERS):
varname = "Z_%02d_%02d" % (i, j)
Z[i][j] = opt_mod.addVar(vtype=gp.GRB.BINARY, name=varname)
varname = "GT_%02d_%02d" % (i, j)
opt_mod.addConstr(X[i][j] >= -M * Z[i][j], name = varname)
varname = "LT_%02d_%02d" % (i, j)
opt_mod.addConstr(X[i][j] <= M * Z[i][j], name = varname)
varname = "SAMEBLK_%02d_%02d" % (i, j)
opt_mod.addConstr(X[i][j] * Y[i][j] >= EPSILON - M * (1 - Z[i][j]), name = varname)However, I am not seeing the correct results :(
X_00_00 0.0 Y_00_00 32.0
X_01_00 0.0 Y_01_00 16.0
X_02_00 0.0 Y_02_00 16.0
X_03_00 0.0 Y_03_00 32.0
X_04_00 0.0 Y_04_00 32.0
X_05_00 0.0 Y_05_00 32.0
X_06_00 0.0 Y_06_00 32.0
X_07_00 0.0 Y_07_00 32.0
X_08_00 0.0 Y_08_00 6.0
X_09_00 0.0 Y_09_00 32.0
X_10_00 0.0 Y_10_00 48.0
X_11_00 0.333 Y_11_00 76.0
X_00_01 0.0 Y_00_01 32.0
X_01_01 0.0 Y_01_01 16.0
X_02_01 0.0 Y_02_01 16.0
X_03_01 0.0 Y_03_01 32.0
X_04_01 0.0 Y_04_01 32.0
X_05_01 0.0 Y_05_01 32.00 -
Hi Sesha,
The results look consistent with the constraints implemented. The variable \(X_{11,00}\) equals a non-zero value (\(0.333\)) and its associated \(Y_{11,00}\) variable equals \(76\) such that \(0.333 \times 76 = 25.308 \geq 0.001\). What else would you like to see? Would you like to further enforce \(Y\) equals 0 if \(X\) equals 0?
Maliheh
0 -
Yes, exactly. :)
0 -
You need to add the following constraints where \(M\) is a sufficiently large value covering the domain of the \(Y\) variables.
- \(Y_i \leq X_i + MZ_i\)
- \(Y_i \geq X_i - MZ_i\)
0 -
Thanks a lot for answering all my queries. Sincerely appreciate it.
0
Post is closed for comments.
Comments
10 comments