vnl_io_nonlinear_minimizer.cxx
Go to the documentation of this file.
1 // This is core/vnl/io/vnl_io_nonlinear_minimizer.cxx
2 //:
3 // \file
4 
6 #include <vsl/vsl_clipon_binary_loader.hxx>
7 
8 //: Create new object of type vnl_nonlinear_minimizer on heap
10 {
11  return new vnl_nonlinear_minimizer;
12 }
13 
14 //: Write derived class to os using vnl_nonlinear_minimizer reference
16  const vnl_nonlinear_minimizer& base) const
17 {
18  vsl_b_write(os,base);
19 }
20 
21 //: Write derived class to os using vnl_nonlinear_minimizer reference
23  vnl_nonlinear_minimizer& base) const
24 {
25  vsl_b_read(is,base);
26 }
27 
28 //: Print summary of derived class to os using vnl_nonlinear_minimizer reference
30  const vnl_nonlinear_minimizer& base) const
31 {
32  vsl_print_summary(os,base);
33 }
34 
35 //: Copy this object onto the heap and return a pointer
37 {
38  return new vnl_io_nonlinear_minimizer(*this);
39 }
40 
41 //==============================================================================
42 //: Binary save self to stream.
43 void vsl_b_write(vsl_b_ostream & os, const vnl_nonlinear_minimizer & p)
44 {
45  constexpr short io_version_no = 1;
46  vsl_b_write(os, io_version_no);
52  vsl_b_write(os, p.get_trace());
53  vsl_b_write(os, p.get_verbose());
55 }
56 
57 //==============================================================================
58 //: Binary load self from stream.
59 void vsl_b_read(vsl_b_istream &is, vnl_nonlinear_minimizer & p)
60 {
61  if (!is) return;
62 
63  short ver;
64  // Load & save variables
65  double ftol; // Termination tolerance on F (sum of squared residuals)
66  double xtol; // Termination tolerance on X (solution vector)
67  double gtol; // Termination tolerance on Grad(F)' * F = 0
68  int maxfev; // Termination maximum number of iterations.
69  double epsfcn; // Step length for FD Jacobian
70  bool trace;
71  bool verbose;
72  int check_derivatives;
73 
74  vsl_b_read(is, ver);
75  switch (ver)
76  {
77  case 1:
78  vsl_b_read(is, ftol);
79  p.set_f_tolerance(ftol);
80  vsl_b_read(is, xtol);
81  p.set_x_tolerance(xtol);
82  vsl_b_read(is, gtol);
83  p.set_g_tolerance(gtol);
84  vsl_b_read(is, maxfev);
85  p.set_max_function_evals(maxfev);
86  vsl_b_read(is, epsfcn);
87  p.set_epsilon_function(epsfcn);
88  vsl_b_read(is, trace);
89  p.set_trace(trace);
90  vsl_b_read(is, verbose);
91  p.set_verbose(verbose);
92  vsl_b_read(is, check_derivatives);
93  p.set_check_derivatives(check_derivatives);
94  break;
95 
96  default:
97  std::cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, vnl_nonlinear_minimizer&)\n"
98  << " Unknown version number "<< ver << '\n';
99  is.is().clear(std::ios::badbit); // Set an unrecoverable IO error on stream
100  return;
101  }
102 }
103 
104 //==============================================================================
105 //: Output a human readable summary to the stream
106 void vsl_print_summary(std::ostream & os,const vnl_nonlinear_minimizer & p)
107 {
108  os<<"Tolerance of {F, X, G}: {"<<p.get_f_tolerance() << ", "
109  << p.get_x_tolerance()<<", "<<p.get_g_tolerance() << "}\n"
110  <<"Max Function Evals:"<<p.get_max_function_evals()<<" Epsilon function:"
111  <<p.get_epsilon_function()<<" Trace:"<<p.get_trace()<<'\n'
112  <<"Verbose:"<<p.get_verbose()<<" Check Derivatives:"
113  <<p.get_check_derivatives()<<'\n';
114 }
115 
116 //: Add example object to list of those that can be loaded
117 // The vsl_binary_loader must see an example of each derived class
118 // before it knows how to deal with them.
119 // A clone is taken of b
121 {
122  vsl_clipon_binary_loader<vnl_nonlinear_minimizer,
123  vnl_io_nonlinear_minimizer>::instance().add(b);
124 }
125 
126 
127 //: Binary save to stream by vnl_nonlinear_minimizer pointer
128 void vsl_b_write(vsl_b_ostream &os, const vnl_nonlinear_minimizer * b)
129 {
130  vsl_clipon_binary_loader<vnl_nonlinear_minimizer,
131  vnl_io_nonlinear_minimizer>::instance().write_object(os,b);
132 }
133 
134 //: Binary read from stream by vnl_nonlinear_minimizer pointer
135 void vsl_b_read(vsl_b_istream &is, vnl_nonlinear_minimizer* &b)
136 {
137  vsl_clipon_binary_loader<vnl_nonlinear_minimizer,
138  vnl_io_nonlinear_minimizer>::instance().read_object(is,b);
139 }
140 
141 //: Print summary to stream by vnl_nonlinear_minimizer pointer
142 void vsl_print_summary(std::ostream &os, const vnl_nonlinear_minimizer * b)
143 {
144  vsl_clipon_binary_loader<vnl_nonlinear_minimizer,
145  vnl_io_nonlinear_minimizer>::instance().print_object_summary(os,b);
146 }
147 
148 // Explicitly instantiate loader
void set_f_tolerance(double v)
Set the convergence tolerance on F (sum of squared residuals).
virtual void b_write_by_base(vsl_b_ostream &os, const vnl_nonlinear_minimizer &base) const
Write derived class to os using vnl_nonlinear_minimizer reference.
void set_check_derivatives(int cd)
Set check_derivatives flag. Negative values may mean fewer checks.
virtual vnl_io_nonlinear_minimizer * clone() const
Copy this object onto the heap and return a pointer.
void vsl_add_to_binary_loader(const vnl_io_nonlinear_minimizer &b)
Add example object to list of those that can be loaded.
virtual vnl_nonlinear_minimizer * new_object() const
Create new object of type vnl_nonlinear_minimizer on heap.
void vsl_print_summary(std::ostream &os, vnl_bignum const &b)
Print human readable summary of object to a stream.
vnl_io_nonlinear_minimizer()=default
Constructor.
void vsl_b_write(vsl_b_ostream &os, const vnl_nonlinear_minimizer &p)
Binary save self to stream.
void set_verbose(bool verb)
Set verbose flag.
virtual void print_summary_by_base(std::ostream &os, const vnl_nonlinear_minimizer &base) const
Print summary of derived class to os.
virtual void b_read_by_base(vsl_b_istream &is, vnl_nonlinear_minimizer &base) const
Write derived class to os using vnl_nonlinear_minimizer reference.
vnl_nonlinear_minimizer is a base class for nonlinear optimization.
void set_g_tolerance(double v)
Set the convergence tolerance on Grad(F)' * F.
VSL_CLIPON_BINARY_LOADER_INSTANTIATE(vnl_nonlinear_minimizer, vnl_io_nonlinear_minimizer)
void vsl_b_read(vsl_b_istream &is, vnl_bignum &v)
Binary load vnl_bignum from stream.
void vsl_print_summary(std::ostream &os, const vnl_nonlinear_minimizer &p)
Output a human readable summary to the stream.
void vsl_b_write(vsl_b_ostream &os, vnl_bignum const &v)
Binary save vnl_bignum to stream.
void set_max_function_evals(int v)
Set the termination maximum number of iterations.
void set_epsilon_function(double v)
Set the step length for FD Jacobian.
Base for objects which provide IO.
void vsl_b_read(vsl_b_istream &is, vnl_nonlinear_minimizer &p)
Binary load self from stream.
void set_x_tolerance(double v)
Set the convergence tolerance on X.
void set_trace(bool on)
Turn on per-iteration printouts.