Writing custom variable selection method using the CPLEX Python API - python

I want to implement a custom variable selection heuristic for solving an MLP using the CPLEX Python API.
Unfortunately, I cannot find any examples or documentation for this.
Ist this actually possible using the CPLEX Python API, or do I need to use C++?
Can I achieve this using the BranchCallback [1]?
[1] https://www.ibm.com/support/knowledgecenter/SSSA5P_12.5.0/ilog.odms.cplex.help/refpythoncplex/html/cplex.callbacks.BranchCallback-class.html

The functionality of the BranchCallback in the CPLEX Python API should be nearly identical to that of BranchCallbackI provided in the C++ API. Parallel callbacks are hindered by the global interpreter lock (aka, the GIL) in Python, however.
The admipex1.py, and admipex3.py Python examples demonstrate how to use the BranchCallback class and are included with CPLEX. For the corresponding C++ examples, see iloadmipex1.cpp and iloadmipex3.cpp.

Related

Branch-and-price using SCIP

I'm currently trying to implement a generic nurse rostering problem (NRP) in a branch-and-price framework in Python on Windows.
I tried using Gurobi, but apparently it isn't possible, see discussion:
https://support.gurobi.com/hc/en-us/community/posts/360043240312-Branch-and-price-example
I'm searching a small example on how to set up the branch-and-price framework using SCIP in Python - to get started. I could only find examples in C and C++, which are languages I'm not familiar with.
Please have a look at PySCIPOpt, which is the Python interface of SCIP. You find in PySCIPOpt/tests/test_pricer.py a column-generation-based example for the cutting stock problem. Still, I would recommend looking at the Binpacking example of SCIP.

CPLEX: Accessing strong branching values via python API

I am working on some advanced branching heuristics for mixed integer programming using CPLEX (12.9) by its Python (3.6) API.
Part of the branching decisions should be based on the strong branching (SB) score of variables.
While I can query the pseudo cost score of variables directly via the API [1], there seems to be no easy way to get calculated SB scores.
Therefore I want to implement the calculation on my own. I found an old forum post, describing how to implement strong branching calculations as efficient as possible [2] by using the C-API. Unfortunately the described mehtod uses library calls (like CPXgetcallbacknodelp()) for which I can't find corresponding python methods.
This leaves me with three questions:
Is there an API call to get SB scores I missed?
Did I overlook the python
wrapper for CPXgetcallbacknodelp() etc.?
Is there an easy way to add
wrappers to unsupported C-API calls to the cplex python wrapper
(maybe by extending the SWIG generated python files)?
[1] https://www.ibm.com/support/knowledgecenter/SSSA5P_12.9.0/ilog.odms.cplex.help/refpythoncplex/html/cplex.callbacks.ControlCallback-class.html
[2] https://www.ibm.com/developerworks/community/forums/html/threadTopic?id=77777777-0000-0000-0000-000014479565&ps=25
There is no way to get direct access to the nodelp via the CPLEX Python API. If you use one of the callbacks that inherit from HSCallback, you can call solve() on it, etc.
What you can do (although, this may not be efficient) is clone the original problem, pass it to the callback when you create it, and then at each node call Cplex.advanced.strong_branching().

Python support for Qt dll

I have my own C++ library project(with source) written in Qt and it uses QTcpsocket, QUdpSocket, QSerialPort signals and slots.
I would like to support this library in Python as well.
What is the preferred way to do this?
Writing a wrapper in Python, if so does it have obstacles?
Dont know if PyQt is just for this purpose?
Or do you thnink is it better to rewrite the lib in Python by just implementing the logic used in C++ library project.
As this is library is part of a SDK, same applies for supporting QT dll with .NET as well in fact, as a second step after supporting Python.
Example API of Qt.
quint16 SendCommandAsync(CmdBaseSpv1* pcommand,
ConnectionArg connectionArg,
emitLogOptions::emitLogOption logOption,
LogInfo &txLogInfo,
LogInfo &rxLogInfo);
I want to call this function from Python.
Function parameters CmdBaseSpv1, ConnectionArg, emitLogOption, LogInfo are all Qt classes.
Some of these arguments are using the QOBJECT base class.
As you see from the function name; it is an Asynchronous function call. Result will emit a signal so I need to get async result as well.
I'll write down what I know about wrapping C++ libraries and will try to source it, but as a huge disclaimer, I have only used this for something very, very simple myself.
You ask about rewriting the library in Python. I would say this depends. If the code is trivial, then I don't see why not. If it is larger and has to be kept up-to-date with other code (as you imply with .Net), I wouldn't. It makes sense to reuse the same code for both.
 My suggestion
