bpp-core  2.1.0
bpp::AbstractOptimizer Class Reference

Partial implementation of the Optimizer interface. More...

#include <Bpp/Numeric/Function/AbstractOptimizer.h>

+ Inheritance diagram for bpp::AbstractOptimizer:
+ Collaboration diagram for bpp::AbstractOptimizer:

List of all members.

Public Member Functions

 AbstractOptimizer (Function *function=0)
 AbstractOptimizer (const AbstractOptimizer &opt)
AbstractOptimizeroperator= (const AbstractOptimizer &opt)
virtual ~AbstractOptimizer ()
void updateParameters (bool yn)
 Tell if we shall update all parameters after one optimization step.
bool updateParameters () const
 Tell if we shall update all parameters after one optimization step.
void setOptimizationProgressCharacter (const std::string &c)
 Set the character to be displayed during optimization.
const std::string & getOptimizationProgressCharacter () const
Optimizerclone () const =0
 Create a copy of this object and send a pointer to it.
The Optimizer interface.
void init (const ParameterList &params) throw (Exception)
 Basic implementation.
double step () throw (Exception)
 Basic implementation.
double optimize () throw (Exception)
 Basic implementation.
bool isInitialized () const
const ParameterListgetParameters () const
double getParameterValue (const std::string &name) const
void setFunction (Function *function)
 Set the function to optimize.
const FunctiongetFunction () const
 Get the current function being optimized.
FunctiongetFunction ()
 Get the current function being optimized.
bool hasFunction () const
 Tell if a funciton is associatied to this optimizer.
double getFunctionValue () const throw (NullPointerException)
 Get the current function value.
void setMessageHandler (OutputStream *mh)
 Set the message handler for this optimizer.
OutputStreamgetMessageHandler () const
void setProfiler (OutputStream *profiler)
 Set the profiler for this optimizer.
OutputStreamgetProfiler () const
unsigned int getNumberOfEvaluations () const
 Get the number of function evaluations performed since the call of the init function.
void setStopCondition (const OptimizationStopCondition &stopCondition)
 Set the stop condition of the optimization algorithm.
OptimizationStopConditiongetStopCondition ()
 Get the stop condition of the optimization algorithm.
const OptimizationStopConditiongetStopCondition () const
 Get the stop condition of the optimization algorithm.
OptimizationStopConditiongetDefaultStopCondition ()
 Get the default stop condition of the optimization algorithm.
const OptimizationStopConditiongetDefaultStopCondition () const
 Get the default stop condition of the optimization algorithm.
bool isToleranceReached () const
 Tell if the tolerance level is reached.
bool isMaximumNumberOfEvaluationsReached () const
 Tell if the maximum number of function evaluations is reached.
void setMaximumNumberOfEvaluations (unsigned int max)
 Set the maximum number of function evaluation to perform during optimization.
void setVerbose (unsigned int v)
 Set the verbose level.
unsigned int getVerbose () const
 Get the verbose level.
void setConstraintPolicy (const std::string &constraintPolicy)
 Set the constraint policy for this optimizer.
std::string getConstraintPolicy () const
 Get the constraint policy for this optimizer.
void addOptimizationListener (OptimizationListener *listener)
 Register a listener to this class.

Protected Member Functions

virtual void doInit (const ParameterList &params)=0 throw (Exception)
 This function is called by the init() method and contains all calculations.
virtual double doStep ()=0 throw (Exception)
 This function is called by the step() method and contains all calculations.
ParameterListgetParameters_ ()
ParametergetParameter_ (size_t i)
FunctiongetFunction_ ()
void setDefaultStopCondition_ (OptimizationStopCondition *osc)
Inner utilitary functions
void autoParameter ()
 Build a list of AutoParameter instead of Parameter.
void ignoreConstraints ()
 Remove the constraints of all the arguments.
void profile (double v)
 Print to the profile if there is one.
void profile (unsigned int v)
 Print to the profile if there is one.
void profile (const std::string &s)
 Print to the profile if there is one.
void profileln (double v)
 Print to the profile if there is one and end line.
void profileln (unsigned int v)
 Print to the profile if there is one and end line.
void profileln (const std::string &s)
 Print to the profile if there is one and end line.
void printPoint (const ParameterList &params, double value)
 Print parameters and corresponding function evaluation to profiler.
