k_best.h
Go to the documentation of this file.
1 /**************************************************************************************************
2  Software License Agreement (BSD License)
3 
4  Copyright (c) 2011-2013, LAR toolkit developers - University of Aveiro - http://lars.mec.ua.pt
5  All rights reserved.
6 
7  Redistribution and use in source and binary forms, with or without modification, are permitted
8  provided that the following conditions are met:
9 
10  *Redistributions of source code must retain the above copyright notice, this list of
11  conditions and the following disclaimer.
12  *Redistributions in binary form must reproduce the above copyright notice, this list of
13  conditions and the following disclaimer in the documentation and/or other materials provided
14  with the distribution.
15  *Neither the name of the University of Aveiro nor the names of its contributors may be used to
16  endorse or promote products derived from this software without specific prior written permission.
17 
18  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
19  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 ***************************************************************************************************/
32 #ifndef _K_BEST_
33 #define _K_BEST_
34 
35 #include <boost/shared_ptr.hpp>
36 
37 #include <Eigen/Dense>
38 #include <Eigen/Cholesky>
39 
40 #include <mtt/munkres.h>
41 
42 #include <sys/time.h>
43 
44 #include <iostream>
45 #include <map>
46 #include <vector>
47 #include <limits>
48 #include <algorithm>
49 
50 using Eigen::MatrixXd;
51 using Eigen::VectorXd;
52 
53 using namespace std;
54 
55 class Node
56 {
57  public:
58 
59  Node()
60  {
61  cost=-1;
62  }
63 
64  vector<orderedPairPtr> fixed;
65  vector<orderedPairPtr> excluded;
66  vector<orderedPairPtr> unspecific;
67  double cost;
68 
69  private:
70 };
71 
72 typedef boost::shared_ptr<Node> NodePtr;
73 
74 ostream &operator<<(ostream &o,vector<orderedPairPtr>& op);
75 
77 {
78  public:
80  {
81  cost=-1;
82  }
83 
84  vector<orderedPairPtr> pairs;
85  double cost;
86 
87  friend ostream& operator<<(ostream& o, Assignments& a)
88  {
89  o<<"cost: "<<a.cost<<" ";
90  o<<a.pairs;
91 
92  return o;
93  }
94 
95  private:
96 };
97 
98 typedef boost::shared_ptr<Assignments> AssignmentsPtr;
99 
100 int factorial(int x);
101 
102 class Timer
103 {
104  public:
105 
106  //initial timer values
107  map<int,timeval> timers;
108 
109  //timer status
110  map<int,bool> active;
111 
112  //final values in seconds
113  map<int,double> values;
114 
116  {
117  }
118 
120  {
121  timers.clear();
122  active.clear();
123  values.clear();
124  }
125 
126  void tic(int t=0)
127  {
128  //get start time
129  timeval start;
130  gettimeofday(&start, NULL);
131 
132  //put in the timer
133  timers[t] = start;
134  //set active
135  active[t] = true;
136  //set final value to zero
137  values[t] = 0;
138  }
139 
140  double toc(int t=0)
141  {
142  //if not active return the last value
143  if(!active[t])
144  return values[t];
145 
146  //get the stop time
147  timeval end;
148  gettimeofday(&end, NULL);
149 
150  //compute final value in seconds
151  values[t] = (end.tv_sec - timers[t].tv_sec); // sec
152  values[t]+= (end.tv_usec - timers[t].tv_usec) / 1000000.0; // us to sec
153 
154  active[t] = false;
155 
156  return values[t];
157  }
158 
159  double toMiliseconds(int t=0)
160  {
161  return values[t]*1000.;
162  }
163 
164  double value(int t=0)
165  {
166  return values[t];
167  }
168 };
169 
170 vector<vector<int> > convertToStdVector(MatrixXd&mat);
171 double munkers_wrapper(MatrixXd&cost_mat,vector<orderedPairPtr>&assignments);
172 vector<AssignmentsPtr> k_best_assignment(MatrixXd& cost_mat,uint k);
173 vector<NodePtr> partitionNode(Node&node_in,MatrixXd&cost_mat);
174 NodePtr calcNodeCost(Node&node_in,MatrixXd&cost_mat,bool non_square_matrix=false);
175 template<class type>
176 uint countRepeatingValues(vector<type> sorted_values);
177 vector<int> getRows(vector<orderedPairPtr>& pairs);
178 vector<int> getCols(vector<orderedPairPtr>& pairs);
180 ostream &operator<<(ostream &o,vector<AssignmentsPtr>& assing);
181 ostream &operator<<(ostream &o,NodePtr& n);
182 ostream &operator<<(ostream &o,Node& n);
183 
184 #endif
vector< AssignmentsPtr > k_best_assignment(MatrixXd &cost_mat, uint k)
Definition: k_best.cpp:275
vector< orderedPairPtr > fixed
Definition: k_best.h:64
bool compareOrdered_pair(orderedPairPtr &p1, orderedPairPtr &p2)
Definition: k_best.cpp:702
vector< orderedPairPtr > pairs
Definition: k_best.h:84
vector< orderedPairPtr > excluded
Definition: k_best.h:65
NodePtr calcNodeCost(Node &node_in, MatrixXd &cost_mat, bool non_square_matrix=false)
Definition: k_best.cpp:527
Definition: k_best.h:55
Node()
Definition: k_best.h:59
vector< int > getRows(vector< orderedPairPtr > &pairs)
Definition: k_best.cpp:682
double toc(int t=0)
Definition: k_best.h:140
~Timer()
Definition: k_best.h:119
map< int, double > values
Definition: k_best.h:113
boost::shared_ptr< Assignments > AssignmentsPtr
Definition: k_best.h:98
map< int, timeval > timers
Definition: k_best.h:107
friend ostream & operator<<(ostream &o, Assignments &a)
Definition: k_best.h:87
double cost
Definition: k_best.h:67
double toMiliseconds(int t=0)
Definition: k_best.h:159
uint countRepeatingValues(vector< type > sorted_values)
Only works on sorted vectors.
Definition: k_best.cpp:671
double munkers_wrapper(MatrixXd &cost_mat, vector< orderedPairPtr > &assignments)
Definition: k_best.cpp:65
vector< NodePtr > partitionNode(Node &node_in, MatrixXd &cost_mat)
Definition: k_best.cpp:397
vector< int > getCols(vector< orderedPairPtr > &pairs)
Definition: k_best.cpp:692
Timer()
Definition: k_best.h:115
double value(int t=0)
Definition: k_best.h:164
External library declaration to preform the Hungarian algorithm (Munkres)
Timer t
Definition: k_best.cpp:34
ostream & operator<<(ostream &o, vector< orderedPairPtr > &op)
Definition: k_best.cpp:80
vector< vector< int > > convertToStdVector(MatrixXd &mat)
Definition: k_best.cpp:41
vector< orderedPairPtr > unspecific
Definition: k_best.h:66
void tic(int t=0)
Definition: k_best.h:126
map< int, bool > active
Definition: k_best.h:110
double cost
Definition: k_best.h:85
boost::shared_ptr< Node > NodePtr
Definition: k_best.h:72
Definition: k_best.h:102
boost::shared_ptr< orderedPair > orderedPairPtr
Definition: munkres.h:49
Assignments()
Definition: k_best.h:79
int factorial(int x)
Definition: k_best.cpp:36


mtt
Author(s): Jorge Almeida
autogenerated on Mon Mar 2 2015 01:32:18