00001 #ifndef mbl_dyn_prog_h_ 00002 #define mbl_dyn_prog_h_ 00003 00004 //: 00005 // \file 00006 // \brief Class to solve simple dynamic programming problems 00007 // \author Tim Cootes 00008 00009 #include <vcl_string.h> 00010 #include <vcl_iosfwd.h> 00011 00012 #include <vsl/vsl_fwd.h> 00013 #include <vnl/vnl_matrix.h> 00014 #include <vcl_vector.h> 00015 #include <vbl/vbl_array_2d.h> 00016 #include <vnl/vnl_vector.h> 00017 00018 //: Class to solve simple dynamic programming problems 00019 // Assume n values x(i) to be chosen, x(i) in [0,k-1], i=0..n-1 00020 // Cost of each is W(i,x(i)) 00021 // Total cost = sum_i W(i,x(i)) + C_i(x(i),x(i-1)) 00022 // The algorithm finds the path (x(0),x(1)..x(n-1)) through which minimises 00023 // the total cost, with a variety of different ways of defining C_i(x(i),x(i-1)) 00024 // 00025 class mbl_dyn_prog { 00026 private: 00027 //: After search, links_(i,j) shows the best prior state 00028 // (ie at i) leading to state j at time i+1 00029 vbl_array_2d<int> links_; 00030 00031 //: Workspace for running cost. 00032 // After search gives cost to get to each state in last row 00033 vnl_vector<double> running_cost_; 00034 00035 //: Workspace for previous running cost. 00036 vnl_vector<double> next_cost_; 00037 00038 //: Construct path from links_, assuming it ends at end_state 00039 void construct_path(vcl_vector<int>& x, int end_state); 00040 00041 //: Compute running costs for DP problem with costs W 00042 // Pair cost term: C_i(x1,x2) = c(|x1-x2|) 00043 // Size of c indicates maximum displacement between neighbouring 00044 // states. 00045 // If first_state>=0 then the first is constrained to that index value 00046 void running_costs(const vnl_matrix<double>& W, 00047 const vnl_vector<double>& pair_cost, 00048 int first_state); 00049 00050 public: 00051 00052 //: Dflt ctor 00053 mbl_dyn_prog(); 00054 00055 //: Destructor 00056 virtual ~mbl_dyn_prog(); 00057 00058 //: Solve the dynamic programming problem with costs W 00059 // Pair cost term: C_i(x1,x2) = pair_cost(|x1-x2|) 00060 // Size of c indicates maximum displacement between neighbouring 00061 // states. 00062 // If first_state>=0 then the first is constrained to that index value 00063 // \retval x Optimal path 00064 // \return Total cost of given path 00065 double solve(vcl_vector<int>& x, 00066 const vnl_matrix<double>& W, 00067 const vnl_vector<double>& pair_cost, 00068 int first_state=-1); 00069 00070 //: Solve the loop dynamic programming problem with costs W 00071 // Pair cost term: C_i(x1,x2) = pair_cost(|x1-x2|) 00072 // Size of c indicates maximum displacement between neighbouring 00073 // states. 00074 // As solve(x,W,c), but includes constraint between first and 00075 // last states. 00076 // \retval x Optimal path 00077 // \return Total cost of given path 00078 double solve_loop(vcl_vector<int>& x, 00079 const vnl_matrix<double>& W, 00080 const vnl_vector<double>& pair_cost); 00081 00082 00083 //: Version number for I/O 00084 short version_no() const; 00085 00086 //: Name of the class 00087 virtual vcl_string is_a() const; 00088 00089 //: Print class to os 00090 virtual void print_summary(vcl_ostream& os) const; 00091 00092 //: Save class to binary file stream 00093 virtual void b_write(vsl_b_ostream& bfs) const; 00094 00095 //: Load class from binary file stream 00096 virtual void b_read(vsl_b_istream& bfs); 00097 }; 00098 00099 //: Binary file stream output operator for class reference 00100 void vsl_b_write(vsl_b_ostream& bfs, const mbl_dyn_prog& b); 00101 00102 //: Binary file stream input operator for class reference 00103 void vsl_b_read(vsl_b_istream& bfs, mbl_dyn_prog& b); 00104 00105 //: Stream output operator for class reference 00106 vcl_ostream& operator<<(vcl_ostream& os,const mbl_dyn_prog& b); 00107 00108 //======================================================================= 00109 #endif 00110 00111