Tuesday, February 16, 2010

Time Limits in Choco Solver

I'm introducing myself to constraint programming as part of a research project. Since I program mainly in Java (I swore off C++ after too much time swearing at it), I went looking for an open-source CP library written in Java.  My second attempt was Choco, which seems to work quite well for my purposes. Like many non-commercial software projects, though, the documentation lags a bit behind the code. For some reason, there's no Javadoc available (at least short of compiling the source code myself, which to date I've been too lazy to do).

What prompts me to write this reminder to myself is the adventure I had with time limits. The Choco documentation (a PDF file dated 01/20/2010, so it's pretty current as of this writing) mentions two methods, setTimeLimit and setCpuTimeLimit. NetBeans refuses to believe that the setCpuTimeLimit method (or anything starting with 'setCpu') exists. That's fine; setTimeLimit should work for me.

Unfortunately, my code started generating an unlabelled exception, which I could not catch even if I surrounded the solve() method (which lists no thrown exceptions) with a try ... catch block. Running the solve() method inside a SwingBackgroundTask probably didn't enhance my debugging efforts, either. I eventually deduced the exception was a consequence of hitting the time limit, but had a heck of time figuring out what was throwing it.

The solve() method returns a Boolean (not boolean) result. Eventually I paid attention to that and figured it out: if the solver hits its time limit, it returns null rather than true or false. I was using the return value in a conditional, as in

if (solver.solve()) { // do something with solution }

and that was causing a null pointer exception if the return value was null.  Live and learn.

1 comment:

If this is your first time commenting on the blog, please read the Ground Rules for Comments. In particular, if you want to ask an operations research-related question not relevant to this post, consider asking it on OR-Exchange.