Friday, 15 March 2013

c++ - Using Python callbacks via SWIG in OR Tools -


I hope this is a simple SWIG problem. I am using the Google OR-Tools Optimization Library, it is a C ++ library which is wrapped in SWIG (which I know something about it). I have a C ++ function to get a Python callback function to work.

  DecisionBuilder * MakePhase (constant std :: vector & lt; intvar * & gt; Amp; Wars, IndexEvaluator1 * var_evaluator, IntValueStrategy val_str);  

with

  typed result ResultCallback1 & lt; Int64, int64 & gt; IndexEvaluator1;  

and the relevant big sip (I believe) is

  DecisionBuilder * VarEvalValStrPhase (constant std :: vector & lt; intVar * & gt; ; & Wars, ResultCallback1 & lt; Int64, int64 & gt; * var_evaluator, operations_research :: Solver :: IntValueStrategy val_str) {return self-> makefaces (Wars, Variables, val_stroll); }  

and we will have to apply it to another SWIG file

 % {static int64 PyCallback1Int64Int64 (PyObject * pyfunc, int64 i) {// () Need to create one-element tubal creation PyObject * pyresult = PyEval_CallFunction (pyfunc, "(l)", static_cast  (i)); Int64 result = 0; If (! PureSult) {PyErr_SetString (PyExc_RuntimeError, "ResultCallback1  invoice failed."); } And {result = PyInt_AsLong (pyresult); Py_DECREF (pyresult); } Return results; }%}% Typemap (in) ResultCallback1 & LT; Int64, int64 & gt; * {{PyErr_SetString (PyExc_TypeError, "A requisite object is required!") (PyCallable_Check ($ input)!); SWIG_fail; } $ 1 = newmer callback (& ​​amp; PyCallback1Int64Int64, $ input); }  

In my Python module I have defined a function, run 1, as follows (and here is that some type should be inserted in my view, but I will collect it that Python Not the way):

  def run 1 (index 1): return index 1  

and set

  Selector_callback = Run1 solver = pywrapcp Finally, I call  
  solver.phase (nodes, selector_callback, solver.INT_VALUE_DEFAULT) .  

And here's where to go, I always get the following error:

  file "C: \ dev \ Python27 \ lib \ site-packages \ ortools-1.3853-py2.7 win-amd64.egg \ ortools \ constraint_solver \ pywrapcp.py ", line 457, in step dig phase (self, * args): return _pywrapcp.Solver_Phase Self, * args) NotImplementedError: The types of arguments for the wrong number or overloaded function 'Solverfos' are possible C / C ++ prototypes: operations_research :: Solver :: MakePhase (std :: Vector & LT; operations_research :: IntVar *, Std :: communicator & lt; operations_research :: intavar *> gt; & gt; Rank & amp;, operations_research :: Solver :: IntVarStrategy, operations_research :: Solver :: IntValueStrategy) operations_research :: Solver :: MakePhase (std :: Vector & lt; operations_research :: IntervalVar *, std :: defiler & lt; operations_research :: IntervalVar * & gt; constants & amp ;, operations_research :: Solver :: IntervalStrategy) operations_research :: Solver :: MakePhase (std :: vector & lt; Operations_research :: sequencever *, std :: allocator & lt; Operations_research :: sequencevar * & gt; & Gt; Constant, Operation_Strest :: Solver :: Sequence strategy)  

The difficulty is with the callback function in the second argument; If I use one of the built-in values ​​instead of callback, the operation succeeds, but I need to do my own work there

I am not importing any SWIG files in my module Am Do I need to do this?

So after days, I got the answer. If I referenced throughout the manual, it works if I was using another logic combination then I believe that I have to use another function name. It appears that overloading fails in this case. That's fine, but a warning from developers would have been good.


No comments:

Post a Comment