Forum Discussion
FICO
1 year agoCommunity Manager
getCallbackSolution failing and yielding error 422 inside a chgbranchobject callback?
Hi sll,I'm using the Python API of the Xpress Optimizer to try and implement through the addcbchgbranchobject callback a custom branching choice during a MIP search.According to the reference manual,the getCallbackSolution method is supposed to return the solution to the LP relaxation at the present node while scanning a MIP tree,but as soon as it gets there,execution stops yielding a 422 ERROR message,'a solution is not available'.Am i missing out on something?(I probably am)
The problem is definitely feasible,at least in the modeling part,but to speed up the creation of the constraints matrix I modeled it with Numpy,with the variables as Numpy arrays with data type npvar.Might this be related to the issue I'm experiencing?
I'm running the code in terminal,OS is Windows 11 x64.
8 Replies
- FICOCommunity Manager
According to the documentation getCallbackSolution() from within a chgbranchobject callback should indeed return the current relaxation.
I cannot reproduce your problem, though. I tried several problems with Xpress 45.1.2 and called getCallbackSolution() for each and every invocation of the callback. I did not observe any problem.
What version of Xpress are you using? What object are you calling getCallbackSolution() on? You must call it on the problem object that is passed as first argument into the callback, not on the problem object on which you called optimize(). Can you share your code or provide a short example that reproduces the problem?
- FICOCommunity Manager
- FICOCommunity Manager
Can you please also show the rest of your code (how does your callback look like, how do you register the callback, how do call optimize())?
What version of Xpress do you use?
In order to reproduce the problem here, we would also need the CSV file with your data.
- FICOCommunity Manager
Thank you Daniel for your reply and sorry for my laconic one,unfortunately I got my account blocked for whatever reason as I tried to attach multiple screenshots.This time I'll paste the code as plain text,for good measure.After the model definition we have:
class controldata:
def __init__(self,inputdata,outputdata,bestbound,nodenumber,brancher,threshold,threshindex,Mleft,Mright):
self.bestbound=bestbound
self.inputdata=inputdata
self.outputdata=outputdata
self.nodenumber=nodenumber
self.brancher=brancher
self.threshold=threshold
self.threshindex=threshindex
self.Mleft=Mleft
self.Mright=Mright
controlmatrix=controldata(X,y,10000,[],[],[],[],[],[])
(An object to be passed to the callback,useful only for providing the input and output samples X and y,most of the attribute lists were meant for an early implementation and will stay empty)
def spatialbranch(prob,obj,obranch):
N=np.shape(obj.inputdata)[0]
X=(obj.inputdata)
y=(obj.outputdata)
h=math.floor(0.8*N)
sol=prob.getCallbackSolution(z)
(The actual callback,up to the point where the issue is encountered.We might argue that z is not defined inside the function,but only as an xpress object of the prob we'll pass as input argument;still,even using getCallbackSolution() with no argument inside the brackets to get the full solution will result in the execution stopping with error 422).
I add the callback to the problem with p.addcbchgbranchobject(spatialbranch,controlmatrix,5),and I then optimize with p.mipoptimize().
As for the CSV file,it's one of several regression datasets available on Kaggle as part of the Starter: R robustbase Datasets 6f223ec8-4
package;I doubt the issue depends on how the CSV's are parsed,since optimization of the problem on the same dataset without custom branching will work fine.
- FICOCommunity Manager
Also,my Xpress version is 45.01.02,as per the output of the xpress.getversion() method.
- FICOCommunity Manager
Thank you for your code. We have reproduced the problem here and are investigating. We will come back once we found the problem.
For the next time: it is best to share code either as attachment or as text. That makes it easier for us to copy/paste and run the code here. Not sure why you got blocked for uploading some screenshots, though. Sorry about that :-(
- FICOCommunity Manager
Thank you,what has been bugging me in the meantime is,do you happen to encounter the same issue,feeding that specific model to the callback?(Notwithstanding what else would be inside the callback,even using a dummy function returning obranch after the sol=getCallbackSolution() )
- FICOCommunity Manager
Yes, we used your exact problem.
As far we can tell at the moment, this is a problem with the strong branch initialization for problems with quadratic objective.
One way to avoid this to disable strong branching and skip the callback for the root node. In order to that, do
p.controls.sbbest=0before calling mipoptimize() and in the callback do
if prob.attributes.nodes < 1: returnThat fixed the problem for me (I did not investigate the performance impact of disabling strong branching on the first node)
Related Content
Related Content
Are 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 unread2Views0likes3CommentsVariable 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 unread0Views0likes6Comments