c++ - Using Python callbacks via SWIG in OR Tools -
i hoping simple swig issue. using google or-tools optimization library. c++ library wrapped in swig (which know little about). having great difficult getting python callback function work. there c++ function
decisionbuilder* makephase(const std::vector<intvar*>& vars, indexevaluator1* var_evaluator, intvaluestrategy val_str);
along with
typedef resultcallback1<int64, int64> indexevaluator1;
and relevant swig (i believe) is
decisionbuilder* varevalvalstrphase( const std::vector<intvar*>& vars, resultcallback1<int64, int64>* var_evaluator, operations_research::solver::intvaluestrategy val_str) { return self->makephase(vars, var_evaluator, val_str); }
and in swig file have
%{ static int64 pycallback1int64int64(pyobject* pyfunc, int64 i) { // () needed force creation of one-element tuple pyobject* pyresult = pyeval_callfunction(pyfunc, "(l)", static_cast<long>(i)); int64 result = 0; if (!pyresult) { pyerr_setstring(pyexc_runtimeerror, "resultcallback1<int64, int64> invocation failed."); } else { result = pyint_aslong(pyresult); py_decref(pyresult); } return result; } %} %typemap(in) resultcallback1<int64, int64>* { if (!pycallable_check($input)) { pyerr_setstring(pyexc_typeerror, "need callable object!"); swig_fail; } $1 = newpermanentcallback(&pycallback1int64int64, $input); }
in python module have defined function, run1, follows (and here part of me thinks there should type casts, gather not python way):
def run1(index1): return index1
and set
selector_callback = run1 solver = pywrapcp.solver("graph-coloring")
finally, call
solver.phase(nodes, selector_callback, solver.int_value_default)
and here alas things go kablooie, 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 phase def phase(self, *args): return _pywrapcp.solver_phase(self, *args) notimplementederror: wrong number or type of arguments overloaded function 'solver_phase'. possible c/c++ prototypes are: operations_research::solver::makephase(std::vector< operations_research::intvar *,std::allocator< operations_research::intvar * > > const &,operations_research::solver::intvarstrategy,operations_research::solver::intvaluestrategy) operations_research::solver::makephase(std::vector< operations_research::intervalvar *,std::allocator< operations_research::intervalvar * > > const &,operations_research::solver::intervalstrategy) operations_research::solver::makephase(std::vector< operations_research::sequencevar *,std::allocator< operations_research::sequencevar * > > const &,operations_research::solver::sequencestrategy)
the difficulty callback function in second argument; if use 1 of built-in values instead of callback operation successful. but, need have own function in there.
i not importing swig files in module. need this?
so after days, found answer. if call
solver.varevalvalstrphase(nodes, selector_callback, solver.int_value_default)
instead of standard function name phase
referenced throughout manuals, works. if use argument combination i'd have use function name believe. appears overloading fails in case. fine, warning developers have been nice.
Comments
Post a Comment