3 * mtp is the ``Multi Tracked Paths'', an implementation of the
4 * k-shortest paths algorithm for multi-target tracking.
6 * Copyright (c) 2012 Idiap Research Institute, http://www.idiap.ch/
7 * Written by Francois Fleuret <francois.fleuret@idiap.ch>
9 * This file is part of mtp.
11 * mtp is free software: you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License version 3 as
13 * published by the Free Software Foundation.
15 * mtp is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
18 * License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with selector. If not, see <http://www.gnu.org/licenses/>.
25 #include "mtp_graph.h"
34 scalar_t length, positivized_length;
35 Vertex *origin_vertex, *terminal_vertex;
37 // These fields are used for the linked list of a vertex's leaving
38 // edge list. We have to do insertions / deletions.
39 Edge *next_leaving_edge, *pred_leaving_edge;
47 scalar_t distance_from_source;
48 Edge *pred_edge_toward_source;
50 int last_change; // Used to mark which edges have already been
51 // processed in some methods
55 inline void add_leaving_edge(Edge *e);
56 inline void del_leaving_edge(Edge *e);
59 //////////////////////////////////////////////////////////////////////
63 positivized_length = - positivized_length;
64 origin_vertex->del_leaving_edge(this);
65 terminal_vertex->add_leaving_edge(this);
66 Vertex *t = terminal_vertex;
67 terminal_vertex = origin_vertex;
71 //////////////////////////////////////////////////////////////////////
77 void Vertex::add_leaving_edge(Edge *e) {
78 e->next_leaving_edge = leaving_edges;
79 e->pred_leaving_edge = 0;
80 if(leaving_edges) { leaving_edges->pred_leaving_edge = e; }
84 void Vertex::del_leaving_edge(Edge *e) {
85 if(e == leaving_edges) {
86 leaving_edges = e->next_leaving_edge;
88 if(e->pred_leaving_edge) {
89 e->pred_leaving_edge->next_leaving_edge = e->next_leaving_edge;
91 if(e->next_leaving_edge) {
92 e->next_leaving_edge->pred_leaving_edge = e->pred_leaving_edge;
96 //////////////////////////////////////////////////////////////////////
98 MTPGraph::MTPGraph(int nb_vertices, int nb_edges,
99 int *vertex_from, int *vertex_to,
100 int source, int sink) {
101 _nb_vertices = nb_vertices;
102 _nb_edges = nb_edges;
104 _edges = new Edge[_nb_edges];
105 _vertices = new Vertex[_nb_vertices];
106 _front = new Vertex *[_nb_vertices];
107 _new_front = new Vertex *[_nb_vertices];
109 _source = &_vertices[source];
110 _sink = &_vertices[sink];
112 for(int e = 0; e < nb_edges; e++) {
113 _vertices[vertex_from[e]].add_leaving_edge(_edges + e);
114 _edges[e].occupied = 0;
115 _edges[e].origin_vertex = _vertices + vertex_from[e];
116 _edges[e].terminal_vertex = _vertices + vertex_to[e];
123 MTPGraph::~MTPGraph() {
128 for(int p = 0; p < nb_paths; p++) delete paths[p];
132 //////////////////////////////////////////////////////////////////////
134 void MTPGraph::print(ostream *os) {
135 for(int k = 0; k < _nb_edges; k++) {
136 Edge *e = _edges + k;
137 (*os) << e->origin_vertex - _vertices
139 << e->terminal_vertex - _vertices
149 void MTPGraph::print_dot(ostream *os) {
150 (*os) << "digraph {" << endl;
151 (*os) << " rankdir=\"LR\";" << endl;
152 (*os) << " node [shape=circle,width=0.75,fixedsize=true];" << endl;
153 (*os) << " edge [color=gray,arrowhead=open]" << endl;
154 (*os) << " " << _source - _vertices << " [peripheries=2];" << endl;
155 (*os) << " " << _sink - _vertices << " [peripheries=2];" << endl;
156 for(int k = 0; k < _nb_edges; k++) {
157 Edge *e = _edges + k;
159 << e->origin_vertex - _vertices
161 << e->terminal_vertex - _vertices
164 (*os) << "style=bold,color=black,";
166 (*os) << "label=\"" << e->length << "\"];" << endl;
168 (*os) << "}" << endl;
171 //////////////////////////////////////////////////////////////////////
173 void MTPGraph::update_positivized_lengths() {
174 for(int k = 0; k < _nb_edges; k++) {
175 Edge *e = _edges + k;
176 e->positivized_length +=
177 e->origin_vertex->distance_from_source - e->terminal_vertex->distance_from_source;
181 void MTPGraph::force_positivized_lengths() {
183 scalar_t residual_error = 0.0;
184 scalar_t max_error = 0.0;
186 for(int k = 0; k < _nb_edges; k++) {
187 Edge *e = _edges + k;
189 if(e->positivized_length < 0) {
192 if((e->origin_vertex->last_change < 0 && e->terminal_vertex->last_change >= 0) ||
193 (e->origin_vertex->last_change >= 0 && e->terminal_vertex->last_change < 0)) {
194 cout << "Inconsistent non-connexity (this should never happen)." << endl;
197 if(e->origin_vertex->last_change >= 0 &&
198 e->terminal_vertex->last_change >= 0 &&
199 e->positivized_length < 0) {
200 residual_error -= e->positivized_length;
201 max_error = max(max_error, - e->positivized_length);
204 e->positivized_length = 0.0;
208 cerr << "residual_error " << residual_error << " max_error " << residual_error << endl;
212 int MTPGraph::is_dag() {
216 // We put everybody in the front
217 for(int k = 0; k < _nb_vertices; k++) {
218 _vertices[k].last_change = -1;
219 _front[k] = &_vertices[k];
223 int front_size = _nb_vertices, pred_front_size;
226 // We set the last_change field of all the vertices with incoming
227 // edges to the current iteration value
228 for(int f = 0; f < front_size; f++) {
230 for(e = v->leaving_edges; e; e = e->next_leaving_edge) {
231 e->terminal_vertex->last_change = iteration;
235 pred_front_size = front_size;
238 // We keep all the vertices with incoming nodes
239 for(int f = 0; f < pred_front_size; f++) {
241 if(v->last_change == iteration) {
242 _front[front_size++] = v;
247 } while(front_size < pred_front_size);
249 return front_size == 0;
252 // This method does not change the edge occupation. It only set
253 // properly, for every vertex, the fields distance_from_source and
254 // pred_edge_toward_source.
256 void MTPGraph::find_shortest_path() {
264 cout << "find_shortest_path: DAG -> ok" << endl;
266 for(int e = 0; e < _nb_edges; e++) {
267 if(_edges[e].positivized_length < 0) abort();
269 cout << "find_shortest_path: All positivized_length are positive -> ok" << endl;
273 for(int k = 0; k < _nb_vertices; k++) {
274 _vertices[k].distance_from_source = FLT_MAX;
275 _vertices[k].pred_edge_toward_source = 0;
276 _vertices[k].last_change = -1;
281 int front_size = 0, new_front_size;
282 _front[front_size++] = _source;
283 _source->distance_from_source = 0;
288 for(int f = 0; f < front_size; f++) {
290 for(e = v->leaving_edges; e; e = e->next_leaving_edge) {
291 d = v->distance_from_source + e->positivized_length;
292 tv = e->terminal_vertex;
293 if(d < tv->distance_from_source) {
294 tv->distance_from_source = d;
295 tv->pred_edge_toward_source = e;
296 if(tv->last_change < iteration) {
297 _new_front[new_front_size++] = tv;
298 tv->last_change = iteration;
304 tmp_front = _new_front; _new_front = _front; _front = tmp_front;
306 front_size = new_front_size;
309 } while(front_size > 0);
312 void MTPGraph::find_best_paths(scalar_t *lengths) {
313 scalar_t total_length;
317 for(int e = 0; e < _nb_edges; e++) {
318 _edges[e].length = lengths[e];
319 _edges[e].occupied = 0;
320 _edges[e].positivized_length = _edges[e].length;
323 // We call find_shortest_path here to set properly the distances to
324 // the source, so that we can make all the edge lengths positive at
325 // the first iteration.
326 find_shortest_path();
329 update_positivized_lengths();
330 force_positivized_lengths();
331 find_shortest_path();
335 // Do we reach the sink?
336 if(_sink->pred_edge_toward_source) {
337 // If yes, compute the length of the best path according to the
338 // original edge lengths
340 while(v->pred_edge_toward_source) {
341 total_length += v->pred_edge_toward_source->length;
342 v = v->pred_edge_toward_source->origin_vertex;
344 // If that length is negative
345 if(total_length < 0.0) {
347 cerr << "Found a path of length " << total_length << endl;
349 // Invert all the edges along the best path
351 while(v->pred_edge_toward_source) {
352 e = v->pred_edge_toward_source;
353 v = e->origin_vertex;
355 // This is the only place where we change the occupations of
357 e->occupied = 1 - e->occupied;
362 } while(total_length < 0.0);
364 // Put back the graph in its original state (i.e. invert edges which
365 // have been inverted in the process)
366 for(int k = 0; k < _nb_edges; k++) {
368 if(e->occupied) { e->invert(); }
372 int MTPGraph::retrieve_one_path(Edge *e, Path *path) {
374 int l = 0, nb_occupied_next;
377 path->nodes[l++] = e->origin_vertex - _vertices;
378 path->length = e->length;
381 while(e->terminal_vertex != _sink) {
383 path->nodes[l++] = e->terminal_vertex - _vertices;
384 path->length += e->length;
387 nb_occupied_next = 0;
388 for(f = e->terminal_vertex->leaving_edges; f; f = f->next_leaving_edge) {
389 if(f->occupied) { nb_occupied_next++; next = f; }
393 if(nb_occupied_next == 0) {
394 cerr << "retrieve_one_path: Non-sink end point." << endl;
398 else if(nb_occupied_next > 1) {
399 cerr << "retrieve_one_path: Non node-disjoint paths." << endl;
408 path->nodes[l++] = e->terminal_vertex - _vertices;
409 path->length += e->length;
415 void MTPGraph::retrieve_disjoint_paths() {
419 for(int p = 0; p < nb_paths; p++) delete paths[p];
423 for(e = _source->leaving_edges; e; e = e->next_leaving_edge) {
424 if(e->occupied) { nb_paths++; }
427 paths = new Path *[nb_paths];
430 for(e = _source->leaving_edges; e; e = e->next_leaving_edge) {
432 l = retrieve_one_path(e, 0);
433 paths[p] = new Path(l);
434 retrieve_one_path(e, paths[p]);