ReFRACtor
connor_solver.h
Go to the documentation of this file.
1 #ifndef CONNOR_SOLVER_H
2 #define CONNOR_SOLVER_H
3 #include "cost_function.h"
4 #include "convergence_check.h"
5 #include "observer.h"
6 #include <blitz/array.h>
7 #include <boost/shared_ptr.hpp>
8 #include <boost/utility.hpp>
9 
10 namespace FullPhysics {
11 
12 // Note that we have the various equations here in Latex. This is
13 // fairly unreadable as text comments, but if you view the doxgyen
14 // documentation created by this it gives a nicely formatted output.
15 
16 
17 /****************************************************************/
55 class ConnorSolverState;
56 
57 class ConnorSolver : public Observable<ConnorSolver>,
58  public Printable<ConnorSolver>,
59  boost::noncopyable {
60 public:
61  //-----------------------------------------------------------------------
67  //-----------------------------------------------------------------------
70  double Gamma_initial = 0.0,
71  const std::string& Save_test_data = "")
72  : save_test_data_(Save_test_data),
73  cost_function_(Cf), convergence_check_(Conv), gamma_initial(Gamma_initial)
74  { }
75  virtual ~ConnorSolver() {}
76 
78  { add_observer_do(Obs, *this);}
80  { remove_observer_do(Obs, *this);}
81 
82  //-----------------------------------------------------------------------
84  //-----------------------------------------------------------------------
85  void save_test_data(const std::string& Fname) { save_test_data_ = Fname;}
86 
87  void to_stream(std::ostream& os) const;
88  void from_stream(std::istream& is);
89 
91  void state(const ConnorSolverState& S);
92 
93  void test_do_inversion(const std::string& Fname,
94  blitz::Array<double, 1>& Dx,
95  blitz::Array<double, 2>& Kt_se_m1_k);
96  virtual bool solve(const blitz::Array<double, 1>& Initial_guess,
97  const blitz::Array<double, 1>& Apriori,
98  const blitz::Array<double, 2>& Apriori_cov);
99  virtual blitz::Array<double, 2> aposteriori_covariance_scaled() const;
100  virtual blitz::Array<double, 2> aposteriori_covariance() const;
101  blitz::Array<double, 1> x_solution_uncertainty() const;
102 
103  virtual blitz::Array<double, 2> averaging_kernel() const;
104 
105 //-----------------------------------------------------------------------
107 //-----------------------------------------------------------------------
108 
109  double gamma_last_step() const {return gamma_last_step_;}
110 
111 //-----------------------------------------------------------------------
113 //-----------------------------------------------------------------------
114 
116  { return cost_function_; }
117 
118 //-----------------------------------------------------------------------
120 //-----------------------------------------------------------------------
121 
123  { return convergence_check_; }
124 
125 //-----------------------------------------------------------------------
127 //-----------------------------------------------------------------------
128 
130 
131 //-----------------------------------------------------------------------
133 //-----------------------------------------------------------------------
134 
136 
137 //-----------------------------------------------------------------------
139 //-----------------------------------------------------------------------
140 
141  int outcome_flag() const { return (int) fit_statistic().outcome; }
142 
143 //-----------------------------------------------------------------------
145 //-----------------------------------------------------------------------
146 
147  blitz::Array<double, 1> x_apriori() const { return x_a;}
148 
149 //-----------------------------------------------------------------------
152 //-----------------------------------------------------------------------
153 
154  blitz::Array<double, 1> x_apriori_uncertainty() const
155  { return sigma_ap; }
156  virtual blitz::Array<double, 1> x_solution() const { return x_i; }
157  int x_solution_size() const { return x_solution().rows();}
158  blitz::Array<double, 1> x_solution_zero_unused() const;
159  blitz::Array<double, 2> apriori_covariance() const;
160  blitz::Array<double, 1> x_update() const { return dx; }
161 
162 //-----------------------------------------------------------------------
171 //-----------------------------------------------------------------------
172 
173  virtual blitz::Array<double, 2> jacobian() const {return k;}
174 
175 //-----------------------------------------------------------------------
177 //-----------------------------------------------------------------------
178 
179  virtual FitStatistic fit_statistic() const {return fstat; }
180 
181 //-----------------------------------------------------------------------
183 //-----------------------------------------------------------------------
184 
185  virtual blitz::Array<double, 1> residual() const {return residual_;}
186 
187 //-----------------------------------------------------------------------
190 //-----------------------------------------------------------------------
191 
192  virtual blitz::Array<double, 1> residual_covariance_diagonal() const
193  {return se;}
194 
195 //-----------------------------------------------------------------------
197 //-----------------------------------------------------------------------
198 
199  virtual blitz::Array<double, 2> apriori_covariance_inv_norm() const
200  {return sa_m1_scaled;}
201 
202  void print(std::ostream& Os) const { Os << "ConnorSolver";}
203 
204  // These members are "protected" just so we get doxygen
205  // documentation on them (private members don't appear in the
206  // documentation in general).
207 protected:
208  blitz::Array<double, 1> zero_unused_parm() const;
209  void do_inversion();
210 
211  //-----------------------------------------------------------------------
214  //-----------------------------------------------------------------------
215  std::string save_test_data_;
216 
217  //-----------------------------------------------------------------------
224  //-----------------------------------------------------------------------
225  blitz::Array<double, 1> se;
226 
227  //-----------------------------------------------------------------------
231  //-----------------------------------------------------------------------
232  blitz::Array<double, 2> k;
233 
234  //-----------------------------------------------------------------------
241  //-----------------------------------------------------------------------
242  blitz::Array<double, 2> kt_se_m1_k;
243 
244  //-----------------------------------------------------------------------
248  //-----------------------------------------------------------------------
249 
250  double gamma;
251 
252  //-----------------------------------------------------------------------
254  //-----------------------------------------------------------------------
255 
257 
258  //-----------------------------------------------------------------------
260  //-----------------------------------------------------------------------
261 
263 
264  //-----------------------------------------------------------------------
266  //-----------------------------------------------------------------------
267 
268  blitz::Array<double, 2> apriori_cov_scaled;
269 
270  //-----------------------------------------------------------------------
274  //-----------------------------------------------------------------------
275 
276  blitz::Array<double, 2> sa_m1_scaled;
277 
278  //-----------------------------------------------------------------------
281  //-----------------------------------------------------------------------
282  blitz::Array<double, 1> sigma_ap;
283 
284  //-----------------------------------------------------------------------
288  //-----------------------------------------------------------------------
289  blitz::Array<double, 1> x_a;
290 
291  //-----------------------------------------------------------------------
296  //-----------------------------------------------------------------------
297  blitz::Array<double, 1> x_i;
298 
299  //-----------------------------------------------------------------------
303  // This gets updated each iteration in "solve".
304  //-----------------------------------------------------------------------
305 
306  blitz::Array<double, 1> residual_;
307 
308  //-----------------------------------------------------------------------
312  // This gets updated each iteration in "do_inversion".
313  //-----------------------------------------------------------------------
314 
315  blitz::Array<double, 1> dx;
316 
317  //-----------------------------------------------------------------------
319  //-----------------------------------------------------------------------
320 
322 
323  //-----------------------------------------------------------------------
325  //-----------------------------------------------------------------------
326 
328 
329  //-----------------------------------------------------------------------
331  //-----------------------------------------------------------------------
333 
334  static double rcond;
335 };
336 
337 /****************************************************************/
341 class ConnorSolverState: public Printable<ConnorSolverState>,
342  boost::noncopyable {
343 public:
344  ConnorSolverState(const blitz::Array<double, 1>& X_i,
345  const blitz::Array<double, 1>& X_a,
346  const blitz::Array<double, 2>& Apriori_cov_scaled,
347  const blitz::Array<double, 2>& Sa_m1_scaled,
348  const blitz::Array<double, 1>& Sigma_ap,
349  double Gamma, double Gamma_last_step, double Gamma_intial,
350  const blitz::Array<double, 1>& Residual,
351  const blitz::Array<double, 1>& Se,
352  const blitz::Array<double, 2>& K,
353  const blitz::Array<double, 2>& Kt_se_m1_k,
354  const blitz::Array<double, 1>& Dx,
355  const FitStatistic& Fstat)
356  : x_i_(X_i.copy()), x_a_(X_a.copy()),
357  apriori_cov_scaled_(Apriori_cov_scaled.copy()),
358  sa_m1_scaled_(Sa_m1_scaled.copy()),
359  sigma_ap_(Sigma_ap.copy()), gamma_(Gamma),
360  gamma_last_step_(Gamma_last_step), gamma_initial_(Gamma_intial),
361  residual_(Residual.copy()), se_(Se.copy()), k_(K.copy()),
362  kt_se_m1_k_(Kt_se_m1_k.copy()), dx_(Dx.copy()), fstat_(Fstat)
363  { }
364  virtual ~ConnorSolverState() {}
365  void print(std::ostream& Os) const
366  { Os << "ConnorSolverState"; }
367  const blitz::Array<double, 1>& x_i() const { return x_i_;}
368  const blitz::Array<double, 1>& x_a() const { return x_a_;}
369  const blitz::Array<double, 2>& apriori_cov_scaled() const
370  { return apriori_cov_scaled_;}
371  const blitz::Array<double, 2>& sa_m1_scaled() const { return sa_m1_scaled_;}
372  const blitz::Array<double, 1>& sigma_ap() const { return sigma_ap_;}
373  double gamma() const { return gamma_;}
374  double gamma_last_step() const { return gamma_last_step_;}
375  double gamma_initial() const { return gamma_initial_;}
376  const blitz::Array<double, 1>& residual() const { return residual_;}
377  const blitz::Array<double, 1>& se() const { return se_;}
378  const blitz::Array<double, 2>& k() const { return k_;}
379  const blitz::Array<double, 2>& kt_se_m1_k() const { return kt_se_m1_k_;}
380  const blitz::Array<double, 1>& dx() const { return dx_;}
381  const FitStatistic& fstat() const { return fstat_;}
382 private:
383  blitz::Array<double, 1> x_i_;
384  blitz::Array<double, 1> x_a_;
385  blitz::Array<double, 2> apriori_cov_scaled_;
386  blitz::Array<double, 2>sa_m1_scaled_;
387  blitz::Array<double, 1> sigma_ap_;
388  double gamma_;
389  double gamma_last_step_;
390  double gamma_initial_;
391  blitz::Array<double, 1> residual_;
392  blitz::Array<double, 1> se_;
393  blitz::Array<double, 2> k_;
394  blitz::Array<double, 2> kt_se_m1_k_;
395  blitz::Array<double, 1> dx_;
396  FitStatistic fstat_;
397 };
398 
399 inline std::istream& operator>>(std::istream& Is, ConnorSolver& Solve)
400 {
401  Solve.from_stream(Is);
402  return Is;
403 }
404 
405 }
406 #endif
blitz::Array< double, 1 > zero_unused_parm() const
Array that is 0 where we are not using a parameter, and 1 elsewhere.
boost::shared_ptr< ConvergenceCheck > convergence_check() const
The convergence check object.
void save_test_data(const std::string &Fname)
Set save_test_data argument, see explanation before class for this.
Definition: connor_solver.h:85
blitz::Array< double, 2 > apriori_cov_scaled
This is the apriori covariance matrix scaled by sigma_ap.
const blitz::Array< double, 1 > & x_i() const
const blitz::Array< double, 2 > & kt_se_m1_k() const
blitz::Array< double, 2 > sa_m1_scaled
This is the inverse of apriori_cov_scaled, but only for the rows and columns of the jacobian that are...
This class holds various parameters describing how good of a fit we have.
void print(std::ostream &Os) const
blitz::Array< double, 1 > se
This is the covariance matrix of the residual, called in the ATB.
blitz::Array< double, 1 > x_a
This is the apriori X value, called in the ATB.
std::istream & operator>>(std::istream &Is, ConnorSolver &Solve)
virtual bool solve(const blitz::Array< double, 1 > &Initial_guess, const blitz::Array< double, 1 > &Apriori, const blitz::Array< double, 2 > &Apriori_cov)
This solves the least squares problem starting at the initial guess.
boost::shared_ptr< ConnorSolverState > state() const
Save state to a ConnorSolverState object.
const blitz::Array< double, 1 > & sigma_ap() const
virtual blitz::Array< double, 1 > residual() const
Return residual for solution to last problem solved.
const FitStatistic & fstat() const
blitz::Array< double, 1 > x_update() const
const blitz::Array< double, 2 > & k() const
double gamma_initial
Initial value of gamma.
Class that saves the state of a ConnorSolver.
void add_observer_do(Observer< ConnorSolver > &Obs, ConnorSolver &t)
Add an observer.
Definition: observer.h:148
boost::shared_ptr< CostFunction > cost_function() const
Cost function.
const blitz::Array< double, 1 > & residual() const
blitz::Array< double, 1 > dx
This is the update to , .
boost::shared_ptr< CostFunction > cost_function_
The cost function.
void test_do_inversion(const std::string &Fname, blitz::Array< double, 1 > &Dx, blitz::Array< double, 2 > &Kt_se_m1_k)
Restore state previously saved, and run do_inversion.
blitz::Array< double, 1 > residual_
This is the residual from the model, .
blitz::Array< double, 1 > x_solution_zero_unused() const
Return the solution to the last problem solved.
int number_iteration() const
Number of iterations for the last problem solved.
virtual void remove_observer(Observer< ConnorSolver > &Obs)
Remove an observer.
Definition: connor_solver.h:79
void remove_observer_do(Observer< ConnorSolver > &Obs, ConnorSolver &t)
Remove an observer.
Definition: observer.h:173
blitz::Array< double, 1 > sigma_ap
This is the sqrt of S_a diagonal, or the sigma of the apriori.
virtual FitStatistic fit_statistic() const
Return fit results for solution to last problem solved.
This is a Mixin for classes that can be printed.
Definition: printable.h:24
ConnorSolver(const boost::shared_ptr< CostFunction > &Cf, const boost::shared_ptr< ConvergenceCheck > &Conv, double Gamma_initial=0.0, const std::string &Save_test_data="")
Constructor.
Definition: connor_solver.h:68
int number_divergent
Number of divergent steps.
blitz::Array< double, 1 > x_i
This is the current value of the state vector X value, called in the ATB.
virtual blitz::Array< double, 2 > aposteriori_covariance() const
Return a posteriori covariance matrix for last problem solved.
const blitz::Array< double, 2 > & apriori_cov_scaled() const
const blitz::Array< double, 1 > & dx() const
ConnorSolverState(const blitz::Array< double, 1 > &X_i, const blitz::Array< double, 1 > &X_a, const blitz::Array< double, 2 > &Apriori_cov_scaled, const blitz::Array< double, 2 > &Sa_m1_scaled, const blitz::Array< double, 1 > &Sigma_ap, double Gamma, double Gamma_last_step, double Gamma_intial, const blitz::Array< double, 1 > &Residual, const blitz::Array< double, 1 > &Se, const blitz::Array< double, 2 > &K, const blitz::Array< double, 2 > &Kt_se_m1_k, const blitz::Array< double, 1 > &Dx, const FitStatistic &Fstat)
blitz::Array< double, 2 > apriori_covariance() const
Apriori covariance matrix.
Mixin for a class that allows other classes to observe it state.
Definition: observer.h:12
void print(std::ostream &Os) const
const Unit K("K", 1.0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0)
double gamma
Levenberg-Marquardt parameter.
void to_stream(std::ostream &os) const
Save state of object.
blitz::Array< double, 2 > kt_se_m1_k
This is .
blitz::Array< double, 1 > x_solution_uncertainty() const
Return the uncertainty of x_solution, this is just the sqrt of the diagonal of the full aposteriori_c...
OUTCOME outcome
Flag indicating success of fit, or why fit wasn&#39;t succesful.
double gamma_last_step() const
Levenberg-Marquardt parameter for last step we processed.
virtual blitz::Array< double, 2 > aposteriori_covariance_scaled() const
Return a scaled a posteriori covariance matrix for last problem solved.
virtual void add_observer(Observer< ConnorSolver > &Obs)
Add an observer.
Definition: connor_solver.h:77
double gamma_last_step_
Stash gamma from last step we processed.
const blitz::Array< double, 1 > & x_a() const
virtual blitz::Array< double, 2 > apriori_covariance_inv_norm() const
Return normalized apriori covariance inverse matrix.
void from_stream(std::istream &is)
Restore state of object previous saved with "to_stream".
Contains classes to abstract away details in various Spurr Radiative Transfer software.
Definition: doxygen_python.h:1
static double rcond
Factor to determine if we treat a singular factor as 0.
virtual blitz::Array< double, 2 > averaging_kernel() const
Return averaging kernel for last problem solved.
int number_iteration
Number of iterations.
virtual blitz::Array< double, 1 > residual_covariance_diagonal() const
Return diagonal of covariance matrix of residual for solution to last problem solved.
const blitz::Array< double, 2 > & sa_m1_scaled() const
blitz::Array< double, 1 > x_apriori() const
Return the a priori of the last problem solved.
blitz::Array< double, 2 > k
This is the Jacobian, called in the ATB.
void do_inversion()
This does an inversion step.
const blitz::Array< double, 1 > & se() const
int number_divergent() const
Number of divergent steps for the last problem solved.
FitStatistic fstat
Results from last fit step.
boost::shared_ptr< ConvergenceCheck > convergence_check_
The convergence check object.
virtual blitz::Array< double, 2 > jacobian() const
Return the last jacobian calculated.
virtual blitz::Array< double, 1 > x_solution() const
blitz::Array< double, 1 > x_apriori_uncertainty() const
Return the uncertainty of the apriori of the last problem solved.
std::string save_test_data_
If this isn&#39;t an empty string, save to this file in the first iteration.
int outcome_flag() const
Outcome flag. This is an integer version of FitStatistic::OUTCOME.

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