KeyError: 'Missing constraint index'
AnsweredHi there, I have a problem when I try to add a constraint into a model of vehicle routing problem. Here is my code:
# Constraints controlling the number of going into and out of a customer node
mdl.addConstrs(gp.quicksum(x[i, j] for j in AllNodes if j != i) == 1 for i in CusNodes)
mdl.addConstrs(gp.quicksum(x[i, j] for i in AllNodes if j != i) == 1 for j in CusNodes)
mdl.addConstrs(gp.quicksum(x[0, j] for j in AllNodes if j != 0)) # second last node as virtual starting depot
The first two constraints indicate that each customer node has only one edge for both going into and out of it. The third constriant indicates that for the starting point, there is only one edge going out and no edge goes into it. I can execute the first two constraints successfully, But when I execute the third constrant, the error message comes out:
Traceback (most recent call last):
File "src\gurobipy\model.pxi", line 3461, in gurobipy.Model.addConstrs
File "src\gurobipy\model.pxi", line 222, in gurobipy.Model.__genexpr_key
AttributeError: 'gurobipy.LinExpr' object has no attribute 'gi_frame'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "src\gurobipy\model.pxi", line 3463, in gurobipy.Model.addConstrs
KeyError: 'Missing constraint index'
Do you know what the problem is?
Thank you!
-
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?. -
I see two issues:
- Model.addConstrs() is used for adding multiple constraints to a model with a single method call. It looks like you only want to add a single constraint with your third line of code. If this is the case, use Model.addConstr() instead.
- \( \texttt{gp.quicksum(x[0, j] for j in AllNodes if j != 0)} \) is not a complete constraint. I would guess you want to set this expression equal to the number of vehicles.
I hope this helps!
0 -
Eli Towle Dear Eli, Thank you, it works!
0
Post is closed for comments.
Comments
3 comments