Minh Duc Vu
Minh Duc Vu

Reputation: 1

Trouble when adding a constraint to a model with Gurobi

I have a map m;

and I am able to run lines of code such as

m[constraint_name] += x_a; or
m[constraint_name] -= x_a;

However, when I want to add a constraint from this map to the model, it always fail:

model.addConstr(m[cons_name] == 1, cons_name);

I obtain the following error code:

Error code = 20001 Not in the model

Thanks for your explanation of the issue and how I can solve it?

Upvotes: 0

Views: 1566

Answers (1)

David Nehme
David Nehme

Reputation: 21572

With the gurobi api, you need to call GRBModel.update() between the time you create a variable and the time you add a constraint involving that variable. It is OK to add the variables to expressions as you are doing with the += operator on the values of your maps, so you aren't getting exceptions there.

You shouldn't overuse the update method as it is expensive. Building up expressions in maps before an update, like you are doing, is a common idiom.

Upvotes: 2

Related Questions