void printMessage (const std::string &message)
 Give a message to print to the message handler.
void fireOptimizationInitializationPerformed (const OptimizationEvent &event)
 Notify all listeners that optimizer initialization was performed.
void fireOptimizationStepPerformed (const OptimizationEvent &event)
 Notify all listeners that an optimization step was performed.
bool listenerModifiesParameters () const

Protected Attributes

unsigned int nbEvalMax_
 The maximum number of function evaluations allowed.
unsigned int nbEval_
 The current number of function evaluations achieved.
double currentValue_
 The current value of the function.
bool tolIsReached_
 Tell if the tolerance level has been reached.

Private Attributes

Functionfunction_
 The function to optimize.
ParameterList parameters_
 The parameters that will be optimized.
OutputStreammessageHandler_
 The message handler.
OutputStreamprofiler_
 The profiler.
std::string constraintPolicy_
 The constraint policy.
OptimizationStopConditionstopCondition_
 The stoping condition to use while optimizing.
OptimizationStopConditiondefaultStopCondition_
 The default stoping condition to use while optimizing.
unsigned int verbose_
 State of the verbose mode: > 0 = enabled.
bool isInitialized_
 Check if the optimizer have been feeded with initial parameters values.
time_t startTime_
std::vector
< OptimizationListener * > 
listeners_
bool updateParameters_
std::string stepChar_

Detailed Description

Partial implementation of the Optimizer interface.

This implementation is designed for unconstrained or simple-bounded optimization. You should not use it with global contraints. It also enables the gestion of listeners by maintaining a vector of pointers toward the listener. Important note: this list of listener is not duplicated in cas of copy of the Optimizer, as listeners are expected to be bounded ot a particular instance.:if expand("%") == ""|browse confirm w|else|confirm w|endif

Definition at line 59 of file AbstractOptimizer.h.


Constructor & Destructor Documentation

Definition at line 54 of file AbstractOptimizer.cpp.

virtual bpp::AbstractOptimizer::~AbstractOptimizer ( ) [inline, virtual]

Definition at line 158 of file AbstractOptimizer.h.

References defaultStopCondition_, and stopCondition_.


Member Function Documentation

Register a listener to this class.

All registered listeners will be informed when an optimization event occur. See the documentation of the class to know what kind of events are supported.

Parameters:
listenerA listener to be registered with.

Implements bpp::Optimizer.

Definition at line 227 of file AbstractOptimizer.h.

References listeners_.

virtual void bpp::AbstractOptimizer::doInit ( const ParameterList params) throw (Exception) [protected, pure virtual]
virtual double bpp::AbstractOptimizer::doStep ( ) throw (Exception) [protected, pure virtual]

Notify all listeners that optimizer initialization was performed.

This method should be called by the init method.

Parameters:
eventAn OptimizationEvent object.

Definition at line 313 of file AbstractOptimizer.cpp.

References listeners_.

Notify all listeners that an optimization step was performed.

This method should be called by the step method.

Parameters:
eventAn OptimizationEvent object.

Definition at line 323 of file AbstractOptimizer.cpp.

References listeners_.

Referenced by step().

std::string bpp::AbstractOptimizer::getConstraintPolicy ( ) const [inline, virtual]

Get the constraint policy for this optimizer.

Returns:
The constraint policy.

Implements bpp::Optimizer.

Definition at line 226 of file AbstractOptimizer.h.

References constraintPolicy_.

Get the default stop condition of the optimization algorithm.

Returns:
The default stop condition used while optimizing.

Implements bpp::Optimizer.

Definition at line 219 of file AbstractOptimizer.h.

References defaultStopCondition_.

Get the current function being optimized.

Returns:
A const pointer toward the function being optimized.

Implements bpp::Optimizer.

Reimplemented in bpp::BfgsMultiDimensions, bpp::ConjugateGradientMultiDimensions, and bpp::NewtonOneDimension.

Definition at line 198 of file AbstractOptimizer.h.

References function_.

double bpp::AbstractOptimizer::getFunctionValue ( ) const throw (NullPointerException) [inline, virtual]

Get the current function value.

Returns:
The value of the function at the point specified by _parameters.
Exceptions:
NullPointerExceptionIf no function is associated with this optimizer.

