I am new to modeling LP with Pulp in Python and I need some help with defining a constraint with multiple variables with different indexes. Please see below.
- The constraint is defined on A, B, C indexes.
Start_Inv
andEnd_Inv
variables are again defined on A, B, C indexes.Production
variable is defined on A, B, C, D indexes.
I want Production
variable to be summed by A, B, C and enter the constraint.
Below is what I wrote and of course it doesn't work.
for A, B, C in Start_Inv_DF.index:
model += Start_Inv[(A, B, C)] + Production[(A, B, C, D)] == End_Inv[(A, B, C)]
Is there an easy way to model this? Probably, I need a coefficient to do "SUM Production
group by A, B, C", can you please advise how to create one?
Adding more details. 1/13/2022
I have tried lpSUM and it works for two indexes but not for three or more. Can you please see what I am doing wrong?
- This 2 indexes case works.
import pandas as pd
import pulp as plp
tuples1 = [('DE', 'Apple'),
('DE', 'Orange'),
('PA', 'Apple'),
('PA', 'Orange')]
index1 = pd.MultiIndex.from_tuples(tuples1, names=["Location", "Product"])
DF1 = pd.DataFrame({'Count A': [12., 70., 30., 20.], 'Count B': [12., 70., 30., 20.]}, index=index1)
Production1 = plp.LpVariable.dicts("Production1",
((Location, Product) for Location, Product in DF1.index),
lowBound=0,
cat='Continuous')
Products = ['Apple', 'Orange']
for Location, Product in DF1.index:
print(plp.lpSum(Production1[(Location, Product)] for Product in Products))
- This 3 indexes case errors out.
import pandas as pd
import pulp as plp
tuples1 = [('DE', 'Apple'),
('DE', 'Orange'),
('PA', 'Apple'),
('PA', 'Orange')]
index1 = pd.MultiIndex.from_tuples(tuples1, names=["Location", "Product"])
DF1 = pd.DataFrame({'Count A': [12., 70., 30., 20.], 'Count B': [12., 70., 30., 20.]}, index=index1)
tuples2 = [('DE', 'Apple', 'Truck'),
('DE', 'Orange', 'Rail'),
('DE', 'Apple', 'Rail'),
('DE', 'Orange', 'Truck'),
('PA', 'Apple', 'Truck'),
('PA', 'Orange', 'Rail')]
index2 = pd.MultiIndex.from_tuples(tuples2, names=["Location", "Product", "Mode"])
DF2 = pd.DataFrame({'Count A': [12., 70., 30., 20., 20., 40], 'Count B': [12., 70., 30., 20., 80., 30.]}, index=index2)
Production2 = plp.LpVariable.dicts("Production2",
((Location, Product, Mode) for Location, Product, Mode in DF2.index),
lowBound=0,
cat='Continuous')
Modes = ['Truck', 'Rail']
for Location, Product in DF1.index:
print(plp.lpSum(Production2[(Location, Product, Mode)] for Mode in Modes))
import pulp
at the beginning, what you need ispulp.lpSum()
method for summing over variable D. So, replace yourProduction[(A,B,C,D)]
withpulp.lpSum(Production[(A,B,C,D] for all D in your_d_index)
$\endgroup$Demand_DF1.index
). If that index is comprised of 3 values of (Month, Product, Customer), then you can't use that to only iterate over Customer. In fact,Customer
infor Customer in Demand_DF1.index
is a tuple of (Month, Product, Customer). If you only wantCustomer
you should use something else. For example,customers = Demand_DF1['Customer'].unique()
and then usefor customer in customers
for your sum. $\endgroup$