core/vnl/io/vnl_io_nonlinear_minimizer.cxx
Go to the documentation of this file.
00001 // This is core/vnl/io/vnl_io_nonlinear_minimizer.cxx
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005 //:
00006 // \file
00007 
00008 #include "vnl_io_nonlinear_minimizer.h"
00009 #include <vsl/vsl_binary_io.h>
00010 #include <vsl/vsl_clipon_binary_loader.txx>
00011 
00012 //: Create new object of type vnl_nonlinear_minimizer on heap
00013 vnl_nonlinear_minimizer* vnl_io_nonlinear_minimizer::new_object() const
00014 {
00015   return new vnl_nonlinear_minimizer;
00016 }
00017 
00018 //: Write derived class to os using vnl_nonlinear_minimizer reference
00019 void vnl_io_nonlinear_minimizer::b_write_by_base(vsl_b_ostream& os,
00020                                                  const vnl_nonlinear_minimizer& base) const
00021 {
00022   vsl_b_write(os,base);
00023 }
00024 
00025 //: Write derived class to os using vnl_nonlinear_minimizer reference
00026 void vnl_io_nonlinear_minimizer::b_read_by_base(vsl_b_istream& is,
00027                                                 vnl_nonlinear_minimizer& base) const
00028 {
00029   vsl_b_read(is,base);
00030 }
00031 
00032 //: Print summary of derived class to os using vnl_nonlinear_minimizer reference
00033 void vnl_io_nonlinear_minimizer::print_summary_by_base(vcl_ostream& os,
00034                                                        const vnl_nonlinear_minimizer& base) const
00035 {
00036   vsl_print_summary(os,base);
00037 }
00038 
00039 //: Copy this object onto the heap and return a pointer
00040 vnl_io_nonlinear_minimizer* vnl_io_nonlinear_minimizer::clone() const
00041 {
00042   return new vnl_io_nonlinear_minimizer(*this);
00043 }
00044 
00045 //==============================================================================
00046 //: Binary save self to stream.
00047 void vsl_b_write(vsl_b_ostream & os, const vnl_nonlinear_minimizer & p)
00048 {
00049   const short io_version_no = 1;
00050   vsl_b_write(os, io_version_no);
00051   vsl_b_write(os, p.get_f_tolerance());
00052   vsl_b_write(os, p.get_x_tolerance());
00053   vsl_b_write(os, p.get_g_tolerance());
00054   vsl_b_write(os, p.get_max_function_evals());
00055   vsl_b_write(os, p.get_epsilon_function());
00056   vsl_b_write(os, p.get_trace());
00057   vsl_b_write(os, p.get_verbose());
00058   vsl_b_write(os, p.get_check_derivatives());
00059 }
00060 
00061 //==============================================================================
00062 //: Binary load self from stream.
00063 void vsl_b_read(vsl_b_istream &is, vnl_nonlinear_minimizer & p)
00064 {
00065   if (!is) return;
00066 
00067   short ver;
00068   // Load & save variables
00069   double ftol;    // Termination tolerance on F (sum of squared residuals)
00070   double xtol;    // Termination tolerance on X (solution vector)
00071   double gtol;    // Termination tolerance on Grad(F)' * F = 0
00072   int    maxfev;  // Termination maximum number of iterations.
00073   double epsfcn;  // Step length for FD Jacobian
00074   bool trace;
00075   bool verbose;
00076   int check_derivatives;
00077 
00078   vsl_b_read(is, ver);
00079   switch (ver)
00080   {
00081    case 1:
00082     vsl_b_read(is, ftol);
00083     p.set_f_tolerance(ftol);
00084     vsl_b_read(is, xtol);
00085     p.set_x_tolerance(xtol);
00086     vsl_b_read(is, gtol);
00087     p.set_g_tolerance(gtol);
00088     vsl_b_read(is, maxfev);
00089     p.set_max_function_evals(maxfev);
00090     vsl_b_read(is, epsfcn);
00091     p.set_epsilon_function(epsfcn);
00092     vsl_b_read(is, trace);
00093     p.set_trace(trace);
00094     vsl_b_read(is, verbose);
00095     p.set_verbose(verbose);
00096     vsl_b_read(is, check_derivatives);
00097     p.set_check_derivatives(check_derivatives);
00098     break;
00099 
00100    default:
00101     vcl_cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, vnl_nonlinear_minimizer&)\n"
00102              << "           Unknown version number "<< ver << '\n';
00103     is.is().clear(vcl_ios::badbit); // Set an unrecoverable IO error on stream
00104     return;
00105   }
00106 }
00107 
00108 //==============================================================================
00109 //: Output a human readable summary to the stream
00110 void vsl_print_summary(vcl_ostream & os,const vnl_nonlinear_minimizer & p)
00111 {
00112   os<<"Tolerance of {F, X, G}: {"<<p.get_f_tolerance() << ", "
00113     << p.get_x_tolerance()<<", "<<p.get_g_tolerance() << "}\n"
00114     <<"Max Function Evals:"<<p.get_max_function_evals()<<"    Epsilon function:"
00115     <<p.get_epsilon_function()<<"       Trace:"<<p.get_trace()<<'\n'
00116     <<"Verbose:"<<p.get_verbose()<<"     Check Derivatives:"
00117     <<p.get_check_derivatives()<<'\n';
00118 }
00119 
00120 //: Add example object to list of those that can be loaded
00121 //  The vsl_binary_loader must see an example of each derived class
00122 //  before it knows how to deal with them.
00123 //  A clone is taken of b
00124 void vsl_add_to_binary_loader(const vnl_io_nonlinear_minimizer& b)
00125 {
00126     vsl_clipon_binary_loader<vnl_nonlinear_minimizer,
00127                              vnl_io_nonlinear_minimizer>::instance().add(b);
00128 }
00129 
00130 
00131 //: Binary save to stream by vnl_nonlinear_minimizer pointer
00132 void vsl_b_write(vsl_b_ostream &os, const vnl_nonlinear_minimizer * b)
00133 {
00134     vsl_clipon_binary_loader<vnl_nonlinear_minimizer,
00135                              vnl_io_nonlinear_minimizer>::instance().write_object(os,b);
00136 }
00137 
00138 //: Binary read from stream by vnl_nonlinear_minimizer pointer
00139 void vsl_b_read(vsl_b_istream &is, vnl_nonlinear_minimizer* &b)
00140 {
00141     vsl_clipon_binary_loader<vnl_nonlinear_minimizer,
00142                              vnl_io_nonlinear_minimizer>::instance().read_object(is,b);
00143 }
00144 
00145 //: Print summary to stream by vnl_nonlinear_minimizer pointer
00146 void vsl_print_summary(vcl_ostream &os, const vnl_nonlinear_minimizer * b)
00147 {
00148     vsl_clipon_binary_loader<vnl_nonlinear_minimizer,
00149                              vnl_io_nonlinear_minimizer>::instance().print_object_summary(os,b);
00150 }
00151 
00152 // Explicitly instantiate loader
00153 VSL_CLIPON_BINARY_LOADER_INSTANTIATE(vnl_nonlinear_minimizer, \
00154                                      vnl_io_nonlinear_minimizer);