Skip to main content

Issue with indicator constraint in GUROBI C++

Awaiting user input

Comments

5 comments

  • Marika Karbstein
    Gurobi Staff Gurobi Staff

    Could you please explain in a bit more detail which relation you want to express?
    From your description, it sounds like w3 should be a variable but in the code snippet, it is a certain integer value.
    In the code, you have a binary variable z4 and an integer variable z3. The indicator constraint you added ensures that whenever the variable z4 is 1 then the variable z3 is equal to the value w3.
    But what do you want to achieve?

    0
  • Mohammad Humyun Fuad Rahman
    Gurobi-versary
    Curious
    First Comment

    Dear Marika,


    Thank you for your prompt reply! In the above code, w3 is a normal integer type variable in C++. Now, my intention is to store an integer value (say x) w3 from the set of integer values 0 to n_ws (0<=x<=n_ws), given that the binary decision variable z4 is true or 1 at x, i.e., z4 [local_b] [x]==1.

    In the later stage of the program, the value of w3 is used as an index of an array to calculate the objective function values and the decision variable z4 can not be used as an index of an array, like a normal variable in C++. Now, could you provide me suggestions on how to link the relationship between z4 and w3?

    Please let me know your concerns. 

    0
  • Marika Karbstein
    Gurobi Staff Gurobi Staff

    If I understand you correctly, in the code snippet, you sent, you want to use w3 as index for an array a.
    Maybe the following is possible, instead of storing the index, you could store the value of the array as variable w3. I assume that the array contains continuous values:

    GRBVar w3 = model.addVar(0, 100, 1, GRB_CONTINUOUS);
    GRBVar z4 = model.addVar(0, 1,1, GRB_BINARY);
    for (int local_c = 0; local_c < n_ws; local_c++)
    {
      for (int local_b = 0; local_b < n_robo; local_b++)
      {
        model.addConstr(z4[local_b][local_c] * a[local_c] == w3);
     }
    }

    Whenever z4[local_b][local_c] is true, w3 is equal to a[local_c] (the value of the array for the respective index) and 0 otherwise.
    Please let me know if I misunderstood you.

    0
  • Mohammad Humyun Fuad Rahman
    Gurobi-versary
    Curious
    First Comment

    I have implemented this concept assuming that a is a normal array type variable with the following expression.

     

    int *a = new int [n_robo]; 
    GRBVar
    w3 = model.addVar(0, 100, 1, GRB_CONTINUOUS); GRBVar z4 = model.addVar(0, 1,1, GRB_BINARY); for (int local_b = 0; local_b = job; local_b++) {   for (int local_c = 0; local_c < n_ws; local_c++)   {     for (int local_b = 0; local_b < n_robo; local_b++)     {         model.addConstr(z4[local_b][local_c] * a[local_c] == w3);      }   } }

    Unfortunately, it didn't work as the value of the array for the respective index is not stored. I have also tried to add integer values to  a but it also doesn't work as the last value of w3 is returned, regardless the constraint is satisfied or not. Just wondering if there another way to fix this issue. Thanks.

    int *a = new int [n_robo]; 
    for (int local_i=0; local_i<n_robo; local_i++)
    a [local_i] =local_i;
    int
    w3; GRBVar z4 = model.addVar(0, 1,1, GRB_BINARY); for (int local_b = 0; local_b = job; local_b++) {   for (int local_c = 0; local_c < n_ws; local_c++)   {     for (int local_b = 0; local_b < n_robo; local_b++)     {
    w3 = local_b;         model.addConstr(z4[local_b][local_c] * a[local_c] == w3);      }   } }

     

     

    0
  • Marika Karbstein
    Gurobi Staff Gurobi Staff

    In the later stage of the program, the value of w3 is used as an index of an array to calculate the objective function values..

    I thought that the array is already defined and the Gurobi variable z4 determines which value of the array should be used. I assumed that z4 can only be 1 for one index and this index also defines the index of the array. But then I do not understand 

    Unfortunately, it didn't work as the value of the array for the respective index is not stored.

    Could you explain your use case in a bit more detail? What are the actual optimization variables and what do you want to optimize? Maybe an example of a possible outcome would also be helpful.

    In an optimization model, only the Gurobi variables are computed. For example,

    int a = local_c;
    int
    w3 = local_b; GRBVar z4 = model.addVar(0, 1,1, GRB_BINARY); model.addConstr(z4 * a == w3);

    The value of z4 is changeable but a and w3 are fixed input data. So yes, w3 will have always the last value it was assigned to.

    Additionally, I did not realize so far, but you use the same index variable local_b for your nested for-loops and the first for loop is also strange.

    for (int local_b = 0; local_b = job; local_b++)
    {
       for (int local_c = 0; local_c < n_ws; local_c++)
       {
          for (int local_b = 0; local_b < n_robo; local_b++)
          {..
    0

Please sign in to leave a comment.