Error with lists
Answeredimport numpy as np
import pandas as p
# Parameters and Data
products=["prod1","prod2","prod3","prod4"] # number of products
months=["Jan","Fev","Mar","Apr","May"] #number of periode
Dem_client={
"Jan":{"prod1":0,"prod2":0,"prod3":0,"prod4":0},
"Fev":{"prod1":0,"prod2":20,"prod3":0,"prod4":10},
"Mar":{"prod1":10,"prod2":0,"prod3":0,"prod4":0},
"Apr":{"prod1":0,"prod2":0,"prod3":0,"prod4":0},
"May":{"prod1":0,"prod2":0,"prod3":15,"prod4":25}
}
Cout_stockage= {
"Jan":{"prod1":1,"prod2":6,"prod3":2,"prod4":2},
"Fev":{"prod1":2,"prod2":9,"prod3":1,"prod4":2},
"Mar":{"prod1":5,"prod2":5,"prod3":4,"prod4":1},
"Apr":{"prod1":1,"prod2":1,"prod3":1,"prod4":1},
"May":{"prod1":2,"prod2":2,"prod3":3,"prod4":4}}
Cout_chan={
"Jan":{"prod1":2,"prod2":1,"prod3":8,"prod4":1},
"Fev":{"prod1":5,"prod2":3,"prod3":9,"prod4":2},
"Mar":{"prod1":8,"prod2":2,"prod3":7,"prod4":1},
"Apr":{"prod1":3,"prod2":4,"prod3":7,"prod4":1},
"May":{"prod1":9,"prod2":1,"prod3":4,"prod4":3}}
L=100# capacité limite de production
# importing gurobipy package
from gurobipy import *
# creating an optimization model
mdl=Model('LSP')
# add decision variables
x=mdl.addVars(months,products,name="x")
s=mdl.addVars(months,products,obj=Cout_stockage,name="s")
y=mdl.addVars(months,products,obj=Cout_chan,vtype=GRB.BINARY,name="y")
#define the objective function
mdl.modelSense = GRB.MINIMIZE
#mdl.setObjective(obj, GRB.MINIMIZE)
mdl.setObjective(0, GRB.MINIMIZE)
#factory.setObjective(obj, GRB.MAXIMIZE)
# Add constraints
mdl.addConstrs((x.sum(month,'*') <=L )for month in months)
mdl.addConstrs((x[month,product]<=L*y[month,product]) for month in months for product in products)
mdl.addConstrs((s[month-1,product]+x[month,product])==(Dem_client[month][product]+s[month,product])
for month in range(len(months)) for product in range(len(products)) if month >=1)
mdl.optimize()
the error is:
mdl.addConstrs((s[month-1,product]+x[month,product])==(Dem_client[month][product]+s[month,product])
KeyError: (0, 0)
-
Official comment
Hi,
The reason this doesn't work, is because the keys for variable s are not numbers, but codes for the month and product. When you create your constraint, you try and use numbers and this doesn't work.
The specific error shows that you're trying to access an element of variable s with keys (0, 0) while you should instead access s["Jan", "prod1"]. The best way to achieve this, is by reusing the lists products and months when you create your constraint.
Best regards,
Ronald -
thanks, Sir but I didn't understand how to reuse the lists of products and months when I create my constraint, can you write the correct constraint, please.
0 -
I still have problem : mdl.addConstrs((s[months[index_month-1],product]+x[month,product])==(Dem_client[month][product]+s[month,product])
ValueError: too many values to unpack (expected 2)import numpy as np
import pandas as p
# Parameters and Data
products=["prod1","prod2","prod3","prod4"] # number of products
months=["Jan","Fev","Mar","Apr","May"] #number of periode
Dem_client={
"Jan":{"prod1":0,"prod2":0,"prod3":0,"prod4":0},
"Fev":{"prod1":0,"prod2":20,"prod3":0,"prod4":10},
"Mar":{"prod1":10,"prod2":0,"prod3":0,"prod4":0},
"Apr":{"prod1":0,"prod2":0,"prod3":0,"prod4":0},
"May":{"prod1":0,"prod2":0,"prod3":15,"prod4":25}
}
Cout_stockage= {
"Jan":{"prod1":1,"prod2":6,"prod3":2,"prod4":2},
"Fev":{"prod1":2,"prod2":9,"prod3":1,"prod4":2},
"Mar":{"prod1":5,"prod2":5,"prod3":4,"prod4":1},
"Apr":{"prod1":1,"prod2":1,"prod3":1,"prod4":1},
"May":{"prod1":2,"prod2":2,"prod3":3,"prod4":4}}
Cout_chan={
"Jan":{"prod1":2,"prod2":1,"prod3":8,"prod4":1},
"Fev":{"prod1":5,"prod2":3,"prod3":9,"prod4":2},
"Mar":{"prod1":8,"prod2":2,"prod3":7,"prod4":1},
"Apr":{"prod1":3,"prod2":4,"prod3":7,"prod4":1},
"May":{"prod1":9,"prod2":1,"prod3":4,"prod4":3}}
L=100# capacité limite de production
# importing gurobipy package
from gurobipy import *
# creating an optimization model
mdl=Model('LSP')
# add decision variables
x=mdl.addVars(months,products,name="x")
s=mdl.addVars(months,products,obj=Cout_stockage,name="s")
y=mdl.addVars(months,products,obj=Cout_chan,vtype=GRB.BINARY,name="y")
#define the objective function
mdl.modelSense = GRB.MINIMIZE
#mdl.setObjective(obj, GRB.MINIMIZE)
mdl.setObjective(0, GRB.MINIMIZE)
#factory.setObjective(obj, GRB.MAXIMIZE)
# Add constraints
mdl.addConstrs((x.sum(month,'*') <=L )for month in months)
mdl.addConstrs((x[month,product]<=L*y[month,product]) for month in months for product in products)
mdl.addConstrs((s[months[index_month-1],product]+x[month,product])==(Dem_client[month][product]+s[month,product])
for month,index_month in months for product in products if month!=months[0])
mdl.optimize()0 -
This has been discussed in Erreur in my code with gurobipy.
0
Please sign in to leave a comment.
Comments
4 comments