Forum Discussion
FICO
8 months agoCommunity Manager
Variable 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...
FICO
8 months agoCommunity Manager
Your code is actually correct. It is allowed to use xp.var() objects in functions which operate on the original problem space, like model.getCallbackSolution. But if you try to print 'value' (or 'value.name') in your for-loop, the wrong name would be printed, due to the bug I mentioned. I am guessing that is the cause of your confusion. If you want to access the var names (or other properties like bounds), you would need to cache them like this:
class XpressSolver():
def __init__(self):
....
def build(self):
self.model = xp.problem()
x = self.model.addVariables(..., name='x')
y = self.model.addVariables(..., name='y')
self.variables['x'] = x
self.variables['y'] = y
# cache var names for later access from callbacks
self.var_names = [v.name for v in self.model.getVariable()]
........
def PreIntSolCallback(self, model, object, soltype, cutoff):
x = self.variables['x']
y = self.variables['y']
....
x_sol, y_sol = {},{}
for key, value in x.items():
x_sol[key] = model.getCallbackSolution(value)
# use var index to look up its name in the original problem
name = self.var_names[value.index]
print(name, x_sol[key])
for key, value in y.items():
y_sol[key] = model.getCallbackSolution(value)
name = self.var_names[value.index]
print(name, y_sol[key])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 unread1View0likes4CommentsAre 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 unread2Views0likes3CommentsObtaining 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 understa...5 years agoPlace FICO® Xpress Optimization ForumFICO® Xpress Optimization Forum2 unread comments2 unread0Views0likes2Comments