primatologist-gpl  5.1.2
newton_d.h
Go to the documentation of this file.
1 /* Copyright (C) 2000-2013 CEA
2  *
3  * This software and supporting documentation were developed by
4  * bioPICSEL
5  * CEA/DSV/I²BM/MIRCen/LMN, Batiment 61,
6  * 18, route du Panorama
7  * 92265 Fontenay-aux-Roses
8  * France
9  */
10 
11 #ifndef PRIMATOLOGIST_OPTIMIZATION_NEWTON_D_H
12 #define PRIMATOLOGIST_OPTIMIZATION_NEWTON_D_H
13 
15 
16 namespace aims {
17 
18  //--------------------------------------------------------------------------
19  // CONSTRUCTOR / DESTRUCTOR / COPY
20  //--------------------------------------------------------------------------
21 
22  template <typename O, template <typename> class L>
23  Newton<O,L>::Newton( const Objective & f, const Vector & x ):
24  _f(f), _x(x)
25  {
26  setGamma();
28  setStopValue();
29  setLineSearch();
30  setMaximize();
32  setVerbose();
33  }
34 
35  template <typename O, template <typename> class L>
36  Newton<O,L>::Newton( const Newton<O,L> & other ):
37  _f(other._f),
38  _x(other._x),
39  _gamma(other._gamma),
40  _ls(other._ls),
41  _verbose(other._verbose),
42  _maximize(other._maximize),
43  _max_it(other._max_it)
44  {}
45 
46  template <typename O, template <typename> class L>
48  {}
49 
50  template <typename O, template <typename> class L>
51  Newton<O,L> &
53  {
54  if( this != &other )
55  {
56  _f = other._f;
57  _x = other._x;
58  _gamma = other._gamma;
59  _ls = other._ls;
60  _verbose = other._verbose;
61  _maximize = other._maximize;
62  _max_it = other._max_it;
63  }
64  return *this;
65  }
66 
67  //--------------------------------------------------------------------------
68  // SET PARAMETERS
69  //--------------------------------------------------------------------------
70 
71  template <typename O, template <typename> class L>
72  void Newton<O,L>::setGamma( float gamma )
73  {
74  _gamma = gamma;
75  }
76 
77  template <typename O, template <typename> class L>
78  void Newton<O,L>::setStopCriterion( const std::string & crit )
79  {
80  _crit = crit;
81  }
82 
83  template <typename O, template <typename> class L>
84  void Newton<O,L>::setStopValue( float epsilon )
85  {
86  _epsilon = epsilon;
87  }
88 
89  template <typename O, template <typename> class L>
91  {
92  _ls = ls;
93  lineSearch().setObjectiveFunction( objectiveFunction() );
94  lineSearch().setMaximize( maximize() );
95  lineSearch().setVerbose( verbose() );
96  }
97 
98  template <typename O, template <typename> class L>
99  void Newton<O,L>::setMaximize( bool maximize )
100  {
101  _maximize = maximize;
102  lineSearch().setMaximize(maximize);
103  }
104 
105  template <typename O, template <typename> class L>
107  {
108  _max_it = max;
109  }
110 
111  template <typename O, template <typename> class L>
113  {
114  _verbose = v;
115  lineSearch().setVerbose(v);
116  }
117 
118  template <typename O, template <typename> class L>
120  {
121  _f = f;
122  lineSearch().setObjectiveFunction(f);
123  }
124 
125  template <typename O, template <typename> class L>
127  {
128  _x = x;
129  }
130 
131  //--------------------------------------------------------------------------
132  // GET PARAMETERS
133  //--------------------------------------------------------------------------
134 
135  template <typename O, template <typename> class L>
136  float Newton<O,L>::gamma() const
137  {
138  return _gamma;
139  }
140 
141  template <typename O, template <typename> class L>
142  const std::string & Newton<O,L>::stopCriterion() const
143  {
144  return _crit;
145  }
146 
147  template <typename O, template <typename> class L>
149  {
150  return _epsilon;
151  }
152 
153  template <typename O, template <typename> class L>
154  typename Newton<O,L>::LineSearch &
156  {
157  return _ls;
158  }
159 
160  template <typename O, template <typename> class L>
162  {
163  return _maximize;
164  }
165 
166  template <typename O, template <typename> class L>
168  {
169  return _max_it;
170  }
171 
172  template <typename O, template <typename> class L>
174  {
175  return _verbose;
176  }
177 
178  template <typename O, template <typename> class L>
179  const typename Newton<O,L>::Vector &
181  {
182  return _x;
183  }
184 
185  template <typename O, template <typename> class L>
186  const typename Newton<O,L>::Objective &
188  {
189  return _f;
190  }
191 
192  //--------------------------------------------------------------------------
193  // EXECUTE
194  //--------------------------------------------------------------------------
195 
196  template <typename O, template <typename> class L>
198  {
199  if( verbose() > 1 )
200  {
201  std::cout << std::string(80, '-') << std::endl;
202  std::cout << std::setw(7) << "Search "
203  << std::setw(4) << "i "
204  << std::setw(15) << "f"
205  << std::setw(15) << "crit"
206  << std::endl;
207  std::cout << std::string(80, '-') << std::endl;
208  }
209 
210  int i =0;
211  bool ok;
212  Vector current_x = position();
213  Vector previous_x;
214  float current_value = objectiveFunction().value( position() );
215  float previous_value;
216  float crit_value;
217  Vector dir;
218 
219  if( verbose() > 0 )
220  {
221  std::cout << std::setw(7) << "N"
222  << std::setw(4) << i
223  << std::setw(15) << std::setprecision(5) << current_value
224  << std::setw(15) << std::setprecision(5) << ""
225  << std::endl;
226  }
227 
228  do
229  {
230  ++i;
231  previous_x = current_x;
232  previous_value = current_value;
233  dir = gamma() * math::invert( objectiveFunction().hessian( previous_x ) )
234  * objectiveFunction().derivative( previous_x );
235  if( not maximize() )
236  dir *= -1.;
237  lineSearch().setPosition( previous_x );
238  lineSearch().setSearchDirection( dir );
239  current_x = lineSearch().execute();
240  current_value = objectiveFunction().value( current_x );
241 
242  crit_value = std::abs( current_value - previous_value );
243  if( stopCriterion() == "gain" )
244  crit_value /= std::abs( previous_value );
245  ok = crit_value < stopValue();
246 
247  if( verbose() > 0 )
248  {
249  std::cout << std::setw(7) << "N"
250  << std::setw(4) << i
251  << std::setw(15) << std::setprecision(5) << current_value
252  << std::setw(15) << std::setprecision(5) << crit_value
253  << std::string( ok ? " OK" : " C" )
254  << std::endl;
255  }
256 
257  } while( not ok and ( maxIterations() < 0 or i < maxIterations() ) );
258 
259  if( maxIterations() >= 0 and i >= maxIterations() )
260  std::cout << "Maximum number of iterations reached." << std::endl;
261 
262  return current_x;
263  }
264 
265 
266 
267 } // namespace aims
268 
269 #endif // PRIMATOLOGIST_OPTIMIZATION_NEWTON_D_H
Newton's Method for optimization.
Definition: newton_decl.h:32
float gamma() const
Definition: newton_d.h:136
void setStopCriterion(const std::string &crit="absdiff")
Can take values:
Definition: newton_d.h:78
float stopValue() const
Definition: newton_d.h:148
Objective _f
objectiveFunction
Definition: newton_decl.h:101
int _max_it
maximum number of iterations
Definition: newton_decl.h:105
int _verbose
verbosity level
Definition: newton_decl.h:103
int maxIterations() const
Definition: newton_d.h:167
L< O > LineSearch
Definition: newton_decl.h:38
void setMaximize(bool maximize=false)
Definition: newton_d.h:99
int verbose() const
Definition: newton_d.h:173
Newton(const Objective &f=Objective(), const Vector &x=Vector())
Definition: newton_d.h:23
Vector execute()
Definition: newton_d.h:197
bool _maximize
maximize or minimize
Definition: newton_decl.h:104
const Objective & objectiveFunction() const
Definition: newton_d.h:187
void setLineSearch(const LineSearch &l=LineSearch())
Definition: newton_d.h:90
const Vector & position() const
Definition: newton_d.h:180
LineSearch & lineSearch()
Definition: newton_d.h:155
void setVerbose(int v=carto::verbose)
Definition: newton_d.h:112
float _gamma
Fixed Gamma or initial gamma for line search.
Definition: newton_decl.h:99
void setObjectiveFunction(const Objective &f)
Definition: newton_d.h:119
void setGamma(float gamma=0.1)
Definition: newton_d.h:72
Newton< O, L > & operator=(const Newton< O, L > &other)
Definition: newton_d.h:52
void setStopValue(float epsilon=1E-5)
Definition: newton_d.h:84
const std::string & stopCriterion() const
Definition: newton_d.h:142
Vector _x
position
Definition: newton_decl.h:100
void setMaxIterations(int n=- 1)
Definition: newton_d.h:106
bool maximize() const
Definition: newton_d.h:161
LineSearch _ls
lineSearch
Definition: newton_decl.h:102
void setPosition(const Vector &x=Vector())
Definition: newton_d.h:126
Matrix class implementing matrix operations.
Definition: matrix.h:50
MatrixBase< T > invert(const MatrixBase< T > &)
Matrix inversion.
Definition: matrix_d.h:182
float max(float x, float y)
T epsilon()
double _gamma