Issues regarding add/remove constraints: multiple constraints with same name while I expect only 1

80 views
Skip to first unread message

Minh Vu Duc

unread,
Oct 2, 2015, 9:31:31 PM10/2/15
to Gurobi Optimization

Hi community,

I have some lines of code which extract constraints from the model/remove these constraints/do the modification and then readd the constraints.

However, in the final model, there are several constraints with the same name instead 1. 

 

map<string, GRBLinExpr > mSubToursLength2;
for (int i = 1; i < G.N0; i++)
for (int j = i + 1; j < G.N0; j++)
if (G.tau[i][j] + G.tau[j][i] < 1e6)
{
ostringstream subTour;
subTour << "SubTour_" << i << "." << j;
mSubToursLength2[subTour.str()] = model.getRow(model.getConstrByName(subTour.str())); //extract a subtour
model.remove(model.getConstrByName(subTour.str())); //remove from model
}
model.update();
//cout << "Size" << newArcs.size()<< endl;
for (set<VarIndex>::iterator it = newArcs.begin(); it != newArcs.end(); it++)
{
//assert(x_a.find(*it) != x_a.end());
int i = min(it->i, it->j);
int j = max(it->i, it->j);

if (i == j) continue; 
ostringstream subTour;
subTour << "SubTour_" << i << "." << j;
mSubToursLength2[subTour.str()] += x_a[*it]; //update subtours - add related variables
}
for (map<string, GRBLinExpr>::iterator it = mSubToursLength2.begin(); it != mSubToursLength2.end(); it++)
model.addConstr(it->second <= 1, it->first);  //re insert to the model
 
                        model.update();



Output has something like:
SubTour_1.14: x(1,125)(14,184) <= 1
SubTour_1.14: x(1,107)(14,184) <= 1
SubTour_1.14: x(1,121)(14,184) <= 1
...
while I expect:
SubTour_1.14:  x(1,125)(14,184)+x(1,107)(14,184)+ x(1,121)(14,184) <=1 

Thanks for your comments and suggestion?

Best,

Minh Vu Duc

unread,
Oct 2, 2015, 10:53:40 PM10/2/15
to Gurobi Optimization

I was able to modify other constraints so there is something not good, However the functions are called many times, so at least there are something not it good states.

I notice that if I made the map variable global, it leads to the following error.


Error code = 20001 in updateSubTourLength2Constraints
Not in the model

 And there is no error (I receive the same outputs as before) if I call map.clear() before the line

      for (int i = 1; i < G.N0; i++)

But I do not know why because I only use the map to insert the constraints?

Bests,



Vào 20:31:31 UTC-5 Thứ Sáu, ngày 02 tháng 10 năm 2015, Minh Vu Duc đã viết:

Kostja Siefen

unread,
Oct 5, 2015, 9:33:25 AM10/5/15
to Gurobi Optimization
You can modify GRBLinExpr objects in a map<string, GRBLinExpr> with the += operator. However, it seems somewhere in you code you are adding multiple constraints with the same name. Consider checking this when calling addConstr().

Regards,
Kostja

Minh Vu Duc

unread,
Oct 5, 2015, 9:37:09 AM10/5/15
to gur...@googlegroups.com

But this is the only place that I do the modification and I removed constraints when i extract them with getConstraintsByName?

--

---
You received this message because you are subscribed to a topic in the Google Groups "Gurobi Optimization" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/gurobi/e_yiUIO8TuU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to gurobi+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Minh Vu Duc

unread,
Oct 5, 2015, 9:39:52 AM10/5/15
to gur...@googlegroups.com

I have created variables and then I store them in a map by using map[var_name] = newvar; then I used variables stored in map. Is this the reason?

Renan Garcia

unread,
Oct 5, 2015, 9:47:43 AM10/5/15
to gur...@googlegroups.com
You have a condition that is tested before removing a constraint (i.e., G.tau[i][j] + G.tau[j][i] < 1e6). My guess is this condition is failing for some i,j pairs, which is why you are getting duplicates.

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.

Minh Vu Duc

