Erreur in my code with gurobipy
Answeredhello I need you help to correct my code
import gurobipy as gp
from gurobipy import GRB
months = range(5)
products=range(4)
L=100
Dem_client=[[0,0,10,0,0],
[0,20,0,0,0],
[0,0,0,0,15],
[0,10,0,0,25]]
Cout_stockage=[[1,2,3,1,2],
[6,9,5,1,2],
[2,1,4,1,3],
[2,2,1,1,4]]
Cout_chan=[[2,5,8,3,9],
[1,3,2,4,1],
[8,9,7,7,4],
[1,2,1,1,3]]
m=gp.Model('LSP')
x=m.addVars(products,months, lb=0.0)
s=m.addVars(products,months,lb=0.0,obj=Cout_stockage)
y=m.addVars(products,months,obj=Cout_chan,vtype=GRB.BINARY)
m.modelSense = GRB.MINIMIZE
m.addConstrs((x.sum('*',month) <=L)for month in months)
m.addConstrs(s[p][m-1]+x[p][m]==Dem_client[p][m]+s[p][m] for p in products for m,index_m in months if m!=1)
for p in months:
print (p)
m.optimize()
this is the erreur
m.addConstrs(s[p][m-1]+x[p][m]==Dem_client[p][m]+s[p][m] for p in products for m, index_m in months if m!=1)
TypeError: cannot unpack non-iterable int object
-
Thank you for providing a reproducible example.
There are a few issues with your \(\texttt{addConstrs}\) statement. You are using \(\texttt{m}\) as index which simultaneously is the name of your model object. You define \(\texttt{index_m}\), which is not used. Gurobi tupledicts are accessed via standard dict indexing \(\texttt{s[p,m-1]}\) instead of \(\texttt{s[p][m-1]}\). Finally, you check for \(\texttt{m!=1}\) which results in accessing \(\texttt{s[p,-1]}\), which is not defined.
The following should work in your case
m.addConstrs((s[p,M-1]+x[p,M] == Dem_client[p][M]+s[p,M] for p in products for M in months if M!=0))
Best regards,
Jaromił0 -
thanks a lot but the best objective is not correct it should be 20 :
this is the Mathematical formulation:
0 -
You can use the write method to write a human readable LP file and analyze which constraints are incorrect or missing. You should provide meaningful names to all your variables and constraints before doing so.
x=m.addVars(products,months, lb=0.0, name="x")
# [...]
m.write("myModel.lp")
m.optimize()At a first glance the constraints
\[x_{p,M} \leq L y_{p,M} \,\, \forall p \in \text{products}, M \in \text{months}\]
are missing.Best regards,
Jaromił0 -
thanks a lot, Sir but the human-readable LP doesn't work on my Laptop. also, I didn't get the best objective function :
import gurobipy as gp
from gurobipy import GRB
months = range(5)
products=range(4);
L=100
Dem_client=[[0,0,10,0,0],
[0,20,0,0,0],
[0,0,0,0,15],
[0,10,0,0,25]]
Cout_stockage=[[1,2,3,1,2],
[6,9,5,1,2],
[2,1,4,1,3],
[2,2,1,1,4]]
Cout_chan=[[2,5,8,3,9],
[1,3,2,4,1],
[8,9,7,7,4],
[1,2,1,1,3]]
m=gp.Model('LSP')
#x=m.addVars(products,months, lb=0.0)
x=m.addVars(products,months, lb=0.0, name="x")
s=m.addVars(products,months,lb=0.0,obj=Cout_stockage)
y=m.addVars(products,months,obj=Cout_chan,vtype=GRB.BINARY)
m.modelSense = GRB.MINIMIZE
m.addConstrs((x.sum('*',month) <=L)for month in months)
m.addConstrs(x[p,M]<=L*y[p,M] for p in products for M in months)
#m.addConstrs(s[p][m]+x[p][m]==Dem_client[p][m]+s[p][m] for p in products for m in months)
m.addConstrs(s[p,M-1]+x[p,M]==Dem_client[p][M]+s[p,M] for p in products for M in months if M>1)
m.write("LSP.lp")
for p in months:
print (p)
m.optimize()Best objective 1.500000000000e+01, best bound 1.500000000000e+01, gap 0.0000%
0 -
Please note that Python indexing starts with \(\texttt{0}\). Thus, the last set of constraints should read
m.addConstrs((s[p,M-1]+x[p,M] == Dem_client[p][M]+s[p,M] for p in products for M in months if M>=1))
thanks a lot, Sir but the human-readable LP doesn't work on my Laptop
What exactly do you mean by "it doesn't work"? Is no file generated? The file should be generated in the execution folder. You could also search your machine for a file named "LSP.lp".
Best regards,
Jaromił0 -
You can let the variable index start at 1 via
months = range(1,6)
products = range(1,5)but then you have to adjust the access of the \(\texttt{Dem_client}\) matrix
m.addConstrs((s[p,M-1]+x[p,M] == Dem_client[p-1][M-1]+s[p,M] for p in products for M in months if M>1))
You can find more on Python's standard functions and objects at, e.g., W3Schools.
Best regards,
Jaromił0 -
Thanks a lot Sir
0 -
I have another error with lists : mdl.addConstrs((s[month-1,product]+x[month,product])==(Dem_client[month][product]+s[month,product])
KeyError: (-1, 0)# 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!=months[0])
mdl.optimize()0 -
Note that the values of your \(\texttt{months}\) list are now Strings and thus, it is not possible to subtract \(1\) from it. If you want to stick with the String values, you have to redefine your \(\texttt{for}\)-loops in the \(\texttt{addConstrs}\) statement
mdl.addConstrs((s[previous_month,product]+x[current_month,product])==(Dem_client[current_month][product]+s[current_month,product])
for previous_month,current_month in zip(months,months[1:]) for product in products)Please note that this issue is no longer a Gurobi issue but a pure Python issue and you should refer to a Python forum/tutorial for more insights.
On a side note, setting of the objective coefficients will not work if you use a "double dictionary". You should either stick with a pure value matrix as in your first post or rewrite the dictionary to hold tuples as keys
Cout_stockage= {
("Jan","prod1"):1,("Jan","prod2"):6,("Jan","prod3"):2,("Jan","prod4"):2,
("Fev","prod1"):2,("Fev","prod2"):9,("Fev","prod3"):1,("Fev","prod4"):2,
("Mar","prod1"):5,("Mar","prod2"):5,("Mar","prod3"):4,("Mar","prod4"):1,
("Apr","prod1"):1,("Apr","prod2"):1,("Apr","prod3"):1,("Apr","prod4"):1,
("May","prod1"):2,("May","prod2"):2,("May","prod3"):3,("May","prod4"):4}
Cout_chan={
("Jan","prod1"):2,("Jan","prod2"):1,("Jan","prod3"):8,("Jan","prod4"):1,
("Fev","prod1"):5,("Fev","prod2"):3,("Fev","prod3"):9,("Fev","prod4"):2,
("Mar","prod1"):8,("Mar","prod2"):2,("Mar","prod3"):7,("Mar","prod4"):1,
("Apr","prod1"):3,("Apr","prod2"):4,("Apr","prod3"):7,("Apr","prod4"):1,
("May","prod1"):9,("May","prod2"):1,("May","prod3"):4,("May","prod4"):3}Best regards,
Jaromił0 -
thanks Sir , but the objective result is not correct it is 0: Best objective 0.000000000000e+00, best bound 0.000000000000e+00, gap 0.0000%
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,("Jan","prod2"):6,("Jan","prod3"):2,("Jan","prod4"):2,
("Fev","prod1"):2,("Fev","prod2"):9,("Fev","prod3"):1,("Fev","prod4"):2,
("Mar","prod1"):5,("Mar","prod2"):5,("Mar","prod3"):4,("Mar","prod4"):1,
("Apr","prod1"):1,("Apr","prod2"):1,("Apr","prod3"):1,("Apr","prod4"):1,
("May","prod1"):2,("May","prod2"):2,("May","prod3"):3,("May","prod4"):4}
Cout_chan={
("Jan","prod1"):2,("Jan","prod2"):1,("Jan","prod3"):8,("Jan","prod4"):1,
("Fev","prod1"):5,("Fev","prod2"):3,("Fev","prod3"):9,("Fev","prod4"):2,
("Mar","prod1"):8,("Mar","prod2"):2,("Mar","prod3"):7,("Mar","prod4"):1,
("Apr","prod1"):3,("Apr","prod2"):4,("Apr","prod3"):7,("Apr","prod4"):1,
("May","prod1"):9,("May","prod2"):1,("May","prod3"):4,("May","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.addConstrs((s[previous_month,product]+x[current_month,product])==(Dem_client[current_month][product]+s[current_month,product])
for previous_month,current_month in zip(months,months[1:]) for product in products)
mdl.optimize()0 -
You explicitly set the objective to equal \(0\)
mdl.setObjective(0, GRB.MINIMIZE)
Best regards,
Jaromił0 -
Thanks a lot, Sir for helping a beginner like me.
0
Please sign in to leave a comment.
Comments
12 comments