Skip to main content

use a decision variable as an index

Answered

Comments

9 comments

  • Official comment
    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 why not try our AI Gurobot?.
  • Eli Towle
    • Gurobi Staff

    Hi Nourhan,

    You can't use a variable to index another variable or parameter in the model.

    What are \( \texttt{C} \) and \( \texttt{P} \)? You can often model this by introducing binary variables to capture the value of the variable you want to use as an index (I'll use \( x \) here instead of \(\texttt{Kc}\)). For example, assuming a slightly simplified case where \( x \) takes on a value from the discrete set \(\{1, \ldots, n\}\):

    $$\begin{align*} \sum_{i = 1}^n i u_i &= x \\ \sum_{i = 1}^n u_i &= 1 \\ C \big( \sum_{i = 1}^n P_{i+1} u_i \big) &= z \\ u &\in \{0,1\}^n. \end{align*}$$

    The first two constraints set \( u_k  = 1 \), where \( k = x \). All other values of \( u_i \) are 0. Then, the value of \( P_{x+1} \) is equal to the expression \( \sum_{i = 1}^n P_{i+1} u_i \).

    Thanks,

    Eli

    1
  • Nourhan Ismail
    • Gurobi-versary
    • First Comment
    • First Question

    this is P:

    blocks= list(range(1,8))

    P={}
    for k in blocks:
    P[1]=4
    P[2]=3
    P[3]=5
    P[4]=1
    P[5]=6
    P[6]=3
    P[7]=2

    and i have this constraints that i use to get the value of Kc

    Con_Kc=m.addConstr(quicksum(M[k] for k in blocks)== Kc)

    Now, i have the value of Kc and I want to use it in another constraint as an index,  for example: Kc= 3

    now if want to get the value of P[Kc+1] which is P[4] which means P of block 4 , P[4]=1

    how to get use Kc to be an index to get the value of P for block[Kc+1]?

     

    0
  • Nourhan Ismail
    • Gurobi-versary
    • First Comment
    • First Question

    another question: 

    Con_Kc=m.addConstr(quicksum(M[k] for k in blocks)== Kc)

    can i say :

    for k in blocks:

         if k == Kc:

            Cont_3=m.addConstr(Z== C*P[k+1])

     

    0
  • Eli Towle
    • Gurobi Staff

    Hi Nourhan,

    Like I mentioned, you cannot use \( \texttt{Kc} \) as an index within the model. If you use the modeling approach I posted, the value of \( \texttt{P[Kc+1]} \) is equivalent to the linear expression \( \sum_{i = 1}^n P_{i+1} u_i \).

    For your second question: no, you cannot only add a constraint if the variable \( \texttt{Kc} \) is equal to a certain value. This is because \( \texttt{Kc} \) is a model variable whose value is not fixed. The only way to handle this is to model it using mathematical programming techniques.

    Thanks,

    Eli

     

    0
  • Nourhan Ismail
    • Gurobi-versary
    • First Comment
    • First Question

    it worked ,thank you so much

    0
  • Jingyue Zhang
    • Gurobi-versary
    • Collaborator
    • Curious

    Hi Nourhan and Eli,

    I've met similar questions for programming like your code before:

    for k in blocks:

         if k == Kc:

            Cont_3=m.addConstr(Z== C*P[k+1])

    I'm wondering what exactly are the mathematical programming techniques we're talking about here or the ways to solve this problem

     

    0
  • Eli Towle
    • Gurobi Staff

    The formulation I posted models the logic of enforcing a constraint for a variable index. Note that I named the variable \( x \) instead of \( \texttt{Kc} \).

    The formulation works as follows. Let \( k \in \{1, \ldots, n\} \) satisfy \( k = x \). Then by the first two constraints, we have \( u_k = 1 \) and \( u_i = 0 \) for all \( i \neq k \). Therefore, the third constraint enforces:

    $$\begin{align*}C \Big( \sum_{i = 1}^n P_{i+1} u_i \Big) &= z \\ C (P_{k+1})(1) + C \Big( \sum_{i \neq k} (P_{i+1})(0) \Big) &= z \\ C \cdot P_{k+1} &= z \\ C \cdot P_{x+1} &= z,\end{align*}$$

    as desired.

    0
  • Jingyue Zhang
    • Gurobi-versary
    • Collaborator
    • Curious

    Thanks!!

    0

Post is closed for comments.