Cbc icon indicating copy to clipboard operation
Cbc copied to clipboard

Discrepancies between Cbc C interface and CbcSolver

Open spoorendonk opened this issue 4 years ago • 9 comments

It is unclear for me if the behavior of the C interface is similar to that of using CbcSolver?

From the C interface it seems like using CbcModel with CbcMain0 and CbcMain1 is the way to go with a messageHandler to do callbacks? It appears one would have to redo the C interface flow to get a C++ version with the same features?

Intuitively it would be nice to have a C++ solver that is thinly to C. Maybe this is related to #309 ?

Am I missing something?

spoorendonk avatar Jun 04 '20 14:06 spoorendonk

Hi @spoorendonk , CbcMain0 and CbcMain1 are the recommended way of calling Cbc. In these two methods the parameters are well tuned by @jjhforrest . The C interface uses them to call CBC.

You should use a messagehandler in CbcMain0 and CbcMain1 to callbacks (mostly monitoring).

h-g-s avatar Jun 04 '20 14:06 h-g-s

@spoorendonk You're suggesting a C++ wrapper around a C wrapper around a C++ interface :)? It doesn't look like it should be that difficult to untangle things and make it possible to call Cbc in the same way from OsiCbc, the C interface, and CbcSolver, but I honestly haven't looked that deeply into it. This would be great if we could do it. It could make sense just to call the C interface itself from OsiCbc, which would then essentially be a C++ wrapper around a C wrapper around a C++ interface.

tkralphs avatar Jun 04 '20 14:06 tkralphs

thanks for answer @h-g-s and @tkralphs

I think I was looking for a C++ interface (CbcSolver) that is then wrapped in C (Cbc_C_Interface) and wrapped in OsiCbc. See also #309

It could be by untangling the C interface code and put it into CbcSolver and then use CbcSolver instead. Then OsiCbc would call CbcSolver.

spoorendonk avatar Jun 04 '20 14:06 spoorendonk

Yes, ideally, we could get everything into CbcSolver and then both the C interface and OsiCbc would just thinly wrap that.

tkralphs avatar Jun 04 '20 14:06 tkralphs

Some additional code in the C interface will probably always be necessary: some customizations in CBC involve creating specialized classes (e.g. cut generators), in C everything should be available in callbacks.

h-g-s avatar Jun 04 '20 15:06 h-g-s

@h-g-s seems right. Perhaps the custom classes could be added to CbcSolver or the C interface could implement a simple derived version CbcSolver that embeds the callbacks in inherited user methods? Just to have an (almost) single point of entry.

What does the cbc_callback do? The other are self explainable - incumbent, progress, cuts.

spoorendonk avatar Jun 04 '20 15:06 spoorendonk

Another important thing that I remembered.

When I was using directly the OsiClpSolverInterface, adding variables/constraints one by one was terribly slow. I implemented a buffer in the C interface that automatically flushes the added variables/constraints only when necessary, in a batch. There are even some queries that can be performed without flushing. This makes these incremental problem creation much faster. Ideally this buffer should be implemented in Osi, I think...

h-g-s avatar Jun 05 '20 01:06 h-g-s

Yes, I believe some similar tricks are used in some of the Cbc interfaces. Those kind of things could be added over time, once the basic functionality is there.

tkralphs avatar Jun 05 '20 01:06 tkralphs

Is this similar to what is done with the CoinModel from CoinUtils? Would it be an option to wrap the CoinModel and CbcModel inside CbcSolver to get this functionality?

spoorendonk avatar Jun 05 '20 08:06 spoorendonk