Oups, some renaming of variables was a bit too brutal. Fixed.
[mtp.git] / mtp_example.cc
1
2 /*
3  *  mtp is the ``Multi Tracked Paths'', an implementation of the
4  *  k-shortest paths algorithm for multi-target tracking.
5  *
6  *  Copyright (c) 2012 Idiap Research Institute, http://www.idiap.ch/
7  *  Written by Francois Fleuret <francois.fleuret@idiap.ch>
8  *
9  *  This file is part of mtp.
10  *
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.
14  *
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.
19  *
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/>.
22  *
23  */
24
25 // Multi-Tracked Path
26
27 #include <iostream>
28 #include <fstream>
29
30 using namespace std;
31
32 #include "tracker.h"
33
34 //////////////////////////////////////////////////////////////////////
35
36 scalar_t noisy_score(scalar_t true_score, scalar_t erroneous_score,
37                      scalar_t score_noise, scalar_t flip_noise) {
38   if(drand48() < flip_noise) {
39     return erroneous_score + score_noise * (2.0 * drand48() - 1.0);
40   } else {
41     return true_score + score_noise * (2.0 * drand48() - 1.0);
42   }
43 }
44
45 int main(int argc, char **argv) {
46   int nb_locations = 7;
47   int nb_time_steps = 8;
48   int motion_amplitude = 1;
49
50   Tracker *tracker = new Tracker();
51
52   tracker->allocate(nb_time_steps, nb_locations);
53
54   // We define the spatial structures by stating what are the possible
55   // motions of targets, and what are the entrances and the
56   // exits.
57
58   // Here our example is a 1D space with motions from any location to
59   // any location less than motion_amplitude away, entrance at
60   // location 0 and exit at location nb_locations-1.
61
62   for(int l = 0; l < nb_locations; l++) {
63     for(int k = 0; k < nb_locations; k++) {
64       tracker->allowed_motion[l][k] = abs(l - k) <= motion_amplitude;
65     }
66     tracker->entrances[0] = 1;
67     tracker->exits[nb_locations - 1] = 1;
68   }
69
70   // We construct the graph corresponding to this structure
71
72   tracker->build_graph();
73
74   // Then, we specify for every location and time step what is the
75   // detection score there.
76
77   scalar_t flip_noise = 0.05;
78   scalar_t score_noise = 0.0;
79
80   // We first put a background noise, with negative scores at every
81   // location.
82
83   for(int t = 0; t < nb_time_steps; t++) {
84     for(int l = 0; l < nb_locations; l++) {
85       tracker->detection_scores[t][l] = noisy_score(-1.0, 1.0, score_noise, flip_noise);
86     }
87   }
88
89   // Then we two targets with the typical local minimum:
90   //
91   // * Target A moves from location 0 to the middle, stays there for a
92   //   while, and comes back, and is strongly detected on the first
93   //   half
94   //
95   // * Target B moves from location nb_locations-1 to the middle, stay
96   //   there for a while, and comes back, and is strongly detected on
97   //   the second half
98
99   int la, lb; // Target locations
100   scalar_t sa, sb; // Target detection scores
101   for(int t = 0; t < nb_time_steps; t++) {
102     if(t < nb_time_steps/2) {
103       la = t;
104       lb = nb_locations - 1 - t;
105       sa = noisy_score(10.0, -1.0, score_noise, flip_noise);
106       sb = noisy_score( 1.0, -1.0, score_noise, flip_noise);
107     } else {
108       la = nb_time_steps - 1 - t;
109       lb = t - nb_time_steps + nb_locations;
110       sa = noisy_score( 1.0, -1.0, score_noise, flip_noise);
111       sb = noisy_score(10.0, -1.0, score_noise, flip_noise);
112     }
113
114     if(la > nb_locations/2 - 1) la = nb_locations/2 - 1;
115     if(lb < nb_locations/2 + 1) lb = nb_locations/2 + 1;
116
117     tracker->detection_scores[t][la] = sa;
118     tracker->detection_scores[t][lb] = sb;
119   }
120
121   { // Write down the tracker setting
122     ofstream out_tracker("tracker.dat");
123     tracker->write(&out_tracker);
124   }
125
126   // Does the tracking per se
127
128   tracker->track();
129
130   // Prints the detected trajectories
131
132   for(int t = 0; t < tracker->nb_trajectories(); t++) {
133     cout << "Trajectory "
134          << t
135          << " starting at " << tracker->trajectory_entrance_time(t)
136          << ", duration " << tracker->trajectory_duration(t)
137          << ", score " << tracker->trajectory_score(t)
138          << ", through nodes ";
139     for(int u = 0; u < tracker->trajectory_duration(t); u++) {
140       cout << " " << tracker->trajectory_location(t, u);
141     }
142     cout << endl;
143   }
144
145   delete tracker;
146
147   exit(EXIT_SUCCESS);
148 }