Forum Discussion

FICO's avatar
FICO
Community Manager
5 years ago

Changing objective function coefficient

Given the following overly simplified code snippet.

XPRBprob bprob = bcl.newProb();
//..
XPRBexpr obj = new XPRBexpr(); //Define some objective
//..
bprob.setSense(XPRB.MAXIM);
bprob.setObj(obj);
//..
bprob.loadMat(); /* BCL loads the matrix into the Optimizer library */
bprob.getXPRSprob().mipOptimize(); //Solve model
modifyObjective1(bprob, myVars, obj); //Modify model
bprob.getXPRSprob().mipOptimize(); //Re-solve model
 
 
public void modifyObjective1(XPRBprob bprob, XPRBvar[] myVars, XPRBexpr obj){
    obj.setTerm(100,myVars[4]); //change var objective coefficient
    bprob.setObj(obj);
    bprob.loadMat();
}

Q1. modifyObjective1 uses BCL to change the objective function. To communicate this change to the Optimizer, loadMat() must be invoked. 

Will the invocation of loadMat() cause the Optimizer to solve the model from scratch, or will the Optimizer still use the solution from the first solve() as a starting basis, i.e. perform a re-optimization?

As an alternative to modifyObjective1, we could simply change the objective function directly through the Optimizer interface:

public void modifyObjective2(XPRBprob bprob, XPRBvar[] myVars){
    bprob.getXPRSprob().chgObj(1, new int[]{myVars[4].getColNum()}, new double[]{100}); //change var objective coefficient
}

Q2. I presume this is more efficient. Should I be concerned here whether the presolve has potentially removed the variable who's objective coefficient I want to change from the model? If so, what would be the correct check to perform?

1 Reply

  • FICO's avatar
    FICO
    Community Manager

    Xpress does not automatically keep feasible MIP solutions as starting point for the next solve after a modification. The reason is that it is not always clear whether it helps performance or not (in some cases it is just better to start from scratch and don't bother with whatever a previous solve produced). So you have to transfer those solutions yourself.

    About the second question: If you change the objective function then the solver has to start from scratch anyway because any previous presolve reduction may have been derived from the objective function. Any such reduction now is invalid. So changing the objective implies dropping the presolved model. Thus you don't need to bother with variables that were presolved out.