File indexing completed on 2025-01-30 09:43:00
0001
0002
0003
0004
0005
0006
0007 #ifndef BOOST_GRAPH_CYCLE_RATIO_HOWARD_HPP
0008 #define BOOST_GRAPH_CYCLE_RATIO_HOWARD_HPP
0009
0010 #include <vector>
0011 #include <list>
0012 #include <algorithm>
0013 #include <functional>
0014 #include <limits>
0015
0016 #include <boost/bind/bind.hpp>
0017 #include <boost/tuple/tuple.hpp>
0018 #include <boost/type_traits/is_same.hpp>
0019 #include <boost/type_traits/remove_const.hpp>
0020 #include <boost/concept_check.hpp>
0021 #include <boost/pending/queue.hpp>
0022 #include <boost/property_map/property_map.hpp>
0023 #include <boost/graph/graph_traits.hpp>
0024 #include <boost/graph/graph_concepts.hpp>
0025 #include <boost/concept/assert.hpp>
0026 #include <boost/algorithm/minmax_element.hpp>
0027
0028
0029
0030
0031
0032
0033
0034 namespace boost
0035 {
0036
0037
0038
0039
0040
0041
0042
0043 template < typename Float = double > struct mcr_float
0044 {
0045 typedef Float value_type;
0046
0047 static Float infinity()
0048 {
0049 return std::numeric_limits< value_type >::infinity();
0050 }
0051
0052 static Float epsilon() { return Float(-0.005); }
0053 };
0054
0055 namespace detail
0056 {
0057
0058 template < typename FloatTraits > struct min_comparator_props
0059 {
0060 typedef std::greater< typename FloatTraits::value_type > comparator;
0061 static const int multiplier = 1;
0062 };
0063
0064 template < typename FloatTraits > struct max_comparator_props
0065 {
0066 typedef std::less< typename FloatTraits::value_type > comparator;
0067 static const int multiplier = -1;
0068 };
0069
0070 template < typename FloatTraits, typename ComparatorProps >
0071 struct float_wrapper
0072 {
0073 typedef typename FloatTraits::value_type value_type;
0074 typedef ComparatorProps comparator_props_t;
0075 typedef typename ComparatorProps::comparator comparator;
0076
0077 static value_type infinity()
0078 {
0079 return FloatTraits::infinity() * ComparatorProps::multiplier;
0080 }
0081
0082 static value_type epsilon()
0083 {
0084 return FloatTraits::epsilon() * ComparatorProps::multiplier;
0085 }
0086 };
0087
0088
0089
0090
0091
0092
0093
0094 template < typename FloatTraits, typename Graph, typename VertexIndexMap,
0095 typename EdgeWeight1, typename EdgeWeight2 >
0096 class mcr_howard
0097 {
0098 public:
0099 typedef typename FloatTraits::value_type float_t;
0100 typedef typename FloatTraits::comparator_props_t cmp_props_t;
0101 typedef typename FloatTraits::comparator comparator_t;
0102 typedef enum
0103 {
0104 my_white = 0,
0105 my_black
0106 } my_color_type;
0107 typedef typename graph_traits< Graph >::vertex_descriptor vertex_t;
0108 typedef typename graph_traits< Graph >::edge_descriptor edge_t;
0109 typedef typename graph_traits< Graph >::vertices_size_type vn_t;
0110 typedef std::vector< float_t > vp_t;
0111 typedef typename boost::iterator_property_map< typename vp_t::iterator,
0112 VertexIndexMap >
0113 distance_map_t;
0114
0115 typedef typename std::vector< edge_t > ve_t;
0116 typedef std::vector< my_color_type > vcol_t;
0117 typedef
0118 typename ::boost::iterator_property_map< typename ve_t::iterator,
0119 VertexIndexMap >
0120 policy_t;
0121 typedef
0122 typename ::boost::iterator_property_map< typename vcol_t::iterator,
0123 VertexIndexMap >
0124 color_map_t;
0125
0126 typedef typename std::list< vertex_t >
0127 pinel_t;
0128 typedef typename std::vector< pinel_t > inedges1_t;
0129 typedef typename ::boost::iterator_property_map<
0130 typename inedges1_t::iterator, VertexIndexMap >
0131 inedges_t;
0132 typedef typename std::vector< edge_t > critical_cycle_t;
0133
0134
0135
0136 typedef
0137 typename boost::iterator_property_map< std::vector< int >::iterator,
0138 VertexIndexMap >
0139 badv_t;
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151 mcr_howard(const Graph& g, VertexIndexMap vim, EdgeWeight1 ewm,
0152 EdgeWeight2 ew2m)
0153 : m_g(g)
0154 , m_vim(vim)
0155 , m_ew1m(ewm)
0156 , m_ew2m(ew2m)
0157 , m_bound(mcr_bound())
0158 , m_cr(m_bound)
0159 , m_V(num_vertices(m_g))
0160 , m_dis(m_V, 0)
0161 , m_dm(m_dis.begin(), m_vim)
0162 , m_policyc(m_V)
0163 , m_policy(m_policyc.begin(), m_vim)
0164 , m_inelc(m_V)
0165 , m_inel(m_inelc.begin(), m_vim)
0166 , m_badvc(m_V, false)
0167 , m_badv(m_badvc.begin(), m_vim)
0168 , m_colcv(m_V)
0169 , m_col_bfs(m_V)
0170 {
0171 }
0172
0173
0174
0175
0176
0177
0178 float_t ocr_howard()
0179 {
0180 construct_policy_graph();
0181 int k = 0;
0182 float_t mcr = 0;
0183 do
0184 {
0185 mcr = policy_mcr();
0186 ++k;
0187 } while (
0188 try_improve_policy(mcr) && k < 100);
0189
0190 const float_t eps_ = -0.00000001 * cmp_props_t::multiplier;
0191 if (m_cmp(mcr, m_bound + eps_))
0192 {
0193 return FloatTraits::infinity();
0194 }
0195 else
0196 {
0197 return mcr;
0198 }
0199 }
0200 virtual ~mcr_howard() {}
0201
0202 protected:
0203 virtual void store_critical_edge(edge_t, critical_cycle_t&) {}
0204 virtual void store_critical_cycle(critical_cycle_t&) {}
0205
0206 private:
0207
0208
0209
0210 float_t mcr_bound()
0211 {
0212 typename graph_traits< Graph >::vertex_iterator vi, vie;
0213 typename graph_traits< Graph >::out_edge_iterator oei, oeie;
0214 float_t cz = (std::numeric_limits< float_t >::max)();
0215
0216 float_t s = 0;
0217 const float_t eps_ = std::numeric_limits< float_t >::epsilon();
0218 for (boost::tie(vi, vie) = vertices(m_g); vi != vie; ++vi)
0219 {
0220 for (boost::tie(oei, oeie) = out_edges(*vi, m_g); oei != oeie;
0221 ++oei)
0222 {
0223 s += std::abs(m_ew1m[*oei]);
0224 float_t a = std::abs(m_ew2m[*oei]);
0225 if (a > eps_ && a < cz)
0226 {
0227 cz = a;
0228 }
0229 }
0230 }
0231 return cmp_props_t::multiplier * (s / cz);
0232 }
0233
0234
0235
0236
0237 void construct_policy_graph()
0238 {
0239 m_sink = graph_traits< Graph >().null_vertex();
0240 typename graph_traits< Graph >::vertex_iterator vi, vie;
0241 typename graph_traits< Graph >::out_edge_iterator oei, oeie;
0242 for (boost::tie(vi, vie) = vertices(m_g); vi != vie; ++vi)
0243 {
0244 using namespace boost::placeholders;
0245
0246 boost::tie(oei, oeie) = out_edges(*vi, m_g);
0247 typename graph_traits< Graph >::out_edge_iterator mei
0248 = boost::first_max_element(oei, oeie,
0249 boost::bind(m_cmp,
0250 boost::bind(&EdgeWeight1::operator[], m_ew1m, _1),
0251 boost::bind(&EdgeWeight1::operator[], m_ew1m, _2)));
0252 if (mei == oeie)
0253 {
0254 if (m_sink == graph_traits< Graph >().null_vertex())
0255 {
0256 m_sink = *vi;
0257 }
0258 m_badv[*vi] = true;
0259 m_inel[m_sink].push_back(*vi);
0260 }
0261 else
0262 {
0263 m_inel[target(*mei, m_g)].push_back(*vi);
0264 m_policy[*vi] = *mei;
0265 }
0266 }
0267 }
0268
0269
0270
0271
0272 void mcr_bfv(vertex_t sv, float_t cr, color_map_t c)
0273 {
0274 boost::queue< vertex_t > Q;
0275 c[sv] = my_black;
0276 Q.push(sv);
0277 while (!Q.empty())
0278 {
0279 vertex_t v = Q.top();
0280 Q.pop();
0281 for (typename pinel_t::const_iterator itr = m_inel[v].begin();
0282 itr != m_inel[v].end(); ++itr)
0283
0284 {
0285 if (*itr != sv)
0286 {
0287 if (m_badv[*itr])
0288 {
0289 m_dm[*itr] = m_dm[v] + m_bound - cr;
0290 }
0291 else
0292 {
0293 m_dm[*itr] = m_dm[v] + m_ew1m[m_policy[*itr]]
0294 - m_ew2m[m_policy[*itr]] * cr;
0295 }
0296 c[*itr] = my_black;
0297 Q.push(*itr);
0298 }
0299 }
0300 }
0301 }
0302
0303
0304
0305
0306
0307
0308 vertex_t find_cycle_vertex(vertex_t sv)
0309 {
0310 vertex_t gv = sv;
0311 std::fill(m_colcv.begin(), m_colcv.end(), my_white);
0312 color_map_t cm(m_colcv.begin(), m_vim);
0313 do
0314 {
0315 cm[gv] = my_black;
0316 if (!m_badv[gv])
0317 {
0318 gv = target(m_policy[gv], m_g);
0319 }
0320 else
0321 {
0322 gv = m_sink;
0323 }
0324 } while (cm[gv] != my_black);
0325 return gv;
0326 }
0327
0328
0329
0330
0331 float_t cycle_ratio(vertex_t sv)
0332 {
0333 if (sv == m_sink)
0334 return m_bound;
0335 std::pair< float_t, float_t > sums_(float_t(0), float_t(0));
0336 vertex_t v = sv;
0337 critical_cycle_t cc;
0338 do
0339 {
0340 store_critical_edge(m_policy[v], cc);
0341 sums_.first += m_ew1m[m_policy[v]];
0342 sums_.second += m_ew2m[m_policy[v]];
0343 v = target(m_policy[v], m_g);
0344 } while (v != sv);
0345 float_t cr = sums_.first / sums_.second;
0346 if (m_cmp(m_cr, cr))
0347 {
0348 m_cr = cr;
0349 store_critical_cycle(cc);
0350 }
0351 return cr;
0352 }
0353
0354
0355
0356
0357 float_t policy_mcr()
0358 {
0359 using namespace boost::placeholders;
0360
0361 std::fill(m_col_bfs.begin(), m_col_bfs.end(), my_white);
0362 color_map_t vcm_ = color_map_t(m_col_bfs.begin(), m_vim);
0363 typename graph_traits< Graph >::vertex_iterator uv_itr, vie;
0364 boost::tie(uv_itr, vie) = vertices(m_g);
0365 float_t mcr = m_bound;
0366 while ((uv_itr = std::find_if(uv_itr, vie,
0367 boost::bind(std::equal_to< my_color_type >(), my_white,
0368 boost::bind(&color_map_t::operator[], vcm_, _1))))
0369 != vie)
0370
0371 {
0372 vertex_t gv = find_cycle_vertex(*uv_itr);
0373 float_t cr = cycle_ratio(gv);
0374 mcr_bfv(gv, cr, vcm_);
0375 if (m_cmp(mcr, cr))
0376 mcr = cr;
0377 ++uv_itr;
0378 }
0379 return mcr;
0380 }
0381
0382
0383
0384
0385 void improve_policy(vertex_t s, edge_t new_edge)
0386 {
0387 vertex_t t = target(m_policy[s], m_g);
0388 typename property_traits< VertexIndexMap >::value_type ti
0389 = m_vim[t];
0390 m_inelc[ti].erase(
0391 std::find(m_inelc[ti].begin(), m_inelc[ti].end(), s));
0392 m_policy[s] = new_edge;
0393 t = target(new_edge, m_g);
0394 m_inel[t].push_back(s);
0395 }
0396
0397
0398
0399
0400 bool try_improve_policy(float_t cr)
0401 {
0402 bool improved = false;
0403 typename graph_traits< Graph >::vertex_iterator vi, vie;
0404 typename graph_traits< Graph >::out_edge_iterator oei, oeie;
0405 const float_t eps_ = FloatTraits::epsilon();
0406 for (boost::tie(vi, vie) = vertices(m_g); vi != vie; ++vi)
0407 {
0408 if (!m_badv[*vi])
0409 {
0410 for (boost::tie(oei, oeie) = out_edges(*vi, m_g);
0411 oei != oeie; ++oei)
0412 {
0413 vertex_t t = target(*oei, m_g);
0414
0415 float_t dis_
0416 = m_ew1m[*oei] - m_ew2m[*oei] * cr + m_dm[t];
0417 if (m_cmp(m_dm[*vi] + eps_, dis_))
0418 {
0419 improve_policy(*vi, *oei);
0420 m_dm[*vi] = dis_;
0421 improved = true;
0422 }
0423 }
0424 }
0425 else
0426 {
0427 float_t dis_ = m_bound - cr + m_dm[m_sink];
0428 if (m_cmp(m_dm[*vi] + eps_, dis_))
0429 {
0430 m_dm[*vi] = dis_;
0431 }
0432 }
0433 }
0434 return improved;
0435 }
0436
0437 private:
0438 const Graph& m_g;
0439 VertexIndexMap m_vim;
0440 EdgeWeight1 m_ew1m;
0441 EdgeWeight2 m_ew2m;
0442 comparator_t m_cmp;
0443 float_t m_bound;
0444
0445 float_t m_cr;
0446
0447 vn_t m_V;
0448 vp_t m_dis;
0449 distance_map_t m_dm;
0450
0451 ve_t m_policyc;
0452 policy_t m_policy;
0453
0454 inedges1_t m_inelc;
0455 inedges_t m_inel;
0456
0457 std::vector< int > m_badvc;
0458 badv_t m_badv;
0459
0460 vcol_t m_colcv, m_col_bfs;
0461 vertex_t m_sink;
0462 };
0463
0464
0465
0466
0467 template < typename FloatTraits, typename Graph, typename VertexIndexMap,
0468 typename EdgeWeight1, typename EdgeWeight2 >
0469 class mcr_howard1 : public mcr_howard< FloatTraits, Graph, VertexIndexMap,
0470 EdgeWeight1, EdgeWeight2 >
0471 {
0472 public:
0473 typedef mcr_howard< FloatTraits, Graph, VertexIndexMap, EdgeWeight1,
0474 EdgeWeight2 >
0475 inhr_t;
0476 mcr_howard1(const Graph& g, VertexIndexMap vim, EdgeWeight1 ewm,
0477 EdgeWeight2 ew2m)
0478 : inhr_t(g, vim, ewm, ew2m)
0479 {
0480 }
0481
0482 void get_critical_cycle(typename inhr_t::critical_cycle_t& cc)
0483 {
0484 return cc.swap(m_cc);
0485 }
0486
0487 protected:
0488 void store_critical_edge(
0489 typename inhr_t::edge_t ed, typename inhr_t::critical_cycle_t& cc)
0490 {
0491 cc.push_back(ed);
0492 }
0493
0494 void store_critical_cycle(typename inhr_t::critical_cycle_t& cc)
0495 {
0496 m_cc.swap(cc);
0497 }
0498
0499 private:
0500 typename inhr_t::critical_cycle_t m_cc;
0501 };
0502
0503
0504
0505
0506
0507
0508
0509
0510
0511
0512 template < typename FT, typename TG, typename TVIM, typename TEW1,
0513 typename TEW2, typename EV >
0514 typename FT::value_type optimum_cycle_ratio(
0515 const TG& g, TVIM vim, TEW1 ewm, TEW2 ew2m, EV* pcc)
0516 {
0517 typedef typename graph_traits< TG >::directed_category DirCat;
0518 BOOST_STATIC_ASSERT(
0519 (is_convertible< DirCat*, directed_tag* >::value == true));
0520 BOOST_CONCEPT_ASSERT((IncidenceGraphConcept< TG >));
0521 BOOST_CONCEPT_ASSERT((VertexListGraphConcept< TG >));
0522 typedef typename graph_traits< TG >::vertex_descriptor Vertex;
0523 BOOST_CONCEPT_ASSERT((ReadablePropertyMapConcept< TVIM, Vertex >));
0524 typedef typename graph_traits< TG >::edge_descriptor Edge;
0525 BOOST_CONCEPT_ASSERT((ReadablePropertyMapConcept< TEW1, Edge >));
0526 BOOST_CONCEPT_ASSERT((ReadablePropertyMapConcept< TEW2, Edge >));
0527
0528 if (pcc == 0)
0529 {
0530 return detail::mcr_howard< FT, TG, TVIM, TEW1, TEW2 >(
0531 g, vim, ewm, ew2m)
0532 .ocr_howard();
0533 }
0534
0535 detail::mcr_howard1< FT, TG, TVIM, TEW1, TEW2 > obj(g, vim, ewm, ew2m);
0536 double ocr = obj.ocr_howard();
0537 obj.get_critical_cycle(*pcc);
0538 return ocr;
0539 }
0540 }
0541
0542
0543
0544
0545 template < typename FloatTraits, typename Graph, typename VertexIndexMap,
0546 typename EdgeWeight1Map, typename EdgeWeight2Map >
0547 inline typename FloatTraits::value_type maximum_cycle_ratio(const Graph& g,
0548 VertexIndexMap vim, EdgeWeight1Map ew1m, EdgeWeight2Map ew2m,
0549 std::vector< typename graph_traits< Graph >::edge_descriptor >* pcc = 0,
0550 FloatTraits = FloatTraits())
0551 {
0552 typedef detail::float_wrapper< FloatTraits,
0553 detail::max_comparator_props< FloatTraits > >
0554 Traits;
0555 return detail::optimum_cycle_ratio< Traits >(g, vim, ew1m, ew2m, pcc);
0556 }
0557
0558 template < typename Graph, typename VertexIndexMap, typename EdgeWeight1Map,
0559 typename EdgeWeight2Map >
0560 inline double maximum_cycle_ratio(const Graph& g, VertexIndexMap vim,
0561 EdgeWeight1Map ew1m, EdgeWeight2Map ew2m,
0562 std::vector< typename graph_traits< Graph >::edge_descriptor >* pcc = 0)
0563 {
0564 return maximum_cycle_ratio(g, vim, ew1m, ew2m, pcc, mcr_float<>());
0565 }
0566
0567
0568
0569 template < typename FloatTraits, typename Graph, typename VertexIndexMap,
0570 typename EdgeWeight1Map, typename EdgeWeight2Map >
0571 typename FloatTraits::value_type minimum_cycle_ratio(const Graph& g,
0572 VertexIndexMap vim, EdgeWeight1Map ew1m, EdgeWeight2Map ew2m,
0573 std::vector< typename graph_traits< Graph >::edge_descriptor >* pcc = 0,
0574 FloatTraits = FloatTraits())
0575 {
0576 typedef detail::float_wrapper< FloatTraits,
0577 detail::min_comparator_props< FloatTraits > >
0578 Traits;
0579 return detail::optimum_cycle_ratio< Traits >(g, vim, ew1m, ew2m, pcc);
0580 }
0581
0582 template < typename Graph, typename VertexIndexMap, typename EdgeWeight1Map,
0583 typename EdgeWeight2Map >
0584 inline double minimum_cycle_ratio(const Graph& g, VertexIndexMap vim,
0585 EdgeWeight1Map ew1m, EdgeWeight2Map ew2m,
0586 std::vector< typename graph_traits< Graph >::edge_descriptor >* pcc = 0)
0587 {
0588 return minimum_cycle_ratio(g, vim, ew1m, ew2m, pcc, mcr_float<>());
0589 }
0590
0591
0592
0593 template < typename FloatTraits, typename Graph, typename VertexIndexMap,
0594 typename EdgeWeightMap, typename EdgeIndexMap >
0595 inline typename FloatTraits::value_type maximum_cycle_mean(const Graph& g,
0596 VertexIndexMap vim, EdgeWeightMap ewm, EdgeIndexMap eim,
0597 std::vector< typename graph_traits< Graph >::edge_descriptor >* pcc = 0,
0598 FloatTraits ft = FloatTraits())
0599 {
0600 typedef typename remove_const<
0601 typename property_traits< EdgeWeightMap >::value_type >::type Weight;
0602 typename std::vector< Weight > ed_w2(boost::num_edges(g), 1);
0603 return maximum_cycle_ratio(
0604 g, vim, ewm, make_iterator_property_map(ed_w2.begin(), eim), pcc, ft);
0605 }
0606
0607 template < typename Graph, typename VertexIndexMap, typename EdgeWeightMap,
0608 typename EdgeIndexMap >
0609 inline double maximum_cycle_mean(const Graph& g, VertexIndexMap vim,
0610 EdgeWeightMap ewm, EdgeIndexMap eim,
0611 std::vector< typename graph_traits< Graph >::edge_descriptor >* pcc = 0)
0612 {
0613 return maximum_cycle_mean(g, vim, ewm, eim, pcc, mcr_float<>());
0614 }
0615
0616
0617
0618 template < typename FloatTraits, typename Graph, typename VertexIndexMap,
0619 typename EdgeWeightMap, typename EdgeIndexMap >
0620 inline typename FloatTraits::value_type minimum_cycle_mean(const Graph& g,
0621 VertexIndexMap vim, EdgeWeightMap ewm, EdgeIndexMap eim,
0622 std::vector< typename graph_traits< Graph >::edge_descriptor >* pcc = 0,
0623 FloatTraits ft = FloatTraits())
0624 {
0625 typedef typename remove_const<
0626 typename property_traits< EdgeWeightMap >::value_type >::type Weight;
0627 typename std::vector< Weight > ed_w2(boost::num_edges(g), 1);
0628 return minimum_cycle_ratio(
0629 g, vim, ewm, make_iterator_property_map(ed_w2.begin(), eim), pcc, ft);
0630 }
0631
0632 template < typename Graph, typename VertexIndexMap, typename EdgeWeightMap,
0633 typename EdgeIndexMap >
0634 inline double minimum_cycle_mean(const Graph& g, VertexIndexMap vim,
0635 EdgeWeightMap ewm, EdgeIndexMap eim,
0636 std::vector< typename graph_traits< Graph >::edge_descriptor >* pcc = 0)
0637 {
0638 return minimum_cycle_mean(g, vim, ewm, eim, pcc, mcr_float<>());
0639 }
0640
0641 }
0642
0643 #endif