Adding comments.
[mtp.git] / mtp.cc
1
2 ///////////////////////////////////////////////////////////////////////////
3 // This program is free software: you can redistribute it and/or modify  //
4 // it under the terms of the version 3 of the GNU General Public License //
5 // as published by the Free Software Foundation.                         //
6 //                                                                       //
7 // This program is distributed in the hope that it will be useful, but   //
8 // WITHOUT ANY WARRANTY; without even the implied warranty of            //
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      //
10 // General Public License for more details.                              //
11 //                                                                       //
12 // You should have received a copy of the GNU General Public License     //
13 // along with this program. If not, see <http://www.gnu.org/licenses/>.  //
14 //                                                                       //
15 // Written by and Copyright (C) Francois Fleuret                         //
16 // Contact <francois.fleuret@idiap.ch> for comments & bug reports        //
17 ///////////////////////////////////////////////////////////////////////////
18
19 // Multi-Tracked Path
20
21 #include <iostream>
22 #include <fstream>
23
24 using namespace std;
25
26 #include "tracker.h"
27
28 //////////////////////////////////////////////////////////////////////
29
30 scalar_t detection_score(scalar_t a, scalar_t b, scalar_t score_noise, scalar_t flip_noise) {
31   if(drand48() > flip_noise) {
32     return a + score_noise * (2.0 * drand48() - 1.0);
33   } else {
34     return b + score_noise * (2.0 * drand48() - 1.0);
35   }
36 }
37
38 int main(int argc, char **argv) {
39   int nb_locations = 7;
40   int nb_time_steps = 8;
41   int motion_amplitude = 1;
42
43   Tracker *tracker = new Tracker(nb_time_steps, nb_locations);
44
45   // We define the spatial structures by stating what are the possible
46   // motions of targets, and what are the entrances and the
47   // exits.
48
49   // Here our example is a 1D space with motions from any location to
50   // any location less than motion_amplitude away, entrance at
51   // location 0 and exit at location nb_locations-1.
52
53   for(int l = 0; l < nb_locations; l++) {
54     for(int k = 0; k < nb_locations; k++) {
55       tracker->allowed_motion[l][k] = abs(l - k) <= motion_amplitude;
56     }
57     tracker->entrances[0] = 1;
58     tracker->exits[nb_locations - 1] = 1;
59   }
60
61   // We construct the graph corresponding to this structure
62
63   tracker->build_graph();
64
65   // Then, we specify for every location and time step what is the
66   // detection score there.
67
68   scalar_t flip_noise = 0.05;
69   scalar_t score_noise = 0.0;
70
71   // We first put a background noise, with negative scores at every
72   // location.
73
74   for(int t = 0; t < nb_time_steps; t++) {
75     for(int l = 0; l < nb_locations; l++) {
76       tracker->detection_scores[t][l] = detection_score(-1.0, 1.0, score_noise, flip_noise);
77     }
78   }
79
80   // Then we two targets with the typical local minimum:
81   //
82   // * Target A moves from location 0 to the middle, stays there for a
83   //   while, and comes back, and is strongly detected on the first
84   //   half
85   //
86   // * Target B moves from location nb_locations-1 to the middle, stay
87   //   there for a while, and comes back, and is strongly detected on
88   //   the second half
89
90   int la, lb; // Target locations
91   scalar_t sa, sb; // Target detection scores
92   for(int t = 0; t < nb_time_steps; t++) {
93     if(t < nb_time_steps/2) {
94       la = t;
95       lb = nb_locations - 1 - t;
96       sa = detection_score(10.0, -1.0, score_noise, flip_noise);
97       sb = detection_score( 1.0, -1.0, score_noise, flip_noise);
98     } else {
99       la = nb_time_steps - 1 - t;
100       lb = t - nb_time_steps + nb_locations;
101       sa = detection_score( 1.0, -1.0, score_noise, flip_noise);
102       sb = detection_score(10.0, -1.0, score_noise, flip_noise);
103     }
104
105     if(la > nb_locations/2 - 1) la = nb_locations/2 - 1;
106     if(lb < nb_locations/2 + 1) lb = nb_locations/2 + 1;
107
108     tracker->detection_scores[t][la] = sa;
109     tracker->detection_scores[t][lb] = sb;
110   }
111
112   // Does the tracking per se
113
114   tracker->track();
115
116   // Prints the detected trajectories
117
118   for(int t = 0; t < tracker->nb_trajectories(); t++) {
119     cout << "TRAJECTORY "
120          << t
121          << " [starting " << tracker->trajectory_entrance_time(t)
122          << ", score " << tracker->trajectory_score(t) << "]";
123     for(int u = 0; u < tracker->trajectory_duration(t); u++) {
124       cout << " " << tracker->trajectory_location(t, u);
125     }
126     cout << endl;
127   }
128
129   // Save the underlying graph in the dot format, with occupied edges
130   // marked in bold.
131
132   {
133     ofstream dot("graph.dot");
134     tracker->print_graph_dot(&dot);
135     cout << "Wrote graph.dot." << endl;
136   }
137
138   delete tracker;
139
140   exit(EXIT_SUCCESS);
141 }