Cortex  10.0.0-a4
LevenbergMarquardt.h
1 //
3 // Copyright (c) 2009-2011, Image Engine Design Inc. All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //
12 // * Redistributions in binary form must reproduce the above copyright
13 // notice, this list of conditions and the following disclaimer in the
14 // documentation and/or other materials provided with the distribution.
15 //
16 // * Neither the name of Image Engine Design nor the names of any
17 // other contributors to this software may be used to endorse or
18 // promote products derived from this software without specific prior
19 // written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
22 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23 // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
25 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 //
34 
35 
36 #ifndef IE_CORE_LEVENBERGMARQUARDT_H
37 #define IE_CORE_LEVENBERGMARQUARDT_H
38 
39 #include "boost/static_assert.hpp"
40 
41 #include "IECore/TypeTraits.h"
42 #include "IECore/VectorTypedData.h"
43 
44 namespace IECore
45 {
46 
47 template<typename T>
48 struct DefaultLevenbergMarquardtTraits;
49 
87 template<typename T, typename ErrorFn, template<typename> class Traits = DefaultLevenbergMarquardtTraits >
88 class LevenbergMarquardt : public boost::noncopyable
89 {
90  BOOST_STATIC_ASSERT( boost::is_floating_point<T>::value );
91 
92  public:
93 
94  typedef T ValueType;
95  typedef ErrorFn ErrorFunctionType;
96  typedef Traits<T> TraitsType;
97 
98  enum Status
99  {
100  Success,
101  Degenerate,
102  CallLimit, // Maximum number of function calls has been reached.
103  FailedFTol, // FTol is too small. No further improvement in solution available.
104  FailedXTol, // XTol is too small. No further improvement in solution available.
105  FailedGTol, // GTol is too small. No further improvement in solution available.
106  };
107 
109 
110  void setParameters(
111  T ftol = Traits<T>::tolerance(),
112  T xtol = Traits<T>::tolerance(),
113  T gtol = Traits<T>::tolerance(),
114  T epsilon = Traits<T>::machinePrecision(),
115  T stepBound = T(100)
116  );
117 
118  void getParameters(
119  T &ftol,
120  T &xtol,
121  T &gtol,
122  T &epsilon,
123  T &stepBound
124  ) const;
125 
126  void setMaxCalls( unsigned maxCalls );
127  unsigned getMaxCalls() const;
128 
130  Status solve( typename TypedData< std::vector<T> >::Ptr parameters, ErrorFn &fn );
131 
132  protected :
133 
134  T m_ftol;
135  T m_xtol;
136  T m_gtol;
137  T m_epsilon;
138  T m_stepBound;
139 
140  unsigned int m_maxCalls;
141  unsigned int m_numCalls;
142 
143  unsigned int m_m;
144  unsigned int m_n;
145 
146  private :
147 
148  inline T sqr( const T &x ) const
149  {
150  return x * x;
151  }
152 
153  void qrFactorize();
154  T computeLMParameter( std::vector<T> &x, std::vector<T> &sdiag, T delta );
155 
156  void qrSolve( std::vector<T> &r, std::vector<T> &diag,
157  std::vector<T> &qtb, std::vector<T> &x, std::vector<T> &sdiag );
158 
160  T euclideanNorm( typename std::vector<T>::const_iterator begin, typename std::vector<T>::const_iterator end ) const;
161 
162  typename TypedData< std::vector<T> >::Ptr m_fvec;
163  std::vector<T> m_diag;
164  std::vector<T> m_qtf;
165  std::vector<T> m_fjac;
166  std::vector<T> m_wa1;
167  typename TypedData< std::vector<T> >::Ptr m_wa2;
168  std::vector<T> m_wa3;
169  typename TypedData< std::vector<T> >::Ptr m_wa4;
170  std::vector<int> m_ipvt;
171 };
172 
173 } // namespace IECore
174 
175 #include "IECore/LevenbergMarquardt.inl"
176 
177 #endif // IE_CORE_LEVENBERGMARQUARDT_H
Status solve(typename TypedData< std::vector< T > >::Ptr parameters, ErrorFn &fn)
Updates parameters in place. Returns true on success.
Definition: TypedData.h:64
Definition: LevenbergMarquardt.h:88
This namespace contains all components of the core library.
Definition: AddSmoothSkinningInfluencesOp.h:43