problem in modelling constraints
AnsweredHello,
I'm facing some difficulties in modelling a few constraints. I will provide a brief explanation of the variables. I have 4 variables, 2 binary and 2 continuous. and 4 sets of indices, d (number of days, 365), t (number of hours in a year, 8760), p (possible production facilities, 222), and i (possible retailer facilities, 109).
Per day, I have a total demand for my product, which is satisfied by my production facilities. In this case, my production variable is name HP and has two set of indices p and t. p since there are 222 possible production facilities, and t in order to have production in an hourly base as the main production cost is based on the electricity price, which varys hourly and depends on the facilities' locations. Thus, the first constraint is to satisfy the daily demand, and I have done so as follows:
for d in range(365):
HD = Prod_daily_demand[d]
t1 = d * 24
t2 = (d + 1) * 24
m.addConstr(gp.quicksum(HP[p, t] for p in range(num_prod) for t in range(t1, t2, 1)) == HD)
Here HD represents the daily demand, HP is a continuous variable that represents the production at facility p and hour t. I think I have managed to write this constraint correctly.
Afterwards, I want to write other constraints. I know the daily demand of each retailer facility, and I want to add a constraint that states that the daily transportation from production facility p to retailer i is equal to its demand and only possible if transportation between p and i has been established. Here, SCAP_i is the daily demand of each retailer (which is the same across all retailers), HT is a continuous variable and Y[p, i] is a binary variable that represents if transportation between production facility p and retailer i has been established or not.
for d_1 in range(365):
SCAP_i = SCAP[d_1]
m.addConstrs(HT[d_1, p, i] == SCAP_i * Y[p, i] for p in range(num_prod) for i in range(num_retailer))
The constraint I am having trouble modelling is the following. Each day, the production of each facility must be equal to the quantity of product that is being transported from that production facility to retailers. The daily production of each facility is the sum of the hourly production (HP) of the corresponding facility over the 24 hours of the corresponding day. I know that to model the sum of the hourly production per production facility I could write HP.sum(p, '*'), however, this would sum the production of that facility over the entire 8760 hours. I want to do this for the time period that I choose. I've tried to do it so as in below but it prompts this error "KeyError: 'Missing constraint index'". I've also tried it with gp.quicksum(HP[p, t]) instead of HP.sum(p, t), however, it summed the values of every production facility p for that specific time period, instead of summing the values of only one production facility at a time. How can I model this constraint? I've tried several ways without success.
for d_2 in range(365):
t3 = d_2 * 24
t4 = (d_2 + 1) * 24
for p in range(num_prod):
m.addConstrs((HP.sum(p, t) for t in range(t3, t4, 1)) == (HT.sum(d_2, p, '*')))
I have yet another question. By having the constraints above, I have that the production facility p will produce the exact demand of a certain number of retailers. How do I model a constraint that states that transportation between p and i has been established so that the sum, Y.sum(p, '*'), is equal to the number of retailers that production facility p satisfys? Can it be something like this:
for d_3 in range(365):
SCAP_i = SCAP[d_3]
m.addConstrs(NUM_R[d_3, p] * HT.sum(d_3, p, '*') == SCAP_i for p in range(num_prod))
m.addConstrs((Y.sum(p, '*')) == NUM_R[d_3, p] for p in range(num_prod))
-
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 try Gurobot, our chatbot interface offering instant, expert-level support. -
Your initial approach was correct except that you should use addConstr instead of addConstrs
for d_2 in range(365):
t3 = d_2 * 24
t4 = (d_2 + 1) * 24
for p in range(num_prod):
m.addConstr((HP.sum(p, t) for t in range(t3, t4, 1)) == (HT.sum(d_2, p, '*')))For your second question, it's quite hard to help here with the limited information. Is NUM_R a constant value? It sounds like you are trying to model a conditional constraint with an equality condition, i.e., something similar to "if sum(HT) = some value then sum(Y) = some other value", is this correct?
0 -
Dear Jaromił,
Thank you for your fast reply. I've done exatcly as you mentioned and this error was prompted:
TypeError: unsupported operand type(s) for *: 'int' and 'generator'
Regarding the last question I just realised that on of the constraints I already have takes care of I what I intended, thank you anyways.
0 -
Could you try
for d_2 in range(365):
t3 = d_2 * 24
t4 = (d_2 + 1) * 24
for p in range(num_prod):
m.addConstr(quicksum(HP[p, t] for t in range(t3, t4, 1)) == (HT.sum(d_2, p, '*'))If this does not help, could you please post a minimal working example to reproduce the TypeError?
Best regards,
Jaromił0 -
Hi,
Yes, it worked! Thank you.
0
Post is closed for comments.
Comments
5 comments