ReFRACtor
connor_convergence.cc
Go to the documentation of this file.
1 #include "connor_convergence.h"
2 #include "fp_exception.h"
3 
4 using namespace FullPhysics;
5 
6 #ifdef HAVE_LUA
7 #include "register_lua.h"
9 .def(luabind::constructor<const boost::shared_ptr<ForwardModel>&,
10  double, int, int, double>())
12 #endif
13 
14 //-----------------------------------------------------------------------
26 //-----------------------------------------------------------------------
27 
30  double Threshold, int Max_iteration, int Max_divergence, double Max_chisq)
31 : fm(Fm),
32  threshold(Threshold), max_iteration(Max_iteration),
33  max_divergence(Max_divergence), max_chisq(Max_chisq)
34 {
35  range_min_check(threshold, 0.0);
36  range_min_check(Max_iteration, 1);
37  range_min_check(Max_divergence, 1);
38  range_min_check(Max_chisq, 0.0);
39 }
40 
41 //-----------------------------------------------------------------------
42 // See ConvergenceCheck for description of this function.
43 //-----------------------------------------------------------------------
44 
46  const FitStatistic& fs_last,
47  FitStatistic& fs,
48  bool& has_converged,
49  bool& convergence_failed,
50  double& gamma,
51  bool& step_diverged)
52 {
53  has_converged = false;
54  convergence_failed = false;
55  step_diverged = false;
56 
57  double r1 = fs.gamma2() - fs_last.gamma2();
58  double r2 = fs_last.gamma2_fc() - fs_last.gamma2();
59  double ratio = 0.0;
60 
61  //if(r2 >= 0.0)
62  // something is wrong
63 
64  if(r1 >= 0.0)
65  ratio = 0.0;
66  //
67  else if(fabs(r2)<(-r1*(1e-20)))
68  ratio = 1.0;
69  else
70  ratio = r1/r2;
71 
72  if(ratio < 0.25 && fs.number_iteration > 1) {
73  if(gamma > 1e-8)
74  gamma *= 10.0;
75  else
76  gamma = 1.0;
77  } else if(ratio > 0.75 && fs.number_iteration > 1)
78  gamma /= 2.0;
79 
80  // The threshold 0.0001 for step acceptance
81  // or rejection appears in More's paper. This
82  // changes are related to Trac ticket #742.
83  //
84  if(ratio <= 0.0001 && fs.number_iteration > 1) {
85  step_diverged = true;
86  if(fs.number_divergent + 1 > max_divergence) {
88  convergence_failed = true;
89  }
90  return;
91  }
92 
93  if(fs.d_sigma_sq_scaled < threshold)
94  has_converged = true;
95  else if(fs.number_iteration >= max_iteration) {
96  convergence_failed = true;
98  }
99 }
100 
101 // See base class for description of this function.
102 
104  const blitz::Array<double, 1>& Residual,
105  const blitz::Array<double, 1>& Residual_cov_diag)
106 {
107  if (not fit_stat.fit_succeeded)
108  throw Exception("Can not evaulate quality when the fit has not succeeded");
109 
110 
111  int quality_count = 0;
112  int nband = 0;
113  for(int i = 0; i < fm->num_channels(); ++i) {
114  boost::optional<blitz::Range> pr = fm->stacked_pixel_range(i);
115  if(pr) {
116  double chisq_m = fit_stat.chisq_measure_norm(Residual(*pr),
117  Residual_cov_diag(*pr));
118  ++nband;
119  if(chisq_m < max_chisq)
120  quality_count++;
121  }
122  }
123 
124  if (quality_count == nband)
126  else
128 }
129 
130 
131 //-----------------------------------------------------------------------
133 //-----------------------------------------------------------------------
134 
135 void ConnorConvergence::print(std::ostream& Os) const
136 {
137  Os << "ConnorConvergence\n"
138  << " threshold: " << threshold << "\n"
139  << " max_iteration: " << max_iteration << "\n";
140 }
This class holds various parameters describing how good of a fit we have.
virtual void convergence_check(const FitStatistic &fit_stat_last, FitStatistic &fit_stat, bool &has_converged, bool &convergence_failed, double &gamma, bool &step_diverged)
Check for the convergence of a Solver, or if we have taken a divergent step.
This is the base of the exception hierarchy for Full Physics code.
Definition: fp_exception.h:16
virtual void evaluate_quality(FitStatistic &fit_stat, const blitz::Array< double, 1 > &Residual, const blitz::Array< double, 1 > &Residual_cov_diag)
Evaluates the quality of a converged fit from the residuals and expected residual error...
#define REGISTER_LUA_DERIVED_CLASS(X, Y)
Definition: register_lua.h:136
double gamma2_fc() const
Parameter "gamma2_fc", which is just chisq_apriori_fc + chisq_measured_fc.
double chisq_measure_norm(const blitz::Array< double, 1 > &Residual, const blitz::Array< double, 1 > &Residual_cov_diag) const
Calculate chisq for given residual and covariance matrix.
int number_divergent
Number of divergent steps.
This class tests for convergence of a Levenberg-Marquardt solver.
bool fit_succeeded
Was the fit successful?
This class tests for convergence of a Levenberg-Marquardt solver.
OUTCOME outcome
Flag indicating success of fit, or why fit wasn&#39;t succesful.
double d_sigma_sq_scaled
This is d_sigma_sq, scaled by the size of the state vector.
Contains classes to abstract away details in various Spurr Radiative Transfer software.
Definition: doxygen_python.h:1
#define REGISTER_LUA_END()
Definition: register_lua.h:134
#define range_min_check(V, Min)
Range check.
Definition: fp_exception.h:167
int number_iteration
Number of iterations.
ConnorConvergence(const boost::shared_ptr< ForwardModel > &Fm, double Threshold, int Max_iteration, int Max_divergence, double Max_chisq)
Constructor.
double gamma2() const
Parameter "gamma2", which is just chi2_apriori + chi2_measured.
virtual void print(std::ostream &Os) const
Print description of object.

Copyright © 2017, California Institute of Technology.
ALL RIGHTS RESERVED.
U.S. Government Sponsorship acknowledged.
Generated Fri Aug 24 2018 15:44:08