primatologist-gpl 6.0.4
gradient_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_GRADIENT_D_H
12#define PRIMATOLOGIST_OPTIMIZATION_GRADIENT_D_H
13
15
16namespace aims {
17
18 //--------------------------------------------------------------------------
19 // CONSTRUCTOR / DESTRUCTOR / COPY
20 //--------------------------------------------------------------------------
21
22 template <typename O, template <typename> class L>
24 const Vector & x ):
25 _f(f), _x(x)
26 {
27 setGamma();
33 setVerbose();
34 }
35
36 template <typename O, template <typename> class L>
38 _f(other._f),
39 _x(other._x),
40 _gamma(other._gamma),
41 _ls(other._ls),
42 _verbose(other._verbose),
43 _maximize(other._maximize),
44 _max_it(other._max_it)
45 {}
46
47 template <typename O, template <typename> class L>
50
51 template <typename O, template <typename> class L>
54 {
55 if( this != &other )
56 {
57 _f = other._f;
58 _x = other._x;
59 _gamma = other._gamma;
60 _ls = other._ls;
61 _verbose = other._verbose;
62 _maximize = other._maximize;
63 _max_it = other._max_it;
64 }
65 return *this;
66 }
67
68 //--------------------------------------------------------------------------
69 // SET PARAMETERS
70 //--------------------------------------------------------------------------
71
72 template <typename O, template <typename> class L>
74 {
75 _gamma = gamma;
76 }
77
78 template <typename O, template <typename> class L>
79 void GradientDescent<O,L>::setStopCriterion( const std::string & crit )
80 {
81 _crit = crit;
82 }
83
84 template <typename O, template <typename> class L>
86 {
87 _epsilon = epsilon;
88 }
89
90 template <typename O, template <typename> class L>
92 {
93 _ls = ls;
94 lineSearch().setObjectiveFunction( objectiveFunction() );
95 lineSearch().setMaximize( maximize() );
96 lineSearch().setVerbose( verbose() );
97 }
98
99 template <typename O, template <typename> class L>
101 {
103 lineSearch().setMaximize(maximize);
104 }
105
106 template <typename O, template <typename> class L>
108 {
109 _max_it = max;
110 }
111
112 template <typename O, template <typename> class L>
114 {
115 _verbose = v;
116 lineSearch().setVerbose(v);
117 }
118
119 template <typename O, template <typename> class L>
121 {
122 _f = f;
123 lineSearch().setObjectiveFunction(f);
124 }
125
126 template <typename O, template <typename> class L>
128 {
129 _x = x;
130 }
131
132 //--------------------------------------------------------------------------
133 // GET PARAMETERS
134 //--------------------------------------------------------------------------
135
136 template <typename O, template <typename> class L>
138 {
139 return _gamma;
140 }
141
142 template <typename O, template <typename> class L>
143 const std::string & GradientDescent<O,L>::stopCriterion() const
144 {
145 return _crit;
146 }
147
148 template <typename O, template <typename> class L>
150 {
151 return _epsilon;
152 }
153
154 template <typename O, template <typename> class L>
157 {
158 return _ls;
159 }
160
161 template <typename O, template <typename> class L>
163 {
164 return _maximize;
165 }
166
167 template <typename O, template <typename> class L>
169 {
170 return _max_it;
171 }
172
173 template <typename O, template <typename> class L>
175 {
176 return _verbose;
177 }
178
179 template <typename O, template <typename> class L>
180 const typename GradientDescent<O,L>::Vector &
182 {
183 return _x;
184 }
185
186 template <typename O, template <typename> class L>
187 const typename GradientDescent<O,L>::Objective &
189 {
190 return _f;
191 }
192
193 //--------------------------------------------------------------------------
194 // EXECUTE
195 //--------------------------------------------------------------------------
196
197 template <typename O, template <typename> class L>
199 {
200 if( verbose() > 1 )
201 {
202 std::cout << std::string(80, '-') << std::endl;
203 std::cout << std::setw(7) << "Search "
204 << std::setw(4) << "i "
205 << std::setw(15) << "f"
206 << std::setw(15) << "crit"
207 << std::endl;
208 std::cout << std::string(80, '-') << std::endl;
209 }
210
211 int i =0;
212 bool ok;
213 Vector current_x = position();
214 Vector previous_x;
215 float current_value = objectiveFunction().value( position() );
216 float previous_value;
217 float crit_value;
218 Vector dir;
219
220 if( verbose() > 0 )
221 {
222 std::cout << std::setw(7) << "G"
223 << std::setw(4) << i
224 << std::setw(15) << std::setprecision(5) << current_value
225 << std::setw(15) << std::setprecision(5) << ""
226 << std::endl;
227 }
228
229 do
230 {
231 ++i;
232 previous_x = current_x;
233 previous_value = current_value;
234 dir = gamma() * 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) << "G"
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_GRADIENT_D_H
LineSearch & lineSearch()
Definition gradient_d.h:156
const Vector & position() const
Definition gradient_d.h:181
void setVerbose(int v=carto::verbose)
Definition gradient_d.h:113
LineSearch _ls
lineSearch
float stopValue() const
Definition gradient_d.h:149
void setObjectiveFunction(const Objective &f)
Definition gradient_d.h:120
int maxIterations() const
Definition gradient_d.h:168
void setMaxIterations(int n=- 1)
Definition gradient_d.h:107
float _epsilon
stop value
void setStopValue(float epsilon=1E-5)
Definition gradient_d.h:85
std::string _crit
stop criterion
GradientDescent< O, L > & operator=(const GradientDescent< O, L > &other)
Definition gradient_d.h:53
int _verbose
verbosity level
void setLineSearch(const LineSearch &l=LineSearch())
Definition gradient_d.h:91
void setMaximize(bool maximize=false)
Definition gradient_d.h:100
const std::string & stopCriterion() const
Definition gradient_d.h:143
void setGamma(float gamma=0.1)
Definition gradient_d.h:73
bool maximize() const
Definition gradient_d.h:162
Objective _f
objectiveFunction
void setPosition(const Vector &x=Vector())
Definition gradient_d.h:127
float _gamma
Fixed Gamma or initial gamma for line search.
GradientDescent(const Objective &f=Objective(), const Vector &x=Vector())
Definition gradient_d.h:23
bool _maximize
maximize or minimize
float gamma() const
Definition gradient_d.h:137
int _max_it
maximum number of iterations
void setStopCriterion(const std::string &crit="absdiff")
Can take values:
Definition gradient_d.h:79
const Objective & objectiveFunction() const
Definition gradient_d.h:188