Skip to main content

Indicator constraint which contains constant value

Answered

Comments

6 comments

  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    You get this error because you are setting a constant equal to another constant in an indicator constraint, but as you said, you know that already. You are trying to use \(t\) to control the index in the sums defined in the \(\texttt{tmp_f}\) function. You can directly use the \(b_i\) variables for that.

    # Indicator constraints
    sum_expression = gp.LinExpr(0)
    for i in range(10):
      sum_expression.add(b[i] + 2*b[i] + 1)

    # Objective function
    obj = gp.quicksum(x[i] + y[i] for i in range(10))
    m.setObjective(obj + sum_expression, GRB.MAXIMIZE)

    The \(\texttt{sum_expression}\) object equals

    \[\sum_{i=0}^9 (b_i + 1)^2\]

    which is equivalent to what you are trying to model with the \(\texttt{z}\) list. Note that \((b_i + 1)^2 = b_i + 2\cdot b_i + 1\), because \(b_i\) are binaries and \(b_i^2 = b_i\).

    Best regards, 
    Jaromił

    0
  • Namkyoung Lee
    Conversationalist
    First Question

    Hi Jaromil,

    You demonstration is very informative as always. I adopted indicator constraint that previously you recommended and focused on programming which means that constant A equals constant B, in the right side of indicator constraint operator, >>.
    At the last question, I should have depicted the indicator constraint more detail. My apologize for the inconvenience. In the sample code, I used t to get the value of index i of b[i]. And t becomes input for the formulation included in objective function. 
    For instance, if b[1], b[3] and b[5] ==1, then t=1, 3, 5. However, in the code, this part brought the error, TypeError: unsupported operand type(s) for -=: 'NoneType' and 'float'. At this point, I have no idea how to fix it. If the error is solved, z becomes [1,3,5]. And z is entered as parameter of tmp_f(), a part of objective.
    Rephrasing the question, I want to get the index value of binary variable using indicator constraint.

    Best regards,

    Namkyoung Lee

    0
  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    Hi Namkyoung Lee,

    Note that through the \(b_i\) variables you know which indices are used through \(b_i\) being equal to either \(1\) or \(0\). Thus, you don't need additional variables \(t\) or \(z\) to model you particular sum. The sum you want to model is adding either the square of \(1\) or \(2\) depending on whether a given index \(t\) is used or not. For example for \(k = [0,0,1,1,0,1,0,1,0,0]\), you would like to compute the sum

    \[\begin{align}
    &\,\sum_{i=0}^9 (k_i+1)^2 \\
    =  &\,6\cdot (0+1)^2 + 4 \cdot(1+1)^2 \\
    = & \,22
    \end{align}\]

    To model the above, you can use the binary variable you already defined. They define your list \(k\). With them you can model your sum as stated in my previous message

    \[\sum_{i=0}^9 (b_i + 1)^2 = \sum_{i=0}^9 (b_i + 2\cdot b_i + 1)\]

    I hope this helps.

    Best regards, 
    Jaromił

    0
  • Namkyoung Lee
    Conversationalist
    First Question

    Hi Jaromił,

    Many thanks for every moment.

    The module included in objective function, tmp_f() merely represents a simplified form of my real formula.

    When I consulted a month ago, you mentioned solver-based approach such as auxiliary variable and Gurobi methods. Unfortunately, the real formula is very complicated and hardly possible to be modified so as to fit into solver syntax. 
    Therefore, I have explored how to access attributes of variable before optimization and next how to get index value of binary variable. Although your latest comment is perfect, I used t and z in the model to extract a set of binary variable's index value in order to input to the real formula. With regard to this issue, I think it's just a kind of programming problem rather than modeling.

    Recalling the original code snatch of this post, I simply want to get the list, z, using indicator constraint for input parameter for my actual module. 

    Best regards,

    Namkyoung

    0
  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    Although your latest comment is perfect, I used t and z in the model to extract a set of binary variable's index value in order to input to the real formula. With regard to this issue, I think it's just a kind of programming problem rather than modeling.

    Recalling the original code snatch of this post, I simply want to get the list, z, using indicator constraint for input parameter for my actual module. 

    If you want that function \(\texttt{tmp_f}\) is part of the model you are going to solve then what you are trying to achieve is not possible. All modeling objects have to be constructed before the optimization has started. In other words, there is no way to dynamically adjust constraints or the objective during an optimization run. Thus, one has to somehow model the dynamical behavior through additional variables and constraints.

    If however, it is enough for you to compute \(\texttt{tmp_f}\) after a successful optimization run, then you can first construct your model without \(\texttt{tmp_f}\). Next, get values of variables \(b_i\) via their X attribute. Once you have the solution values of \(b_i\), you can construct your list \(\texttt{z}\) and re-optimize the model. Based on the description in your other post I doubt that this is what you are looking for.

    Best regards, 
    Jaromił

    0
  • Namkyoung Lee
    Conversationalist
    First Question

    Hi Jaromił,

    Thanks to you, I am able to realize only now the optimizing behavior of model.
    With regard to the formula issue, I am going to take another measures that implement LP relaxation, modeling by stages and so on.

    Recently I would like to meet you in person and implore your detail advice with showing my full code. I am sorry to be a bother and somewhat uncomfortable for a while.
    I will revert to you with better subject next time. Thank you very much.

    Best regards,

    Namkyoung

    0

Please sign in to leave a comment.