Forum Discussion

FICO's avatar
FICO
Community Manager
5 years ago

Obtaining the variable values in a callback

In the XPRSoptNodeEvent callback, in order to separate valid inequalities, one must access the values of the variables. Let's say we want to obtain the values of XBRBvar[] myVarSubset. To my understanding, we can't simply invoke myVarSubset.getSol() because at this stage, the values have not yet been installed. Instead, the getLpSol(.) function must be used.

Currently I use the following code for this:

public void XPRSoptNodeEvent(XPRSprob xprSprob, Object o, IntHolder intHolder) {
	//Determine the total number of variables in the model;
	int nrCols=xprSprob.getIntAttrib(XPRS.ORIGINALCOLS);
 
	//Get the LP values to all variables
	double[] varValues=new double[nrCols];
	xprSprob.getLpSol(varValues); 
	for(int i=0; i<myVarSubset.length; i++){
	    //Determine the index of the var of interest. The value of this var is stored at varValues[varIndex].
	    int varIndex = xprsProb.getIndex(2, myVarSubset[i].getName());
	    System.out.println("Variable "+myVarSubset[i]+" has value: "+ varValues[varIndex]);
	    //...
	}
}

Q1: Instead of using xprsProb.getIndex(2, myVarSubset.getName()), could I have used myVarSubset.getColNum()? Or is there a difference between those functions?

Q2: getLPSol gets the values of all variables; There does not seem to be a method getLPSol(myVarSubset) through which I can only query the variables of interest? Is there some alternative way?

Q3: In the above code snippet, the varIndex is recomputed each time XPRSoptNodeEvent(.) is executed. Can I assume that the column indices do not change during the solve process, and hence I could precompute (and store) the varIndices of the variables in the model? This would avoid a large number of unnecessary getIndex calls.

2 Replies

  • FICO's avatar
    FICO
    Community Manager
    • Q1: The numbers returned by getColNum() are established when XPRBprob.loadMat() is invoked. If you do not modify your problem afterwards in any way, then the function should return the index of the variable in the matrix. Notice however that this index is unrelated to the order in which you create the XPRBvar instances. It is defined by the order in which variables are extracted in loadMat().
    • Q2: In order to fetch a single solution value you can use XPRSprob.getLpSolVal. Depending on how many values you need to fetch, it may be faster to fetch the whole array in one shot, though.
    • Q3: Once the solution process has started, the indices in the original model are not going to change. So it is safe to precompute the name/index mapping.