The error message "KeyError: 'Missing constraint index'" appears when you try to add a single constraint using the method Model.addConstrs(). This is used for adding multiple constraints to a model with a single method call.
To add a single constraint, use Model.addConstr() instead.
Example
>>> model = gp.Model() >>> x = model.addVar() >>> y = model.addVar() >>> model.addConstrs(x + y <= 1) # try adding a single constraint Traceback (most recent call last): File "src/gurobipy/model.pxi", line 3536, in gurobipy.Model.addConstrs File "src/gurobipy/model.pxi", line 223, in gurobipy.Model.__genexpr_key AttributeError: 'TempConstr' object has no attribute 'gi_frame' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "<stdin>", line 1, in <module> File "src/gurobipy/model.pxi", line 3538, in gurobipy.Model.addConstrs KeyError: 'Missing constraint index'
Instead, use Model.addConstr() (without the 's' in the end):
>>> model.addConstr(x + y <= 1) <gurobi.Constr *Awaiting Model Update*>
The plural version Model.addConstrs() can be used as follows when adding multiple constraints (with a similar structure):
>>> z = model.addVars(5) >>> model.addConstrs(z[i] + z[i+1] <= 1 for i in range(4)) {0: <gurobi.Constr *Awaiting Model Update*>, 1: <gurobi.Constr *Awaiting Model Update*>, 2: <gurobi.Constr *Awaiting Model Update*>, 3: <gurobi.Constr *Awaiting Model Update*>}
Comments
0 comments
Article is closed for comments.