bpp-core3  3.0.0
MetaOptimizer.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #ifndef BPP_NUMERIC_FUNCTION_METAOPTIMIZER_H
6 #define BPP_NUMERIC_FUNCTION_METAOPTIMIZER_H
7 
8 
9 #include "AbstractOptimizer.h"
10 
11 // From the STL:
12 #include <vector>
13 
14 namespace bpp
15 {
20  public virtual Clonable
21 {
22 public:
23  static std::string IT_TYPE_STEP;
24  static std::string IT_TYPE_FULL;
25 
26 private:
27  std::vector<std::string> names_;
28  std::vector< std::shared_ptr<OptimizerInterface>> optimizers_;
29  std::vector< std::vector<std::string>> parameterNames_;
30  std::vector<unsigned short> derivatives_;
31  std::vector<std::string> itTypes_;
32 
33 public:
34  MetaOptimizerInfos() : names_(), optimizers_(), parameterNames_(), derivatives_(), itTypes_() {}
36  names_(infos.names_),
37  optimizers_(infos.optimizers_),
38  parameterNames_(infos.parameterNames_),
39  derivatives_(infos.derivatives_),
40  itTypes_(infos.itTypes_)
41  {
42  for (size_t i = 0; i < optimizers_.size(); ++i)
43  {
44  optimizers_[i] = std::shared_ptr<OptimizerInterface>(infos.optimizers_[i]->clone());
45  }
46  }
47 
49  {
50  names_ = infos.names_;
51  optimizers_ = infos.optimizers_;
52  parameterNames_ = infos.parameterNames_;
53  derivatives_ = infos.derivatives_;
54  itTypes_ = infos.itTypes_;
55  for (size_t i = 0; i < optimizers_.size(); ++i)
56  {
57  optimizers_[i] = std::shared_ptr<OptimizerInterface>(infos.optimizers_[i]->clone());
58  }
59  return *this;
60  }
61 
62  virtual ~MetaOptimizerInfos() {}
63 
64 public:
65  MetaOptimizerInfos* clone() const override { return new MetaOptimizerInfos(*this); }
66 
67 public:
77  virtual void addOptimizer(const std::string& name, std::shared_ptr<OptimizerInterface> optimizer, const std::vector<std::string>& params, unsigned short derivatives = 0, const std::string& type = IT_TYPE_STEP)
78  {
79  names_.push_back(name);
80  optimizers_.push_back(optimizer);
81  parameterNames_.push_back(params);
82  derivatives_.push_back(derivatives);
83  itTypes_.push_back(type);
84  }
85 
89  virtual const std::string& getName(size_t i) const { return names_[i]; }
90 
94  virtual OptimizerInterface& optimizer(size_t i) { return *optimizers_[i]; }
95 
99  virtual const OptimizerInterface& optimizer(size_t i) const { return *optimizers_[i]; }
100 
104  virtual std::shared_ptr<OptimizerInterface> getOptimizer(size_t i) { return optimizers_[i]; }
105 
109  virtual std::shared_ptr<const OptimizerInterface> getOptimizer(size_t i) const { return optimizers_[i]; }
110 
114  virtual std::vector<std::string>& getParameterNames(size_t i) { return parameterNames_[i]; }
118  virtual const std::vector<std::string>& getParameterNames(size_t i) const { return parameterNames_[i]; }
119 
123  virtual std::string& getIterationType(size_t i) { return itTypes_[i]; }
127  virtual const std::string& getIterationType(size_t i) const { return itTypes_[i]; }
128 
132  virtual bool requiresFirstOrderDerivatives(size_t i) const { return derivatives_[i] > 0; }
136  virtual bool requiresSecondOrderDerivatives(size_t i) const { return derivatives_[i] > 1; }
137 
141  virtual size_t getNumberOfOptimizers() const { return optimizers_.size(); }
142 };
143 
161  public AbstractOptimizer
162 {
163 private:
164  std::unique_ptr<MetaOptimizerInfos> optDesc_;
165  std::vector<ParameterList> optParameters_;
166  std::vector<size_t> nbParameters_;
167  unsigned int n_;
169  unsigned int stepCount_;
171 
172 public:
181  MetaOptimizer(std::shared_ptr<FunctionInterface> function, std::unique_ptr<MetaOptimizerInfos> desc, unsigned int n = 1);
182 
183  virtual ~MetaOptimizer();
184 
185  MetaOptimizer(const MetaOptimizer& opt);
186 
188 
189  MetaOptimizer* clone() const override { return new MetaOptimizer(*this); }
190 
191 public:
192  void setFunction(std::shared_ptr<FunctionInterface> function) override
193  {
195  for (unsigned int i = 0; i < optDesc_->getNumberOfOptimizers(); ++i)
196  {
197  optDesc_->getOptimizer(i)->setFunction(function);
198  }
199  }
200 
201  void doInit(const ParameterList& parameters) override;
202 
203  double doStep() override;
204 
208  MetaOptimizerInfos& optimizers() { return *optDesc_; }
209 
213  const MetaOptimizerInfos optimizers() const { return *optDesc_; }
214 };
215 } // end of namespace bpp.
216 #endif // BPP_NUMERIC_FUNCTION_METAOPTIMIZER_H
MetaOptimizerInfos & optimizers()
virtual size_t getNumberOfOptimizers() const
MetaOptimizerInfos * clone() const override
Create a copy of this object and send a pointer to it.
Definition: MetaOptimizer.h:65
std::unique_ptr< MetaOptimizerInfos > optDesc_
void setFunction(std::shared_ptr< FunctionInterface > function) override
Set the function to optimize.
virtual const OptimizerInterface & optimizer(size_t i) const
Definition: MetaOptimizer.h:99
virtual OptimizerInterface & optimizer(size_t i)
Definition: MetaOptimizer.h:94
virtual const std::vector< std::string > & getParameterNames(size_t i) const
virtual const std::string & getIterationType(size_t i) const
static std::string IT_TYPE_STEP
Definition: MetaOptimizer.h:23
const MetaOptimizerInfos optimizers() const
virtual void addOptimizer(const std::string &name, std::shared_ptr< OptimizerInterface > 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.
Definition: MetaOptimizer.h:77
virtual bool requiresFirstOrderDerivatives(size_t i) const
MetaOptimizerInfos(const MetaOptimizerInfos &infos)
Definition: MetaOptimizer.h:35
static std::string IT_TYPE_FULL
Definition: MetaOptimizer.h:24
virtual std::shared_ptr< const OptimizerInterface > getOptimizer(size_t i) const
The parameter list object.
Definition: ParameterList.h:27
std::vector< std::vector< std::string > > parameterNames_
Definition: MetaOptimizer.h:29
virtual bool requiresSecondOrderDerivatives(size_t i) const
std::vector< ParameterList > optParameters_
void setFunction(std::shared_ptr< FunctionInterface > function) override
Set the function to optimize.
std::vector< std::string > itTypes_
Definition: MetaOptimizer.h:31
virtual std::string & getIterationType(size_t i)
virtual std::shared_ptr< OptimizerInterface > getOptimizer(size_t i)
std::vector< unsigned short > derivatives_
Definition: MetaOptimizer.h:30
MetaOptimizerInfos & operator=(const MetaOptimizerInfos &infos)
Definition: MetaOptimizer.h:48
Meta-optimizer.
MetaOptimizer * clone() const override
Create a copy of this object and send a pointer to it.
The Clonable interface (allow an object to be cloned).
Definition: Clonable.h:63
This is the basal interface for all optimization methods.
Definition: Optimizer.h:88
Provide a list of optimizer and corresponding options to be used with the MetaOptimizer class...
Definition: MetaOptimizer.h:19
Partial implementation of the Optimizer interface.
virtual std::vector< std::string > & getParameterNames(size_t i)
std::vector< std::shared_ptr< OptimizerInterface > > optimizers_
Definition: MetaOptimizer.h:28
std::vector< std::string > names_
Definition: MetaOptimizer.h:27
virtual const std::string & getName(size_t i) const
Definition: MetaOptimizer.h:89
unsigned int stepCount_
std::vector< size_t > nbParameters_