bpp-core  2.2.0
MetaOptimizer.h
Go to the documentation of this file.
1 //
2 // File: MetaOptimizer.h
3 // Created by: Julien Dutheil
4 // Created on: Fri Oct 12 16:05 2007
5 // From file: NewtonBrentMetaOptimizer.h
6 // Created on: Tue Nov 17 17:22 2004
7 //
8 //
9 
10 /*
11  Copyright or © or Copr. Bio++ Development Team, (November 17, 2004)
12 
13  This software is a computer program whose purpose is to provide classes
14  for phylogenetic data analysis.
15 
16  This software is governed by the CeCILL license under French law and
17  abiding by the rules of distribution of free software. You can use,
18  modify and/ or redistribute the software under the terms of the CeCILL
19  license as circulated by CEA, CNRS and INRIA at the following URL
20  "http://www.cecill.info".
21 
22  As a counterpart to the access to the source code and rights to copy,
23  modify and redistribute granted by the license, users are provided only
24  with a limited warranty and the software's author, the holder of the
25  economic rights, and the successive licensors have only limited
26  liability.
27 
28  In this respect, the user's attention is drawn to the risks associated
29  with loading, using, modifying and/or developing or reproducing the
30  software by the user in light of its specific status of free software,
31  that may mean that it is complicated to manipulate, and that also
32  therefore means that it is reserved for developers and experienced
33  professionals having in-depth computer knowledge. Users are therefore
34  encouraged to load and test the software's suitability as regards their
35  requirements in conditions enabling the security of their systems and/or
36  data to be ensured and, more generally, to use and operate it in the
37  same conditions as regards security.
38 
39  The fact that you are presently reading this means that you have had
40  knowledge of the CeCILL license and that you accept its terms.
41 */
42 
43 #ifndef METAOPTIMIZER_H__
44 #define METAOPTIMIZER_H__
45 
46 #include "AbstractOptimizer.h"
47 
48 // From the STL:
49 #include <vector>
50 
51 namespace bpp
52 {
53 
58  public virtual Clonable
59  {
60  public:
61  static std::string IT_TYPE_STEP;
62  static std::string IT_TYPE_FULL;
63 
64  private:
65  std::vector<std::string> names_;
66  std::vector<Optimizer*> optimizers_;
67  std::vector< std::vector<std::string> > parameterNames_;
68  std::vector<unsigned short> derivatives_;
69  std::vector<std::string> itTypes_;
70 
71  public:
74  names_(infos.names_),
75  optimizers_(infos.optimizers_),
78  itTypes_(infos.itTypes_)
79  {
80  for (unsigned int i = 0; i < optimizers_.size(); i++)
81  optimizers_[i] = dynamic_cast<Optimizer*>(infos.optimizers_[i]->clone());
82  }
83 
85  {
86  names_ = infos.names_;
87  optimizers_ = infos.optimizers_;
89  derivatives_ = infos.derivatives_;
90  itTypes_ = infos.itTypes_;
91  for (unsigned int i = 0; i < optimizers_.size(); i++)
92  optimizers_[i] = dynamic_cast<Optimizer *>(infos.optimizers_[i]->clone());
93  return *this;
94  }
95 
97  {
98  for(unsigned int i = 0; i < optimizers_.size(); i++)
99  delete optimizers_[i];
100  }
101 
102  public:
103 #ifndef NO_VIRTUAL_COV
104  MetaOptimizerInfos* clone() const { return new MetaOptimizerInfos(*this); }
105 #else
106  Clonable* clone() const { return new MetaOptimizerInfos(*this); }
107 #endif
108 
109  public:
119  virtual void addOptimizer(const std::string & name, Optimizer * optimizer, const std::vector<std::string> & params, unsigned short derivatives = 0, const std::string & type = IT_TYPE_STEP)
120  {
121  names_.push_back(name);
122  optimizers_.push_back(optimizer);
123  parameterNames_.push_back(params);
124  derivatives_.push_back(derivatives);
125  itTypes_.push_back(type);
126  }
127 
131  virtual const std::string& getName(size_t i) const { return names_[i]; }
132 
136  virtual Optimizer* getOptimizer(size_t i) { return optimizers_[i]; }
140  virtual const Optimizer* getOptimizer(size_t i) const { return optimizers_[i]; }
141 
145  virtual std::vector<std::string>& getParameterNames(size_t i) { return parameterNames_[i]; }
149  virtual const std::vector<std::string>& getParameterNames(size_t i) const { return parameterNames_[i]; }
150 
154  virtual std::string& getIterationType(size_t i) { return itTypes_[i]; }
158  virtual const std::string& getIterationType(size_t i) const { return itTypes_[i]; }
159 
163  virtual bool requiresFirstOrderDerivatives(size_t i) const { return derivatives_[i] > 0; }
167  virtual bool requiresSecondOrderDerivatives(size_t i) const { return derivatives_[i] > 1; }
168 
172  virtual size_t getNumberOfOptimizers() const { return optimizers_.size(); }
173  };
174 
192  public AbstractOptimizer
193  {
194  private:
196  std::vector<ParameterList> optParameters_;
197  std::vector<size_t> nbParameters_;
198  unsigned int n_;
200  unsigned int stepCount_;
202 
203  public:
212  MetaOptimizer(Function* function, MetaOptimizerInfos* desc, unsigned int n = 1);
213 
214  virtual ~MetaOptimizer();
215 
216  MetaOptimizer(const MetaOptimizer& opt);
217 
219 
220  MetaOptimizer* clone() const { return new MetaOptimizer(*this); }
221 
222  public:
223 
224  void setFunction(Function* function)
225  {
227  for(unsigned int i = 0; i < optDesc_->getNumberOfOptimizers(); i++)
228  optDesc_->getOptimizer(i)->setFunction(function);
229  }
230 
231  void doInit(const ParameterList& parameters) throw (Exception);
232 
233  double doStep() throw (Exception);
234 
239 
243  const MetaOptimizerInfos* getOptimizers() const { return optDesc_; }
244 
245  };
246 
247 } //end of namespace bpp.
248 
249 #endif //METAOPTIMIZER_H__
250 
void setFunction(Function *function)
Set the function to optimize.
virtual Optimizer * getOptimizer(size_t i)
virtual size_t getNumberOfOptimizers() const
MetaOptimizer & operator=(const MetaOptimizer &opt)
This class allows to perform a correspondence analysis.
void doInit(const ParameterList &parameters)
This function is called by the init() method and contains all calculations.
This is the function abstract class.
Definition: Functions.h:86
MetaOptimizerInfos * getOptimizers()
double doStep()
This function is called by the step() method and contains all calculations.
virtual const std::vector< std::string > & getParameterNames(size_t i) const
virtual const std::string & getIterationType(size_t i) const
MetaOptimizerInfos * optDesc_
virtual void addOptimizer(const std::string &name, Optimizer *optimizer, const std::vector< std::string > &params, unsigned short derivatives=0, const std::string &type=IT_TYPE_STEP)
Add a new optimizer to the set.
static std::string IT_TYPE_STEP
Definition: MetaOptimizer.h:61
virtual const Optimizer * getOptimizer(size_t i) const
virtual bool requiresFirstOrderDerivatives(size_t i) const
MetaOptimizerInfos(const MetaOptimizerInfos &infos)
Definition: MetaOptimizer.h:73
static std::string IT_TYPE_FULL
Definition: MetaOptimizer.h:62
The parameter list object.
Definition: ParameterList.h:61
std::vector< std::vector< std::string > > parameterNames_
Definition: MetaOptimizer.h:67
virtual bool requiresSecondOrderDerivatives(size_t i) const
std::vector< ParameterList > optParameters_
std::vector< std::string > itTypes_
Definition: MetaOptimizer.h:69
MetaOptimizer(Function *function, MetaOptimizerInfos *desc, unsigned int n=1)
Build a new MetaOptimizer object.
virtual std::string & getIterationType(size_t i)
virtual void setFunction(Function *function)=0
Set the function to optimize.
std::vector< unsigned short > derivatives_
Definition: MetaOptimizer.h:68
MetaOptimizerInfos & operator=(const MetaOptimizerInfos &infos)
Definition: MetaOptimizer.h:84
Meta-optimizer.
const MetaOptimizerInfos * getOptimizers() const
MetaOptimizer * clone() const
Create a copy of this object and send a pointer to it.
void setFunction(Function *function)
Set the function to optimize.
Exception base class.
Definition: Exceptions.h:57
The Clonable interface (allow an object to be cloned).
Definition: Clonable.h:99
Provide a list of optimizer and corresponding options to be used with the MetaOptimizer class...
Definition: MetaOptimizer.h:57
std::vector< Optimizer * > optimizers_
Definition: MetaOptimizer.h:66
Partial implementation of the Optimizer interface.
virtual std::vector< std::string > & getParameterNames(size_t i)
std::vector< std::string > names_
Definition: MetaOptimizer.h:65
This is the basal interface for all optimization methods.
Definition: Optimizer.h:125
virtual const std::string & getName(size_t i) const
MetaOptimizerInfos * clone() const
Create a copy of this object and send a pointer to it.
unsigned int stepCount_
std::vector< size_t > nbParameters_