Skip to main content

TypeError: object of type 'gurobipy.LinExpr' has no len()

Answered

Comments

5 comments

  • Official comment
    Simranjit Kaur
    • Gurobi Staff
    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?.
  • Matthias Miltenberger
    • Gurobi Staff

    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,
    Matthias

    0
  • Hernani Delgado Chantre
    • Gurobi-versary
    • Conversationalist
    • First Question

    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 array
    0
  • Matthias Miltenberger
    • Gurobi Staff

    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,
    Matthias

    0
  • Hernani Delgado Chantre
    • Gurobi-versary
    • Conversationalist
    • First Question

    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

Post is closed for comments.