Removing ALL constraints from a model

2,455 views
Skip to first unread message

MdB

unread,
Mar 4, 2011, 3:18:43 AM3/4/11
to Gurobi Optimization, stefan...@fmi.uni-stuttgart.de
Hello,
I have some questions about the possibility of deleting all
constraints from an existing model (I should mention that I use Gurobi
within a C++ program). As far as I understood, there is no method to
do so directly, hence the constraints must be deleted one by one,
right? That´s quite inconvenient, but OK. I tried it the following
way:

if ( some condition )
{
GRBConstr *NBen = mymodel.getConstrs();
while ( mymodel.get(GRB_IntAttr_NumConstrs) > 0 )
{
mymodel.remove(NBen[mymodel.get(GRB_IntAttr_NumConstrs) - 1]);
mymodel.update();
}
delete[] NBen;
GRBLinExpr lex = GRBLinExpr(0.0);
for (unsigned short i = 0; vars.size() > i; i++ ) //
*vars = mymodel.getVars();
{
GRBLinExpr linex = GRBLinExpr(vars[i], 1.0);
lex+=linex;
}
mymodel.addConstr(lex, GRB_LESS_EQUAL,

(mymodel.get(GRB_DoubleAttr_ObjVal) * 1.001) );
}

That is, first I delete all constraints, then I add a new one, which
is mainly the objective function <= 100.1 % of its last found optimal
value (I want to maximize).

Well, the result is a GRBException, which must come from the line

mymodel.addConstr(lex, GRB_LESS_EQUAL,

(mymodel.get(GRB_DoubleAttr_ObjVal) * 1.001) ); .

So, why does this occur, how do I help it, and how can I - efficiently
- remove all constraints from my model and set other ones afterwards?

By the way, is it possible that the documentation is incomplete? When
I enter "mymodel.r" in my programming environment, I get offered some
completions, like "reset" and "remove", but also "relax", and there is
no member function relax() listed for GRBModel in the Reference
Manual.

Thanks & Regards,

MdB

Greg Glockner

unread,
Mar 8, 2011, 12:40:46 AM3/8/11
to gur...@googlegroups.com, stefan...@fmi.uni-stuttgart.de
> I have some questions about the possibility of deleting all
> constraints from an existing model (I should mention that I use Gurobi
> within a C++ program). As far as I understood, there is no method to
> do so directly, hence the constraints must be deleted one by one,
> right? That´s quite inconvenient, but OK. I tried it the following
> way:
>
> if ( some condition )
> {
> GRBConstr *NBen = mymodel.getConstrs();
> while ( mymodel.get(GRB_IntAttr_NumConstrs) > 0 )
> {
> mymodel.remove(NBen[mymodel.get(GRB_IntAttr_NumConstrs) - 1]);
> mymodel.update();
> }
> delete[] NBen;
> GRBLinExpr lex = GRBLinExpr(0.0);
> for (unsigned short i = 0; vars.size() > i; i++ ) //
> *vars = mymodel.getVars();
> {
> GRBLinExpr linex = GRBLinExpr(vars[i], 1.0);
> lex+=linex;
> }
> mymodel.addConstr(lex, GRB_LESS_EQUAL,
>
> (mymodel.get(GRB_DoubleAttr_ObjVal) * 1.001) );
> }
>
> That is, first I delete all constraints, then I add a new one, which
> is mainly the objective function <= 100.1 % of its last found optimal
> value (I want to maximize).

This seems inefficient. You are right that there is no single method (function) to remove all constraints from a model. You are correct to iterate over all the constraints and remove them. However, you should wait to call GRBModel::update() until after the loop is finished. Thanks to the lazy updates, this will be far more efficient than calling GRBModel::update() in each iteration.

Other ideas that may be better, depending on your application:

1) Start fresh with a new GRBModel object.
2) If you simply need to add one new constraint (on the objective), you will be more efficient to add that through a model modification.


> Well, the result is a GRBException, which must come from the line
>
> mymodel.addConstr(lex, GRB_LESS_EQUAL,
>
> (mymodel.get(GRB_DoubleAttr_ObjVal) * 1.001) ); .
>
> So, why does this occur, how do I help it, and how can I - efficiently
> - remove all constraints from my model and set other ones afterwards?

One should always catch the exception to determine the exact error. I can't reproduce the situation based on this code fragment, but my guess is that the ObjVal attribute is no longer available after you've made the model modifications.

> By the way, is it possible that the documentation is incomplete? When
> I enter "mymodel.r" in my programming environment, I get offered some
> completions, like "reset" and "remove", but also "relax", and there is
> no member function relax() listed for GRBModel in the Reference
> Manual.

