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

Atmost k constraint in Gurobipy.

ユーザーの入力を待っています。

コメント

6件のコメント

  • 正式なコメント
    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

    Hi Tooba,

    we would need more details to help you better. Are the variables binary, integer, or continuous?

    For a simple case of binaries, you could try:

    \( \sum_{i=1,..,5} x_i \leq 5\).

    Best regards
    Jonasz

    0
  • Tooba Khan
    • Gurobi-versary
    • First Comment
    • First Question

    The variables are continuous.

    0
  • Jonasz Staszek
    • Community Moderator
    • Gurobi-versary
    • Thought Leader
    • First Question

    I guess you'll need BigM constraints. I would advise you to familiarize yourself with this article since such constraints could cause instability during your optimization process.

    For each continuous variable \( v \) you will need to define a binary variable \( x \) to indicate whether \( v \) is zero or not. This can be done via constraints:

    \( v_i \leq M_{i1} x_i  \enspace \forall i \),

    \( -v_i \leq M_{i2} x_i \enspace  \forall i \).

    You can then enforce that at most five variables are non zero by using the following constraint:

    \( \sum_{i} x_i \leq 5\).

    It would be up to you to determine the appropriate values for \( M_{i1}, M_{i2} \) for each \(i\). It should not be too big. Perhaps there are some bounds on the continuous variables you could exploit?

    Could you please tell us more about the problem you are trying to solve? We could perhaps use some other characteristics of the problem to come up with a different approach.

    Best regards
    Jonasz

    0
  • Tooba Khan
    • Gurobi-versary
    • First Comment
    • First Question

    Thank you. The solution worked for me. Although I am using the sum of x_{i}'s as the optimisation function. But Gurobi does not seem to do it correctly. For instance, I want the sum to be minimum and I have put an upper bound of 50 on the sum. But In all cases, Gurobi returns the sum as 50, i.e 50 variable are in the on state. If I decrease the lower bound on sum and make it 25, then a solution exists with sum==25 which means when I use sum<=50 and minimise sum, then a solution with sum==25 existed but Gurobi did not return it to me.

    Can you suggest why is this possibly happening?

    0
  • Jonasz Staszek
    • Community Moderator
    • Gurobi-versary
    • Thought Leader
    • First Question

    Hi Tooba,

    could you share some more details about the model you are solving, or share a minimal reproducible example of the code in which the behavior you describe can be observed?

    We will then be able to help you further.

    Best regards
    Jonasz

    0

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