How to change coefficients on the objective function?

1,313 views
Skip to first unread message

Paulo Sousa

unread,
Jan 13, 2011, 12:36:16 PM1/13/11
to Gurobi Optimization
Hi,

In connection to the Gurobi 4 C++ API, I am using a LP model, whose
objective function I set as follows:

for (unsigned i = 0; i < n; i++)
{
double coef = 1.0;

exprObj.addTerms(&coef, &a1[i], 1);
exprObj.addTerms(&coef, &a2[i], 1);

}

model->setObjective(exprObj);

Suppose now that after setting up the model, I want to change the
coefficient of, e.g., a1[6] in the objective function; how can one
accomplish that without setting the whole objective function?

I considered to use

- GRBModel::chgCoeff()
- GRBModel::chgCoeffs()

but they seem to apply only to constraints.

In short, I am looking for a computationally *efficient* way of
changing objective function coefficients.

Thanks!

Regards,

Paulo

Ed Rothberg

unread,
Jan 13, 2011, 12:44:34 PM1/13/11
to Gurobi Optimization

From the C++ RefMan...

http://gurobi.com/doc/40/refman/node96.html

"For linear objective functions, an alternative to setObjective is to
use the Obj variable attribute to modify individual linear objective
coefficients."

In other words...

a1[6].set(GRB_DoubleAttr_Obj, 2.0);

Ed

Paulo Sousa

unread,
Jan 13, 2011, 1:04:07 PM1/13/11
to Gurobi Optimization
Thanks, Ed. That is it!

Paulo
Reply all
Reply to author
Forward
0 new messages