core/vnl/algo/vnl_conjugate_gradient.h
Go to the documentation of this file.
00001 // This is core/vnl/algo/vnl_conjugate_gradient.h
00002 #ifndef vnl_conjugate_gradient_h_
00003 #define vnl_conjugate_gradient_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 //  \file
00009 //  \brief  real function minimization
00010 //  \author Geoffrey Cross, Oxford RRG
00011 //  \date   15 Feb 99
00012 //
00013 // \verbatim
00014 // Modifications
00015 // 990215 Geoff Initial version.
00016 // 000628 David Capel - Major rewrite. Now derived from vnl_nonlinear_minimizer and operates on a vnl_cost_function.
00017 //   Feb.2002 - Peter Vanroose - brief doxygen comment placed on single line
00018 // \endverbatim
00019 //
00020 //-----------------------------------------------------------------------------
00021 
00022 #include <vcl_iosfwd.h>
00023 #include <vnl/vnl_vector.h>
00024 #include <vnl/vnl_matrix.h>
00025 #include <vnl/vnl_nonlinear_minimizer.h>
00026 
00027 class vnl_cost_function;
00028 
00029 //: real function minimization
00030 
00031 class vnl_conjugate_gradient : public vnl_nonlinear_minimizer
00032 {
00033  public:
00034   // Constructors/Destructors--------------------------------------------------
00035 
00036   //: Initialize with the function object that is to be minimized.
00037   vnl_conjugate_gradient(vnl_cost_function& f) { init( f); }
00038 
00039   //: Initialize as above, and then run minimization.
00040   vnl_conjugate_gradient(vnl_cost_function& f, vnl_vector<double>& x) {
00041     init(f);
00042     minimize(x);
00043   }
00044 
00045   //: Initialize all variables
00046   void init(vnl_cost_function &f);
00047 
00048   //: Destructor.
00049   ~vnl_conjugate_gradient();
00050 
00051   // Operations----------------------------------------------------------------
00052 
00053   void diagnose_outcome(vcl_ostream&) const;
00054   void diagnose_outcome(/*vcl_ostream& = vcl_cout*/) const;
00055 
00056   // Computations--------------------------------------------------------------
00057 
00058   //: Minimize the function supplied in the constructor until convergence or failure.
00059   // On return, x is such that f(x) is the lowest value achieved.
00060   // Returns true for convergence, false for failure.
00061   bool minimize(vnl_vector<double>& x);
00062 
00063  protected:
00064   // Data Members--------------------------------------------------------------
00065 
00066   vnl_cost_function *f_;
00067   double final_step_size_;
00068 
00069   // Helpers-------------------------------------------------------------------
00070 
00071 #ifdef VCL_SUNPRO_CC
00072  public:
00073 #endif
00074   static double valuecomputer_( double *x, void* userdata);
00075   static void gradientcomputer_( double *g, double *x, void* userdata);
00076   static void valueandgradientcomputer_( double *v, double *g, double *x, void* userdata);
00077   static void preconditioner_( double *out, double *in, void* userdata);
00078 
00079 #if 0
00080  protected:
00081   void approximate_gradient( const vnl_vector<double> &x,
00082                              vnl_vector<double> &g, const double step);
00083   void approximate_hessian( const vnl_vector<double> &x,
00084                             vnl_matrix<double> &h, const double step);
00085 #endif
00086 };
00087 
00088 #endif // vnl_conjugate_gradient_h_