29 double const epsilon,
size_t const maxNoImproveSteps ){
43 double const epsilon,
size_t const maxNoImproveSteps )
44 :
parameters { alpha, beta, epsilon, maxNoImproveSteps },
function( function ){}
48 if( alpha <= 0 ) alpha = std::numeric_limits<double>::epsilon();
49 if( alpha >= 0.5 ) alpha = 0.5 - std::numeric_limits<double>::epsilon();
55 if( beta <= 0 ) beta = std::numeric_limits<double>::epsilon();
56 if( beta >= 1 ) beta = 1 - std::numeric_limits<double>::epsilon();
62 if( epsilon <= 0 ) epsilon = std::numeric_limits<double>::epsilon();
63 this->epsilon = epsilon;
83 double const functionValue, gsl::vector
const gradientValue ){
86 size_t const SIZE { vector.size() };
89 gsl::vector testPoint { gsl::vector( SIZE ) };
91 auto fDeltat = [&testPoint,
this]( gsl::vector
const& x, gsl::vector
const& delta,
92 double const t )->
double {
94 testPoint.memcpy( delta );
97 return this->
function( testPoint );
101 auto gradient = [
this,&epsilon,&vector,&gradientValue]()->gsl::vector {
102 if( gradientValue.get() == nullptr ){
105 gradientEstimate( this->
function, epsilon );
109 return gradientValue;
113 gsl::blas::ddot( gradient, direction, &d );
118 functionValue :
function( vector ) };
127 size_t nonImproving { 0 };
128 bool feasibleStep {
false };
131 bool positiveStepSize {
false };
132 double f { fDeltat( vector, direction, t ) };
134 size_t iteration { 0 };
136 if( outputStream !=
nullptr ){
137 *outputStream <<
"Line search:" << std::endl;
138 *outputStream <<
"step "
139 << ( digits > 0 ?
"x " :
"") <<
"f(x)" << std::endl;
140 *outputStream <<
" " << std::setw( 4 ) << iteration <<
" ";
142 *outputStream <<
"( ";
143 for(
size_t i = 0; i < SIZE; ++i ){
144 *outputStream << std::setw( 4 + digits ) << std::setprecision( digits ) << std::fixed
145 << testPoint.get( i ) <<
" ";
147 *outputStream <<
")";
149 size_t const d { std::max( static_cast<size_t>( 6 ), digits ) };
150 *outputStream << std::setw( 4 + d ) << std::setprecision( d ) << std::fixed
153 while( f > f0 + t * delta or f != f ){
155 positiveStepSize =
true;
157 if( f < f0 ) feasibleStep =
true;
163 }
else if( f < fLast ){
171 constexpr
double std_eps { std::numeric_limits<double>::epsilon() };
172 if( t < std_eps )
break;
178 f = fDeltat( vector, direction, t );
181 *outputStream <<
" " << std::setw( 4 ) << iteration <<
" ";
183 *outputStream <<
"( ";
184 for(
size_t i { 0 }; i < SIZE; ++i ){
185 *outputStream << std::setw( 4 + digits ) << std::setprecision( digits ) << std::fixed
186 << testPoint.get( i ) <<
" ";
188 *outputStream <<
")";
190 size_t const d { std::max( static_cast<size_t>( 6 ), digits ) };
191 *outputStream << std::setw( 4 + d ) << std::setprecision( d ) << std::fixed
197 f = fDeltat( vector, direction, t );
200 if( f < f0 and (positiveStepSize or
true) ){
214 this->maxNoImproveSteps = maxNoImproveSteps;
219 return maxNoImproveSteps;
virtual void setVector(gsl::vector const &vector)
Set the vector to a new value.
void setMaxNoImproveSteps(size_t maxNoImproveSteps)
Set the value of maxNoImproveSteps.
double functionScale
Scaling factor for output of function values.
size_t getVectorDigits() const
Get the value of vectorDigits.
This class estimates a function value, gradient and Hessian at a given vector.
double getAlpha() const
Get the value of alpha.
LineSearch(ipo_function::Function &function, double const alpha=1e-3, double const beta=0.8, double const epsilon=std::sqrt(std::numeric_limits< double >::epsilon()), size_t const maxNoImproveSteps=10)
Set up the line search function object with a function and some default parameters.
virtual gsl::vector gradient() const
double alpha
A value in (0,0.5): usually quite small 0.0001–0.3 (default 1e-3 though 1e-4 is suggested in Dennis ...
Parameters & getParameters()
Get parameters by reference.
double operator()(gsl::vector &vector, gsl::vector const direction, double const functionValue=-std::numeric_limits< double >::infinity(), gsl::vector const gradientValue=gsl::vector(nullptr))
Carry out the line search.
void setVectorDigits(size_t const vectorDigits)
Set the number of significant digits of entries of vector to show in each step.
void setOutputStream(std::ostream *outputStream)
Set outputStream.
Namespace for classes that handle details of interior-point optimisation that are not ordinarily acce...
This class computes a function at a vector.
void setBeta(double beta)
Set the value of beta.
Parameters(double const alpha=1e-3, double const beta=0.8, double const epsilon=std::sqrt(std::numeric_limits< double >::epsilon()), size_t const maxNoImproveSteps=10)
Set up some default parameters.
void setEpsilon(double epsilon)
Set the value of epsilon.
void setAlpha(double alpha)
Set the value of alpha.
double getBeta() const
Get the value of beta.
std::ostream * getOutputStream() const
Get the current value of outputStream.
double getEpsilon() const
Get the value of epsilon.
size_t getMaxNoImproveSteps() const
Get the value of maxNoImproveSteps.