Cosmetics.
[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 *origin_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 k = 0; k < _nb_edges; k++) {
60     Edge *e = edges + k;
61     cout << e->origin_vertex->id
62          << " -> "
63          << e->terminal_vertex->id
64          << " "
65          << e->length;
66     if(e->occupied) {
67       cout << " *";
68     }
69     cout << endl;
70   }
71 }
72
73 void MTPGraph::print_dot() {
74   cout << "digraph {" << endl;
75   cout << "  node[shape=circle];" << endl;
76   for(int k = 0; k < _nb_edges; k++) {
77     Edge *e = edges + k;
78     if(e->occupied) {
79       cout << "  " << e->origin_vertex->id << " -> " << e->terminal_vertex->id
80            << " [style=bold,color=black,label=\"" << -e->length << "\"];" << endl;
81     } else {
82       cout << "  " << e->origin_vertex->id << " -> " << e->terminal_vertex->id
83            << " [color=gray,label=\"" << e->length << "\"];" << endl;
84     }
85   }
86   cout << "}" << endl;
87 }
88
89 MTPGraph::MTPGraph(int nb_vertices, int nb_edges,
90                    int *from, int *to,
91                    int src, int snk) {
92   _nb_vertices = nb_vertices;
93   _nb_edges = nb_edges;
94
95   edges = new Edge[_nb_edges];
96   vertices = new Vertex[_nb_vertices];
97   _front = new Vertex *[_nb_vertices];
98   _new_front = new Vertex *[_nb_vertices];
99
100   _source = &vertices[src];
101   _sink = &vertices[snk];
102
103   for(int v = 0; v < _nb_vertices; v++) {
104     vertices[v].id = v;
105   }
106
107   for(int e = 0; e < nb_edges; e++) {
108     vertices[from[e]].add_edge(&edges[e]);
109     edges[e].occupied = 0;
110     edges[e].id = e;
111     edges[e].origin_vertex = &vertices[from[e]];
112     edges[e].terminal_vertex = &vertices[to[e]];
113   }
114
115 }
116
117 MTPGraph::~MTPGraph() {
118   delete[] vertices;
119   delete[] edges;
120   delete[] _front;
121   delete[] _new_front;
122 }
123
124 void MTPGraph::initialize_work_lengths() {
125   scalar_t length_min = 0;
126   for(int n = 0; n < _nb_vertices; n++) {
127     for(Edge *e = vertices[n].root_edge; e; e = e->next) {
128       length_min = min(e->length, length_min);
129     }
130   }
131   for(int n = 0; n < _nb_vertices; n++) {
132     for(Edge *e = vertices[n].root_edge; e; e = e->next) {
133       e->work_length = e->length - length_min;
134     }
135   }
136 }
137
138 void MTPGraph::update_work_lengths() {
139   for(int k = 0; k < _nb_edges; k++) {
140     Edge *e = edges + k;
141     e->work_length += e->terminal_vertex->distance_from_source - e->terminal_vertex->distance_from_source;
142   }
143 }
144
145 void MTPGraph::force_positive_work_lengths() {
146 #ifdef VERBOSE
147   scalar_t residual_error = 0.0;
148 #endif
149   for(int n = 0; n < _nb_vertices; n++) {
150     for(Edge *e = vertices[n].root_edge; e; e = e->next) {
151       if(e->work_length < 0) {
152 #ifdef VERBOSE
153         residual_error -= e->work_length;
154 #endif
155         e->work_length = 0.0;
156       }
157     }
158   }
159 #ifdef VERBOSE
160   cerr << "residual_error " << residual_error << endl;
161 #endif
162 }
163
164 void MTPGraph::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   for(int v = 0; v < _nb_vertices; v++) {
171     vertices[v].distance_from_source = FLT_MAX;
172     vertices[v].pred_vertex = 0;
173     vertices[v].pred_edge = 0;
174     vertices[v].iteration = 0;
175   }
176
177   int iteration = 0;
178
179   int _front_size = 0, _new_front_size;
180   _front[_front_size++] = _source;
181   _source->distance_from_source = 0;
182
183   do {
184     _new_front_size = 0;
185     iteration++;
186     for(int f = 0; f < _front_size; f++) {
187       v = _front[f];
188       for(Edge *e = v->root_edge; e; e = e->next) {
189         d = v->distance_from_source + e->work_length;
190         tv = e->terminal_vertex;
191         if(d < tv->distance_from_source) {
192           tv->distance_from_source = d;
193           tv->pred_vertex = v;
194           tv->pred_edge = e;
195           if(tv->iteration < iteration) {
196             _new_front[_new_front_size++] = tv;
197             tv->iteration = iteration;
198           }
199         }
200       }
201     }
202
203     tmp_front = _new_front;
204     _new_front = _front;
205     _front = tmp_front;
206
207     tmp_front_size = _new_front_size;
208     _new_front_size = _front_size;
209     _front_size = tmp_front_size;
210   } while(_front_size > 0);
211 }
212
213 void MTPGraph::find_best_paths(scalar_t *lengths, int *result_edge_occupation) {
214   scalar_t total_length;
215
216   for(int e = 0; e < _nb_edges; e++) {
217     edges[e].length = lengths[e];
218     edges[e].work_length = edges[e].length;
219   }
220
221 #warning
222   // find_shortest_path(_front, _new_front);
223   // update_work_lengths();
224
225   initialize_work_lengths();
226
227   do {
228     force_positive_work_lengths();
229     find_shortest_path(_front, _new_front);
230     update_work_lengths();
231
232     total_length = 0.0;
233
234     // Do we reach the _sink?
235     if(_sink->pred_edge) {
236
237       // If yes, compute the length of the best path
238       for(Vertex *v = _sink; v->pred_vertex; v = v->pred_vertex) {
239         total_length += v->pred_edge->length;
240       }
241
242       // If that length is negative
243       if(total_length < 0.0) {
244 #ifdef VERBOSE
245         cout << "Found a path of length " << total_length << endl;
246 #endif
247         // Invert all the edges along the best path
248         for(Vertex *v = _sink; v->pred_edge; v = v->pred_vertex) {
249           Edge *e = v->pred_edge;
250           e->occupied = 1 - e->occupied;
251           e->length = - e->length;
252           e->work_length = - e->work_length;
253           e->origin_vertex->del_edge(e);
254           e->terminal_vertex->add_edge(e);
255           Vertex *t = e->terminal_vertex;
256           e->terminal_vertex = e->origin_vertex;
257           e->origin_vertex = t;
258         }
259       }
260     }
261
262   } while(total_length < 0.0);
263
264   for(int k = 0; k < _nb_edges; k++) {
265     Edge *e = edges + k;
266     if(e->occupied) {
267       e->length = - e->length;
268       e->work_length = 0;
269       e->origin_vertex->del_edge(e);
270       e->terminal_vertex->add_edge(e);
271       Vertex *t = e->terminal_vertex;
272       e->terminal_vertex = e->origin_vertex;
273       e->origin_vertex = t;
274     }
275   }
276
277   // for(Edge *e = _sink->root_edge; e; e = e->next) {
278     // if(e->occupied) {
279       // Edge *f = e;
280       // cout << "PATH " << _sink->id;
281       // while(f) {
282         // cout << " " << f->terminal_vertex->id;
283         // for(f = f->terminal_vertex->root_edge; f && !f->occupied; f = f->next);
284       // }
285       // cout << endl;
286     // }
287   // }
288
289   // int nb_occupied = 0;
290   // for(int e = 0; e < _nb_edges; e++) {
291     // for(int n = 0; n < _nb_vertices; n++) {
292       // Vertex *v = &vertices[n];
293       // for(Edge *e = v->root_edge; e; e = e->next) {
294         // if(e->occupied) nb_occupied++;
295       // }
296     // }
297   // }
298
299   for(int n = 0; n < _nb_vertices; n++) {
300     Vertex *v = &vertices[n];
301     for(Edge *e = v->root_edge; e; e = e->next) {
302       result_edge_occupation[e->id] = e->occupied;
303     }
304   }
305 }