通过swigin或工具使用Python回调

2024-10-02 00:42:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我希望这只是一个简单的酗酒问题。我正在使用谷歌或工具优化库。它是一个用SWIG(我不知道的东西)封装的C++库。我很难让Python回调函数正常工作。有一个C++函数< /p>

DecisionBuilder* MakePhase(const std::vector<IntVar*>& vars,
                           IndexEvaluator1* var_evaluator,
                           IntValueStrategy val_str);

连同

^{pr2}$

相关的SWIG(我相信)是

  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);
  }

在另一份档案里

%{
static int64 PyCallback1Int64Int64(PyObject* pyfunc, int64 i) {
  // () needed to 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 a callable object!");
    SWIG_fail;
  }
  $1 = NewPermanentCallback(&PyCallback1Int64Int64, $input);
}

在我的Python模块中,我定义了一个函数Run1,如下所示(我的一部分人认为应该有一些类型转换,但我认为这不是Python的方式):

def Run1(index1):
    return index1

然后设置

selector_callback = Run1
solver = pywrapcp.Solver("graph-coloring")

最后,我打电话来

solver.Phase(nodes,
             selector_callback,
             solver.INT_VALUE_DEFAULT)

唉,这就是事情的发展方向kablooie,我总是得到以下错误:

  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 for 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)

难点在于第二个参数中的回调函数;如果我使用一个内置值而不是回调,那么操作就成功了。但是,我需要有我自己的功能。在

我没有在我的模块中导入任何SWIG文件。我需要这样做吗?在


Tags: 函数returnswigoperationsstdvectorsolverresearch
1条回答
网友
1楼 · 发布于 2024-10-02 00:42:12

所以几天后,我找到了答案。如果我打电话

solver.VarEvalValStrPhase(nodes,
             selector_callback,
             solver.INT_VALUE_DEFAULT)

它不是在手册中引用的标准函数名Phase,而是工作的。如果我要使用另一个参数组合,我必须使用另一个我相信的函数名。在这种情况下,重载似乎失败了。这很好,但是开发者的警告会很好。在

相关问题 更多 >

    热门问题