Is there any function like count(X) in gurobi?
回答済みHi there, I wonder how to count a specific number in the Var list. For example, my variable is X, and it is defined as (m.addVars(M, N, vtype=GRB.CONTINUOUS),lb=0, ub=1, and I want to limit total times X[m][n] is a float(or X[m][n]!=0 and X[m][n]!=1) in X. How can I add this constraint to my model? Is there any function like count(X) in gurobi? Thanks a lot !!
-
正式なコメント
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 Yikun,
below comes my idea for tackling the problem you see. Perhaps someone from the Gurobi team can suggest a better approach?
Generally, the idea would be to reformulate the model in such a way that three additional indicator variables and two indicator constraints plus one linear constraint is added for each variable in X.
For each \(x_{m,n}\), we would need to add three binary indicator variables: \(z^1_{m,n}, z^2_{m,n}, z^3_{m, n}\).
Using indicator constraints, we can state that:
\( z^1_{m,n} = 1 >> x_{m,n} = 1\)
\( z^2_{m,n} = 1 >> x_{m,n} = 0\)
We then need to add one linear constraint to make sure that exactly one of the indicator variables is selected:
\(z^1_{m,n} + z^2_{m,n} + z^3_{m, n} = 1\).
With these at hand, you can limit the number of non-binary values to any constant \(c\), for example using such a constraint:
\( \sum_m \sum_n z^3_{m, n} \leq c\).
Remember though that equality will hold subject to tolerances. If you are happy with this, I imagine the following approach should work.
Best regards
Jonasz1 -
I added another (less general) approach to your other post here.
0 -
Hi Yikun,
In general, it is quite difficult to formulate a condition like \(x \neq 0 \text{and} x\neq 1\). As Jonasz already mentioned, you need to use manual tolerances to define what you consider feasible for the specific conditions: How do you treat a value of \(x=0.000001\)?
Maybe you can come up with a completely different formulation that still captures the specifications of your problem. Otherwise, Jonasz's approach looks very reasonable - whether it's practical depends on the size of your model, of course.
Best regards,
Matthias0 -
Sorry for the slow reply. That's really a good idea to use Jonasz's and Silke's approach. Actually, I come up with the idea as follow:
First, we introduce two variables:
cnt_x0 = m.addVars(M, N, vtype=GRB.BINARY, name="cnt_x0[m,n]")
cnt_x1 = m.addVars(M, N, vtype=GRB.BINARY, name="cnt_x1[m,n]")Then, we will make constrains like:for i in range(M):
for j in range(N):
m.addConstr(cnt_x0[i, j] + x[i, j] >= 0.01)
m.addConstr(cnt_x0[i, j] + x[i, j] <= 1.0)
m.addConstr(x[i, j] - cnt_x1[i, j] >= 0.0)
m.addConstr(x[i, j] - cnt_x1[i, j] <= 0.99)
# m.addConstr(cnt_x0[i, j] + cnt_x1[i, j] <= 1.0)This way, we can count the occurrence of 0, 1, and number in(0,1). But as Matthias mentioned, there will be a problem with tolerance, which number in (0,1) should meet the constraint that x>0.01 and x < 0.99. Hence the problem becomes that, how to deal with this constraint?Thanks again for your kind and quick response! :)0
投稿コメントは受け付けていません。
コメント
5件のコメント