Unable to convert argument to an expression
Answeredwhile I was trying to create an objective function
\(\sum x_i 30000 + \sum y_j 100000 + \sum \sum d_{ki} 100 d_i + \sum \sum d_{kj} 100 d_j + \sum \sum d_{ij} 100 d_j + S 15000\)
it showed the error
GurobiError: Unable to convert argument to an expression
these are my codes
coorD=[]
for i in range(50):
coorD.append([])
for j in range(2):
s=randint(1,100)
coorD[i].append(s)
demandpoint=[i for i in range(1,51)]
coorL=[]
for i in range(50):
coorL.append([])
for j in range(2):
s=randint(1,100)
coorL[i].append(s)
Launchstationpoint=[i for i in range(1,51)]
coorR=[]
for i in range(50):
coorR.append([])
for j in range(2):
s=randint(1,100)
coorR[i].append(s)
Rechargestationpoint=[i for i in range(1,51)]
#dictionaries contains points and coordinates
demand={demandpoint[i]:coorD[i] for i in range(50)}
Launchstation={Launchstationpoint[i]:coorL[i] for i in range(50)}
Rechargestation={Rechargestationpoint[i]:coorR[i] for i in range(50)}
#dictionaries contains distance between points
disDL={}
for i in range(1,51):
for j in range(1,51):
s1=math.sqrt(((demand[i])[0]-(Launchstation[j])[0])**2+((demand[i])[1]-(Launchstation[j])[1])**2)
disDL.setdefault((demandpoint[i-1],Launchstationpoint[j-1]),s1)
disDR={}
for i in range(1,51):
for j in range(1,51):
s2=math.sqrt(((demand[i])[0]-(Rechargestation[j])[0])**2+((demand[i])[1]-(Rechargestation[j])[1])**2)
disDR.setdefault((demandpoint[i-1],Rechargestationpoint[j-1]),s2)
disRL={}
for i in range(1,51):
for j in range(1,51):
s3=math.sqrt(((Rechargestation[i])[0]-(Launchstation[j])[0])**2+((Rechargestation[i])[1]-(Launchstation[j])[1])**2)
disRL.setdefault((Rechargestationpoint[i-1],Launchstationpoint[j-1]),s3)
Cj=100000
Cs=100
E=50
#time
T=240
ave=0
for i in range(100):
ave+=randint(1,50)
ave/=100
Ti=ave/0.83
aveR=0
for i in range(200):
aveR+=randint(1,50)
aveR/=100
Tj=aveR/0.83
dTi=int(T/Ti)
dTj=int(T/Tj)
m=Model("project1")
Di=m.addVars(demandpoint,Launchstationpoint,vtype=GRB.BINARY,name='D')
Dj=m.addVars(demandpoint,Rechargestationpoint,vtype=GRB.BINARY,name='D')
x=m.addVars(Launchstationpoint,vtype=GRB.BINARY,name="X")
y=m.addVars(Rechargestationpoint,vtype=GRB.BINARY,name="Y")
m.setObjective((quicksum(x[i]*300000)+quicksum(y[j]*100000)+quicksum(disDL[k,i]*100*Di[k,i])+quicksum(disDR[k,j]*100*Dj[k,j])+quicksum(disRL[j,i]*100*Dj[k,j])+S*15000 for i in Launchstationpoint for j in Rechargestationpoint for k in demandpoint),GRB.MINIMIZE)
I've tried several different methods like:
s=[quicksum(x[i]*300000)+quicksum(y[j]*100000)+quicksum(disDL[k,i]*100*Di[k,i])+quicksum(disDR[k,j]*100*Dj[k,j])+quicksum(disRL[j,i]*100*Dj[k,j])+S*15000 for i in Launchstationpoint for j in Rechargestationpoint for k in demandpoint]
m.setObjective(s,GRB.MINIMIZE)
then this occurs:
TypeError: 'gurobipy.LinExpr' object is not iterable
How should I modify it?
p.s. I haven't finished it yet, I just run it after every steps to see if there are any errors
-
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 why not try our AI Gurobot?. -
The quicksum function requires a data set and not just single elements. Thus, you get the error that a single LinExpr object \(\texttt{x[i]*30000}\) is not iterable.
One way to write the objective function would be
m.setObjective((gp.quicksum(x[i]*300000 for i in Launchstationpoint)
+gp.quicksum(y[j]*100000 for j in Rechargestationpoint)
+gp.quicksum(disDL[k,i]*100*Di[k,i] for k in demandpoint for i in Launchstationpoint)
+gp.quicksum(disDR[k,j]*100*Dj[k,j] for k in demandpoint for j in Rechargestationpoint)
+gp.quicksum(disRL[j,i]*100*Dj[k,j] for i in Launchstationpoint for j in Rechargestationpoint for k in demandpoint)
+S*15000),GRB.MINIMIZE)This way, you provide a list in each call of the quicksum function.
0 -
This is a duplicate of this stackoverflow post.
0 -
thanks for your help!
but after I got my answer, I found something weird, my d variable should be {0 or 1}
but why are there so many -0s? Or are they just 0?
0 -
The \(-0\) are actually just \(0\). You will see a \(-0\) often in computer codes and software. This is due to floating point operations and precision. For more information, see Signed zero.
0 -
Thanks for your help!
0
Post is closed for comments.
Comments
6 comments