Implements bpp::Optimizer.

Reimplemented in bpp::GoldenSectionSearch.

Definition at line 200 of file AbstractOptimizer.h.

References currentValue_, and function_.

Returns:
The stream used for handling messages, if any.

Implements bpp::Optimizer.

Definition at line 207 of file AbstractOptimizer.h.

References messageHandler_.

Referenced by bpp::PowellMultiDimensions::doStep().

unsigned int bpp::AbstractOptimizer::getNumberOfEvaluations ( ) const [inline, virtual]

Get the number of function evaluations performed since the call of the init function.

Returns:
The number of function evaluations.

Implements bpp::Optimizer.

Definition at line 211 of file AbstractOptimizer.h.

References nbEval_.

Referenced by bpp::SimpleNewtonMultiDimensions::doStep(), bpp::SimpleMultiDimensions::doStep(), bpp::OneDimensionOptimizationTools::lineMinimization(), and bpp::OneDimensionOptimizationTools::lineSearch().

const std::string& bpp::AbstractOptimizer::getOptimizationProgressCharacter ( ) const [inline]
Returns:
The character to be displayed during optimization.

Definition at line 265 of file AbstractOptimizer.h.

References stepChar_.

double bpp::AbstractOptimizer::getParameterValue ( const std::string &  name) const [inline, virtual]
Returns:
The value associated with a given parameter name.

Implements bpp::Optimizer.

Definition at line 191 of file AbstractOptimizer.h.

References bpp::ParameterList::getParameterValue(), and parameters_.

OutputStream* bpp::AbstractOptimizer::getProfiler ( ) const [inline, virtual]
Returns:
The stream used for profiling, if any.

Implements bpp::Optimizer.

Definition at line 209 of file AbstractOptimizer.h.

References profiler_.

Get the stop condition of the optimization algorithm.

Returns:
The stop condition used while optimizing.

Implements bpp::Optimizer.

Definition at line 217 of file AbstractOptimizer.h.

References stopCondition_.

bool bpp::AbstractOptimizer::hasFunction ( ) const [inline, virtual]

Tell if a funciton is associatied to this optimizer.

Returns:
True if a function has been associated to this optimizer.

Implements bpp::Optimizer.

Definition at line 199 of file AbstractOptimizer.h.

References function_.

Referenced by bpp::GoldenSectionSearch::getFunctionValue().

Remove the constraints of all the arguments.

Definition at line 303 of file AbstractOptimizer.cpp.

References parameters_, and bpp::ParameterList::size().

Referenced by AbstractOptimizer(), and operator=().

void AbstractOptimizer::init ( const ParameterList params) throw (Exception) [virtual]

Basic implementation.

Store all parameters, call the doInit method, print to profiler, initialize timer and notify all listeners.

Implements bpp::Optimizer.

Definition at line 153 of file AbstractOptimizer.cpp.

References bpp::AutoParameter::CONSTRAINTS_AUTO, and bpp::AutoParameter::CONSTRAINTS_IGNORE.

Referenced by bpp::SimpleNewtonMultiDimensions::doStep(), bpp::SimpleMultiDimensions::doStep(), bpp::OneDimensionOptimizationTools::lineMinimization(), and bpp::OneDimensionOptimizationTools::lineSearch().

bool bpp::AbstractOptimizer::isInitialized ( ) const [inline, virtual]
Returns:
'true' if this optimizer has been initialized.

Implements bpp::Optimizer.

Definition at line 189 of file AbstractOptimizer.h.

References isInitialized_.

Tell if the maximum number of function evaluations is reached.

Returns:
Whether the maximum number of function evaluations is reached or not.

Implements bpp::Optimizer.

Definition at line 221 of file AbstractOptimizer.h.

References nbEval_, and nbEvalMax_.

bool bpp::AbstractOptimizer::isToleranceReached ( ) const [inline, virtual]

Tell if the tolerance level is reached.

Returns:
Whether the tolerance is reached or not.
See also:
OptimizationStopCondition

Implements bpp::Optimizer.

Definition at line 220 of file AbstractOptimizer.h.

References tolIsReached_.

Definition at line 333 of file AbstractOptimizer.cpp.

References listeners_.

Referenced by step().

void AbstractOptimizer::printMessage ( const std::string &  message) [protected]

