41 #include "../AutoParameter.h" 42 #include "../../Text/TextTools.h" 43 #include "../../App/ApplicationTools.h" 54 AbstractOptimizer::AbstractOptimizer(
Function*
function):
60 stopCondition_(0), defaultStopCondition_(0),
61 verbose_(true), isInitialized_(false), startTime_(), listeners_(),
62 updateParameters_(false), stepChar_(
"*"),
63 nbEvalMax_(1000000), nbEval_(0),
64 currentValue_(0), tolIsReached_(false)
71 function_(opt.function_),
72 parameters_(opt.parameters_),
73 messageHandler_(opt.messageHandler_),
74 profiler_(opt.profiler_),
75 constraintPolicy_(opt.constraintPolicy_),
76 stopCondition_(0), defaultStopCondition_(0),
77 verbose_(opt.verbose_),
78 isInitialized_(opt.isInitialized_),
79 startTime_(opt.startTime_),
81 updateParameters_(opt.updateParameters_),
82 stepChar_(opt.stepChar_),
83 nbEvalMax_(opt.nbEvalMax_),
85 currentValue_(opt.currentValue_),
86 tolIsReached_(opt.tolIsReached_)
155 if (!function_)
throw Exception(
"AbstractOptimizer::init. Optimizer currently has no function.");
157 parameters_ = params;
165 tolIsReached_ =
false;
166 isInitialized_ =
true;
168 currentValue_ = function_->getValue();
171 for (
unsigned int i = 0; i < parameters_.size(); i++)
173 profile(parameters_[i].getName() +
"\t");
175 profileln(
"Function\tTime");
179 printPoint(parameters_, currentValue_);
182 stopCondition_->init();
211 throw Exception(
"AbstractOptimizer::optimize. Optimizer not initialized: call the 'init' method first!");
267 size_t ndim = params.
size();
270 for (
size_t j = 0; j < ndim; j++)
315 for (
unsigned int i = 0; i <
listeners_.size(); i++)
317 listeners_[i]->optimizationInitializationPerformed(event);
325 for (
unsigned int i = 0; i <
listeners_.size(); i++)
327 listeners_[i]->optimizationStepPerformed(event);
335 for (
unsigned int i = 0; i <
listeners_.size(); i++)
bool isInitialized_
Check if the optimizer have been feeded with initial parameters values.
static std::string CONSTRAINTS_IGNORE
OptimizationStopCondition * clone() const =0
Create a copy of this object and send a pointer to it.
AbstractOptimizer & operator=(const AbstractOptimizer &opt)
OptimizationStopCondition * defaultStopCondition_
The default stoping condition to use while optimizing.
std::vector< OptimizationListener * > listeners_
virtual double f(const ParameterList ¶meters)
Get the value of the function according to a given set of parameters.
This class allows to perform a correspondence analysis.
double optimize()
Basic implementation.
virtual bool matchParametersValues(const ParameterList ¶ms, std::vector< size_t > *updatedParameters=0)
Update the parameters from params.
Function * function_
The function to optimize.
This is the function abstract class.
Interface for otimization stop condition objet.
void profileln(double v)
Print to the profile if there is one and end line.
virtual const ParameterList & getParameters() const =0
Get all parameters available.
virtual bool isToleranceReached() const =0
Tell if the we reached the desired tolerance with a given new set of estimates.
void ignoreConstraints()
Remove the constraints of all the arguments.
static std::string toString(T t)
General template method to convert to a string.
The parameter list object.
bool tolIsReached_
Tell if the tolerance level has been reached.
OutputStream * profiler_
The profiler.
virtual void setMessageHandler(OutputStream *mh)
Set the message handler for this AutoParameter.
virtual void setOptimizer(const Optimizer *optimizer)=0
Set the optimizer attached to this instance.
double step()
Basic implementation.
bool listenerModifiesParameters() const
unsigned int verbose_
State of the verbose mode: > 0 = enabled.
virtual void setParameter(size_t index, const Parameter ¶m)
Change given parameter.
void printPoint(const ParameterList ¶ms, double value)
Print parameters and corresponding function evaluation to profiler.
An event object which indicates that an optimization event occured.
OptimizationStopCondition * stopCondition_
The stoping condition to use while optimizing.
static std::string CONSTRAINTS_AUTO
unsigned int nbEvalMax_
The maximum number of function evaluations allowed.
unsigned int nbEval_
The current number of function evaluations achieved.
Partial implementation of the Optimizer interface.
void fireOptimizationInitializationPerformed(const OptimizationEvent &event)
Notify all listeners that optimizer initialization was performed.
double currentValue_
The current value of the function.
std::string constraintPolicy_
The constraint policy.
AbstractOptimizer(Function *function=0)
virtual double doStep()=0
This function is called by the step() method and contains all calculations.
OutputStream * messageHandler_
The message handler.
void profile(double v)
Print to the profile if there is one.
void fireOptimizationStepPerformed(const OptimizationEvent &event)
Notify all listeners that an optimization step was performed.
void printMessage(const std::string &message)
Give a message to print to the message handler.
ParameterList parameters_
The parameters that will be optimized.
void autoParameter()
Build a list of AutoParameter instead of Parameter.
void init(const ParameterList ¶ms)
Basic implementation.