bpp-core3  3.0.0
MetaOptimizer.h
Go to the documentation of this file.
1 //
2 // File: MetaOptimizer.h
3 // Authors:
4 // Julien Dutheil
5 // Created: 2004-11-17 17:22:00
6 //
7 
8 /*
9  Copyright or © or Copr. Bio++ Development Team, (November 17, 2004)
10 
11  This software is a computer program whose purpose is to provide classes
12  for phylogenetic data analysis.
13 
14  This software is governed by the CeCILL license under French law and
15  abiding by the rules of distribution of free software. You can use,
16  modify and/ or redistribute the software under the terms of the CeCILL
17  license as circulated by CEA, CNRS and INRIA at the following URL
18  "http://www.cecill.info".
19 
20  As a counterpart to the access to the source code and rights to copy,
21  modify and redistribute granted by the license, users are provided only
22  with a limited warranty and the software's author, the holder of the
23  economic rights, and the successive licensors have only limited
24  liability.
25 
26  In this respect, the user's attention is drawn to the risks associated
27  with loading, using, modifying and/or developing or reproducing the
28  software by the user in light of its specific status of free software,
29  that may mean that it is complicated to manipulate, and that also
30  therefore means that it is reserved for developers and experienced
31  professionals having in-depth computer knowledge. Users are therefore
32  encouraged to load and test the software's suitability as regards their
33  requirements in conditions enabling the security of their systems and/or
34  data to be ensured and, more generally, to use and operate it in the
35  same conditions as regards security.
36 
37  The fact that you are presently reading this means that you have had
38  knowledge of the CeCILL license and that you accept its terms.
39 */
40 
41 #ifndef BPP_NUMERIC_FUNCTION_METAOPTIMIZER_H
42 #define BPP_NUMERIC_FUNCTION_METAOPTIMIZER_H
43 
44 
45 #include "AbstractOptimizer.h"
46 
47 // From the STL:
48 #include <vector>
49 
50 namespace bpp
51 {
56  public virtual Clonable
57 {
58 public:
59  static std::string IT_TYPE_STEP;
60  static std::string IT_TYPE_FULL;
61 
62 private:
63  std::vector<std::string> names_;
64  std::vector<Optimizer*> optimizers_;
65  std::vector< std::vector<std::string> > parameterNames_;
66  std::vector<unsigned short> derivatives_;
67  std::vector<std::string> itTypes_;
68 
69 public:
72  names_(infos.names_),
73  optimizers_(infos.optimizers_),
76  itTypes_(infos.itTypes_)
77  {
78  for (unsigned int i = 0; i < optimizers_.size(); i++)
79  {
80  optimizers_[i] = dynamic_cast<Optimizer*>(infos.optimizers_[i]->clone());
81  }
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  {
93  optimizers_[i] = dynamic_cast<Optimizer*>(infos.optimizers_[i]->clone());
94  }
95  return *this;
96  }
97 
99  {
100  for (unsigned int i = 0; i < optimizers_.size(); i++)
101  {
102  delete optimizers_[i];
103  }
104  }
105 
106 public:
107  MetaOptimizerInfos* clone() const { return new MetaOptimizerInfos(*this); }
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  void setFunction(Function* function)
224  {
226  for (unsigned int i = 0; i < optDesc_->getNumberOfOptimizers(); i++)
227  {
228  optDesc_->getOptimizer(i)->setFunction(function);
229  }
230  }
231 
232  void doInit(const ParameterList& parameters);
233 
234  double doStep();
235 
240 
244  const MetaOptimizerInfos* getOptimizers() const { return optDesc_; }
245 };
246 } // end of namespace bpp.
247 #endif // BPP_NUMERIC_FUNCTION_METAOPTIMIZER_H
Partial implementation of the Optimizer interface.
void setFunction(Function *function)
Set the function to optimize.
The Clonable interface (allow an object to be cloned).
Definition: Clonable.h:103
This is the function abstract class.
Definition: Functions.h:89
Provide a list of optimizer and corresponding options to be used with the MetaOptimizer class.
Definition: MetaOptimizer.h:57
virtual const Optimizer * getOptimizer(size_t i) const
virtual const std::string & getName(size_t i) const
virtual Optimizer * getOptimizer(size_t i)
virtual bool requiresSecondOrderDerivatives(size_t i) const
MetaOptimizerInfos & operator=(const MetaOptimizerInfos &infos)
Definition: MetaOptimizer.h:84
virtual size_t getNumberOfOptimizers() const
std::vector< unsigned short > derivatives_
Definition: MetaOptimizer.h:66
std::vector< std::string > names_
Definition: MetaOptimizer.h:63
virtual const std::string & getIterationType(size_t i) const
static std::string IT_TYPE_STEP
Definition: MetaOptimizer.h:59
virtual std::string & getIterationType(size_t i)
MetaOptimizerInfos * clone() const
Create a copy of this object and send a pointer to it.
MetaOptimizerInfos(const MetaOptimizerInfos &infos)
Definition: MetaOptimizer.h:71
virtual bool requiresFirstOrderDerivatives(size_t i) const
virtual std::vector< std::string > & getParameterNames(size_t i)
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.
std::vector< std::string > itTypes_
Definition: MetaOptimizer.h:67
std::vector< Optimizer * > optimizers_
Definition: MetaOptimizer.h:64
static std::string IT_TYPE_FULL
Definition: MetaOptimizer.h:60
std::vector< std::vector< std::string > > parameterNames_
Definition: MetaOptimizer.h:65
virtual const std::vector< std::string > & getParameterNames(size_t i) const
Meta-optimizer.
double doStep()
This function is called by the step() method and contains all calculations.
MetaOptimizer & operator=(const MetaOptimizer &opt)
MetaOptimizer * clone() const
Create a copy of this object and send a pointer to it.
std::vector< size_t > nbParameters_
MetaOptimizerInfos * optDesc_
MetaOptimizerInfos * getOptimizers()
MetaOptimizer(Function *function, MetaOptimizerInfos *desc, unsigned int n=1)
Build a new MetaOptimizer object.
const MetaOptimizerInfos * getOptimizers() const
void setFunction(Function *function)
Set the function to optimize.
void doInit(const ParameterList &parameters)
This function is called by the init() method and contains all calculations.
std::vector< ParameterList > optParameters_
unsigned int stepCount_
This is the basal interface for all optimization methods.
Definition: Optimizer.h:119
virtual void setFunction(Function *function)=0
Set the function to optimize.
The parameter list object.
Definition: ParameterList.h:65