Give a message to print to the message handler.

Parameters:
messageThe message to print.

Definition at line 284 of file AbstractOptimizer.cpp.

References messageHandler_.

Referenced by bpp::NewtonOneDimension::doStep(), and bpp::BfgsMultiDimensions::doStep().

void AbstractOptimizer::printPoint ( const ParameterList params,
double  value 
) [protected]

Print parameters and corresponding function evaluation to profiler.

Parameters:
paramsThe parameters to print.
valueThe function evaluation.

Definition at line 265 of file AbstractOptimizer.cpp.

References nbEval_, profile(), profileln(), bpp::ParameterList::size(), startTime_, and bpp::TextTools::toString().

Referenced by bpp::PowellMultiDimensions::doStep(), and step().

void AbstractOptimizer::profile ( double  v) [protected]

Print to the profile if there is one.

Parameters:
vThe double value to print.

Definition at line 224 of file AbstractOptimizer.cpp.

References profiler_.

Referenced by printPoint().

void AbstractOptimizer::profile ( unsigned int  v) [protected]

Print to the profile if there is one.

Parameters:
vThe unsigned int value to print.

Definition at line 238 of file AbstractOptimizer.cpp.

References profiler_.

void AbstractOptimizer::profile ( const std::string &  s) [protected]

Print to the profile if there is one.

Parameters:
sThe string to print to the profile.

Definition at line 251 of file AbstractOptimizer.cpp.

References profiler_.

void AbstractOptimizer::profileln ( double  v) [protected]

Print to the profile if there is one and end line.

Parameters:
vThe double value to print.

Definition at line 231 of file AbstractOptimizer.cpp.

References profiler_.

Referenced by printPoint().

void AbstractOptimizer::profileln ( unsigned int  v) [protected]

Print to the profile if there is one and end line.

Parameters:
vThe unsigned int value to print.

Definition at line 244 of file AbstractOptimizer.cpp.

References profiler_.

void AbstractOptimizer::profileln ( const std::string &  s) [protected]

Print to the profile if there is one and end line.

Parameters:
sThe string to print to the profile.

Definition at line 258 of file AbstractOptimizer.cpp.

References profiler_.

void bpp::AbstractOptimizer::setConstraintPolicy ( const std::string &  constraintPolicy) [inline, virtual]

Set the constraint policy for this optimizer.

Parameters:
constraintPolicyThe constraint policy.

Implements bpp::Optimizer.

Definition at line 225 of file AbstractOptimizer.h.

References constraintPolicy_.

Referenced by bpp::OneDimensionOptimizationTools::lineMinimization(), and bpp::OneDimensionOptimizationTools::lineSearch().

void bpp::AbstractOptimizer::setFunction ( Function function) [inline, virtual]

Set the function to optimize.

Parameters:
functionThe function to optimize.

Implements bpp::Optimizer.

Reimplemented in bpp::MetaOptimizer, bpp::SimpleMultiDimensions, and bpp::SimpleNewtonMultiDimensions.

Definition at line 192 of file AbstractOptimizer.h.

References function_, bpp::OptimizationStopCondition::init(), and stopCondition_.

Referenced by bpp::SimpleNewtonMultiDimensions::setFunction(), and bpp::SimpleMultiDimensions::setFunction().

void bpp::AbstractOptimizer::setMaximumNumberOfEvaluations ( unsigned int  max) [inline, virtual]

Set the maximum number of function evaluation to perform during optimization.

Parameters:
maxThe maximum number of evaluations to perform.

Implements bpp::Optimizer.

Definition at line 222 of file AbstractOptimizer.h.

References nbEvalMax_.

Referenced by bpp::BrentOneDimension::BrentOneDimension(), and bpp::NewtonBacktrackOneDimension::NewtonBacktrackOneDimension().

void bpp::AbstractOptimizer::setMessageHandler ( OutputStream mh) [inline, virtual]

Set the message handler for this optimizer.

The message handler keeps all messages that the optimizer may send. The default handler is set to standard output, but you can pass any ostream object (cerr, ofstream, etc.).

A NULL pointer disables message output.

Parameters:
mhThe message handler to use.

Implements bpp::Optimizer.

Definition at line 206 of file AbstractOptimizer.h.

References messageHandler_.

