Verbose output in cerr.
[mtp.git] / mtp.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 // Multi-Tracked Path
20
21 // Takes the graph description file as input and produces a dot file.
22
23 // EXAMPLE: ./mtp ./graph2.txt  | dot -T pdf -o- | xpdf -
24
25 #define VERBOSE
26
27 #include <iostream>
28 #include <fstream>
29 #include <cmath>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <float.h>
33
34 using namespace std;
35
36 typedef float scalar_t;
37
38 #ifdef DEBUG
39 #define ASSERT(x) if(!(x)) { \
40   std::cerr << "ASSERT FAILED IN " << __FILE__ << ":" << __LINE__ << endl; \
41   abort(); \
42 }
43 #else
44 #define ASSERT(x)
45 #endif
46
47 class Vertex;
48
49 class Edge {
50 public:
51   int id, occupied;
52   scalar_t length, work_length;
53   Vertex *terminal_vertex;
54   Edge *next, *pred;
55 };
56
57 class Vertex {
58 public:
59   int id, iteration;
60   Edge *root_edge;
61   scalar_t distance_from_source;
62   Vertex *pred_vertex;
63   Edge *pred_edge;
64
65   Vertex() { root_edge = 0; }
66
67   inline void add_edge(Edge *e) {
68     e->next = root_edge;
69     e->pred = 0;
70     if(root_edge) { root_edge->pred = e; }
71     root_edge = e;
72   }
73
74   inline void del_edge(Edge *e) {
75     if(e == root_edge) { root_edge = e->next; }
76     if(e->pred) { e->pred->next = e->next; }
77     if(e->next) { e->next->pred = e->pred; }
78   }
79 };
80
81 class Graph {
82   void initialize_work_lengths();
83   void update_work_length();
84   void find_shortest_path(Vertex **front, Vertex **new_front);
85
86   int nb_vertices;
87   Edge *edge_heap;
88   Vertex *vertices;
89   Vertex *source, *sink;
90 public:
91   Graph(int nb_vertices, int nb_edges, int *from, int *to, scalar_t *lengths,
92         int source, int sink);
93
94   ~Graph();
95
96   void find_best_paths(int *result_edge_occupation);
97   void print();
98 };
99
100 void Graph::print() {
101   for(int n = 0; n < nb_vertices; n++) {
102     for(Edge *e = vertices[n].root_edge; e; e = e->next) {
103       cout << n << " -> " << e->terminal_vertex->id << " " << e->length;
104       if(e->occupied) {
105         cout << " *";
106       }
107       cout << endl;
108     }
109   }
110 }
111
112 Graph::Graph(int nb_vrt, int nb_edges,
113              int *from, int *to, scalar_t *lengths,
114              int src, int snk) {
115   nb_vertices = nb_vrt;
116
117   edge_heap = new Edge[nb_edges];
118   vertices = new Vertex[nb_vertices];
119
120   source = &vertices[src];
121   sink = &vertices[snk];
122
123   for(int v = 0; v < nb_vertices; v++) {
124     vertices[v].id = v;
125   }
126
127   for(int e = 0; e < nb_edges; e++) {
128     vertices[from[e]].add_edge(&edge_heap[e]);
129     edge_heap[e].occupied = 0;
130     edge_heap[e].id = e;
131     edge_heap[e].length = lengths[e];
132     edge_heap[e].terminal_vertex = &vertices[to[e]];
133   }
134 }
135
136 Graph::~Graph() {
137   delete[] vertices;
138   delete[] edge_heap;
139 }
140
141 void Graph::initialize_work_lengths() {
142   scalar_t length_min = 0;
143   for(int n = 0; n < nb_vertices; n++) {
144     for(Edge *e = vertices[n].root_edge; e; e = e->next) {
145       length_min = min(e->length, length_min);
146     }
147   }
148   for(int n = 0; n < nb_vertices; n++) {
149     for(Edge *e = vertices[n].root_edge; e; e = e->next) {
150       e->work_length = e->length - length_min;
151     }
152   }
153 }
154
155 void Graph::update_work_length() {
156   for(int n = 0; n < nb_vertices; n++) {
157     scalar_t d = vertices[n].distance_from_source;
158     for(Edge *e = vertices[n].root_edge; e; e = e->next) {
159       e->work_length += d - e->terminal_vertex->distance_from_source;
160     }
161   }
162 }
163
164 void Graph::find_shortest_path(Vertex **front, Vertex **new_front) {
165   Vertex **tmp_front;
166   int tmp_front_size;
167   Vertex *v, *tv;
168   scalar_t d;
169
170 #ifdef VERBOSE
171   scalar_t residual_error = 0.0;
172 #endif
173   for(int n = 0; n < nb_vertices; n++) {
174     for(Edge *e = vertices[n].root_edge; e; e = e->next) {
175       if(e->work_length < 0) {
176 #ifdef VERBOSE
177         residual_error -= e->work_length;
178 #endif
179         e->work_length = 0.0;
180       }
181     }
182   }
183 #ifdef VERBOSE
184   cerr << "residual_error " << residual_error << endl;
185 #endif
186
187   for(int v = 0; v < nb_vertices; v++) {
188     vertices[v].distance_from_source = FLT_MAX;
189     vertices[v].pred_vertex = 0;
190     vertices[v].pred_edge = 0;
191     vertices[v].iteration = 0;
192   }
193
194   int iteration = 0;
195
196   int front_size = 0, new_front_size;
197   front[front_size++] = source;
198   source->distance_from_source = 0;
199
200   do {
201     new_front_size = 0;
202     iteration++;
203     for(int f = 0; f < front_size; f++) {
204       v = front[f];
205       for(Edge *e = v->root_edge; e; e = e->next) {
206         d = v->distance_from_source + e->work_length;
207         tv = e->terminal_vertex;
208         if(d < tv->distance_from_source) {
209           tv->distance_from_source = d;
210           tv->pred_vertex = v;
211           tv->pred_edge = e;
212           if(tv->iteration < iteration) {
213             new_front[new_front_size++] = tv;
214             tv->iteration = iteration;
215           }
216         }
217       }
218     }
219
220     tmp_front = new_front;
221     new_front = front;
222     front = tmp_front;
223
224     tmp_front_size = new_front_size;
225     new_front_size = front_size;
226     front_size = tmp_front_size;
227   } while(front_size > 0);
228 }
229
230 void Graph::find_best_paths(int *result_edge_occupation) {
231   Vertex **front = new Vertex *[nb_vertices];
232   Vertex **new_front = new Vertex *[nb_vertices];
233
234   scalar_t total_length;
235
236   initialize_work_lengths();
237
238   do {
239     total_length = 0.0;
240     find_shortest_path(front, new_front);
241     update_work_length();
242
243     // Do we reach the sink?
244     if(sink->pred_edge) {
245
246       // If yes, compute the length of the best path
247       for(Vertex *v = sink; v->pred_edge; v = v->pred_vertex) {
248         total_length += v->pred_edge->length;
249       }
250
251       // If that length is negative
252       if(total_length < 0.0) {
253         // Invert all the edges along the best path
254         for(Vertex *v = sink; v->pred_edge; v = v->pred_vertex) {
255           Edge *e = v->pred_edge;
256           e->terminal_vertex = v->pred_vertex;
257           e->occupied = 1 - e->occupied;
258           e->length = - e->length;
259           e->work_length = - e->work_length;
260           v->pred_vertex->del_edge(e);
261           v->add_edge(e);
262         }
263       }
264     }
265   } while(total_length < 0.0);
266
267   delete[] front;
268   delete[] new_front;
269
270   for(int n = 0; n < nb_vertices; n++) {
271     Vertex *v = &vertices[n];
272     for(Edge *e = v->root_edge; e; e = e->next) {
273       result_edge_occupation[e->id] = e->occupied;
274     }
275   }
276 }
277
278 void find_best_paths(int nb_vertices,
279                      int nb_edges, int *ea, int *eb, scalar_t *el,
280                      int source, int sink,
281                      int *result_edge_occupation) {
282   Graph graph(nb_vertices, nb_edges, ea, eb, el, source, sink);
283   graph.find_best_paths(result_edge_occupation);
284 }
285
286 void dot_print(int nb_vertices,
287                int nb_edges, int *ea, int *eb, scalar_t *el,
288                int source, int sink,
289                int *edge_occupation) {
290   cout << "digraph {" << endl;
291   cout << "  node[shape=circle];" << endl;
292   for(int e = 0; e < nb_edges; e++) {
293     if(edge_occupation[e]) {
294       cout << "  " << ea[e] << " -> " << eb[e] << " [style=bold,color=black,label=\"" << el[e] << "\"];" << endl;
295     } else {
296       cout << "  " << ea[e] << " -> " << eb[e] << " [color=gray,label=\"" << el[e] << "\"];" << endl;
297     }
298   }
299   cout << "}" << endl;
300 }
301
302 //////////////////////////////////////////////////////////////////////
303
304 int main(int argc, char **argv) {
305
306   if(argc < 2) {
307     cerr << argv[0] << " <graph file>" << endl;
308     exit(EXIT_FAILURE);
309   }
310
311   ifstream *file = new ifstream(argv[1]);
312
313   int nb_edges, nb_vertices;
314   int source, sink;
315
316   if(file->good()) {
317
318     (*file) >> nb_vertices >> nb_edges;
319     (*file) >> source >> sink;
320
321     scalar_t *edge_lengths = new scalar_t[nb_edges];
322     int *vertex_from = new int[nb_edges];
323     int *vertex_to = new int[nb_edges];
324     int *result_edge_occupation = new int[nb_edges];
325
326     for(int e = 0; e < nb_edges; e++) {
327       (*file) >> vertex_from[e] >> vertex_to[e] >> edge_lengths[e];
328     }
329
330     find_best_paths(nb_vertices, nb_edges,
331                     vertex_from, vertex_to, edge_lengths,
332                     source, sink,
333                     result_edge_occupation);
334
335     dot_print(nb_vertices, nb_edges,
336               vertex_from, vertex_to, edge_lengths,
337               source, sink,
338               result_edge_occupation);
339
340     delete[] result_edge_occupation;
341     delete[] edge_lengths;
342     delete[] vertex_from;
343     delete[] vertex_to;
344
345   } else {
346
347     cerr << "Can not open " << argv[1] << endl;
348
349     delete file;
350     exit(EXIT_FAILURE);
351
352   }
353
354   delete file;
355   exit(EXIT_SUCCESS);
356 }