Forum Discussion
FICO
5 years agoCommunity Manager
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
- FICOCommunity 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.
- FICOCommunity Manager
When working with Optimizer callbacks on BCL entities you can use the BCL synchronization mechanism as described in the BCL manual and also shown in this example of cut generation with BCL Java.
Related Content
Related Content
Variable names in callback (Java API)
I construct a cut in a preintsol callback using variables from the model containing the callback. When constructing the model, I give the variables names (using withName(...)) and store them in a vec...9 months agoPlace FICO® Xpress Optimization ForumFICO® Xpress Optimization Forum4 unread comments4 unread1View0likes4CommentsVariable object mapping issue in Python Callbacks with Presolve enabled
Hi, I am implementing a Branch-and-Cut algorithm using the Xpress Python interface. I have structured my code as a class where I create variables (using xp.var) and store them in a dictionary attrib...8 months agoPlace FICO® Xpress Optimization ForumFICO® Xpress Optimization Forum6 unread comments6 unread0Views0likes6CommentsAre preintsol callbacks threadsafe?
The optimizer documentation states "When the MIP callbacks are called they are MUTEX protected to allow non threadsafe user callbacks." In my Java code for one-tree Benders, if I specify a single thr...9 months agoPlace FICO® Xpress Optimization ForumFICO® Xpress Optimization Forum3 unread comments3 unread2Views0likes3Comments