From what I see of your code I would try to wrap it using boost::python or SWIG.
How to wrap
The main trouble is going to be to create CmdBaseSpv1, ConnectionArg, etc. in Python.
If you don't need any Qt-classes to instantiate your classes, this should be straightforward. However, in case you need the Qt types inside of Python (e.g. because the constructor of CmdBaseSpv1 requires a QString), your task is a lot more complicated because you need a way to convert a Python-string into a QString. If you can, you should only use stl-types.
Everything in Python
The simplest way to wrap a small C library is to use the cffi module (or ctypes). You can write the full binding in Python. However, this is a lot of manual work if your API is large and can get difficult.
There is another problem: ctypes is only compatible with C, not C++. So you'd need to change your interface to be compatible with C, internally you could still use C++ and Qt.
Wrap by hand
An alternative is to wrap the library calls yourself. You can either do this by using the Python API. There are also a few libraries that help you create the bindings. Boost::python seems especially promising and works with C++.
Binding generators
If your API is very large, you should use a binding generator which parses the C++ code and generates the bindings itself. For example sip is one of them. It is used to create the bindings for the whole Qt library. There are a few binding generators out there, one mentioned in the Python docs is SWIG. PySide uses Shiboken and also has a nice description of it on their website.
SWIG has the additional advantage, that you can create bindings for multiple languages, including C#.
PyQt
PyQt is a binding generated from Qt using sip. You'll probably not need it, unless you need to access the full power of Qt from inside Python. If this is the case, consider using sipfor generating the bindings, so things like the signal-slot mechanism are compatible between your library and PyQt.
Challenges with bindings
Bindings come with a few challenges because Python and C++ are different in some key areas.
Memory-management
Memory management in Python is almost automatic, in C++ you're required to do it manually. For example
def myfunc():
mywidget = QWidget()
at the end of myfunc() mywidget gets garbage collected. In C++ however
void myfunc() {
auto mywidget = new QWidget();
}
mywidget is still around. This means that even when inside Python, you need to take care of the C++ memory management. The problems I've seen are memory leaks and dangling pointers. Watch out for this when using callbacks, you don't want Python to garbage collect the callback while C++ thinks it's still alive.
Exceptions
Not all programming languages have exceptions or deal with them the same way. For example, it would be nice if an exception inside C++ can be caught inside Python.
Links to related question
How to wrap a c++ library for python? (example of boost::python)
Exposing a C++ API to Python (discussion about boost::python, SWIG and more)
https://stackoverflow.com/a/5686873 (discusses Cython, another choice)

Getting a C pointer to a function generated by Theano?

I would like to use a Theano function from C/Fortran code (in particular, I want to use an implicit ODE solver written in Fortran with a function created in Theano). Are there any examples/resources on how to do that?
You've tagged your question with ffi/cffi but that's for calling foreign code from Python. However it sounds like you actually want to call Python/Theano code from C/Fortran. For that, the documentation on Embedding Python in Another Application might be helpful.
In principle you could just run Theano Python code from your C/Fortran code via facilities in Python.h.
Although Theano compiles some operations via C code, I don't believe it produces an natively executable function/library for the entire computation graph that could then be linked in by some other, non-Python, application.
Update: via the thread on the Theano mailing list... apparently a prototype for having Theano create a linkable library was done some time ago but isn't currently integrated into Theano.

Wrapping C++ class to use in Python

I have a device which can be controlled through a C++ class (https://github.com/stanleyseow/RF24/tree/master/RPi/RF24).
I'd like to be able to use this class in Python, and thought I could wrap it.
I found many ways to do it, but not much detailed documentation with examples. In particular, I found Boost, Cython, SWIG and the native C Python API.
Which one is the best method in which case ? And do you have some links to detailed documentations / examples about this ?
Thanks !
There is no "best"; it depends entirely on your circumstances.
For a single class, the native C Python API isn't too difficult,
but you do have to create an entire module, then the class. It
would be simpler if you exposed a procedural interface, rather
than a class. If you only have one instance of the device, this
would be an appropriate solution.
SWIG is very good for taking C++ class definitions and
generating a Python module which contains them. The resulting
code is relatively complex, since SWIG tries to cover all
possible versions of Python; for anything 2.7 or later (and
perhaps a little earlier), you can do everything directly in
C++, without any intermediate Python.
Boost makes extensive use of templates. This isn't really an
appropriate solution for the problem; it adds a lot of
complexity for something that is relatively simple if done with
external tools, rather than metaprogramming. Still, if the
underlying complexity doesn't scare you, it might not be that
hard to use.
I'm not familiar with Cython.
Globally, if all you have is one instance of one simple class,
using the native C API is probably no more difficult than the
other solutions, and introduces a minimum of added internal
complexity.

Categories

Resources