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"
36 scalar_t length, positivized_length;
37 Vertex *origin_vertex, *terminal_vertex;
39 // These are the links in the origin_vertex leaving edge list
40 Edge *next_leaving_edge, *pred_leaving_edge;
49 scalar_t distance_from_source;
50 Edge *pred_edge_toward_source;
52 int last_change; // Used to mark which edges have already been
53 // processed in some methods
57 inline void add_leaving_edge(Edge *e);
58 inline void del_leaving_edge(Edge *e);
61 //////////////////////////////////////////////////////////////////////
65 positivized_length = - positivized_length;
66 origin_vertex->del_leaving_edge(this);
67 terminal_vertex->add_leaving_edge(this);
68 Vertex *t = terminal_vertex;
69 terminal_vertex = origin_vertex;
73 //////////////////////////////////////////////////////////////////////
79 void Vertex::add_leaving_edge(Edge *e) {
80 e->next_leaving_edge = leaving_edges;
81 e->pred_leaving_edge = 0;
82 if(leaving_edges) { leaving_edges->pred_leaving_edge = e; }
86 void Vertex::del_leaving_edge(Edge *e) {
87 if(e == leaving_edges) {
88 leaving_edges = e->next_leaving_edge;
90 if(e->pred_leaving_edge) {
91 e->pred_leaving_edge->next_leaving_edge = e->next_leaving_edge;
93 if(e->next_leaving_edge) {
94 e->next_leaving_edge->pred_leaving_edge = e->pred_leaving_edge;
98 //////////////////////////////////////////////////////////////////////
100 MTPGraph::MTPGraph(int nb_vertices, int nb_edges,
101 int *vertex_from, int *vertex_to,
102 int source, int sink) {
103 _nb_vertices = nb_vertices;
104 _nb_edges = nb_edges;
106 _edges = new Edge[_nb_edges];
107 _vertices = new Vertex[_nb_vertices];
108 _front = new Vertex *[_nb_vertices];
109 _new_front = new Vertex *[_nb_vertices];
111 _source = &_vertices[source];
112 _sink = &_vertices[sink];
114 for(int k = 0; k < _nb_vertices; k++) {
118 for(int e = 0; e < nb_edges; e++) {
119 _vertices[vertex_from[e]].add_leaving_edge(_edges + e);
120 _edges[e].occupied = 0;
122 _edges[e].origin_vertex = _vertices + vertex_from[e];
123 _edges[e].terminal_vertex = _vertices + vertex_to[e];
130 MTPGraph::~MTPGraph() {
135 for(int p = 0; p < nb_paths; p++) delete paths[p];
139 //////////////////////////////////////////////////////////////////////
141 void MTPGraph::print(ostream *os) {
142 for(int k = 0; k < _nb_edges; k++) {
143 Edge *e = _edges + k;
144 (*os) << e->origin_vertex->id
146 << e->terminal_vertex->id
156 void MTPGraph::print_dot(ostream *os) {
157 (*os) << "digraph {" << endl;
158 (*os) << " rankdir=\"LR\";" << endl;
159 (*os) << " node [shape=circle,width=0.75,fixedsize=true];" << endl;
160 (*os) << " edge [color=gray,arrowhead=open]" << endl;
161 (*os) << " " << _source->id << " [peripheries=2];" << endl;
162 (*os) << " " << _sink->id << " [peripheries=2];" << endl;
163 for(int k = 0; k < _nb_edges; k++) {
164 Edge *e = _edges + k;
165 // (*os) << " " << e->origin_vertex->id << " -> " << e->terminal_vertex->id
168 (*os) << " " << e->origin_vertex->id << " -> " << e->terminal_vertex->id
171 (*os) << "style=bold,color=black,";
173 (*os) << "label=\"" << e->length << "\"];" << endl;
175 (*os) << "}" << endl;
178 //////////////////////////////////////////////////////////////////////
180 void MTPGraph::update_positivized_lengths() {
181 for(int k = 0; k < _nb_edges; k++) {
182 Edge *e = _edges + k;
183 e->positivized_length +=
184 e->origin_vertex->distance_from_source - e->terminal_vertex->distance_from_source;
188 void MTPGraph::force_positivized_lengths() {
190 scalar_t residual_error = 0.0;
191 scalar_t max_error = 0.0;
193 for(int n = 0; n < _nb_vertices; n++) {
194 for(Edge *e = _vertices[n].leaving_edges; e; e = e->next_leaving_edge) {
195 if(e->positivized_length < 0) {
197 residual_error -= e->positivized_length;
198 max_error = max(max_error, - e->positivized_length);
200 e->positivized_length = 0.0;
205 cerr << "residual_error " << residual_error << " max_error " << residual_error << endl;
209 int MTPGraph::is_dag() {
213 // We put everybody in the front
214 for(int k = 0; k < _nb_vertices; k++) {
215 _vertices[k].last_change = -1;
216 _front[k] = &_vertices[k];
220 int front_size = _nb_vertices, pred_front_size;
223 // We set the iteration field of all vertex with incoming edges to
224 // the current iteration value
225 for(int f = 0; f < front_size; f++) {
227 for(e = v->leaving_edges; e; e = e->next_leaving_edge) {
228 e->terminal_vertex->last_change = iteration;
232 pred_front_size = front_size;
235 // We remove all the vertices without incoming edge
236 for(int f = 0; f < pred_front_size; f++) {
238 if(v->last_change == iteration) {
239 _front[front_size++] = v;
244 } while(front_size < pred_front_size);
246 return front_size == 0;
249 // This method does not change the edge occupation. It only set
250 // properly for every vertex the fields distance_from_source and
251 // pred_edge_toward_source.
253 void MTPGraph::find_shortest_path() {
261 cout << "find_shortest_path: DAG -> ok" << endl;
263 for(int e = 0; e < _nb_edges; e++) {
264 if(_edges[e].positivized_length < 0) abort();
266 cout << "find_shortest_path: All positivized_length are positive -> ok" << endl;
270 for(int k = 0; k < _nb_vertices; k++) {
271 _vertices[k].distance_from_source = FLT_MAX;
272 _vertices[k].pred_edge_toward_source = 0;
273 _vertices[k].last_change = -1;
278 int front_size = 0, new_front_size;
279 _front[front_size++] = _source;
280 _source->distance_from_source = 0;
285 for(int f = 0; f < front_size; f++) {
287 for(e = v->leaving_edges; e; e = e->next_leaving_edge) {
288 d = v->distance_from_source + e->positivized_length;
289 tv = e->terminal_vertex;
290 if(d < tv->distance_from_source) {
291 tv->distance_from_source = d;
292 tv->pred_edge_toward_source = e;
293 if(tv->last_change < iteration) {
294 _new_front[new_front_size++] = tv;
295 tv->last_change = iteration;
301 tmp_front = _new_front; _new_front = _front; _front = tmp_front;
303 front_size = new_front_size;
306 } while(front_size > 0);
309 void MTPGraph::find_best_paths(scalar_t *lengths) {
310 scalar_t total_length;
314 for(int e = 0; e < _nb_edges; e++) {
315 _edges[e].length = lengths[e];
316 _edges[e].occupied = 0;
317 _edges[e].positivized_length = _edges[e].length;
320 // We call find_shortest_path here to set properly the distances to
321 // the source, so that we can make all the edge lengths positive at
322 // the first iteration.
323 find_shortest_path();
326 update_positivized_lengths();
327 force_positivized_lengths();
328 find_shortest_path();
332 // Do we reach the sink?
333 if(_sink->pred_edge_toward_source) {
334 // If yes, compute the length of the best path according to the
335 // original edge lengths
337 while(v->pred_edge_toward_source) {
338 total_length += v->pred_edge_toward_source->length;
339 v = v->pred_edge_toward_source->origin_vertex;
341 // If that length is negative
342 if(total_length < 0.0) {
344 cerr << "Found a path of length " << total_length << endl;
346 // Invert all the edges along the best path
348 while(v->pred_edge_toward_source) {
349 e = v->pred_edge_toward_source;
350 v = e->origin_vertex;
352 // This is the only place where we change the occupations of
354 e->occupied = 1 - e->occupied;
359 } while(total_length < 0.0);
361 // Put back the graph in its original state (i.e. invert edges which
362 // have been inverted in the process)
363 for(int k = 0; k < _nb_edges; k++) {
364 Edge *e = _edges + k;
365 if(e->occupied) { e->invert(); }
369 int MTPGraph::retrieve_one_path(Edge *e, Path *path) {
374 path->nodes[l++] = e->origin_vertex->id;
375 path->length = e->length;
378 while(e->terminal_vertex != _sink) {
380 path->nodes[l++] = e->terminal_vertex->id;
381 path->length += e->length;
384 for(f = e->terminal_vertex->leaving_edges; f; f = f->next_leaving_edge) {
385 if(f->occupied) { nb_choices++; next = f; }
386 if(nb_choices == 0) {
387 cerr << "retrieve_one_path: Non-sink end point." << endl;
391 cerr << "retrieve_one_path: Non node-disjoint paths." << endl;
399 path->nodes[l++] = e->terminal_vertex->id;
400 path->length += e->length;
406 void MTPGraph::retrieve_disjoint_paths() {
409 for(int p = 0; p < nb_paths; p++) delete paths[p];
413 for(e = _source->leaving_edges; e; e = e->next_leaving_edge) {
414 if(e->occupied) { nb_paths++; }
417 paths = new Path *[nb_paths];
420 for(e = _source->leaving_edges; e; e = e->next_leaving_edge) {
422 int l = retrieve_one_path(e, 0);
423 paths[p] = new Path(l);
424 retrieve_one_path(e, paths[p]);