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