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

Add constraint

回答済み

コメント

7件のコメント

  • Jaromił Najman
    • Gurobi Staff Gurobi Staff

    You would have to introduce an additional binary variable for every entry of your \(\texttt{goal}\) array. The binary variables would be used to check whether the equality holds as described in the stackexchange post In an integer program, how I can force a binary vairable to equal 1 if some condition holds. Then you can use the addGenConstrOr method to check whether at least one of the equality constraints is fulfilled.

    Best regards, 
    Jaromił

    0
  • KK
    • Gurobi-versary
    • First Comment
    • First Question

    Thank you for answering my question. 

    I still have questions about introducing an additional binary variable for every entry of my goal array.  I forget to say my goal array is some random integer value(e.g.,[1,41,5,37]) the post you provide assume the input variable x is binary or continuous. This might be an issue? 

     

    0
  • Jaromił Najman
    • Gurobi Staff Gurobi Staff

    I forget to say my goal array is some random integer value(e.g.,[1,41,5,37]) the post you provide assume the input variable x is binary or continuous. This might be an issue? 

    Integer values should work the same as continuous but without the "numerical inaccuracy" part. So, since all your \(\texttt{goal}\) values are integer, you could use \(\delta=0.5\).

    0
  • KK
    • Gurobi-versary
    • First Comment
    • First Question

    Hello Jaromil,

    According to the link, how do I code these two formulation in Gurobi

    Mz1 >= x-b+ sigma (1)

    M(1-z1)> b-x-sigma(2)

    As far as I understand, M and sigma is some constant value, z1 is a GRB binary VAR, X and b is GRB VAR.

    Could you give me an example?

     

     

    0
  • Jaromił Najman
    • Gurobi Staff Gurobi Staff

    mip.R is a good example to start with.

    The two inequalities would be constructed via

    model$A      <- matrix(c(1,-1,-M,-1,1,M), nrow=2, ncol=3, byrow=T)
    model$rhs <- c(0.5,M+0.5)
    model$sense <- c('<', '<')

    The above constructs

    \[\begin{align}
    x - b - Mz_1 &\leq 0.5\\
    -x + b + Mz_1 &\leq M + 0.5
    \end{align}\]

    Best regards, 
    Jaromił

    0
  • KK
    • Gurobi-versary
    • First Comment
    • First Question

    Thank you for giving me the example in R! 

    I have tried to code the relationship without matrix( because my original code is in C#)

    x−b−Mz1≤0.5

    −x+b+Mz1≤M+0.5

    I follow your suggest and introduce an additional binary variable for every entry of my goal array. Then I  encode these two inequalities by using "AddGenConstrIndicator" method.

     

    GRBLinExpr fixedLhs = 0.0;
                   
    for (int ti = 0; ti < alltask_CountVars.Count; ti++)
    {
    int taskProVal = GetTaskPropertyVal();
    for (int diffidx = 0; diffidx < 3; diffidx++)
    {
    fixedLhs.AddTerm(taskProVal, alltask_CountVars[ti][diffidx]); //

    }
    }
    for (int i = 0; i < goals.Count; i++)
    {

     //if x>b−sigma, then forces z1=1 , where x is lhs, b is goal[i], z1 is binary var
    model.AddGenConstrIndicator(z1[i], 1, fixedLhs, GRB.GREATER_EQUAL, goals[i] -0.5, "");

    //If x<b−sigma , then (2) forces z1=0
    model.AddGenConstrIndicator(z1[i], 0, fixedLhs, GRB.LESS_EQUAL, goals[i] - 0.5, "");
    //skip z2 here
    }

    //in the end check min_z1+min_z2 -1 >=1
    model.AddConstr(min_z1+min_z2 -1, GRB.GREATER_EQUAL, 1.0, "min_z1+min_z2 -1  >= 1");

     

    The optimization result seems fine, except it only gives solutions that satisfied one goal. (I also set pool search mode = 2. )  I expect the optimizer will give me all solutions that satisfy the constraint " if lhs is equals to any value in the "goal" array."

     

    Thank you in advance!

     

    0
  • Jaromił Najman
    • Gurobi Staff Gurobi Staff

    It seems like you implemented only constraints (1) and (2) from  In an integer program, how I can force a binary vairable to equal 1 if some condition holds. Note that you also need constraints (3) and (4). Alternatively, you could use the formulation posted in the last comment of the stackexchange thread.

    I expect the optimizer will give me all solutions that satisfy the constraint " if lhs is equals to any value in the "goal" array."

    You have exactly one \(\texttt{fixedLhs}\) object. So as long as there are not 2 same \(\texttt{goal}\) values, it is not possible that more that one \(\texttt{goal}\) entry equals \(\texttt{fixedLhs}\) at the same time.

    Best regards, 
    Jaromił

    0

サインインしてコメントを残してください。