how to efficiently add columns and rows in a LP model

363 views
Skip to first unread message

chivalry

unread,
May 20, 2015, 4:50:06 PM5/20/15
to gur...@googlegroups.com
I know that use


m.addConstr(y1<=x1+x2+x3);---(1)


could be added efficiently, however, is there any efficient way to change the constraint to


m.addConstr(y1<=x1+x2+x3+x4);---(2)


i know that i could remove by 


m.remove(y1<=x1+x2+x3)

m.addConstr(y1<=x1+x2+x3+x4);



however, imagining that our problem is much much larger (tens thousands of constraints and variables).... How could we achieve this? Thank you.

Renan Garcia

unread,
May 20, 2015, 4:56:24 PM5/20/15
to gur...@googlegroups.com
Assuming you are using the C++ API (as in other posts), try the GRBModel::chgCoeff method (see http://www.gurobi.com/documentation/6.0/refman/cpp_grbmodel_chgcoeff.html). Other APIs have a similar method.

--

---
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.

chivalry

unread,
May 21, 2015, 2:30:34 PM5/21/15
to gur...@googlegroups.com
The problem is that there are exponential number of possible variables.... for example 

suppose initial constraint is:

m.addConstr(y1<=x1+x2+x3);

The new variables we would like to add might be

m.addConstr(y1<=x1+x2+x3+x42154461318784);

or 

m.addConstr(y1<=x1+x2+x3+x254215423145130379169);

The point is that the possibility is is exponential ~ 2^80.... Could GRBModel::chgCoeff be useful in this case?

Renan Garcia

unread,
May 22, 2015, 12:37:07 PM5/22/15
to gur...@googlegroups.com
I'm not sure I follow your question. In order to use GRBModel::chgCoeff, you need a reference to a constraint (i.e., GRBConstr) and a single variable (i.e., GRBVar) that have already been added to the model. Note, if you've added ~2^80 variables, then you've most certainly run out of memory. If you are asking about how to reference many variables without creating unique variable names, you should use an array. For example:

  // add variables y1 and x0, x1, ..., x999

  GRBVar y1 = model.addVar(0, GRB_INFINITY, 0, GRB_CONTINUOUS);
  GRBVar *x = model.addVars(1000, GRB_CONTINUOUS);
  model.update();

  // add original constraint y1 <= x0 + x1 + x2

  GRBLinExpr expr = 0;
  for(int i = 0; i <= 3; i++) expr += x[i];
  GRBConstr c = model.addConstr(y1 <= expr);
  model.update();

  // modify constraint to y1 <= x0 + x1 + x2 + x71

  model.chgCoeff(c, x[71], -1.0);

chivalry

unread,
May 26, 2015, 10:31:06 AM5/26/15
to gur...@googlegroups.com
Great! This solves the problem:D 
Reply all
Reply to author
Forward
0 new messages