メインコンテンツへスキップ

How to limit the occurrences of variables with a specific value?

回答済み

コメント

4件のコメント

  • 正式なコメント
    Simranjit Kaur
    • Gurobi Staff
    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.
  • Jonasz Staszek
    • Community Moderator
    • Gurobi-versary
    • Thought Leader
    • First Question

    Cross-posted here

    0
  • Silke Horn
    • Gurobi Staff

    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
  • yikun wang
    • Gurobi-versary
    • First Comment
    • First Question

    Cross-posted here

    Thanks again for your kind and quick respond! :)

    0

投稿コメントは受け付けていません。