Update.
[mtp.git] / mtp_graph.cc
1
2 ///////////////////////////////////////////////////////////////////////////
3 // This program is free software: you can redistribute it and/or modify  //
4 // it under the terms of the version 3 of the GNU General Public License //
5 // as published by the Free Software Foundation.                         //
6 //                                                                       //
7 // This program is distributed in the hope that it will be useful, but   //
8 // WITHOUT ANY WARRANTY; without even the implied warranty of            //
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      //
10 // General Public License for more details.                              //
11 //                                                                       //
12 // You should have received a copy of the GNU General Public License     //
13 // along with this program. If not, see <http://www.gnu.org/licenses/>.  //
14 //                                                                       //
15 // Written by and Copyright (C) Francois Fleuret                         //
16 // Contact <francois.fleuret@idiap.ch> for comments & bug reports        //
17 ///////////////////////////////////////////////////////////////////////////
18
19 #include "mtp_graph.h"
20
21 #include <iostream>
22 #include <float.h>
23
24 using namespace std;
25
26 class Edge {
27 public:
28   int id, occupied;
29   scalar_t length, work_length;
30   Vertex *terminal_vertex;
31   Edge *next, *pred;
32 };
33
34 class Vertex {
35 public:
36   int id, iteration;
37   Edge *root_edge;
38   scalar_t distance_from_source;
39   Vertex *pred_vertex;
40   Edge *pred_edge;
41
42   Vertex() { root_edge = 0; }
43
44   inline void add_edge(Edge *e) {
45     e->next = root_edge;
46     e->pred = 0;
47     if(root_edge) { root_edge->pred = e; }
48     root_edge = e;
49   }
50
51   inline void del_edge(Edge *e) {
52     if(e == root_edge) { root_edge = e->next; }
53     if(e->pred) { e->pred->next = e->next; }
54     if(e->next) { e->next->pred = e->pred; }
55   }
56 };
57
58 void MTPGraph::print() {
59   for(int n = 0; n < _nb_vertices; n++) {
60     for(Edge *e = vertices[n].root_edge; e; e = e->next) {
61       cout << n << " -> " << e->terminal_vertex->id << " " << e->length;
62       if(e->occupied) {
63         cout << " *";
64       }
65       cout << endl;
66     }
67   }
68 }
69
70 void MTPGraph::print_dot() {
71   cout << "digraph {" << endl;
72   cout << "  node[shape=circle];" << endl;
73   for(int n = 0; n < _nb_vertices; n++) {
74     int a = vertices[n].id;
75     for(Edge *e = vertices[n].root_edge; e; e = e->next) {
76       int b = e->terminal_vertex->id;
77       if(e->occupied) {
78         cout << "  " << b << " -> " << a << " [style=bold,color=black,label=\"" << -e->length << "\"];" << endl;
79       } else {
80         cout << "  " << a << " -> " << b << " [color=gray,label=\"" << e->length << "\"];" << endl;
81       }
82     }
83   }
84   cout << "}" << endl;
85 }
86
87 MTPGraph::MTPGraph(int nb_vertices, int nb_edges,
88                    int *from, int *to,
89                    int src, int snk) {
90   _nb_vertices = nb_vertices;
91   _nb_edges = nb_edges;
92
93   edges = new Edge[_nb_edges];
94   vertices = new Vertex[_nb_vertices];
95   _front = new Vertex *[_nb_vertices];
96   _new_front = new Vertex *[_nb_vertices];
97
98   _source = &vertices[src];
99   _sink = &vertices[snk];
100
101   for(int v = 0; v < _nb_vertices; v++) {
102     vertices[v].id = v;
103   }
104
105   for(int e = 0; e < nb_edges; e++) {
106     vertices[from[e]].add_edge(&edges[e]);
107     edges[e].occupied = 0;
108     edges[e].id = e;
109     edges[e].terminal_vertex = &vertices[to[e]];
110   }
111
112 }
113
114 MTPGraph::~MTPGraph() {
115   delete[] vertices;
116   delete[] edges;
117   delete[] _front;
118   delete[] _new_front;
119 }
120
121 void MTPGraph::initialize_work_lengths() {
122   scalar_t length_min = 0;
123   for(int n = 0; n < _nb_vertices; n++) {
124     for(Edge *e = vertices[n].root_edge; e; e = e->next) {
125       length_min = min(e->length, length_min);
126     }
127   }
128   for(int n = 0; n < _nb_vertices; n++) {
129     for(Edge *e = vertices[n].root_edge; e; e = e->next) {
130       e->work_length = e->length - length_min;
131     }
132   }
133 }
134
135 void MTPGraph::update_work_lengths() {
136   for(int n = 0; n < _nb_vertices; n++) {
137     scalar_t d = vertices[n].distance_from_source;
138     for(Edge *e = vertices[n].root_edge; e; e = e->next) {
139       e->work_length += d - e->terminal_vertex->distance_from_source;
140     }
141   }
142 }
143
144 void MTPGraph::force_positive_work_lengths() {
145 #ifdef VERBOSE
146   scalar_t residual_error = 0.0;
147 #endif
148   for(int n = 0; n < _nb_vertices; n++) {
149     for(Edge *e = vertices[n].root_edge; e; e = e->next) {
150       if(e->work_length < 0) {
151 #ifdef VERBOSE
152         residual_error -= e->work_length;
153 #endif
154         e->work_length = 0.0;
155       }
156     }
157   }
158 #ifdef VERBOSE
159   cerr << "residual_error " << residual_error << endl;
160 #endif
161 }
162
163 void MTPGraph::find_shortest_path(Vertex **_front, Vertex **_new_front) {
164   Vertex **tmp_front;
165   int tmp_front_size;
166   Vertex *v, *tv;
167   scalar_t d;
168
169   for(int v = 0; v < _nb_vertices; v++) {
170     vertices[v].distance_from_source = FLT_MAX;
171     vertices[v].pred_vertex = 0;
172     vertices[v].pred_edge = 0;
173     vertices[v].iteration = 0;
174   }
175
176   int iteration = 0;
177
178   int _front_size = 0, _new_front_size;
179   _front[_front_size++] = _source;
180   _source->distance_from_source = 0;
181
182   do {
183     _new_front_size = 0;
184     iteration++;
185     for(int f = 0; f < _front_size; f++) {
186       v = _front[f];
187       for(Edge *e = v->root_edge; e; e = e->next) {
188         d = v->distance_from_source + e->work_length;
189         tv = e->terminal_vertex;
190         if(d < tv->distance_from_source) {
191           tv->distance_from_source = d;
192           tv->pred_vertex = v;
193           tv->pred_edge = e;
194           if(tv->iteration < iteration) {
195             _new_front[_new_front_size++] = tv;
196             tv->iteration = iteration;
197           }
198         }
199       }
200     }
201
202     tmp_front = _new_front;
203     _new_front = _front;
204     _front = tmp_front;
205
206     tmp_front_size = _new_front_size;
207     _new_front_size = _front_size;
208     _front_size = tmp_front_size;
209   } while(_front_size > 0);
210 }
211
212 void MTPGraph::find_best_paths(scalar_t *lengths, int *result_edge_occupation) {
213   scalar_t total_length;
214
215   for(int e = 0; e < _nb_edges; e++) {
216     edges[e].length = lengths[e];
217     edges[e].work_length = edges[e].length;
218   }
219
220   find_shortest_path(_front, _new_front);
221   update_work_lengths();
222
223   // initialize_work_lengths();
224
225   do {
226     force_positive_work_lengths();
227     find_shortest_path(_front, _new_front);
228     update_work_lengths();
229
230     total_length = 0.0;
231
232     // Do we reach the _sink?
233     if(_sink->pred_edge) {
234
235       // If yes, compute the length of the best path
236       for(Vertex *v = _sink; v->pred_edge; v = v->pred_vertex) {
237         total_length += v->pred_edge->length;
238       }
239
240       // If that length is negative
241       if(total_length < 0.0) {
242         // Invert all the edges along the best path
243         for(Vertex *v = _sink; v->pred_edge; v = v->pred_vertex) {
244           Edge *e = v->pred_edge;
245           e->terminal_vertex = v->pred_vertex;
246           e->occupied = 1 - e->occupied;
247           e->length = - e->length;
248           e->work_length = - e->work_length;
249           v->pred_vertex->del_edge(e);
250           v->add_edge(e);
251         }
252       }
253     }
254
255   } while(total_length < 0.0);
256
257   for(Edge *e = _sink->root_edge; e; e = e->next) {
258     if(e->occupied) {
259       Edge *f = e;
260       cout << "PATH " << _sink->id;
261       while(f) {
262         cout << " " << f->terminal_vertex->id;
263         for(f = f->terminal_vertex->root_edge; f && !f->occupied; f = f->next);
264       }
265       cout << endl;
266     }
267   }
268
269   for(int n = 0; n < _nb_vertices; n++) {
270     Vertex *v = &vertices[n];
271     for(Edge *e = v->root_edge; e; e = e->next) {
272       result_edge_occupation[e->id] = e->occupied;
273     }
274   }
275 }