Referenced by bpp::OneDimensionOptimizationTools::lineMinimization(), and bpp::OneDimensionOptimizationTools::lineSearch().

void bpp::AbstractOptimizer::setProfiler ( OutputStream profiler) [inline, virtual]

Set the profiler for this optimizer.

The profiler keeps all the intermediate values taken by the parameters. The default profiler is set to standard output, but you can pass any ostream object (cerr, ofstream, etc.).

A NULL pointer disables message output.

Parameters:
profilerThe profiler to use.

Implements bpp::Optimizer.

Definition at line 208 of file AbstractOptimizer.h.

References profiler_.

Referenced by bpp::OneDimensionOptimizationTools::lineMinimization(), and bpp::OneDimensionOptimizationTools::lineSearch().

void bpp::AbstractOptimizer::setVerbose ( unsigned int  v) [inline, virtual]

Set the verbose level.

0 = off 1 = on 2 = more verbose 3 = even more, etc.

In most cases, only the 0 and 1 levels are implemented.

Parameters:
vverbose level.

Implements bpp::Optimizer.

Definition at line 223 of file AbstractOptimizer.h.

References verbose_.

Referenced by bpp::OneDimensionOptimizationTools::lineMinimization(), and bpp::OneDimensionOptimizationTools::lineSearch().

double AbstractOptimizer::step ( ) throw (Exception) [virtual]

Basic implementation.

Check if the optimizer is initialized, check if parameters need update because of listeners, call the doStep method, print the current point to the profiler, notify all listeners and return the current value of the function.

Implements bpp::Optimizer.

Definition at line 188 of file AbstractOptimizer.cpp.

References currentValue_, doStep(), bpp::Function::f(), fireOptimizationStepPerformed(), function_, bpp::Parametrizable::getParameters(), bpp::OptimizationStopCondition::isToleranceReached(), listenerModifiesParameters(), bpp::ParameterList::matchParametersValues(), parameters_, printPoint(), stopCondition_, tolIsReached_, and updateParameters_.

Referenced by optimize().

void bpp::AbstractOptimizer::updateParameters ( bool  yn) [inline]

Tell if we shall update all parameters after one optimization step.

This is required only for functions that have non-independent parameters, which means that setting one parameter value may modify one or several other parameters. Depending on the optimizer, this may have no effect.

Parameters:
yntrue/false

Definition at line 243 of file AbstractOptimizer.h.

References updateParameters_.

Tell if we shall update all parameters after one optimization step.

This is required only for functions that have non-independent parameters, which means that setting one parameter value may modify one or several other parameters. Depending on the optimizer, this may have no effect.

Returns:
yn true/false

Definition at line 254 of file AbstractOptimizer.h.

References updateParameters_.


Member Data Documentation

The constraint policy.

Must be one the following:

  • CONSTRAINTS_KEEP: keep the constraint associated to the parameters (default).
  • CONSTRAINTS_IGNORE: remove all constraints.
  • CONSTRAINTS_AUTO: use AutoParameters to deal with constraints.
See also:
AutoParameter

Definition at line 94 of file AbstractOptimizer.h.

Referenced by AbstractOptimizer(), getConstraintPolicy(), operator=(), and setConstraintPolicy().

The function to optimize.

Definition at line 67 of file AbstractOptimizer.h.

Referenced by getFunction(), getFunction_(), getFunctionValue(), hasFunction(), operator=(), setFunction(), and step().

Check if the optimizer have been feeded with initial parameters values.

Definition at line 116 of file AbstractOptimizer.h.

Referenced by AbstractOptimizer(), isInitialized(), operator=(), and optimize().

The profiler.

Definition at line 82 of file AbstractOptimizer.h.

Referenced by getProfiler(), operator=(), profile(), profileln(), and setProfiler().

Definition at line 118 of file AbstractOptimizer.h.

Referenced by operator=(), and printPoint().

Definition at line 122 of file AbstractOptimizer.h.

Referenced by operator=(), step(), and updateParameters().

unsigned int bpp::AbstractOptimizer::verbose_ [private]

State of the verbose mode: > 0 = enabled.

This may not be used by the Optimizer.

Definition at line 111 of file AbstractOptimizer.h.

Referenced by getVerbose(), operator=(), optimize(), and setVerbose().


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Friends