unread,
Oct 5, 2015, 10:08:23 AM10/5/15
to gur...@googlegroups.com

I guess no. The map only contains constraints that i extracted from the model. Remove. Do the modification. Reinsert.

Renan Garcia

unread,
Oct 5, 2015, 10:52:32 AM10/5/15
to gur...@googlegroups.com
In your second loop, you are using the i and j indices from the newArcs iterator to map to the constraint "Subtour_i.j". Doesn't that mean you will have a different constraint for each pair of arcs i,j and j,i (i.e., only subtours of type i-j-i)?

Minh Vu Duc

unread,
Oct 5, 2015, 11:34:25 AM10/5/15
to gur...@googlegroups.com
Hi,

I have many arcs with form (i,t)-(j,t') (i,j: terminals; t,t' - time points). And I want to have a subtour constraint regarding to a pair of (i,j).  

e.g. x(1,0)(3,5)+x(1,3)(2,5)+x(3,3)(1,2)+x(3,1)(1,0) + x(3,5)(1,0)<=1.

Minh Vu Duc

unread,
Oct 5, 2015, 6:17:11 PM10/5/15
to Gurobi Optimization

Is this the reason? I create the variables and then stored them in a map:

GRBVar new_arc = model.addVar(0.0, 1.0, (i!=j?G.rtau[i][j]:1e9), GRB_BINARY, var_name.str()); //add the variable associated to this arc to the model

x_a[VarIndex(i, t, j, t_prime)] = new_arc;

Previously, I changed the model using new_arc and my other constraints worked.

Now, I do not use "new_arc", but using "x_a". It gives me what I dont want as I said.


Vào 10:34:25 UTC-5 Thứ Hai, ngày 05 tháng 10 năm 2015, Minh Vu Duc đã viết:
To unsubscribe from this group and all its topics, send an email to gurobi+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--

---
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+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--

---
You received this message because you are subscribed to a topic in the Google Groups "Gurobi Optimization" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/gurobi/e_yiUIO8TuU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to gurobi+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--

---
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+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--

---
You received this message because you are subscribed to a topic in the Google Groups "Gurobi Optimization" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/gurobi/e_yiUIO8TuU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to gurobi+unsubscribe@googlegroups.com.

Minh Vu Duc

unread,
Oct 5, 2015, 7:18:40 PM10/5/15
to Gurobi Optimization

I found the issue and was able to obtain what I expected 

I use variables stored in the map; but in fact, I need to use ones in the model (by using getVarByName method)

Thanks for your discussion. 

Vào 17:17:11 UTC-5 Thứ Hai, ngày 05 tháng 10 năm 2015, Minh Vu Duc đã viết:

Minh Vu Duc

unread,
Oct 5, 2015, 7:42:16 PM10/5/15
to Gurobi Optimization


Sorry, the problem is still unsolved. The final model still has constraints with same name. I do not know why : )

 SubTour_1.15: x(1,50)(15,108) <= 1
 SubTour_1.15: x(1,48)(15,108) <= 1
 SubTour_1.15: x(1,62)(15,108) <= 1

while I am expecting  x(1,50)(15,108) + x(1,48)(15,108) +  x(1,62)(15,108) <= 1


Vào 18:18:40 UTC-5 Thứ Hai, ngày 05 tháng 10 năm 2015, Minh Vu Duc đã viết:

Minh Vu Duc

unread,
Oct 5, 2015, 8:12:20 PM10/5/15
to Gurobi Optimization


I do the last method, I update the constraint using directly the variables I just created (e.g new_var). It worked for my other constraints and now it works for this set of constraint.

You can see the difference between these two methods: the not OK file is the one produced by using variables from set/map or getVarByName variables. They have many sub-tour constraints with a same name. The OK ones contain only one for each pair of terminal.
 
 
Vào 18:42:16 UTC-5 Thứ Hai, ngày 05 tháng 10 năm 2015, Minh Vu Duc đã viết:
finalNOTOK.lp
finalOK.lp
finalNOTOK1.lp
finalOK1.lp
Reply all
Reply to author
Forward
0 new messages