How to limit the occurrences of variables with a specific value?
回答済み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. -
Cross-posted here
0 -
Hi,
You could add some auxiliary binary variables; some that are 1 if and only if X > 0 and some others that are 1 if and only if X < 1.
B0 = m.addVars(M, N, vtype=GRB.BINARY)
B1 = m.addVars(M, N, vtype=GRB.BINARY)To model that B0[m,n]=1 if X[m,n] > 0, you could simply do
m.addConstrs(B0[m,n] >= X[m,n] for m in M for n in N)
That way, if X > 0 (by more than the tolerance), B0 will always be 1. And conversely:
m.addConstrs(1 - B1[m,n] <= X[m,n] for m in M for n in N)
Then B1 will always be 1 if X < 1 (again, by more than the tolerance).
Note though that those constraints would only work in one direction. That is, if X=0, B0 could still become 1. If you do need the other direction, you may want to look into Jonasz's answer to your other post.
However, you don't need this other direction if you simply want to limit the number of values strictly between 0 and 1: For a variable at the boundary (0 or 1), exactly one of B0 or B1 will be one. For one strictly between 0 and 1, both will be one. So to limit this number to a maximal value A, you could do:
m.addConstr(B0.sum() + B1.sum() <= len(M) * len(N) + A)
0 -
Cross-posted here
Thanks again for your kind and quick respond! :)
0
投稿コメントは受け付けていません。
コメント
4件のコメント