Skip to main content

GenConstrIndicator

Answered

Comments

7 comments

  • David Torres Sanchez
    • Gurobi Staff

    Hi,

    Please follow the arguments in the documentation:  GRBModel::addGenConstrIndicator

    Cheers, 
    David

    0
  • Alessandra Vieira
    • Gurobi-versary
    • Conversationalist
    • First Question

    Hi David, 

    I had tried to follow the documentation examples but I am still getting the same error. Could you give an example of using C++ / VS?  , previous settings:  

     for ( gen = 0; gen <N_GEN ; gen++) {

                cdht[gen] = model.addVar(0.0, 1.0, 0.0, GRB_BINARY);
            }

     GRBGenConstr* orCDHT = new GRBGenConstr; 
     GRBQuadExpr*cdexpr = new GRBQuadExpr[N_GEN];
           

    orCDHT=&model.addGenConstrOr(*cdht, *q, N_GEN * T);

    GRBGenConstr addGenConstrIndicator(*cdht,1.0,*cdexpr == 0);

    In the snippet code is indicated that * from cdht is not correct. 

    ERROR MESSAGE (in error list) :
    Error (active)    E0289    804    no instance of constructor "GRBGenConstr::GRBGenConstr" matches the argument list   

     

    Thanks in advance! 

    0
  • David Torres Sanchez
    • Gurobi Staff

    The first argument in GRBModel::addGenConstrIndicator() is a single binary variable itself not a pointer to an array of variables.
    You need to loop over \(\texttt{gen}\) and add a single indicator constraint for each of the binary variables.

    Cheers, 
    David

    0
  • Alessandra Vieira
    • Gurobi-versary
    • Conversationalist
    • First Question

     

    Hi David! 

    Thanks for the answer ! 

    The problem is that I need a constraint to generate a condition for another constraint and not a variable, so would be GenConstrIndicator( expr1== value, *binvar,1.0,len,"name");

    The binvar is array  ==> binvar[len]; If I understood correctly the syntax that you purpose is not acceptable as I need it.

    Could you give an example?   I found it ORConstraint but doesn't work as well, so I do not know what I am doing wrong.

    ctr[gen]=model.addGenConstrOr(cdht[len], q[len], T, "cdht");

    obj += value* cdht[len];

    Really thank you for your Help! 

    0
  • David Torres Sanchez
    • Gurobi Staff

    Hi Alessandra,

    Could you please share the error that you are seeing?
    So, you want \(\texttt{expr1 == value}\) implies that  \(\texttt{binvar[len] == 1}\)?
    Would \(\texttt{binvar[len] == 1}\) implies \(\texttt{expr1 == value}\) work?
    Please note that the expression must a linear expression and not a quadratic one.

    // define coeff as vector of float
    // define someothervar as vector of GRBVar
    for (int gen = 0; gen < N_GEN; ++gen) { bvars[gen] = model.addVar(0.0, 1.0, 0.0, GRB_BINARY);
    GRBLinExpr myexpr; // You have to populate the linear expression
    for (int term = 0; term < TERML; ++term) {
    myexpr += coeff[term] * someothervar[term]
    }
    model.addConstrIndicator(bvars[gen], 1.0, myexpr == 0.0);
    }

    Here you can find a more complete example genconstr_c++.cpp.

    Cheers, 
    David

    0
  • Alessandra Vieira
    • Gurobi-versary
    • Conversationalist
    • First Question

    Hallo David,

    Thanks for your answer! 

    What I need is exactly what stays at your first question "expr1==value implies that binvar[len]=1", the vice versa would be a problem since I don't know to turn this binvar off, meaning 0 as value. I also need this binvar becomes 1 just once.

    OR you may have a better idea of how  I make once a variable that is ON , one function will be added to the cost function. After that, the same variable turns one again, as is not the first time another value will be added to the cost function. 

    for (int gen = 0; gen < N_GEN; ++gen) {
      bvars[gen] = model.addVar(0.0, 1.0, 0.0, GRB_BINARY);
      GRBLinExpr myexpr; 
      for (int term = 0; term < TERML; ++term) {
        myexpr += coeff[term] * someothervar[term]
      }
      model.addConstrIndicator(binvar, 1.0, obj=full_Cost*binvar);
    }


    Cheers,
    Alessandra.

    0
  • David Torres Sanchez
    • Gurobi Staff

    Hi Alessandra,

    What I need is exactly what stays at your first question "expr1==value implies that binvar[len]=1", the vice versa would be a problem since I don't know to turn this binvar off, meaning 0 as value. I also need this binvar becomes 1 just once.

    If I'm not mistaken, you can write:

    • \(\texttt{binvar[len] == 1}\) implies \(\texttt{expr == value}\)
    • \(\texttt{binvar[len] == 0}\) implies \(\texttt{expr <= value - eps}\)

    This way, we are sure that when the binary variable takes the value 1 (which will happen only once), the constraint is fulfilled, when it is 0, then the constraint is strictly less than the value (for a chosen small value \(\texttt{eps}\) e.g. 1e-3.

    In code:

    for (int gen = 0; gen < N_GEN; ++gen) {
      bvars[gen] = model.addVar(0.0, 1.0, 0.0, GRB_BINARY);
      GRBLinExpr myexpr; // You have to populate the linear expression
      for (int term = 0; term < TERML; ++term) {
        myexpr += coeff[term] * someothervar[term]
      }
      model.addConstrIndicator(bvars[gen], 1.0, myexpr == value);
    model.addConstrIndicator(bvars[gen], 0.0, myexpr <= value - 1e-3);
    }

    (assuming a positive expression, otherwise things get a bit more complicated)

    Cheers, 
    David

    0

Please sign in to leave a comment.