Skip to main content

if conditions for addConstrs ?

Answered

Comments

8 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?.
  • Jaromił Najman
    • Gurobi Staff

    Hi Olivia,

    It is not fully clear what is the exact problem here. Could you elaborate?

    If you want to add the min/max constraint only if a bulb is on, you could introduce additional binary variables \(b_{t1,n}\). These binaries are equal \(1\) if a bulb is on and \(0\) otherwise. You can then use these binaries and indicator constraints to model

    \[b_{t1,n} \rightarrow  p_{\min} \leq P_{t1,n}\\
    b_{t1,n} \rightarrow  p_{\max} \geq P_{t1,n}\]

    Best regards,
    Jaromił

    0
  • Olivia Park
    • Gurobi-versary
    • Conversationalist
    • Curious

    Dear Jaromil, 

    Thank you for the response. 

    My list below (hoursof_sll) which has 24 items indicates the specific time in which the bulbs are turned on (1) and off(0). 

    print(hoursofsll) 
    >>>[0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0]

    Would it be possible if an expression like this exists,,? i see this in https://www.gurobi.com/documentation/9.1/refman/py_model_addconstrs.html 

    but can't seem to apply this to my model.. 

    model.addConstrs(Psl[t1,n] >= p_sl_min for t1 in range (T) for n in range (N)
    if hoursof_sll[i]!= 0)
    0
  • Jaromił Najman
    • Gurobi Staff

    Hi Olivia,

    If the list \(\texttt{hoursof_sll}\) is fixed and given a priori, then your approach is correct. However, in your addConstrs call you are accessing the \(\texttt{hoursof_sll}\) via the index \(\texttt{i}\) but I guess it should be index \(\texttt{t1}\). So your code should read:

    model.addConstrs(Psl[t1,n] >= p_sl_min for t1 in range (T) for n in range (N)
    if hoursof_sll[t1]!= 0)

    Best regards,
    Jaromił

    0
  • Olivia Park
    • Gurobi-versary
    • Conversationalist
    • Curious

    Thank you ! the model works!!!

    0
  • Olivia Park
    • Gurobi-versary
    • Conversationalist
    • Curious

    Dear Jaromil, 

    I'm not sure If I can post a follow up question with this thread - I just had one more question. 

    with the above condition, I added one more variable with identical constraints. After adding this third constraint, I receive the bottom message, am I not allowed to insert a third or fourth variable with similar constraints?

    Prl = model.addVars(T,R, name = 'irrigation')
    R = 4

    #irrigation
    p_r_min = 0.85
    p_r_max = 0.88
    p_r_ttl = 4.4

    #each irrigation motor cannot exceed or fall behind the min max
    model.addConstrs(Prl[t1,r] <= p_r_min for t1 in range(T) for r in range (R))
    model.addConstrs(Prl[t1,r] >= p_r_max for t1 in range(T) for r in range (R)
    if hoursof_ir[t1]!= 0)

    KeyError: 0
    
    During handling of the above exception, another exception occurred:
    
    KeyError                                  Traceback (most recent call last)
    <ipython-input-3-33f692cd7f9e> in <module>
        121 #each irrigation motor cannot exceed or fall behind the min max
        122 model.addConstrs(Prl[t1,r] <= p_r_min for t1 in range(T) for r in range (R))
    --> 123 model.addConstrs(Prl[t1,r] >= p_r_max for t1 in range(T) for r in range (R)
        124                 if hoursof_ir[t1]!= 0)
        125 
    
    src/gurobipy/model.pxi in gurobipy.Model.addConstrs()
    
    <ipython-input-3-33f692cd7f9e> in <genexpr>(.0)
        122 model.addConstrs(Prl[t1,r] <= p_r_min for t1 in range(T) for r in range (R))
        123 model.addConstrs(Prl[t1,r] >= p_r_max for t1 in range(T) for r in range (R)
    --> 124                 if hoursof_ir[t1]!= 0)
        125 
        126 #supplemental lighting constraint multiplying with time so that it operates during 0500-2300
    
    ~/Documents/Programming_with_Python/anaconda3/lib/python3.8/site-packages/pandas/core/frame.py in __getitem__(self, key)
       2798             if self.columns.nlevels > 1:
       2799                 return self._getitem_multilevel(key)
    -> 2800             indexer = self.columns.get_loc(key)
       2801             if is_integer(indexer):
       2802                 indexer = [indexer]
    
    ~/Documents/Programming_with_Python/anaconda3/lib/python3.8/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
       2646                 return self._engine.get_loc(key)
       2647             except KeyError:
    -> 2648                 return self._engine.get_loc(self._maybe_cast_indexer(key))
       2649         indexer = self.get_indexer([key], method=method, tolerance=tolerance)
       2650         if indexer.ndim > 1 or indexer.size > 1:
    0
  • Jaromił Najman
    • Gurobi Staff

    This conversation continues here.

    0
  • Olivia Park
    • Gurobi-versary
    • Conversationalist
    • Curious

    is it possible to send the jupyter notebook file ?

    0

Post is closed for comments.