00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00032 #ifndef _K_BEST_
00033 #define _K_BEST_
00034 
00035 #include <boost/shared_ptr.hpp>
00036 
00037 #include <Eigen/Dense>
00038 #include <Eigen/Cholesky>
00039 
00040 #include <mtt/munkres.h>
00041 
00042 #include <sys/time.h> 
00043 
00044 #include <iostream>
00045 #include <map>
00046 #include <vector>
00047 #include <limits>
00048 #include <algorithm>
00049 
00050 using Eigen::MatrixXd;
00051 using Eigen::VectorXd;
00052 
00053 using namespace std;
00054 
00055 class Node
00056 {
00057         public:
00058                 
00059                 Node()
00060                 {
00061                         cost=-1;
00062                 }
00063                 
00064                 vector<orderedPairPtr> fixed;
00065                 vector<orderedPairPtr> excluded;
00066                 vector<orderedPairPtr> unspecific;
00067                 double cost;    
00068         
00069         private:
00070 };
00071 
00072 typedef boost::shared_ptr<Node> NodePtr;
00073 
00074 ostream &operator<<(ostream &o,vector<orderedPairPtr>& op);
00075 
00076 class Assignments
00077 {
00078         public:
00079                 Assignments()
00080                 {
00081                         cost=-1;
00082                 }
00083                 
00084                 vector<orderedPairPtr> pairs;
00085                 double cost;
00086                 
00087                 friend ostream& operator<<(ostream& o, Assignments& a)
00088                 {
00089                         o<<"cost: "<<a.cost<<" ";
00090                         o<<a.pairs;
00091                         
00092                         return o;
00093                 }
00094                 
00095         private:
00096 };
00097 
00098 typedef boost::shared_ptr<Assignments> AssignmentsPtr;
00099 
00100 int factorial(int x);
00101 
00102 class Timer
00103 {
00104         public:
00105                 
00106                 
00107                 map<int,timeval> timers;
00108                 
00109                 
00110                 map<int,bool> active;
00111                 
00112                 
00113                 map<int,double> values;
00114                                 
00115                 Timer()
00116                 {
00117                 }
00118                 
00119                 ~Timer()
00120                 {
00121                         timers.clear();
00122                         active.clear();
00123                         values.clear();
00124                 }
00125                 
00126                 void tic(int t=0)
00127                 {
00128                         
00129                         timeval start;
00130                         gettimeofday(&start, NULL);
00131                         
00132                         
00133                         timers[t] = start;
00134                         
00135                         active[t] = true;
00136                         
00137                         values[t] = 0;
00138                 }
00139                 
00140                 double toc(int t=0)
00141                 {
00142                         
00143                         if(!active[t])
00144                                 return values[t];
00145                         
00146                         
00147                         timeval end;
00148                         gettimeofday(&end, NULL);
00149                         
00150                         
00151                         values[t] = (end.tv_sec - timers[t].tv_sec); 
00152                         values[t]+= (end.tv_usec - timers[t].tv_usec) / 1000000.0; 
00153                         
00154                         active[t] = false;
00155                         
00156                         return values[t];
00157                 }
00158                 
00159                 double toMiliseconds(int t=0)
00160                 {
00161                         return values[t]*1000.;
00162                 }
00163                 
00164                 double value(int t=0)
00165                 {
00166                         return values[t];
00167                 }
00168 };
00169 
00170 vector<vector<int> > convertToStdVector(MatrixXd&mat);
00171 double munkers_wrapper(MatrixXd&cost_mat,vector<orderedPairPtr>&assignments);
00172 vector<AssignmentsPtr> k_best_assignment(MatrixXd& cost_mat,uint k);
00173 vector<NodePtr> partitionNode(Node&node_in,MatrixXd&cost_mat);
00174 NodePtr calcNodeCost(Node&node_in,MatrixXd&cost_mat,bool non_square_matrix=false);
00175 template<class type>
00176 uint countRepeatingValues(vector<type> sorted_values);
00177 vector<int> getRows(vector<orderedPairPtr>& pairs);
00178 vector<int> getCols(vector<orderedPairPtr>& pairs);
00179 bool compareOrdered_pair(orderedPairPtr& p1,orderedPairPtr& p2);
00180 ostream &operator<<(ostream &o,vector<AssignmentsPtr>& assing);
00181 ostream &operator<<(ostream &o,NodePtr& n);
00182 ostream &operator<<(ostream &o,Node& n);
00183 
00184 #endif