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"
27 // #include <iostream>
35 scalar_t length, positivized_length;
36 Vertex *origin_vertex, *terminal_vertex;
38 // These are the links in the origin_vertex leaving edge list
39 Edge *next_leaving_edge, *pred_leaving_edge;
48 scalar_t distance_from_source;
49 Edge *pred_edge_toward_source;
51 int last_change; // Used to mark which edges have already been
52 // processed in some methods
56 inline void add_leaving_edge(Edge *e);
57 inline void del_leaving_edge(Edge *e);
60 //////////////////////////////////////////////////////////////////////
64 positivized_length = - positivized_length;
65 origin_vertex->del_leaving_edge(this);
66 terminal_vertex->add_leaving_edge(this);
67 Vertex *t = terminal_vertex;
68 terminal_vertex = origin_vertex;
72 //////////////////////////////////////////////////////////////////////
78 void Vertex::add_leaving_edge(Edge *e) {
79 e->next_leaving_edge = leaving_edges;
80 e->pred_leaving_edge = 0;
81 if(leaving_edges) { leaving_edges->pred_leaving_edge = e; }
85 void Vertex::del_leaving_edge(Edge *e) {
86 if(e == leaving_edges) {
87 leaving_edges = e->next_leaving_edge;
89 if(e->pred_leaving_edge) {
90 e->pred_leaving_edge->next_leaving_edge = e->next_leaving_edge;
92 if(e->next_leaving_edge) {
93 e->next_leaving_edge->pred_leaving_edge = e->pred_leaving_edge;
97 //////////////////////////////////////////////////////////////////////
99 MTPGraph::MTPGraph(int nb_vertices, int nb_edges,
100 int *vertex_from, int *vertex_to,
101 int source, int sink) {
102 _nb_vertices = nb_vertices;
103 _nb_edges = nb_edges;
105 _edges = new Edge[_nb_edges];
106 _vertices = new Vertex[_nb_vertices];
107 _front = new Vertex *[_nb_vertices];
108 _new_front = new Vertex *[_nb_vertices];
110 _source = &_vertices[source];
111 _sink = &_vertices[sink];
113 for(int k = 0; k < _nb_vertices; k++) {
117 for(int e = 0; e < nb_edges; e++) {
118 _vertices[vertex_from[e]].add_leaving_edge(_edges + e);
119 _edges[e].occupied = 0;
121 _edges[e].origin_vertex = _vertices + vertex_from[e];
122 _edges[e].terminal_vertex = _vertices + vertex_to[e];
129 MTPGraph::~MTPGraph() {
134 for(int p = 0; p < nb_paths; p++) delete paths[p];
138 //////////////////////////////////////////////////////////////////////
140 void MTPGraph::print(ostream *os) {
141 for(int k = 0; k < _nb_edges; k++) {
142 Edge *e = _edges + k;
143 (*os) << e->origin_vertex->id
145 << e->terminal_vertex->id
155 void MTPGraph::print_dot(ostream *os) {
156 (*os) << "digraph {" << endl;
157 (*os) << " rankdir=\"LR\";" << endl;
158 (*os) << " node [shape=circle,width=0.75,fixedsize=true];" << endl;
159 (*os) << " edge [color=gray,arrowhead=open]" << endl;
160 (*os) << " " << _source->id << " [peripheries=2];" << endl;
161 (*os) << " " << _sink->id << " [peripheries=2];" << endl;
162 for(int k = 0; k < _nb_edges; k++) {
163 Edge *e = _edges + k;
164 // (*os) << " " << e->origin_vertex->id << " -> " << e->terminal_vertex->id
167 (*os) << " " << e->origin_vertex->id << " -> " << e->terminal_vertex->id
170 (*os) << "style=bold,color=black,";
172 (*os) << "label=\"" << e->length << "\"];" << endl;
174 (*os) << "}" << endl;
177 //////////////////////////////////////////////////////////////////////
179 void MTPGraph::update_positivized_lengths() {
180 for(int k = 0; k < _nb_edges; k++) {
181 Edge *e = _edges + k;
182 e->positivized_length +=
183 e->origin_vertex->distance_from_source - e->terminal_vertex->distance_from_source;
187 void MTPGraph::force_positivized_lengths() {
189 scalar_t residual_error = 0.0;
190 scalar_t max_error = 0.0;
192 for(int n = 0; n < _nb_vertices; n++) {
193 for(Edge *e = _vertices[n].leaving_edges; e; e = e->next_leaving_edge) {
194 if(e->positivized_length < 0) {
196 residual_error -= e->positivized_length;
197 max_error = max(max_error, - e->positivized_length);
199 e->positivized_length = 0.0;
204 cerr << "residual_error " << residual_error << " max_error " << residual_error << endl;
208 int MTPGraph::is_dag() {
212 // We put everybody in the front
213 for(int k = 0; k < _nb_vertices; k++) {
214 _vertices[k].last_change = -1;
215 _front[k] = &_vertices[k];
219 int front_size = _nb_vertices, pred_front_size;
222 // We set the iteration field of all vertex with incoming edges to
223 // the current iteration value
224 for(int f = 0; f < front_size; f++) {
226 for(e = v->leaving_edges; e; e = e->next_leaving_edge) {
227 e->terminal_vertex->last_change = iteration;
231 pred_front_size = front_size;
234 // We remove all the vertices without incoming edge
235 for(int f = 0; f < pred_front_size; f++) {
237 if(v->last_change == iteration) {
238 _front[front_size++] = v;
243 } while(front_size < pred_front_size);
245 return front_size == 0;
248 // This method does not change the edge occupation. It only set
249 // properly for every vertex the fields distance_from_source and
250 // pred_edge_toward_source.
252 void MTPGraph::find_shortest_path() {
260 cout << "find_shortest_path: DAG -> ok" << endl;
262 for(int e = 0; e < _nb_edges; e++) {
263 if(_edges[e].positivized_length < 0) abort();
265 cout << "find_shortest_path: All positivized_length are positive -> ok" << endl;
269 for(int k = 0; k < _nb_vertices; k++) {
270 _vertices[k].distance_from_source = FLT_MAX;
271 _vertices[k].pred_edge_toward_source = 0;
272 _vertices[k].last_change = -1;
277 int front_size = 0, new_front_size;
278 _front[front_size++] = _source;
279 _source->distance_from_source = 0;
284 for(int f = 0; f < front_size; f++) {
286 for(e = v->leaving_edges; e; e = e->next_leaving_edge) {
287 d = v->distance_from_source + e->positivized_length;
288 tv = e->terminal_vertex;
289 if(d < tv->distance_from_source) {
290 tv->distance_from_source = d;
291 tv->pred_edge_toward_source = e;
292 if(tv->last_change < iteration) {
293 _new_front[new_front_size++] = tv;
294 tv->last_change = iteration;
300 tmp_front = _new_front; _new_front = _front; _front = tmp_front;
302 front_size = new_front_size;
305 } while(front_size > 0);
308 void MTPGraph::find_best_paths(scalar_t *lengths) {
309 scalar_t total_length;
313 for(int e = 0; e < _nb_edges; e++) {
314 _edges[e].length = lengths[e];
315 _edges[e].occupied = 0;
316 _edges[e].positivized_length = _edges[e].length;
319 // We call find_shortest_path here to set properly the distances to
320 // the source, so that we can make all the edge lengths positive at
321 // the first iteration.
322 find_shortest_path();
325 update_positivized_lengths();
326 force_positivized_lengths();
327 find_shortest_path();
331 // Do we reach the sink?
332 if(_sink->pred_edge_toward_source) {
333 // If yes, compute the length of the best path according to the
334 // original edge lengths
336 while(v->pred_edge_toward_source) {
337 total_length += v->pred_edge_toward_source->length;
338 v = v->pred_edge_toward_source->origin_vertex;
340 // If that length is negative
341 if(total_length < 0.0) {
343 cerr << "Found a path of length " << total_length << endl;
345 // Invert all the edges along the best path
347 while(v->pred_edge_toward_source) {
348 e = v->pred_edge_toward_source;
349 v = e->origin_vertex;
351 // This is the only place where we change the occupations of
353 e->occupied = 1 - e->occupied;
358 } while(total_length < 0.0);
360 // Put back the graph in its original state (i.e. invert edges which
361 // have been inverted in the process)
362 for(int k = 0; k < _nb_edges; k++) {
363 Edge *e = _edges + k;
364 if(e->occupied) { e->invert(); }
368 int MTPGraph::retrieve_one_path(Edge *e, Path *path) {
373 path->nodes[l++] = e->origin_vertex->id;
374 path->length = e->length;
377 while(e->terminal_vertex != _sink) {
379 path->nodes[l++] = e->terminal_vertex->id;
380 path->length += e->length;
383 for(f = e->terminal_vertex->leaving_edges; f; f = f->next_leaving_edge) {
384 if(f->occupied) { nb_choices++; next = f; }
385 if(nb_choices == 0) {
386 cerr << "retrieve_one_path: Non-sink end point." << endl;
390 cerr << "retrieve_one_path: Non node-disjoint paths." << endl;
398 path->nodes[l++] = e->terminal_vertex->id;
399 path->length += e->length;
405 void MTPGraph::retrieve_disjoint_paths() {
408 for(int p = 0; p < nb_paths; p++) delete paths[p];
412 for(e = _source->leaving_edges; e; e = e->next_leaving_edge) {
413 if(e->occupied) { nb_paths++; }
416 paths = new Path *[nb_paths];
419 for(e = _source->leaving_edges; e; e = e->next_leaving_edge) {
421 int l = retrieve_one_path(e, 0);
422 paths[p] = new Path(l);
423 retrieve_one_path(e, paths[p]);