11 #ifndef PRIMATOLOGIST_OPTIMIZATION_WOLFE_D_H
12 #define PRIMATOLOGIST_OPTIMIZATION_WOLFE_D_H
49 _strong(other._strong),
50 _maximize(other._maximize),
51 _max_it(other._max_it),
52 _verbose(other._verbose),
53 _previous_value(other._previous_value),
54 _previous_scalarprod(other._previous_scalarprod),
55 _current_value(other._current_value)
105 template <
typename O>
111 template <
typename O>
117 template <
typename O>
123 template <
typename O>
129 template <
typename O>
132 _maximize = maximize;
135 template <
typename O>
145 template <
typename O>
151 template <
typename O>
157 template <
typename O>
164 template <
typename O>
171 template <
typename O>
178 template <
typename O>
184 template <
typename O>
190 template <
typename O>
196 template <
typename O>
206 template <
typename O>
212 std::cout << std::string(80,
'-') << std::endl;
213 std::cout << std::setw(7) <<
"Search "
214 << std::setw(4) <<
"i "
215 << std::setw(15) <<
"f"
216 << std::setw(15) <<
"a"
218 std::cout << std::string(80,
'-') << std::endl;
223 std::cout << std::setw(7) <<
"W"
225 << std::setw(15) << std::setprecision(5) << _previous_value
226 << std::setw(15) << std::setprecision(5) << 0.;
229 if( !checkDirection() )
234 << std::string( maximize() ?
"an ascent" :
"a descent" )
235 << std::string(
" direction." ) << std::endl;
239 else if( verbose() > 0 )
240 std::cout << std::endl;
245 float a_min = 0., a_max;
246 bool a_max_set =
false;
250 _current_value = objectiveFunction().value( _x + a * _p );
251 if( ( maximize() &&_current_value <= _previous_value ) ||
252 _current_value >= _previous_value )
261 std::cout << std::setw(7) <<
"W"
263 << std::setw(15) << std::setprecision(5) << _current_value
264 << std::setw(15) << std::setprecision(5) << a
270 a = ( a_max - a_min ) / 2. + a_min;
272 else if( not wolfe1(a) or not wolfe2(a) )
276 std::cout << std::setw(7) <<
"W"
278 << std::setw(15) << std::setprecision(5) << _current_value
279 << std::setw(15) << std::setprecision(5) << a
286 a = ( a_max - a_min ) / 2. + a_min;
294 std::cout << std::setw(7) <<
"W"
296 << std::setw(15) << std::setprecision(5) << _current_value
297 << std::setw(15) << std::setprecision(5) << a
304 }
while( not ok and ( maxIterations() < 0 or i < maxIterations() ) );
306 if( verbose() > 0 and maxIterations() > 0 and i >= maxIterations() )
307 std::cout <<
"Maximum number of iterations reached." << std::endl;
316 template <
typename O>
319 _previous_value = objectiveFunction().value( _x );
320 _previous_scalarprod = (
math::transpose( _p ) * objectiveFunction().derivative( _x )).
at(0);
322 return _previous_scalarprod > 0;
324 return _previous_scalarprod < 0;
327 template <
typename O>
330 float righthand = _previous_value + _c1 * a * _previous_scalarprod;
332 return _current_value >= righthand;
334 return _current_value <= righthand;
337 template <
typename O>
340 float current_scalarprod = (
math::transpose(_p) * objectiveFunction().derivative( _x + a * _p )).
at(0);
341 float righthand = _c2 * _previous_scalarprod;
343 return current_scalarprod <= righthand;
345 return current_scalarprod >= righthand;
348 template <
typename O>
351 float current_scalarprod = (
math::transpose(_p) * objectiveFunction().derivative( _x + a * _p )).
at(0);
352 float righthand = _c2 * _previous_scalarprod;
353 return std::abs(current_scalarprod) <= std::abs(righthand);
356 template <
typename O>
Once an ascent direction is given, this class performs a line search that respects wolfe conditions.
WolfeLineSearch(const Objective &f=Objective(), const Vector &x=Vector(), const Vector &p=Vector())
void setMaxIterations(int n=- 1)
WolfeLineSearch< O > & operator=(const WolfeLineSearch< O > &other)
Objective _f
objectiveFunction
bool wolfe1(float a) const
Armijo (= Wolfe 1st) condition.
void setSearchDirection(const Vector &p=Vector())
void setObjectiveFunction(const Objective &f)
void setMaximize(bool maximize=false)
bool wolfe2w(float a) const
weak 2nd Wolfe condition
float _previous_value
f(x)
bool checkDirection()
Compute f(x) and <p, g(x)>.
const Objective & objectiveFunction() const
void setStrongWolfe(bool strong=false)
void setPosition(const Vector &x=Vector())
float _previous_scalarprod
<p, g(x)>
int maxIterations() const
void setC1(float c1=1E-4)
bool _maximize
maximize or minimize
int _max_it
maximum number of iterations
bool wolfe2s(float a) const
strong 2nd Wolfe condition
int _verbose
verbosity level
void setVerbose(int v=carto::verbose)
const Vector & searchDirection() const
const Vector & position() const
bool wolfe2(float a) const
select condition based on _strong
float _current_value
f(x + a.p)
Matrix class implementing matrix operations.
const T & at(const carto::VolumeRef< T > &vol, long px, long py, long pz, long pt, const Point4dl &fullsize, const Point4dl &binf)
MatrixBase< T > transpose(const MatrixBase< T > &)
Matrix transposition.