1 | /* |
---|
2 | * Copyright (c) 2007 Anton A. Patrushev, Orkney, Inc. |
---|
3 | * |
---|
4 | * This program is free software; you can redistribute it and/or modify |
---|
5 | * it under the terms of the GNU General Public License as published by |
---|
6 | * the Free Software Foundation; either version 2 of the License, or |
---|
7 | * (at your option) any later version. |
---|
8 | * |
---|
9 | * This program is distributed in the hope that it will be useful, |
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | * GNU General Public License for more details. |
---|
13 | * |
---|
14 | * You should have received a copy of the GNU General Public License |
---|
15 | * along with this program; if not, write to the Free Software |
---|
16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
17 | * |
---|
18 | */ |
---|
19 | |
---|
20 | #ifndef BOOST_GRAPH_SHOOTING_STAR_RELAX_HPP |
---|
21 | #define BOOST_GRAPH_SHOOTING_STAR_RELAX_HPP |
---|
22 | |
---|
23 | #include <functional> |
---|
24 | #include <boost/limits.hpp> // for numeric limits |
---|
25 | #include <boost/graph/graph_traits.hpp> |
---|
26 | #include <boost/property_map.hpp> |
---|
27 | |
---|
28 | |
---|
29 | #define U_TURN_COST 100000 |
---|
30 | |
---|
31 | bool is_equal ( int a[], int b[], int size ) |
---|
32 | { |
---|
33 | for ( int i = 0; i < size; i++ ) |
---|
34 | { |
---|
35 | if ( a[i] != b[i] ) |
---|
36 | return false; |
---|
37 | } |
---|
38 | |
---|
39 | return true; |
---|
40 | } |
---|
41 | |
---|
42 | namespace boost { |
---|
43 | |
---|
44 | template <class Edge, class Graph, class WeightMap, class EdgeMap, |
---|
45 | class PredecessorMap, class DistanceMap, class CostMap, |
---|
46 | class BinaryFunction, class BinaryPredicate> |
---|
47 | bool relax(Edge e, |
---|
48 | Edge pe, |
---|
49 | const Graph& g, const WeightMap& w, const EdgeMap& em, |
---|
50 | PredecessorMap& p, DistanceMap& d, CostMap& c, |
---|
51 | const BinaryFunction& combine, const BinaryPredicate& compare, |
---|
52 | int e_max_id) |
---|
53 | { |
---|
54 | typedef typename graph_traits<Graph>::directed_category DirCat; |
---|
55 | bool is_undirected = is_same<DirCat, undirected_tag>::value; |
---|
56 | |
---|
57 | typedef typename graph_traits<Graph>::vertex_descriptor Vertex; |
---|
58 | |
---|
59 | typedef typename property_traits<DistanceMap>::value_type D; |
---|
60 | typedef typename property_traits<WeightMap>::value_type W; |
---|
61 | |
---|
62 | typedef typename property_traits<EdgeMap>::value_type E; |
---|
63 | |
---|
64 | D d_e = get(d, e); |
---|
65 | D d_pe = get(d, pe); |
---|
66 | |
---|
67 | W w_pe_e; |
---|
68 | |
---|
69 | E w_pe = get(em, e); |
---|
70 | |
---|
71 | int contains = -1; |
---|
72 | |
---|
73 | Edge ce = pe; |
---|
74 | int e_id; |
---|
75 | |
---|
76 | std::vector<Edge> edge_h; |
---|
77 | typedef typename std::vector<Edge>::iterator It; |
---|
78 | |
---|
79 | for(int i=0; i< w_pe[g[e].id].size(); ++i) |
---|
80 | |
---|
81 | if(w_pe[g[e].id].at(i).second.size() < 1 || w_pe[g[e].id].at(i).second.back() > 0) |
---|
82 | { |
---|
83 | |
---|
84 | for(int j=0; j<w_pe[g[e].id].at(i).second.size(); ++j) |
---|
85 | { |
---|
86 | e_id = g[ce].id; |
---|
87 | |
---|
88 | if(w_pe[g[e].id].at(i).second.at(j) == -1) |
---|
89 | continue; |
---|
90 | |
---|
91 | if(w_pe[g[e].id].at(i).second.at(j) == e_id || w_pe[g[e].id].at(i).second.at(j)+e_max_id == e_id|| |
---|
92 | w_pe[g[e].id].at(i).second.at(j) == e_id+e_max_id || w_pe[g[e].id].at(i).second.at(j)+e_max_id == e_id+e_max_id) |
---|
93 | { |
---|
94 | contains = i; |
---|
95 | edge_h.push_back(ce); |
---|
96 | } |
---|
97 | else if(i == contains) |
---|
98 | { |
---|
99 | contains = -1; |
---|
100 | } |
---|
101 | |
---|
102 | ce = p[g[ce].id]; |
---|
103 | } |
---|
104 | |
---|
105 | } |
---|
106 | |
---|
107 | //calculate w_pe_e |
---|
108 | if(contains >= 0) |
---|
109 | { |
---|
110 | w_pe_e = w_pe[g[e].id].at(contains).first; |
---|
111 | } |
---|
112 | else if(abs(g[e].id-g[pe].id) == e_max_id) |
---|
113 | { |
---|
114 | w_pe_e = U_TURN_COST; |
---|
115 | } |
---|
116 | else |
---|
117 | { |
---|
118 | w_pe_e = 0; |
---|
119 | } |
---|
120 | |
---|
121 | //Ugly combination with w_e_pe. |
---|
122 | |
---|
123 | if ( compare(combine(combine(d_pe, get(w, pe)), w_pe_e), d_e)) |
---|
124 | { |
---|
125 | put(d, e, combine(combine(d_pe, get(w, pe)), w_pe_e)); |
---|
126 | p[g[e].id] = pe; |
---|
127 | |
---|
128 | return true; |
---|
129 | } |
---|
130 | else |
---|
131 | { |
---|
132 | return false; |
---|
133 | } |
---|
134 | } |
---|
135 | |
---|
136 | template <class Graph, class WeightMap, class EdgeMap, |
---|
137 | class PredecessorMap, class DistanceMap> |
---|
138 | bool relax(typename graph_traits<Graph>::edge_descriptor e, |
---|
139 | const Graph& g, WeightMap w, EdgeMap em, PredecessorMap p, DistanceMap d) |
---|
140 | { |
---|
141 | typedef typename property_traits<DistanceMap>::value_type D; |
---|
142 | typedef closed_plus<D> Combine; |
---|
143 | typedef std::less<D> Compare; |
---|
144 | return relax(e, g, w, em, p, d, Combine(), Compare()); |
---|
145 | } |
---|
146 | |
---|
147 | } // namespace boost |
---|
148 | |
---|
149 | #endif |
---|