Equality of two Mvar
回答済みDear Sir/Madam
Thank you indeed for developing a nice toolkit like Gurobi, In my code, I have to check the equality of two Mvar, in the following, I attached to code that I used in my model:
The model that I use works, fortunately, but the size of the Matrix forces causes a long execution time of the Gourbi. I am looking for a way to get rid of using the helping variables.
I mean that I am looking for an approach to have the simple following constraint:
if (LKN[k,i] == LKN[l,i]) then (Sim_check_help[k,l,i]==1)
model.addConstr((LKN[k,i] == LKN[l,i])) >> (Sim_check_help[k,l,i]==1) )
. I will be thankful if you, please help me with that.
Best Regards
help_var1 = model.addMVar((grid_size,grid_size,grid_size),vtype=GRB.BINARY)
help_var2 = model.addMVar((grid_size,grid_size,grid_size),vtype=GRB.BINARY)
help_var3 = model.addMVar((grid_size,grid_size,grid_size),vtype=GRB.BINARY)
for k in range (grid_size):
for l in range(grid_size):
for i in range(grid_size):
model.addConstr(help_var1[k,l,i] + help_var2[k,l,i] + help_var3[k,l,i] == 1)
model.addConstr((-(Anchor_number) * help_var1[k,l,i] + 0 * help_var2[k,l,i] + 1 * help_var3[k,l,i]) <= (LKN[k,i] - LKN[l,i]))
model.addConstr((LKN[k,i] - LKN[l,i]) <= (-1 * help_var1[k,l,i] + 0 * help_var2[k,l,i] + Anchor_number * help_var3[k,l,i]))
model.addConstr((help_var2[k,l,i] == 1) >> (Sim_check_help[k,l,i]==1) )
model.addConstr((help_var2[k,l,i] == 0) >> (Sim_check_help[k,l,i]==0))
-
正式なコメント
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 try Gurobot, our chatbot interface offering instant, expert-level support. -
Hi Shahab,
The model that I use works, fortunately, but the size of the Matrix forces causes a long execution time of the Gourbi
Using auxiliary variables to enforce constraints is not necessarily the cause of slow progress. As you know, to use the Gurobi Python API for indicator constraints, the indicator variable needs to be binary.
Let us assume \(x\) and \(y\) variables are binary, to implement a relationship in the form
\[\mbox{if}~~ x_{ki} = x_{li} \rightarrow y_{kli} = 1,\]
one idea is to define a new binary variable \(z_{kli} = |x_{ki} - x_{li}|\) and then implement
\[\mbox{if}~~ z_{kli} =0 \rightarrow y_{kli} = 1\]
either using the Gurobi Python API for indicator constraints or simply having \(y_{kli} \geq 1 - z_{kli}\) to ensure \(y_{kli} = 1\) if \(z_{kli} = 0\).
Are the \(\texttt{LKN}\) variables binary in your model?
Best regards,
Maliheh
0 -
Dear Maliheh
Thank you indeed for responding and explaining; the problem is that LKN is not a binary variable but an integer variable. If the variable was a binary, I updated the code like below and instead of using (help_var2). I just put the (Sim_check )Variable directly. But still, the number of variables is considerable, and I should find a way to eliminate this helping variable.
Best Regards
help_var1 = model.addMVar((grid_size,grid_size,grid_size),vtype=GRB.BINARY)
help_var3 = model.addMVar((grid_size,grid_size,grid_size),vtype=GRB.BINARY)
for k in range (grid_size):
for l in range(grid_size):
for i in range(grid_size):
model.addConstr(help_var1[k,l,i] +Sim_check_help[k,l,i]+ help_var3[k,l,i] == 1)
model.addConstr((-(Anchor_number) * help_var1[k,l,i]+ 1 * help_var3[k,l,i]) <= (LKN[k,i] - LKN[l,i]))
model.addConstr((LKN[k,i] - LKN[l,i]) <= (-1 * help_var1[k,l,i] + Anchor_number * help_var3[k,l,i]))0 -
Hi Shahab,
As already mentioned, the difficulty of a model is not necessarily a function of its size.
A similar idea still works if \(\texttt{LKN}\) are integer variables. Let us refer to \(\texttt{LKN}\) as \(x\):
- Define a new non-negative integer variable \(z_{kli} = |x_{ki} - x_{li}|\).
- To do this, you would need to first define an auxiliary variable \(w_{kli} = x_{ki} - x_{li}\) and then use the Model.addGenConstrAbs() method to define the constraint \(z_{kli} = |w_{kli}|\). Please note that the variables \(w_{kli}\) can take negative values, so you would need to set their lower bounds accordingly when defining these variables as the default variable lower bound is 0 in Gurobi.
- To implement \(\mbox{if}~ z_{kli} = 0 \rightarrow y_{kli} = 1\), we can have \(y_{kli} \geq 1 - 2z_{kli}\). This ensures \(y_{kli} = 1\) if \(z_{kli} = 0\). Otherwise, it is a redundant constraint.
Could you please give this a try? The Gurobi Optimizer solves the presolved model. It might be also a good idea to experiment with the parameter Presolve=2 to see whether it helps to reduce the size of the presolved model further or not.
Hope this helps.
Best regards,
Maliheh
0 - Define a new non-negative integer variable \(z_{kli} = |x_{ki} - x_{li}|\).
-
Dear Maliheh
Thank you indeed for responding, I tried all the abovementioned approaches, but unfortunately, It was the same and the execution time is still considerable, I should mention that the number of Variables here exceeds 10 million Variables and I think it is the reason that it takes too long. Thank you indeed for helping me.
Best Regards
Shahab
0
投稿コメントは受け付けていません。
コメント
5件のコメント