This is an undocumented feature. In most cases, the undocumented features are not officially supported. In this case, you should relax a model by modifying the VarType attributes.

Lyndon Shi

unread,
Jun 18, 2017, 9:58:17 AM6/18/17
to Gurobi Optimization, stefan...@fmi.uni-stuttgart.de, gloc...@gurobi.com
On Tuesday, March 8, 2011 at 12:40:46 AM UTC-5, Greg Glockner wrote:

This seems inefficient.  You are right that there is no single method (function) to remove all constraints from a model.  You are correct to iterate over all the constraints and remove them.  However, you should wait to call GRBModel::update() until after the loop is finished.  Thanks to the lazy updates, this will be far more efficient than calling GRBModel::update() in each iteration.

 


You need to call GRBModel::update() on every iteration because of the lazy updates. 

The loop is 
while ( mymodel.get(GRB_IntAttr_NumConstrs) > 0 )

If you don't update, the NumConstrs will never decrease after you remove a constraint because Gurobi thinks the constraint is still there.

Renan Garcia

unread,
Jun 18, 2017, 10:17:02 AM6/18/17
to gur...@googlegroups.com, stefan...@fmi.uni-stuttgart.de, gloc...@gurobi.com
If you were to use a for loop instead of a while loop, then you'd only need to query for NumConstrs once:

int NumConstrs = mymodel.get(GRB_IntAttr_NumConstrs);
GRBConstr *NBen = mymodel.getConstrs();
for (int i = 0; i < NumConstrs; i++{
  mymodel.remove(NBen[i]);
}
mymodel.update();
delete[] NBen;

--

---
You received this message because you are subscribed to the Google Groups "Gurobi Optimization" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gurobi+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Antonio Medrano

unread,
Apr 14, 2018, 5:22:55 PM4/14/18
to Gurobi Optimization
Why not just simply this?
model.remove(model.getConstrs())

GurobiUser

unread,
Oct 2, 2018, 3:53:23 PM10/2/18
to Gurobi Optimization
Hi All,

I am using Gurobi Python to solve my optimization model and now I am interested to delete some of the constraints and add some new one in different discrete events. For this I used model.remove(model.getConstrByname("c0"))  (http://www.gurobi.com/documentation/8.0/refman/py_model_getconstrbyname.html) method, where "c0" can be like:

m.addConstrs((Y[i][j] + Z[i][i] <= 1 for i in range(ng) for j in range(np)), name="c0")

but it seems it does not work properly. Why? is there any way to delete the constraints in Gurobi by referring to their names?

because with the above method (model.remove(model.getConstrs())), is difficult to work as one does not know exactly the position of the constraints during different events...

Thanks,

Tobias Achterberg

unread,
Oct 3, 2018, 6:34:05 AM10/3/18
to gur...@googlegroups.com
The m.addConstrs() method adds several constraints at once and constructs names that are
based on the "name" parameter. In your case, the names would be something like "c0[0,0]",
"c0[0,1]" and so on, I think. You can write out the LP file to take a look:
m.write("myprob.lp").

To refer to constraints I never would go by names. Instead, keep track of the constraint
objects that model.addConstr() or model.addConstrs() return. Then, use those objects
directly in the remove() method. Like this:

myconstrs = m.addConstrs((Y[i][j] + Z[i][i] <= 1 for i in range(ng) for j in range(np)),
name="c0")

...

m.remove(myconstrs[7,5])


Tobias

GurobiUser

unread,
Oct 3, 2018, 11:14:12 AM10/3/18
to Gurobi Optimization
Hi Tobias, Thank you very much for your reply.

I get your point and it would work if I need to delete some specific constraints. But in my case I want to delete many constraints at once. 

For example, in the case you propose I would need to delete all the constraints build by myconstrs, so should I do like this:

m.remove(myconstrs)? or something else?

Many thanks again,

Tobias Achterberg

unread,
Oct 3, 2018, 3:25:56 PM10/3/18
to gur...@googlegroups.com
Yes, I think this should work. Please go ahead and try it.


Tobias

GurobiUser

unread,
Oct 4, 2018, 7:53:28 AM10/4/18
to Gurobi Optimization
Hi Tobias, Thank you very much again for your reply. 

It did work actually, but only for the first event. When I run multiple events the new added constraints seems to be added to the model multiple times and are not deleted per event even though I apply the same method.  Do you think I am still missing something else?

Thank you!
Reply all
Reply to author
Forward
0 new messages