bpp-core  2.1.0
AbstractOptimizer.h
Go to the documentation of this file.
00001 //
00002 // File: AbstractOptimizer.h
00003 // Created by: Julien Dutheil
00004 // Created on: Mon Dec 22 12:18:09 2003
00005 //
00006 
00007 /*
00008 Copyright or © or Copr. Bio++ Development Team, (November 17, 2004)
00009 
00010 This software is a computer program whose purpose is to provide classes
00011 for numerical calculus.
00012 
00013 This software is governed by the CeCILL  license under French law and
00014 abiding by the rules of distribution of free software.  You can  use, 
00015 modify and/ or redistribute the software under the terms of the CeCILL
00016 license as circulated by CEA, CNRS and INRIA at the following URL
00017 "http://www.cecill.info". 
00018 
00019 As a counterpart to the access to the source code and  rights to copy,
00020 modify and redistribute granted by the license, users are provided only
00021 with a limited warranty  and the software's author,  the holder of the
00022 economic rights,  and the successive licensors  have only  limited
00023 liability. 
00024 
00025 In this respect, the user's attention is drawn to the risks associated
00026 with loading,  using,  modifying and/or developing or reproducing the
00027 software by the user in light of its specific status of free software,
00028 that may mean  that it is complicated to manipulate,  and  that  also
00029 therefore means  that it is reserved for developers  and  experienced
00030 professionals having in-depth computer knowledge. Users are therefore
00031 encouraged to load and test the software's suitability as regards their
00032 requirements in conditions enabling the security of their systems and/or 
00033 data to be ensured and,  more generally, to use and operate it in the 
00034 same conditions as regards security. 
00035 
00036 The fact that you are presently reading this means that you have had
00037 knowledge of the CeCILL license and that you accept its terms.
00038 */
00039 
00040 #ifndef _ABSTRACTOPTIMIZER_H_
00041 #define _ABSTRACTOPTIMIZER_H_
00042 
00043 #include "Optimizer.h"
00044 
00045 namespace bpp
00046 {
00047 
00059 class AbstractOptimizer:
00060   public virtual Optimizer
00061 {
00062   private:
00063     
00067     Function* function_;
00068   
00072     ParameterList parameters_;
00073   
00077     OutputStream* messageHandler_;
00078   
00082     OutputStream* profiler_;
00083     
00094     std::string constraintPolicy_;
00095     
00099     OptimizationStopCondition* stopCondition_;
00100     
00104     OptimizationStopCondition* defaultStopCondition_;
00105 
00111     unsigned int verbose_;
00112 
00116     bool isInitialized_;
00117 
00118     time_t startTime_;
00119 
00120     std::vector<OptimizationListener*> listeners_;
00121 
00122     bool updateParameters_;
00123 
00124     std::string stepChar_;
00125 
00126   protected:
00127 
00131     unsigned int nbEvalMax_;
00132     
00136     unsigned int nbEval_;
00137 
00141     double currentValue_;
00142 
00149     bool tolIsReached_;
00150 
00151   public:
00152     AbstractOptimizer(Function* function = 0);
00153 
00154     AbstractOptimizer(const AbstractOptimizer& opt);
00155     
00156     AbstractOptimizer& operator=(const AbstractOptimizer& opt);
00157 
00158     virtual ~AbstractOptimizer()
00159     {
00160       delete stopCondition_;
00161       delete defaultStopCondition_;
00162     }
00163   
00164   public:
00165     
00176     void init(const ParameterList& params) throw (Exception);
00182     double step() throw (Exception);
00188     double optimize() throw (Exception);
00189     bool isInitialized() const { return isInitialized_; }
00190     const ParameterList& getParameters() const { return parameters_; }
00191   double getParameterValue(const std::string& name) const { return parameters_.getParameterValue(name); }
00192     void setFunction(Function* function)
00193     { 
00194       function_ = function;
00195       if (function) stopCondition_->init();
00196     }
00197     const Function* getFunction() const { return function_; }
00198     Function* getFunction() { return function_; }
00199     bool hasFunction() const { return function_ != 0; }
00200     double getFunctionValue() const throw (NullPointerException)
00201     {
00202       if (!function_) throw NullPointerException("AbstractOptimizer::getFunctionValue. No function associated to this optimizer.");
00203       return currentValue_;
00204     }
00205     
00206     void setMessageHandler(OutputStream* mh) { messageHandler_ = mh; }
00207     OutputStream* getMessageHandler() const { return messageHandler_; }
00208     void setProfiler(OutputStream* profiler) { profiler_ = profiler; }
00209     OutputStream* getProfiler() const { return profiler_; }
00210 
00211     unsigned int getNumberOfEvaluations() const { return nbEval_; }
00212     void setStopCondition(const OptimizationStopCondition& stopCondition)
00213     {
00214       stopCondition_ = dynamic_cast<OptimizationStopCondition*>(stopCondition.clone());
00215     }
00216     OptimizationStopCondition* getStopCondition() { return stopCondition_; }
00217     const OptimizationStopCondition* getStopCondition() const { return stopCondition_; }
00218     OptimizationStopCondition* getDefaultStopCondition() { return defaultStopCondition_; }
00219     const OptimizationStopCondition* getDefaultStopCondition() const { return defaultStopCondition_; }
00220     bool isToleranceReached() const { return tolIsReached_; }
00221     bool isMaximumNumberOfEvaluationsReached() const { return nbEval_ >= nbEvalMax_; }
00222     void setMaximumNumberOfEvaluations(unsigned int max) { nbEvalMax_ = max; }
00223     void setVerbose(unsigned int v) { verbose_ = v; }
00224     unsigned int getVerbose() const { return verbose_; }
00225     void setConstraintPolicy(const std::string& constraintPolicy) { constraintPolicy_ = constraintPolicy; }
00226     std::string getConstraintPolicy() const { return constraintPolicy_; }
00227     void addOptimizationListener(OptimizationListener* listener)
00228     {
00229       if (listener)
00230         listeners_.push_back(listener);
00231     }
00243     void updateParameters(bool yn) { updateParameters_ = yn; }
00244 
00254     bool updateParameters() const { return updateParameters_; }
00255 
00261     void setOptimizationProgressCharacter(const std::string& c) { stepChar_ = c; }
00265     const std::string& getOptimizationProgressCharacter() const { return stepChar_; }
00266   
00267   protected:
00268 
00274     virtual void doInit(const ParameterList& params) throw (Exception) = 0;
00275     
00281     virtual double doStep() throw (Exception) = 0;
00282     
00292     void autoParameter();
00293   
00297     void ignoreConstraints();
00298   
00304     void profile(double v);
00305   
00311     void profile(unsigned int v);
00312  
00318     void profile(const std::string& s);
00319   
00325     void profileln(double v);
00326   
00332     void profileln(unsigned int v);
00333  
00339     void profileln(const std::string& s);
00340   
00347     void printPoint(const ParameterList& params, double value);
00348     
00354     void printMessage(const std::string& message);
00355 
00363     void fireOptimizationInitializationPerformed(const OptimizationEvent& event);
00364 
00372     void fireOptimizationStepPerformed(const OptimizationEvent& event);
00373 
00374     bool listenerModifiesParameters() const;
00377   protected:
00378     ParameterList& getParameters_() { return parameters_; }
00379     Parameter& getParameter_(size_t i) { return parameters_[i]; }
00380     Function* getFunction_() { return function_; }
00381     void setDefaultStopCondition_(OptimizationStopCondition* osc)
00382     {
00383       defaultStopCondition_ = osc;
00384     }
00385   
00386 };
00387 
00388 } //end of namespace bpp.
00389 
00390 #endif  //_ABSTRACTOPTIMIZER_H_
00391 
 All Classes Namespaces Files Functions Variables Typedefs Friends