| 1 | //======================================================================= |
|---|
| 2 | // Copyright 1997, 1998, 1999, 2000 University of Notre Dame. |
|---|
| 3 | // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek, |
|---|
| 4 | // |
|---|
| 5 | // Copyright 2007 Orkney, Inc. |
|---|
| 6 | // Author: Anton A. Patrushev |
|---|
| 7 | // |
|---|
| 8 | // Distributed under the Boost Software License, Version 1.0. (See |
|---|
| 9 | // accompanying file LICENSE_1_0.txt or copy at |
|---|
| 10 | // http://www.boost.org/LICENSE_1_0.txt) |
|---|
| 11 | //======================================================================= |
|---|
| 12 | #ifndef BOOST_GRAPH_SHOOTING_STAR_RELAX_HPP |
|---|
| 13 | #define BOOST_GRAPH_SHOOTING_STAR_RELAX_HPP |
|---|
| 14 | |
|---|
| 15 | #include <functional> |
|---|
| 16 | #include <boost/limits.hpp> // for numeric limits |
|---|
| 17 | #include <boost/graph/graph_traits.hpp> |
|---|
| 18 | #include <boost/property_map.hpp> |
|---|
| 19 | |
|---|
| 20 | #include <iostream> |
|---|
| 21 | #include <fstream> |
|---|
| 22 | #include <postgres.h> |
|---|
| 23 | |
|---|
| 24 | bool is_equal ( int a[], int b[], int size ) |
|---|
| 25 | { |
|---|
| 26 | for ( int i = 0; i < size; i++ ) |
|---|
| 27 | { |
|---|
| 28 | if ( a[i] != b[i] ) |
|---|
| 29 | return false; |
|---|
| 30 | } |
|---|
| 31 | |
|---|
| 32 | return true; |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | namespace boost { |
|---|
| 36 | |
|---|
| 37 | // The following version of the plus functor prevents |
|---|
| 38 | // problems due to overflow at positive infinity. |
|---|
| 39 | |
|---|
| 40 | template <class T> |
|---|
| 41 | struct closed_plus |
|---|
| 42 | { |
|---|
| 43 | // std::abs just isn't portable :( |
|---|
| 44 | template <class X> |
|---|
| 45 | inline X my_abs(const X& x) const { return x < 0 ? -x : x; } |
|---|
| 46 | |
|---|
| 47 | T operator()(const T& a, const T& b) const { |
|---|
| 48 | using namespace std; |
|---|
| 49 | T inf = (numeric_limits<T>::max)(); |
|---|
| 50 | if (b > 0 && my_abs(inf - a) < b) |
|---|
| 51 | return inf; |
|---|
| 52 | return a + b; |
|---|
| 53 | } |
|---|
| 54 | }; |
|---|
| 55 | |
|---|
| 56 | template <class Edge, class Graph, class WeightMap, class EdgeMap, |
|---|
| 57 | class PredecessorMap, class DistanceMap, class CostMap, |
|---|
| 58 | class HistoryMap, class BinaryFunction, class BinaryPredicate> |
|---|
| 59 | bool relax(Edge e, |
|---|
| 60 | Edge pe, |
|---|
| 61 | const Graph& g, const WeightMap& w, const EdgeMap& em, |
|---|
| 62 | PredecessorMap& p, DistanceMap& d, CostMap& c, |
|---|
| 63 | HistoryMap& his, |
|---|
| 64 | const BinaryFunction& combine, const BinaryPredicate& compare, |
|---|
| 65 | int e_max_id) |
|---|
| 66 | { |
|---|
| 67 | typedef typename graph_traits<Graph>::directed_category DirCat; |
|---|
| 68 | bool is_undirected = is_same<DirCat, undirected_tag>::value; |
|---|
| 69 | |
|---|
| 70 | typedef typename graph_traits<Graph>::vertex_descriptor Vertex; |
|---|
| 71 | |
|---|
| 72 | Vertex u = source(e, g), v = target(e, g), pu = source(pe, g); |
|---|
| 73 | |
|---|
| 74 | typedef typename property_traits<DistanceMap>::value_type D; |
|---|
| 75 | typedef typename property_traits<WeightMap>::value_type W; |
|---|
| 76 | |
|---|
| 77 | typedef typename property_traits<EdgeMap>::value_type E; |
|---|
| 78 | |
|---|
| 79 | D d_e = get(d, e); |
|---|
| 80 | D d_pe = get(d, pe); |
|---|
| 81 | |
|---|
| 82 | W w_e = get(w, e); |
|---|
| 83 | |
|---|
| 84 | //myfile<<"w(e) = "<<w_e<<"\n"<<std::flush; |
|---|
| 85 | |
|---|
| 86 | //myfile<<"cost("<<g[e].id<<") = "<<get(c, e)<<"\n"<<std::flush; |
|---|
| 87 | |
|---|
| 88 | //myfile<<"d("<<g[e].id<<") = "<<get(d, e)<<"\n"<<std::flush; |
|---|
| 89 | |
|---|
| 90 | //myfile<<g[u].id<<" - "<<g[v].id<<" : d_e="<<d_e<<"\n"<<std::flush; |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | //edge where we came from |
|---|
| 94 | bool edge_exists; |
|---|
| 95 | //Edge pe; |
|---|
| 96 | |
|---|
| 97 | //pe = get(p, g[e].id); |
|---|
| 98 | |
|---|
| 99 | W w_pe_e; |
|---|
| 100 | |
|---|
| 101 | //E w_pe = get(em, pe); |
|---|
| 102 | E w_pe = get(em, e); |
|---|
| 103 | |
|---|
| 104 | bool contains = false; |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | //myfile<<"w(pe, e) = "<<w_pe_e<<"\n"<<std::flush; |
|---|
| 108 | |
|---|
| 109 | //myfile<<"id = "<<g[e].id<<", size = "<<w_pe[g[e].id].second.size()<<"\n"<<std::flush; |
|---|
| 110 | |
|---|
| 111 | Edge ce = pe; |
|---|
| 112 | int e_id; |
|---|
| 113 | |
|---|
| 114 | std::vector<Edge> edge_h; |
|---|
| 115 | typedef typename std::vector<Edge>::iterator It; |
|---|
| 116 | |
|---|
| 117 | //for(int i=w_pe[g[e].id].second.size()-1; i>=0; --i) |
|---|
| 118 | |
|---|
| 119 | if(w_pe[g[e].id].second.size() < 1 || w_pe[g[e].id].second.back() > 0) |
|---|
| 120 | for(int i=0; i<w_pe[g[e].id].second.size(); ++i) |
|---|
| 121 | { |
|---|
| 122 | e_id = g[ce].id; |
|---|
| 123 | //myfile<<"e_id="<<e_id<<", second["<<g[e].id<<"]="<<w_pe[g[e].id].second.at(i)<<"\n"<<std::flush; |
|---|
| 124 | |
|---|
| 125 | if(w_pe[g[e].id].second.at(i) == -1) |
|---|
| 126 | continue; |
|---|
| 127 | |
|---|
| 128 | if(w_pe[g[e].id].second.at(i) == e_id || w_pe[g[e].id].second.at(i)+e_max_id == e_id) |
|---|
| 129 | { |
|---|
| 130 | contains = true; |
|---|
| 131 | edge_h.push_back(ce); |
|---|
| 132 | } |
|---|
| 133 | else |
|---|
| 134 | { |
|---|
| 135 | contains = false; |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | ce = p[g[ce].id]; |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | //calculate w_pe_e |
|---|
| 142 | if(contains) |
|---|
| 143 | { |
|---|
| 144 | |
|---|
| 145 | w_pe_e = w_pe[g[e].id].first; |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | } |
|---|
| 149 | else |
|---|
| 150 | { |
|---|
| 151 | w_pe_e = 0; |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | //Ugly combination with w_e_pe. |
|---|
| 155 | |
|---|
| 156 | if ( compare(combine(combine(d_pe, get(w, pe)), w_pe_e), d_e)) |
|---|
| 157 | { |
|---|
| 158 | put(d, e, combine(combine(d_pe, get(w, pe)), w_pe_e)); |
|---|
| 159 | p[g[e].id] = pe; |
|---|
| 160 | |
|---|
| 161 | return true; |
|---|
| 162 | } |
|---|
| 163 | else |
|---|
| 164 | { |
|---|
| 165 | return false; |
|---|
| 166 | } |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | template <class Graph, class WeightMap, class EdgeMap, |
|---|
| 170 | class PredecessorMap, class DistanceMap> |
|---|
| 171 | bool relax(typename graph_traits<Graph>::edge_descriptor e, |
|---|
| 172 | const Graph& g, WeightMap w, EdgeMap em, PredecessorMap p, DistanceMap d) |
|---|
| 173 | { |
|---|
| 174 | typedef typename property_traits<DistanceMap>::value_type D; |
|---|
| 175 | typedef closed_plus<D> Combine; |
|---|
| 176 | typedef std::less<D> Compare; |
|---|
| 177 | return relax(e, g, w, em, p, d, Combine(), Compare()); |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | } // namespace boost |
|---|
| 181 | |
|---|
| 182 | #endif |
|---|