TypeError: object of type 'gurobipy.LinExpr' has no len()
回答済みHi there, I am new in Gurobi, so I wonder if you could help me on a problem.
I have this constraint
where \(x_{u,v,i,k}\) is Binary decision variable and \(\psi_{uik}\) is Continuous decision variables and \(\tau_{v}\) is an array of constant.
I wrote the constraint as follow:
for u in U:
for i in range(C_u[u]):
for v in V:
for k in range(1,K):
model.addConstr(psi_ui[u,i,k] == max_(tau_v[v]*x[u,v,i,k]))
And I got this Error : TypeError: object of type 'gurobipy.LinExpr' has no len()
would like to get some insights how to solve or write the constraint properly.
Thanks
-
正式なコメント
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?. -
Hi Hernani,
The argument in the \(\texttt{max_()}\) call is just a single element because both \(\tau_v\) and \(x_{uvik}\) are scalar elements. You should use list comprehension to pass the entire array:
model.addConstr(psi_ui[u,i,k] == max_([tau_v[v]*x[u,v,i,k] for v in V]))
You also need to remove the for-loop over \(\texttt{V}\) as a consequence.
Cheers,
Matthias0 -
Hi Matthias, thank you in advance for you time,
I wrote the problem as follow
for u in U:
for i in range(C_u[u]):
for k in range(K):
model.addConstr(psi_ui[u,i,k] == max_([tau_v[v]*x[u,v,i,k] for v in V]))And got this error: line 185, in model1NKpsv1qs
model.addConstr(psi_ui[u,i,k] == max_([tau_v[v]*x[u,v,i,k] for v in V]))
File "model.pxi", line 2943, in gurobipy.Model.addConstr
File "model.pxi", line 3287, in gurobipy.Model.addGenConstrMax
gurobipy.GurobiError: Invalid data in vars array0 -
Hi Hermani,
You need to rewrite your constraints and probably add new auxiliary variables The \(\texttt{max_()}\) constraint only takes a list of variables - you cannot pass coefficients or weights for the variables.
Please excuse the confusion.
Cheers,
Matthias0 -
Hi Matthias, thank you for the your advise it solved my problem this is how i solve the problem,
adding new auxiliary variables and than passing it to the model.addGenConstrMax(psi_ui[u,i,k],maxVarsList1)
for u in U:for i in range(C_u[u]):for v in V:
for k in range(1,K):model.addConstr(tpsi_ui[u,i,k] == tau_v[v]*x[u,v,i,k])maxVarsList1 = [tpsi_ui[u,i,k] for i in range(C_u[u]) for k in range(1,K)]model.addGenConstrMax(psi_ui[u,i,k] ,maxVarsList1)0
投稿コメントは受け付けていません。
コメント
5件のコメント