1 // This is core/vnl/algo/vnl_conjugate_gradient.h 2 #ifndef vnl_conjugate_gradient_h_ 3 #define vnl_conjugate_gradient_h_ 4 //: 5 // \file 6 // \brief real function minimization 7 // \author Geoffrey Cross, Oxford RRG 8 // \date 15 Feb 99 9 // 10 // \verbatim 11 // Modifications 12 // 990215 Geoff Initial version. 13 // 000628 David Capel - Major rewrite. Now derived from vnl_nonlinear_minimizer and operates on a vnl_cost_function. 14 // Feb.2002 - Peter Vanroose - brief doxygen comment placed on single line 15 // \endverbatim 16 // 17 //----------------------------------------------------------------------------- 18 19 #include <iosfwd> 20 #ifdef _MSC_VER 21 # include <vcl_msvc_warnings.h> 22 #endif 23 #include <vnl/vnl_vector.h> 24 #include <vnl/vnl_matrix.h> 25 #include <vnl/vnl_nonlinear_minimizer.h> 26 #include <vnl/algo/vnl_algo_export.h> 27 28 class vnl_cost_function; 29 30 //: real function minimization 31 32 class VNL_ALGO_EXPORT vnl_conjugate_gradient : public vnl_nonlinear_minimizer 33 { 34 public: 35 // Constructors/Destructors-------------------------------------------------- 36 37 //: Initialize with the function object that is to be minimized. vnl_conjugate_gradient(vnl_cost_function & f)38 vnl_conjugate_gradient(vnl_cost_function& f) { init( f); } 39 40 //: Initialize as above, and then run minimization. vnl_conjugate_gradient(vnl_cost_function & f,vnl_vector<double> & x)41 vnl_conjugate_gradient(vnl_cost_function& f, vnl_vector<double>& x) { 42 init(f); 43 minimize(x); 44 } 45 46 //: Initialize all variables 47 void init(vnl_cost_function &f); 48 49 //: Destructor. 50 ~vnl_conjugate_gradient() override; 51 52 // Operations---------------------------------------------------------------- 53 54 void diagnose_outcome(std::ostream&) const; 55 void diagnose_outcome(/*std::ostream& = std::cout*/) const; 56 57 // Computations-------------------------------------------------------------- 58 59 //: Minimize the function supplied in the constructor until convergence or failure. 60 // On return, x is such that f(x) is the lowest value achieved. 61 // Returns true for convergence, false for failure. 62 bool minimize(vnl_vector<double>& x); 63 64 protected: 65 // Data Members-------------------------------------------------------------- 66 67 vnl_cost_function *f_; 68 double final_step_size_; 69 70 // Helpers------------------------------------------------------------------- 71 72 static double valuecomputer_( double *x, void* userdata); 73 static void gradientcomputer_( double *g, double *x, void* userdata); 74 static void valueandgradientcomputer_( double *v, double *g, double *x, void* userdata); 75 static void preconditioner_( double *out, double *in, void* userdata); 76 77 }; 78 79 #endif // vnl_conjugate_gradient_h_ 80