if conditions for addConstrs ?
AnsweredThis is an energy consumption model.
There are 286 power consuming bulbs that are turned on for only between a specific time frame in a day (hoursofsll - list of binary variables).
During the hours in which light in turned on each bulb has a min and max kWh. Providing this min and max KWh usage conditions assigns the bulb to be switched on during the hours in which they should be turned off.
#each light bulb cannot exceed or fall behind the min and max
model.addConstrs(Psl[t1,n] <= p_sl_max for t1 in range(T) for n in range (N))
model.addConstrs(Psl[t1,n] >= p_sl_min for t1 in range (T) for n in range (N))
I think the below constraint is creating the issue but this cannot be taken out from the model to ensure min. kWh usage during times when bulbs are switched on. What can I do ?
model.addConstrs(Psl[t1,n] >= p_sl_min for t1 in range (T) for n in range (N))
N= 200 #number of bulbs
p_sl_ttl = 292.864 #total amount of kWh consumed in a day
T = 24
Psl = model.addVars(T,N, name ='light')
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]
model.addConstrs(gp.quicksum(Psl[t1,n]* hoursof_sll[t1] for n in range (N)) <= p_sl_ttl for t1 in range(T))
-
Official comment
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?. -
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 -
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 -
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 -
Thank you ! the model works!!!
0 -
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 -
This conversation continues here.
0 -
is it possible to send the jupyter notebook file ?
0
Post is closed for comments.
Comments
8 comments