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 iteration; // Used in find_shortest_path to know if we already
53 // added this vertex to the front
55 inline void add_leaving_edge(Edge *e);
56 inline void del_leaving_edge(Edge *e);
59 //////////////////////////////////////////////////////////////////////
63 positivized_length = 0;
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 k = 0; k < _nb_vertices; k++) {
116 for(int e = 0; e < nb_edges; e++) {
117 _vertices[vertex_from[e]].add_leaving_edge(_edges + e);
118 _edges[e].occupied = 0;
120 _edges[e].origin_vertex = _vertices + vertex_from[e];
121 _edges[e].terminal_vertex = _vertices + vertex_to[e];
128 MTPGraph::~MTPGraph() {
133 for(int p = 0; p < nb_paths; p++) delete paths[p];
137 //////////////////////////////////////////////////////////////////////
139 void MTPGraph::print(ostream *os) {
140 for(int k = 0; k < _nb_edges; k++) {
141 Edge *e = _edges + k;
142 (*os) << e->origin_vertex->id
144 << e->terminal_vertex->id
154 void MTPGraph::print_dot(ostream *os) {
155 (*os) << "digraph {" << endl;
156 (*os) << " node [shape=circle,width=0.75,fixedsize=true];" << endl;
157 (*os) << " edge [color=gray,arrowhead=open]" << endl;
158 (*os) << " " << _source->id << " [peripheries=2];" << endl;
159 (*os) << " " << _sink->id << " [peripheries=2];" << endl;
160 for(int k = 0; k < _nb_edges; k++) {
161 Edge *e = _edges + k;
162 // (*os) << " " << e->origin_vertex->id << " -> " << e->terminal_vertex->id
165 (*os) << " " << e->origin_vertex->id << " -> " << e->terminal_vertex->id
168 (*os) << "style=bold,color=black,";
170 (*os) << "label=\"" << e->length << "\"];" << endl;
172 (*os) << "}" << endl;
175 //////////////////////////////////////////////////////////////////////
177 void MTPGraph::update_positivized_lengths() {
178 for(int k = 0; k < _nb_edges; k++) {
179 Edge *e = _edges + k;
180 e->positivized_length +=
181 e->origin_vertex->distance_from_source - e->terminal_vertex->distance_from_source;
185 void MTPGraph::force_positivized_lengths() {
187 scalar_t residual_error = 0.0;
188 scalar_t max_error = 0.0;
190 for(int n = 0; n < _nb_vertices; n++) {
191 for(Edge *e = _vertices[n].leaving_edges; e; e = e->next_leaving_edge) {
192 if(e->positivized_length < 0) {
194 residual_error -= e->positivized_length;
195 max_error = max(max_error, - e->positivized_length);
197 e->positivized_length = 0.0;
202 cerr << "residual_error " << residual_error << " max_error " << residual_error << endl;
206 int MTPGraph::is_dag() {
210 // We put everybody in the front
211 for(int k = 0; k < _nb_vertices; k++) {
212 _vertices[k].iteration = 0;
213 _front[k] = &_vertices[k];
216 int front_size = _nb_vertices, nb_with_incoming;
218 int new_front_size, pred_front_size;
222 nb_with_incoming = 0;
224 // We set the iteration field of all vertex with incoming edges to
225 // the current iteration value
226 for(int f = 0; f < front_size; f++) {
228 for(e = v->leaving_edges; e; e = e->next_leaving_edge) {
229 tv = e->terminal_vertex;
230 tv->iteration = iteration;
235 // We remove all vertex without incoming edge
236 for(int f = 0; f < front_size; f++) {
238 if(v->iteration == iteration) {
239 _front[new_front_size++] = v;
243 pred_front_size = front_size;
244 front_size = new_front_size;
245 } while(front_size < pred_front_size);
247 return front_size == 0;
250 // This method does not change the edge occupation. It update
251 // distance_from_source and pred_edge_toward_source.
252 void MTPGraph::find_shortest_path() {
259 for(int k = 0; k < _nb_vertices; k++) {
260 _vertices[k].distance_from_source = FLT_MAX;
261 _vertices[k].pred_edge_toward_source = 0;
262 _vertices[k].iteration = 0;
267 int front_size = 0, new_front_size;
268 _front[front_size++] = _source;
269 _source->distance_from_source = 0;
275 for(int f = 0; f < front_size; f++) {
277 for(e = v->leaving_edges; e; e = e->next_leaving_edge) {
278 d = v->distance_from_source + e->positivized_length;
279 tv = e->terminal_vertex;
280 if(d < tv->distance_from_source) {
281 tv->distance_from_source = d;
282 tv->pred_edge_toward_source = e;
283 if(tv->iteration < iteration) {
284 _new_front[new_front_size++] = tv;
285 tv->iteration = iteration;
291 tmp_front = _new_front;
295 tmp_front_size = new_front_size;
296 new_front_size = front_size;
297 front_size = tmp_front_size;
298 } while(front_size > 0);
301 void MTPGraph::find_best_paths(scalar_t *lengths) {
302 scalar_t total_length;
306 for(int e = 0; e < _nb_edges; e++) {
307 _edges[e].length = lengths[e];
308 _edges[e].occupied = 0;
309 _edges[e].positivized_length = _edges[e].length;
312 // Let's be a bit paranoid
315 // We use one iteration of find_shortest_path simply to propagate
316 // the distance to make all the edge lengths positive.
317 find_shortest_path();
320 update_positivized_lengths();
321 force_positivized_lengths();
322 find_shortest_path();
326 // Do we reach the _sink?
327 if(_sink->pred_edge_toward_source) {
328 // If yes, compute the length of the best path
330 while(v->pred_edge_toward_source) {
331 total_length += v->pred_edge_toward_source->length;
332 v = v->pred_edge_toward_source->origin_vertex;
334 // If that length is negative
335 if(total_length < 0.0) {
337 cerr << "Found a path of length " << total_length << endl;
339 // Invert all the edges along the best path
341 while(v->pred_edge_toward_source) {
342 e = v->pred_edge_toward_source;
343 v = e->origin_vertex;
345 // This is the only place where we change the occupations of
347 e->occupied = 1 - e->occupied;
352 } while(total_length < 0.0);
354 // Put back the graph in its original state (i.e. invert edges which
355 // have been inverted in the process)
356 for(int k = 0; k < _nb_edges; k++) {
357 Edge *e = _edges + k;
358 if(e->occupied) { e->invert(); }
362 int MTPGraph::retrieve_one_path(Edge *e, Path *path) {
367 path->nodes[l++] = e->origin_vertex->id;
368 path->length = e->length;
371 while(e->terminal_vertex != _sink) {
373 path->nodes[l++] = e->terminal_vertex->id;
374 path->length += e->length;
377 for(f = e->terminal_vertex->leaving_edges; f; f = f->next_leaving_edge) {
378 if(f->occupied) { nb_choices++; next = f; }
379 if(nb_choices == 0) {
380 cerr << "retrieve_one_path: Non-sink end point." << endl;
384 cerr << "retrieve_one_path: Non node-disjoint paths." << endl;
392 path->nodes[l++] = e->terminal_vertex->id;
393 path->length += e->length;
399 void MTPGraph::retrieve_disjoint_paths() {
402 for(int p = 0; p < nb_paths; p++) delete paths[p];
406 for(e = _source->leaving_edges; e; e = e->next_leaving_edge) {
407 if(e->occupied) { nb_paths++; }
410 paths = new Path *[nb_paths];
413 for(e = _source->leaving_edges; e; e = e->next_leaving_edge) {
415 int l = retrieve_one_path(e, 0);
416 paths[p] = new Path(l);
417 retrieve_one_path(e, paths[p]);