35 #ifndef AIMS_OPTIMIZATION_POWELL_H 36 #define AIMS_OPTIMIZATION_POWELL_H 49 #define SIGN( a, b ) ( (b) >= 0.0 ? fabs( a ) : -fabs( a ) ) 51 #define TOL 2.0e-4 // tolerance passed to brent 53 #define CGOLD 0.3819660 54 #define SQR( a ) ( ( a ) * ( a ) ) 60 template <
class T,
int D >
78 void mnbrak( T& ax, T& bx, T& cx, T& fa, T& fb, T& fc );
79 T brent( T ax, T bx, T cx, T& fb, T& xmin );
83 std::list< T > _costs;
91 template <
class T,
int D >
103 template <
class T,
int D >
113 template <
class T,
int D >
127 for (
int i = 0; i < D; i++ )
128 for (
int j = 0; j < D; j++ )
129 _xi[ i ][ j ] = ( i == j ? deltaP[ i ] : ( T )0 );
131 _fret = this->
_func.eval( p );
134 this->
_func.fillMonitoredParameters( monitoredParams ) ;
135 monitoredParams.setProperty(
"info", std::string(
"first evaluation") ) ;
136 this->
_probe->test( p, monitoredParams, &_fret) ;
139 for (
int iter = 0; ; iter++ )
145 for (
int i = 0; i < D; i++ )
151 if ( ( T )fabs( fptt - _fret ) > del )
154 del = ( T )fabs( fptt - _fret );
161 this->
_func.fillMonitoredParameters( monitoredParams ) ;
162 monitoredParams.setProperty(
"info", std::string(
"iteration step") ) ;
163 this->
_probe->iteration( p, monitoredParams, &_fret) ;
166 if ( 2.0 * fabs( fp - _fret ) <= this->
_error * ( fabs( fp ) + fabs( _fret ) ) )
175 std::cerr <<
"Reached maximal iteration" << std::endl ;
180 ptt = p * ( T )2 - pt;
184 fptt = this->
_func.eval( ptt );
185 this->
_func.fillMonitoredParameters( monitoredParams ) ;
186 monitoredParams.setProperty(
"info",
187 std::string(
"conjugate directions investigation") );
188 this->
_probe->test( ptt, monitoredParams, &_fret) ;
191 t = ( T )( 2.0 * ( fp - 2.0 * _fret + fptt ) *
SQR( fp - _fret - del )
192 - del *
SQR( fp - fptt ) );
196 _fret = this->
_func.eval( p );
197 this->
_func.fillMonitoredParameters( monitoredParams ) ;
198 monitoredParams.setProperty(
"info",
199 std::string(
"conjugate directions computing") ) ;
200 this->
_probe->test( p, monitoredParams, &_fret) ;
202 _xi[ ibig ] = _xi[ D - 1 ];
208 _costs.push_back( _fret );
218 template <
class T,
int D >
229 template <
class T,
int D >
239 template <
class T,
int D >
249 template <
class T,
int D >
254 T xx, xmin, fx, fb, fa, bx, ax;
261 mnbrak( ax, xx, bx, fa, fx, fb );
262 _fret = brent( ax, xx, bx, fx, xmin );
269 template <
class T,
int D >
274 T ulim, u, r, q, fu, tmp;
276 fb = this->
_func.eval( _pcom + _xicom * bx );
279 this->
_func.fillMonitoredParameters( monitoredParams ) ;
280 monitoredParams.setProperty(
"info", std::string(
"mnbrak_bx") ) ;
281 this->
_probe->test( _pcom + _xicom * bx, monitoredParams, &fb) ;
294 cx = bx + ( T )
GOLD * ( bx - ax );
295 fc = this->
_func.eval( _pcom + _xicom * cx );
297 this->
_func.fillMonitoredParameters( monitoredParams ) ;
298 monitoredParams.setProperty(
"info", std::string(
"mnbrak_cx") ) ;
299 this->
_probe->test( _pcom + _xicom * cx, monitoredParams, &fc) ;
305 r = ( bx - ax ) * ( fb - fc );
306 q = ( bx - cx ) * ( fb - fa );
307 u = bx - ( ( bx - cx ) * q - ( bx - ax ) * r ) /
310 ulim = bx + ( T )
GLIMIT * ( cx - bx );
311 if ( ( bx - u ) * ( u - cx ) > ( T )0 )
314 fu = this->
_func.eval( _pcom + _xicom * u );
316 this->
_func.fillMonitoredParameters( monitoredParams ) ;
317 monitoredParams.setProperty(
"info", std::string(
"mnbrak_u") ) ;
318 this->
_probe->test( _pcom + _xicom * u, monitoredParams, &fu) ;
338 u = cx + ( T )
GOLD * ( cx - bx );
339 fu = this->
_func.eval( _pcom + _xicom * u );
341 this->
_func.fillMonitoredParameters( monitoredParams ) ;
342 monitoredParams.setProperty(
"info", std::string(
"mnbrak_u") ) ;
343 this->
_probe->test( _pcom + _xicom * u, monitoredParams, &fu) ;
346 else if ( ( cx - u ) * ( u - ulim ) > ( T )0 )
349 fu = this->
_func.eval( _pcom + _xicom * u );
351 this->
_func.fillMonitoredParameters( monitoredParams ) ;
352 monitoredParams.setProperty(
"info", std::string(
"mnbrak_u") ) ;
353 this->
_probe->test( _pcom + _xicom * u, monitoredParams, &fu) ;
360 u = cx + ( T )
GOLD * ( cx - bx );
363 fu = this->
_func.eval( _pcom + _xicom * u );
368 else if ( ( u - ulim ) * ( ulim - cx ) >= ( T )0 )
372 fu = this->
_func.eval( _pcom + _xicom * u );
374 this->
_func.fillMonitoredParameters( monitoredParams ) ;
375 monitoredParams.setProperty(
"info", std::string(
"mnbrak_u") ) ;
376 this->
_probe->test( _pcom + _xicom * u, monitoredParams, &fu) ;
382 u = cx + ( T )
GOLD * ( cx - bx );
383 fu = this->
_func.eval( _pcom + _xicom * u );
385 this->
_func.fillMonitoredParameters( monitoredParams ) ;
386 monitoredParams.setProperty(
"info", std::string(
"mnbrak_u") ) ;
387 this->
_probe->test( _pcom + _xicom * u, monitoredParams, &fu) ;
403 template <
class T,
int D >
408 T a, b, d, etemp, fu, fv, fw, fx, p, q, r, tol1, tol2, u, v, w, x, xm, e;
412 a = ( ax < cx ? ax : cx );
413 b = ( ax > cx ? ax : cx );
416 for (
int iter = 0; iter <
ITMAX; iter++ )
419 xm = ( T )( 0.5 * ( a + b ) );
420 tol1 = ( T )(
TOL * fabs( x ) +
ZEPS );
421 tol2 = ( T )2 * tol1;
422 if ( fabs( x - xm ) <= tol2 - 0.5 * ( b - a ) )
429 if ( fabs( e ) > tol1 )
432 r = ( x - w ) * ( fx - fv );
433 q = ( x - v ) * ( fx - fw );
434 p = ( x - v ) * q - ( x - w ) * r;
435 q = ( T )( 2.0 * ( q - r ) );
441 if ( fabs( p ) >= fabs( 0.5 * q * etemp ) ||
442 p <= q * ( a - x ) || p >= q * ( b - x ) )
445 e = ( x >= xm ? a - x : b - x );
454 if ( u - a < tol2 || b - u < tol2 )
455 d =
SIGN( tol1, xm - x );
463 e = ( x >= xm ? a - x : b - x );
467 u = ( T )( fabs( d ) >= tol1 ? x + d : x +
SIGN( tol1, d ) );
468 fu = this->
_func.eval( _pcom + _xicom * u );
471 this->
_func.fillMonitoredParameters( monitoredParams ) ;
472 monitoredParams.setProperty(
"info",
473 std::string(
"mnbrak_bx") ) ;
474 this->
_probe->test( _pcom + _xicom * u, monitoredParams, &fu) ;
498 if ( fu <= fw || w == x )
507 else if ( fu <= fv || v == x || v == w )
518 std::cerr <<
"brent: too many iterations" << std::endl;
float max(float x, float y)
const std::list< T > & getCostEvolution() const
OptimizerProbe< T, D > * _probe
AimsVector< T, D > doit(const AimsVector< T, D > &pinit, const AimsVector< T, D > &deltaP)
PowellOptimizer(const ObjectiveFunc< T, D > &func, T error)
const T & getOptCost() const
virtual ~PowellOptimizer()
const AimsVector< T, D > & getDirection(int which) const
const ObjectiveFunc< T, D > & _func