GurobiError: Invalid argument to LinExpr multiplication
回答済みHello I am getting the following error: GurobiError: Invalid argument to LinExpr multiplication
here is my code;
from gurobipy import GRB,Model,quicksum #*
import numpy as np # array kullanmak için
import pandas as pd #excel'i taşımak için
def Output(m):
# Print the result
status_code = {1:'LOADED', 2:'OPTIMAL', 3:'INFEASIBLE', 4:'INF_OR_UNBD', 5:'UNBOUNDED'} #this is how a 'dictionary'
#is defined in Python
status = m.status
print('The optimization status is ' + status_code[status])
if status == 2:
# Retrieve variables value
print('Optimal solution:')
for v in m.getVars():
print(str(v.varName) + " = " + str(v.x))
print('Optimal objective value: ' + str(m.objVal) + "\n")
model = Model("CPL_model");
n=5 #number of stations
first_station_set = range(n) # i / i and j # n+1
#second_station_set = range(5) # j
bigM = 1000
m = 3 #number of vehicles
q = [[80,90,110,120,100,120]] #demand of stations
Q=[200,200,200] #capacity of the vehicle
cost=[[5,3,4,6,7,8]]
# Add Decision Varibales
#m = mdl.addVar(lb = 0, vtype = GRB.INTEGER, name = 'm') # vehicle amount
#q = mdl.addVars(first_station_set, vtype = GRB.INTEGER,name = 'Q') # station request
x = model.addVars(first_station_set, first_station_set, vtype=GRB.BINARY, name=["x_"+str(i+1) + "," +str(j+1)
for i in first_station_set for j in first_station_set])
u= model.addVars(first_station_set, lb = 0,vtype = GRB.INTEGER,name = ['u_'+str(i) for i in first_station_set])
#Add constraint
# Constraint 2
model.addConstrs((quicksum(x[i,j] for i in first_station_set) == 1 for j in range(1,n)), name = 'C2')
# Constraint 3
model.addConstrs((quicksum(x[j,i] for i in first_station_set) == 1 for j in range(1,n)), name = 'C3')
# Constraint 4
model.addConstr((quicksum(x[0,j] for j in first_station_set) <= m), name = 'C4')
# Constraint 5
model.addConstr(((quicksum(x[0,j] for j in range(1,n))) == (quicksum(x[j,0] for j in range(1,n)))), name = 'C5')
#BURADA YAZIYORUZ UUUUUUYU
model.addConstrs(((u[i]-u[j] + Q *x[i,j] <= Q - q[j]) for i in range(n) for j in range(n)), name = 'C11')
#Objective FUnction
model.setObjective((quicksum(cost[i][j] * x[i][j] for i in range(n) for j in range(n))),GRB.MINIMIZE)
model.optimize()
model.write("DellAmico1.lp") # LP dosyası ürettirmek
status = model.status
object_Value = model.objVal
print()
print("model status is: ", status)
print()
print("Objective value is: ", object_Value)
Can you help me with this error please. Thanks in advance.
-
The error occurs because you are trying to multiply (and add) a list of values with an optimization variable while constructing a constraint.
model.addConstrs(((u[i]-u[j] + Q *x[i,j] <= Q - q[j]) for i in range(n) for j in range(n)), name = 'C11')
Here \(\texttt{Q * x[i,j]}\) and \(\texttt{Q - q[j]}\) are the problem. Since all vehicle capacities are equal, you could just replace \(\texttt{Q}\) by 200.
Then you try to subtract a list of lists \(\texttt{q}\). I am not sure what you want to achieve here. I would guess that \(\texttt{q}\) should be
q = [80,90,110,120,100,120]
but then it still has 1 entry to many (which does not result in an error).
Then, there is another issue with the list of lists \(\texttt{cost}\)
cost=[[5,3,4,6,7,8]]
which you are trying to access via
cost[i][j] * x[i][j] for i in range(n) for j in range(n)
This will not work because there is only 1 dimension in the i-axis (and again 6 entries in the j-axis which are 1 too many given your n=5). You could fix it either by adding cost lists for all remaining i entries
cost=[[5,3,4,6,7,8],[5,3,4,6,7,8],[5,3,4,6,7,8],[5,3,4,6,7,8],[5,3,4,6,7,8]]
or using only one dimensional costs
cost=[5,3,4,6,7,8]
and adjusting the objective
model.setObjective((quicksum(cost[i] * x[i][j] for i in range(n) for j in range(n))),GRB.MINIMIZE)
Additionally, \(\texttt{x[i][j]}\) should read \(\texttt{x[i,j]}\)
model.setObjective((quicksum(cost[i] * x[i,j] for i in range(n) for j in range(n))),GRB.MINIMIZE)
With all the above fixes, you can execute the optimization. The model you generated is infeasible. Please have a look at How do I determine why my model is infeasible? for more information. A first start would be to analyze the IIS computed by Gurobi
model.optimize()
model.write("DellAmico1.lp") # LP dosyası ürettirmek
if model.status == GRB.INFEASIBLE:
model.computeIIS()
model.write("iis.ilp")
exit()Best regards,
Jaromił0
サインインしてコメントを残してください。
コメント
1件のコメント