Files
ortools-clone/src/constraint_solver/python/routing.swig

151 lines
5.3 KiB
Plaintext
Raw Normal View History

2014-07-09 11:17:29 +00:00
// Copyright 2010-2014 Google
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2014-07-09 11:17:29 +00:00
// TODO(user): Refactor this file to adhere to the SWIG style guide.
2016-02-09 12:07:21 +01:00
%include "base/base.swig"
%include "constraint_solver/python/constraint_solver.swig"
// TODO(user): remove this when we no longer use callbacks in the routing.
#define FATAL_CALLBACK_EXCEPTION
%include "base/python/callbacks.swig"
// We need to forward-declare the proto here, so that PROTO_INPUT involving it
// works correctly. The order matters very much: this declaration needs to be
// before the %{ #include ".../routing.h" %}.
namespace operations_research {
class RoutingModelParameters;
class RoutingSearchParameters;
} // namespace operations_research
// Include the file we want to wrap a first time.
%{
#include "constraint_solver/routing.h"
%}
2016-02-09 12:07:21 +01:00
// Convert RoutingModel::NodeIndex to (32-bit signed) integers.
%typemap(in) operations_research::RoutingModel::NodeIndex {
$1 = operations_research::RoutingModel::NodeIndex(PyInt_AsLong($input));
}
%typemap(out) operations_research::RoutingModel::NodeIndex {
$result = PyInt_FromLong($1.value());
}
// Convert std::vector<RoutingModel::NodeIndex> to/from int arrays.
%{
template<>
bool PyObjAs(PyObject *py, operations_research::RoutingModel::NodeIndex* i) {
int temp;
if (!PyObjAs(py, &temp)) return false;
*i = operations_research::RoutingModel::NodeIndex(temp);
return true;
}
%}
PY_LIST_OUTPUT_TYPEMAP(operations_research::RoutingModel::NodeIndex,
PyInt_Check, PyInt_FromLong);
2016-03-21 09:48:13 +01:00
PY_LIST_LIST_INPUT_TYPEMAP(operations_research::RoutingModel::NodeIndex,
PyInt_Check);
2014-07-09 11:17:29 +00:00
// TODO(user): also support std::vector<std::vector<>> <-> list of list.
// Create input mapping for NodeEvaluator2
%{
static int64 PyCallback2NodeIndexNodeIndex(
PyObject* pyfunc,
operations_research::RoutingModel::NodeIndex i,
operations_research::RoutingModel::NodeIndex j) {
int64 result = 0;
// Cast to int needed, no int64 support
PyObject* arglist = Py_BuildValue("ll",
i.value<int>(),
j.value<int>());
PyObject* pyresult = PyEval_CallObject(pyfunc, arglist);
Py_DECREF(arglist);
if (pyresult) {
result = PyInt_AsLong(pyresult);
}
Py_XDECREF(pyresult);
return result;
}
%}
%typemap(in) operations_research::RoutingModel::NodeEvaluator2* {
if (!PyCallable_Check($input)) {
PyErr_SetString(PyExc_TypeError, "Need a callable object!");
SWIG_fail;
}
$1 = NewPermanentCallback(&PyCallback2NodeIndexNodeIndex, $input);
}
// Create conversion of vectors of NodeEvaluator2
%{
template<>
bool PyObjAs(PyObject* py_obj,
operations_research::RoutingModel::NodeEvaluator2** b) {
if (!PyCallable_Check(py_obj)) {
PyErr_SetString(PyExc_TypeError, "Need a callable object!");
return false;
}
*b = NewPermanentCallback(&PyCallback2NodeIndexNodeIndex, py_obj);
return true;
}
%}
// Passing an empty parameter as converter is ok here since no API outputs
// a vector of NodeEvaluator2*.
PY_LIST_OUTPUT_TYPEMAP(operations_research::RoutingModel::NodeEvaluator2*,
PyCallable_Check, );
%ignore operations_research::RoutingModel::AddVectorDimension(
const int64* values,
int64 capacity,
const std::string& name);
%ignore operations_research::RoutingModel::AddMatrixDimension(
const int64* const* values,
int64 capacity,
const std::string& name);
2016-03-21 09:48:13 +01:00
%ignore operations_research::RoutingModel::MakeStateDependentTransit;
%ignore operations_research::RoutingModel::AddDimensionDependentDimensionWithVehicleCapacity;
%extend operations_research::RoutingModel {
void AddVectorDimension(const std::vector<int64>& values,
int64 capacity,
bool fix_start_cumul_to_zero,
const std::string& name) {
DCHECK_EQ(values.size(), $self->nodes());
$self->AddVectorDimension(values.data(), capacity,
fix_start_cumul_to_zero, name);
}
}
PY_PROTO_TYPEMAP(ortools.constraint_solver.routing_parameters_pb2,
RoutingModelParameters,
operations_research::RoutingModelParameters)
PY_PROTO_TYPEMAP(ortools.constraint_solver.routing_parameters_pb2,
RoutingSearchParameters,
operations_research::RoutingSearchParameters)
%ignore operations_research::RoutingModel::WrapIndexEvaluator(
Solver::IndexEvaluator2* evaluator);
%ignore operations_research::RoutingModel::RoutingModel(
int nodes, int vehicles,
const std::vector<std::pair<NodeIndex, NodeIndex> >& start_end);
%ignore operations_research::RoutingModel::RoutingModel(
int nodes, int vehicles,
const std::vector<std::pair<NodeIndex, NodeIndex> >& start_end,
const RoutingModelParameters& parameters);
// Wrap cp includes
%include "constraint_solver/routing.h"