1 // Copyright 2004 The Trustees of Indiana University.
2
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6
7 // Authors: Jeremiah Willcock
8 // Douglas Gregor
9 // Andrew Lumsdaine
10 #ifndef BOOST_GRAPH_GURSOY_ATUN_LAYOUT_HPP
11 #define BOOST_GRAPH_GURSOY_ATUN_LAYOUT_HPP
12
13 // Gürsoy-Atun graph layout, based on:
14 // "Neighbourhood Preserving Load Balancing: A Self-Organizing Approach"
15 // in 6th International Euro-Par Conference Munich, Germany, August 29 –
16 // September 1, 2000 Proceedings, pp 234-241
17 // https://doi.org/10.1007/3-540-44520-X_32
18
19 #include <boost/config/no_tr1/cmath.hpp>
20 #include <boost/throw_exception.hpp>
21 #include <boost/assert.hpp>
22 #include <vector>
23 #include <exception>
24 #include <algorithm>
25
26 #include <boost/graph/visitors.hpp>
27 #include <boost/graph/properties.hpp>
28 #include <boost/random/uniform_01.hpp>
29 #include <boost/random/linear_congruential.hpp>
30 #include <boost/shared_ptr.hpp>
31 #include <boost/graph/breadth_first_search.hpp>
32 #include <boost/graph/dijkstra_shortest_paths.hpp>
33 #include <boost/graph/named_function_params.hpp>
34 #include <boost/graph/topology.hpp>
35
36 namespace boost
37 {
38
39 namespace detail
40 {
41
42 struct over_distance_limit : public std::exception
43 {
44 };
45
46 template < typename PositionMap, typename NodeDistanceMap,
47 typename Topology, typename Graph >
48 struct update_position_visitor
49 {
50 typedef typename Topology::point_type Point;
51 PositionMap position_map;
52 NodeDistanceMap node_distance;
53 const Topology& space;
54 Point input_vector;
55 double distance_limit;
56 double learning_constant;
57 double falloff_ratio;
58
59 typedef boost::on_examine_vertex event_filter;
60
61 typedef
62 typename graph_traits< Graph >::vertex_descriptor vertex_descriptor;
63
update_position_visitorboost::detail::update_position_visitor64 update_position_visitor(PositionMap position_map,
65 NodeDistanceMap node_distance, const Topology& space,
66 const Point& input_vector, double distance_limit,
67 double learning_constant, double falloff_ratio)
68 : position_map(position_map)
69 , node_distance(node_distance)
70 , space(space)
71 , input_vector(input_vector)
72 , distance_limit(distance_limit)
73 , learning_constant(learning_constant)
74 , falloff_ratio(falloff_ratio)
75 {
76 }
77
operator ()boost::detail::update_position_visitor78 void operator()(vertex_descriptor v, const Graph&) const
79 {
80 #ifndef BOOST_NO_STDC_NAMESPACE
81 using std::pow;
82 #endif
83
84 if (get(node_distance, v) > distance_limit)
85 BOOST_THROW_EXCEPTION(over_distance_limit());
86 Point old_position = get(position_map, v);
87 double distance = get(node_distance, v);
88 double fraction
89 = learning_constant * pow(falloff_ratio, distance * distance);
90 put(position_map, v,
91 space.move_position_toward(
92 old_position, fraction, input_vector));
93 }
94 };
95
96 template < typename EdgeWeightMap > struct gursoy_shortest
97 {
98 template < typename Graph, typename NodeDistanceMap,
99 typename UpdatePosition >
runboost::detail::gursoy_shortest100 static inline void run(const Graph& g,
101 typename graph_traits< Graph >::vertex_descriptor s,
102 NodeDistanceMap node_distance, UpdatePosition& update_position,
103 EdgeWeightMap weight)
104 {
105 boost::dijkstra_shortest_paths(g, s,
106 weight_map(weight).visitor(boost::make_dijkstra_visitor(
107 std::make_pair(boost::record_distances(
108 node_distance, boost::on_edge_relaxed()),
109 update_position))));
110 }
111 };
112
113 template <> struct gursoy_shortest< dummy_property_map >
114 {
115 template < typename Graph, typename NodeDistanceMap,
116 typename UpdatePosition >
runboost::detail::gursoy_shortest117 static inline void run(const Graph& g,
118 typename graph_traits< Graph >::vertex_descriptor s,
119 NodeDistanceMap node_distance, UpdatePosition& update_position,
120 dummy_property_map)
121 {
122 boost::breadth_first_search(g, s,
123 visitor(boost::make_bfs_visitor(
124 std::make_pair(boost::record_distances(
125 node_distance, boost::on_tree_edge()),
126 update_position))));
127 }
128 };
129
130 } // namespace detail
131
132 template < typename VertexListAndIncidenceGraph, typename Topology,
133 typename PositionMap, typename Diameter, typename VertexIndexMap,
134 typename EdgeWeightMap >
gursoy_atun_step(const VertexListAndIncidenceGraph & graph,const Topology & space,PositionMap position,Diameter diameter,double learning_constant,VertexIndexMap vertex_index_map,EdgeWeightMap weight)135 void gursoy_atun_step(const VertexListAndIncidenceGraph& graph,
136 const Topology& space, PositionMap position, Diameter diameter,
137 double learning_constant, VertexIndexMap vertex_index_map,
138 EdgeWeightMap weight)
139 {
140 #ifndef BOOST_NO_STDC_NAMESPACE
141 using std::exp;
142 using std::pow;
143 #endif
144
145 typedef
146 typename graph_traits< VertexListAndIncidenceGraph >::vertex_iterator
147 vertex_iterator;
148 typedef
149 typename graph_traits< VertexListAndIncidenceGraph >::vertex_descriptor
150 vertex_descriptor;
151 typedef typename Topology::point_type point_type;
152 vertex_iterator i, iend;
153 std::vector< double > distance_from_input_vector(num_vertices(graph));
154 typedef boost::iterator_property_map< std::vector< double >::iterator,
155 VertexIndexMap, double, double& >
156 DistanceFromInputMap;
157 DistanceFromInputMap distance_from_input(
158 distance_from_input_vector.begin(), vertex_index_map);
159 std::vector< double > node_distance_map_vector(num_vertices(graph));
160 typedef boost::iterator_property_map< std::vector< double >::iterator,
161 VertexIndexMap, double, double& >
162 NodeDistanceMap;
163 NodeDistanceMap node_distance(
164 node_distance_map_vector.begin(), vertex_index_map);
165 point_type input_vector = space.random_point();
166 vertex_descriptor min_distance_loc
167 = graph_traits< VertexListAndIncidenceGraph >::null_vertex();
168 double min_distance = 0.0;
169 bool min_distance_unset = true;
170 for (boost::tie(i, iend) = vertices(graph); i != iend; ++i)
171 {
172 double this_distance = space.distance(get(position, *i), input_vector);
173 put(distance_from_input, *i, this_distance);
174 if (min_distance_unset || this_distance < min_distance)
175 {
176 min_distance = this_distance;
177 min_distance_loc = *i;
178 }
179 min_distance_unset = false;
180 }
181 BOOST_ASSERT(!min_distance_unset); // Graph must have at least one vertex
182 boost::detail::update_position_visitor< PositionMap, NodeDistanceMap,
183 Topology, VertexListAndIncidenceGraph >
184 update_position(position, node_distance, space, input_vector, diameter,
185 learning_constant, exp(-1. / (2 * diameter * diameter)));
186 std::fill(
187 node_distance_map_vector.begin(), node_distance_map_vector.end(), 0);
188 try
189 {
190 typedef detail::gursoy_shortest< EdgeWeightMap > shortest;
191 shortest::run(
192 graph, min_distance_loc, node_distance, update_position, weight);
193 }
194 catch (const detail::over_distance_limit&)
195 {
196 /* Thrown to break out of BFS or Dijkstra early */
197 }
198 }
199
200 template < typename VertexListAndIncidenceGraph, typename Topology,
201 typename PositionMap, typename VertexIndexMap, typename EdgeWeightMap >
gursoy_atun_refine(const VertexListAndIncidenceGraph & graph,const Topology & space,PositionMap position,int nsteps,double diameter_initial,double diameter_final,double learning_constant_initial,double learning_constant_final,VertexIndexMap vertex_index_map,EdgeWeightMap weight)202 void gursoy_atun_refine(const VertexListAndIncidenceGraph& graph,
203 const Topology& space, PositionMap position, int nsteps,
204 double diameter_initial, double diameter_final,
205 double learning_constant_initial, double learning_constant_final,
206 VertexIndexMap vertex_index_map, EdgeWeightMap weight)
207 {
208 #ifndef BOOST_NO_STDC_NAMESPACE
209 using std::exp;
210 using std::pow;
211 #endif
212
213 typedef
214 typename graph_traits< VertexListAndIncidenceGraph >::vertex_iterator
215 vertex_iterator;
216 vertex_iterator i, iend;
217 double diameter_ratio = (double)diameter_final / diameter_initial;
218 double learning_constant_ratio
219 = learning_constant_final / learning_constant_initial;
220 std::vector< double > distance_from_input_vector(num_vertices(graph));
221 typedef boost::iterator_property_map< std::vector< double >::iterator,
222 VertexIndexMap, double, double& >
223 DistanceFromInputMap;
224 DistanceFromInputMap distance_from_input(
225 distance_from_input_vector.begin(), vertex_index_map);
226 std::vector< int > node_distance_map_vector(num_vertices(graph));
227 typedef boost::iterator_property_map< std::vector< int >::iterator,
228 VertexIndexMap, double, double& >
229 NodeDistanceMap;
230 NodeDistanceMap node_distance(
231 node_distance_map_vector.begin(), vertex_index_map);
232 for (int round = 0; round < nsteps; ++round)
233 {
234 double part_done = (double)round / (nsteps - 1);
235 // fprintf(stderr, "%2d%% done\n", int(rint(part_done * 100.)));
236 int diameter = (int)(diameter_initial * pow(diameter_ratio, part_done));
237 double learning_constant = learning_constant_initial
238 * pow(learning_constant_ratio, part_done);
239 gursoy_atun_step(graph, space, position, diameter, learning_constant,
240 vertex_index_map, weight);
241 }
242 }
243
244 template < typename VertexListAndIncidenceGraph, typename Topology,
245 typename PositionMap, typename VertexIndexMap, typename EdgeWeightMap >
gursoy_atun_layout(const VertexListAndIncidenceGraph & graph,const Topology & space,PositionMap position,int nsteps,double diameter_initial,double diameter_final,double learning_constant_initial,double learning_constant_final,VertexIndexMap vertex_index_map,EdgeWeightMap weight)246 void gursoy_atun_layout(const VertexListAndIncidenceGraph& graph,
247 const Topology& space, PositionMap position, int nsteps,
248 double diameter_initial, double diameter_final,
249 double learning_constant_initial, double learning_constant_final,
250 VertexIndexMap vertex_index_map, EdgeWeightMap weight)
251 {
252 typedef
253 typename graph_traits< VertexListAndIncidenceGraph >::vertex_iterator
254 vertex_iterator;
255 vertex_iterator i, iend;
256 for (boost::tie(i, iend) = vertices(graph); i != iend; ++i)
257 {
258 put(position, *i, space.random_point());
259 }
260 gursoy_atun_refine(graph, space, position, nsteps, diameter_initial,
261 diameter_final, learning_constant_initial, learning_constant_final,
262 vertex_index_map, weight);
263 }
264
265 template < typename VertexListAndIncidenceGraph, typename Topology,
266 typename PositionMap, typename VertexIndexMap >
gursoy_atun_layout(const VertexListAndIncidenceGraph & graph,const Topology & space,PositionMap position,int nsteps,double diameter_initial,double diameter_final,double learning_constant_initial,double learning_constant_final,VertexIndexMap vertex_index_map)267 void gursoy_atun_layout(const VertexListAndIncidenceGraph& graph,
268 const Topology& space, PositionMap position, int nsteps,
269 double diameter_initial, double diameter_final,
270 double learning_constant_initial, double learning_constant_final,
271 VertexIndexMap vertex_index_map)
272 {
273 gursoy_atun_layout(graph, space, position, nsteps, diameter_initial,
274 diameter_final, learning_constant_initial, learning_constant_final,
275 vertex_index_map, dummy_property_map());
276 }
277
278 template < typename VertexListAndIncidenceGraph, typename Topology,
279 typename PositionMap >
gursoy_atun_layout(const VertexListAndIncidenceGraph & graph,const Topology & space,PositionMap position,int nsteps,double diameter_initial,double diameter_final=1.0,double learning_constant_initial=0.8,double learning_constant_final=0.2)280 void gursoy_atun_layout(const VertexListAndIncidenceGraph& graph,
281 const Topology& space, PositionMap position, int nsteps,
282 double diameter_initial, double diameter_final = 1.0,
283 double learning_constant_initial = 0.8,
284 double learning_constant_final = 0.2)
285 {
286 gursoy_atun_layout(graph, space, position, nsteps, diameter_initial,
287 diameter_final, learning_constant_initial, learning_constant_final,
288 get(vertex_index, graph));
289 }
290
291 template < typename VertexListAndIncidenceGraph, typename Topology,
292 typename PositionMap >
gursoy_atun_layout(const VertexListAndIncidenceGraph & graph,const Topology & space,PositionMap position,int nsteps)293 void gursoy_atun_layout(const VertexListAndIncidenceGraph& graph,
294 const Topology& space, PositionMap position, int nsteps)
295 {
296 #ifndef BOOST_NO_STDC_NAMESPACE
297 using std::sqrt;
298 #endif
299
300 gursoy_atun_layout(
301 graph, space, position, nsteps, sqrt((double)num_vertices(graph)));
302 }
303
304 template < typename VertexListAndIncidenceGraph, typename Topology,
305 typename PositionMap >
gursoy_atun_layout(const VertexListAndIncidenceGraph & graph,const Topology & space,PositionMap position)306 void gursoy_atun_layout(const VertexListAndIncidenceGraph& graph,
307 const Topology& space, PositionMap position)
308 {
309 gursoy_atun_layout(graph, space, position, num_vertices(graph));
310 }
311
312 template < typename VertexListAndIncidenceGraph, typename Topology,
313 typename PositionMap, typename P, typename T, typename R >
gursoy_atun_layout(const VertexListAndIncidenceGraph & graph,const Topology & space,PositionMap position,const bgl_named_params<P,T,R> & params)314 void gursoy_atun_layout(const VertexListAndIncidenceGraph& graph,
315 const Topology& space, PositionMap position,
316 const bgl_named_params< P, T, R >& params)
317 {
318 #ifndef BOOST_NO_STDC_NAMESPACE
319 using std::sqrt;
320 #endif
321
322 std::pair< double, double > diam(sqrt(double(num_vertices(graph))), 1.0);
323 std::pair< double, double > learn(0.8, 0.2);
324 gursoy_atun_layout(graph, space, position,
325 choose_param(get_param(params, iterations_t()), num_vertices(graph)),
326 choose_param(get_param(params, diameter_range_t()), diam).first,
327 choose_param(get_param(params, diameter_range_t()), diam).second,
328 choose_param(get_param(params, learning_constant_range_t()), learn)
329 .first,
330 choose_param(get_param(params, learning_constant_range_t()), learn)
331 .second,
332 choose_const_pmap(get_param(params, vertex_index), graph, vertex_index),
333 choose_param(get_param(params, edge_weight), dummy_property_map()));
334 }
335
336 } // namespace boost
337
338 #endif // BOOST_GRAPH_GURSOY_ATUN_LAYOUT_HPP
339