Commit 2e322f85 authored by Tiago Peixoto's avatar Tiago Peixoto
Browse files

Allow arbitrary non-scalar types for distance values in search functions

This includes user-defined types, as well as vector types and strings.
This affects dijkstra_search(), astar_search() and bellman_ford_search().
parent a1a6748f
......@@ -31,28 +31,39 @@ using namespace std;
using namespace boost;
using namespace graph_tool;
template <class T>
python::object operator |(const python::object& a, const T& b)
{
}
struct do_astar_search
{
template <class Graph, class DistanceMap, class WeightMap>
template <class Graph, class DistanceMap>
void operator()(const Graph& g, size_t s, DistanceMap dist,
boost::any pred_map, WeightMap weight,
boost::any pred_map, boost::any aweight,
AStarVisitorWrapper vis, pair<AStarCmp, AStarCmb> cmp,
pair<python::object, python::object> range,
pair<python::object, python::object> h) const
{
typedef typename graph_traits<Graph>::edge_descriptor edge_t;
typedef typename property_traits<DistanceMap>::value_type dtype_t;
dtype_t z = python::extract<dtype_t>(range.first);
dtype_t i = python::extract<dtype_t>(range.second);
typedef typename property_map_type::
apply<int32_t, typeof(get(vertex_index, g))>::type pred_t;
pred_t pred = any_cast<pred_t>(pred_map);
checked_vector_property_map<default_color_type,
typeof(get(vertex_index, g))>
color(get(vertex_index, g._g));
checked_vector_property_map<dtype_t,
typeof(get(vertex_index, g))>
cost(get(vertex_index, g._g));
DynamicPropertyMapWrap<dtype_t, edge_t> weight(aweight,
edge_properties());
astar_search(g, vertex(s, g), AStarH<dtype_t>(h.first, h.second),
visitor(vis).weight_map(weight).
predecessor_map(pred).
distance_map(dist).distance_compare(cmp.first).
distance_combine(cmp.second).distance_inf(i).
distance_zero(z));
}
vis, pred, cost, dist, weight, get(vertex_index, g), color,
cmp.first, cmp.second, i, z);
}
};
......@@ -62,13 +73,11 @@ void a_star_search(GraphInterface& g, python::object gi, size_t source,
python::object zero, python::object inf, python::object h)
{
run_action<graph_tool::detail::all_graph_views,mpl::true_>()
(g, bind<void>(do_astar_search(), _1, source, _2, pred_map, _3,
(g, bind<void>(do_astar_search(), _1, source, _2, pred_map, weight,
AStarVisitorWrapper(gi, vis), make_pair(AStarCmp(cmp),
AStarCmb(cmb)),
make_pair(zero, inf), make_pair(gi, h)),
writable_vertex_scalar_properties(),
edge_scalar_properties())
(dist_map, weight);
writable_vertex_properties())(dist_map);
}
void export_astar()
......
......@@ -157,4 +157,3 @@ private:
};
} // namespace graph_tool
......@@ -34,13 +34,14 @@ using namespace graph_tool;
struct do_astar_search
{
template <class Graph, class DistanceMap, class WeightMap>
template <class Graph, class DistanceMap>
void operator()(const Graph& g, size_t s, DistanceMap dist,
pair<boost::any, boost::any> pc, WeightMap weight,
pair<boost::any, boost::any> pc, boost::any aweight,
AStarVisitorWrapper vis, pair<AStarCmp, AStarCmb> cmp,
pair<python::object, python::object> range,
pair<python::object, python::object> h) const
{
typedef typename property_traits<DistanceMap>::value_type dtype_t;
dtype_t z = python::extract<dtype_t>(range.first);
dtype_t i = python::extract<dtype_t>(range.second);
......@@ -50,6 +51,9 @@ struct do_astar_search
color(get(vertex_index, g._g));
typedef typename property_map_type::
apply<int32_t, typeof(get(vertex_index, g))>::type pred_t;
typedef typename graph_traits<Graph>::edge_descriptor edge_t;
DynamicPropertyMapWrap<dtype_t, edge_t> weight(aweight,
edge_properties());
astar_search_no_init(g, vertex(s, g),
AStarH<dtype_t>(h.first, h.second), vis,
any_cast<pred_t>(pc.first),
......@@ -69,13 +73,11 @@ void a_star_search_implicit(GraphInterface& g, python::object gi, size_t source,
{
run_action<graph_tool::detail::all_graph_views,mpl::true_>()
(g, bind<void>(do_astar_search(), _1, source, _2, make_pair(pred, cost),
_3,
weight,
AStarVisitorWrapper(gi, vis), make_pair(AStarCmp(cmp),
AStarCmb(cmb)),
make_pair(zero, inf), make_pair(gi, h)),
writable_vertex_scalar_properties(),
edge_scalar_properties())
(dist_map, weight);
writable_vertex_properties())(dist_map);
}
void export_astar_implicit()
......
......@@ -108,10 +108,10 @@ private:
struct do_bf_search
{
template <class Graph, class DistanceMap, class WeightMap>
template <class Graph, class DistanceMap>
void operator()(const Graph& g, size_t s, DistanceMap dist,
boost::any pred_map, WeightMap weight, BFVisitorWrapper vis,
pair<BFCmp, BFCmb> cm,
boost::any pred_map, boost::any aweight,
BFVisitorWrapper vis, pair<BFCmp, BFCmb> cm,
pair<python::object, python::object> range, bool& ret) const
{
typedef typename property_traits<DistanceMap>::value_type dtype_t;
......@@ -121,6 +121,9 @@ struct do_bf_search
typedef typename property_map_type::
apply<int32_t, typeof(get(vertex_index, g))>::type pred_t;
pred_t pred = any_cast<pred_t>(pred_map);
typedef typename graph_traits<Graph>::edge_descriptor edge_t;
DynamicPropertyMapWrap<dtype_t, edge_t> weight(aweight,
edge_properties());
ret = bellman_ford_shortest_paths
(g, HardNumVertices()(g),
root_vertex(vertex(s, g)).visitor(vis).weight_map(weight).
......@@ -141,13 +144,12 @@ bool bellman_ford_search(GraphInterface& g, python::object gi, size_t source,
{
bool ret = false;
run_action<graph_tool::detail::all_graph_views,mpl::true_>()
(g, bind<void>(do_bf_search(), _1, source, _2, pred_map, _3,
BFVisitorWrapper(gi, vis),
make_pair(BFCmp(cmp), BFCmb(cmb)), make_pair(zero, inf),
ref(ret)),
writable_vertex_scalar_properties(),
edge_scalar_properties())
(dist_map, weight);
(g, bind<void>(do_bf_search(), _1, source, _2, pred_map, weight,
BFVisitorWrapper(gi, vis),
make_pair(BFCmp(cmp), BFCmb(cmb)), make_pair(zero, inf),
ref(ret)),
writable_vertex_properties())
(dist_map);
return ret;
}
......
......@@ -118,9 +118,9 @@ private:
struct do_djk_search
{
template <class Graph, class DistanceMap, class WeightMap>
template <class Graph, class DistanceMap>
void operator()(const Graph& g, size_t s, DistanceMap dist,
boost::any pred_map, WeightMap weight,
boost::any pred_map, boost::any aweight,
DJKVisitorWrapper vis, const DJKCmp& cmp, const DJKCmb& cmb,
pair<python::object, python::object> range) const
{
......@@ -130,6 +130,9 @@ struct do_djk_search
typedef typename property_map_type::
apply<int32_t, typeof(get(vertex_index, g))>::type pred_t;
pred_t pred = any_cast<pred_t>(pred_map);
typedef typename graph_traits<Graph>::edge_descriptor edge_t;
DynamicPropertyMapWrap<dtype_t, edge_t> weight(aweight,
edge_properties());
dijkstra_shortest_paths_no_color_map
(g, vertex(s, g), visitor(vis).weight_map(weight).
predecessor_map(pred).
......@@ -146,12 +149,10 @@ void dijkstra_search(GraphInterface& g, python::object gi, size_t source,
python::object inf)
{
run_action<graph_tool::detail::all_graph_views,mpl::true_>()
(g, bind<void>(do_djk_search(), _1, source, _2, pred_map, _3,
(g, bind<void>(do_djk_search(), _1, source, _2, pred_map, weight,
DJKVisitorWrapper(gi, vis), DJKCmp(cmp), DJKCmb(cmb),
make_pair(zero, inf)),
writable_vertex_scalar_properties(),
edge_scalar_properties())
(dist_map, weight);
writable_vertex_properties())(dist_map);
}
void export_dijkstra()
......
......@@ -1500,7 +1500,7 @@ def astar_search(g, source, weight, visitor=AStarVisitor(),
if dist_type is not object:
h = lambda v: dist_type(heuristic(v))
else:
h = heurustic
h = heuristic
try:
if not implicit:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment