Skip to main content

GurobiError: Invalid argument to LinExpr multiplication

Answered

Comments

1 comment

  • Jaromił Najman
    • Gurobi Staff

    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

Please sign in to leave a comment.