NOMAD Source  Version 4.0.0 Beta
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
MegaIteration.hpp
Go to the documentation of this file.
1 #ifndef __NOMAD400_MEGAITERATION__
2 #define __NOMAD400_MEGAITERATION__
3 
4 
5 #include "../Algos/Iteration.hpp"
6 
7 #include "../nomad_nsbegin.hpp"
8 
9 /// Class to manage the iterations.
10 /**
11  A mega iteration of an algorithm:
12  * - generates a lot of points over multiple strategies (ex., Poll and Search for Mads).
13  * - evaluates points
14  * - performs post-processing
15 
16  The run and start functions of mega iteration must be implemented for each optimizer
17  that has several phases of points creation that we want to combine before launching evaluations (for example, ::MadsMegaIteration, ::NMMegaIteration, ...).
18 
19  \note As an hypothesis, the time load is taken by the evaluation,
20  which is parallelized over all evaluations simultaneously.
21  The iteration generation, including trial points generation,
22  has little time load, so they do not need to be parallelized.
23  It is also preferable to keep parallelization to the only place where
24  it matters the most to avoid errors. \n
25  There is no parallelization at the algorithmic level.
26  The algorithms are run in master thread only.
27 */
28 class MegaIteration: public Step
29 {
30 
31 
32 protected:
33  std::vector<std::shared_ptr<Iteration>> _iterList; ///< A collection of additional iterations: Help generate more eval points.
34 
35  /**
36  The barrier holds xFeas, xInf and hMax.
37  xFeas and xInf will be used as frame centers.
38  \note This barrier is in subspace.
39  */
40  std::shared_ptr<Barrier> _barrier;
41 
42 
43  size_t _k; ///< Main counter
44  size_t _nbIterRun; ///< Number of iterations run within this MegaIteration
45 
46  /**
47  Success type of this step. Initialized with the run of the previous
48  mega iteration, so that the update step knows what to do
49  (for example,enlarge or reduce the mesh).
50  At the end of run step during the mega iteration of an algorithm,
51  the success type is updated (::setSuccessType) with the latest
52  success type.
53  */
55 
56 
57 
58 public:
59  /// Constructor
60  /**
61  \param parentStep The parent step of this step -- \b IN.
62  \param k The main iteration counter -- \b IN.
63  \param barrier The barrier for constraints handling. Belongs to subproblem space. -- \b IN.
64  \param success Success type of the previous MegaIteration. -- \b IN.
65  */
66  explicit MegaIteration(const Step* parentStep,
67  size_t k,
68  std::shared_ptr<Barrier> barrier,
69  SuccessType success )
70  : Step(parentStep ),
71  _iterList(),
72  _barrier(barrier),
73  _k(k),
74  _nbIterRun(0),
75  _success(success)
76  {
77  if (nullptr == _barrier)
78  {
79  throw Exception(__FILE__, __LINE__, "MegaIteration constructor: barrier must not be NULL.");
80  }
81  init();
82  }
83  // No Destructor needed - keep defaults.
84 
85 
86  /*---------*/
87  /* Get/Set */
88  /*---------*/
89 
90  size_t getNbIterations() const { return _iterList.size(); }
91  std::shared_ptr<Iteration> getIter(size_t i) const { return _iterList[i]; }
92 
93  size_t getK() const { return _k; }
94  void setK(const size_t k) { _k = k; }
95  size_t getNextK() const;
96 
97  // Barrier
98  const std::shared_ptr<NOMAD::Barrier> getBarrier() const { return _barrier; }
99  void setBarrier(const std::shared_ptr<NOMAD::Barrier> &barrier) { _barrier = barrier; }
100 
101 
102  // Inherited from Step
103  virtual void start() override = 0;
104  virtual bool run() override = 0;
105  virtual void end() override;
106 
107  const SuccessType& getSuccessType() const { return _success; }
108 
109  void setSuccessType(const SuccessType& success) { _success = success; }
110 
111  /*---------*/
112  /* Others */
113  /*---------*/
114 
115  virtual void read( std::istream& is );
116  virtual void display( std::ostream& os ) const ;
117 
118 private:
119  /// Helper for constructor
120  void init();
121 
122 };
123 
124 
125 /**
126  Display useful values so that a new MegaIteration could be constructed using these values.
127  */
128 std::ostream& operator<<(std::ostream& os, const MegaIteration& megaIteration);
129 
130 /// Get an MegaIteration values from a stream
131 std::istream& operator>>(std::istream& is, MegaIteration& megaIteration);
132 
133 
134 
135 #include "../nomad_nsend.hpp"
136 
137 #endif // __NOMAD400_MEGAITERATION__
Exception utility.
size_t _k
Main counter.
SuccessType _success
virtual void display(std::ostream &os) const
virtual void end() override
What a step does.
Class to manage the iterations.
size_t getNbIterations() const
const SuccessType & getSuccessType() const
SuccessType
Success type of an iteration.
Definition: defines.hpp:107
void setK(const size_t k)
virtual void read(std::istream &is)
std::istream & operator>>(std::istream &is, Algorithm &algo)
Operator to read parameters used for hot restart.
void setBarrier(const std::shared_ptr< NOMAD::Barrier > &barrier)
virtual void start() override=0
What a step does.
std::shared_ptr< Iteration > getIter(size_t i) const
size_t getK() const
virtual bool run() override=0
What a step does.
std::vector< std::shared_ptr< Iteration > > _iterList
A collection of additional iterations: Help generate more eval points.
size_t getNextK() const
void setSuccessType(const SuccessType &success)
size_t _nbIterRun
Number of iterations run within this MegaIteration.
std::ostream & operator<<(std::ostream &os, const Algorithm &algo)
Operator to write parameters used for hot restart.
Base class of all types of steps (Iteration, Termination, Initialization, Poll, Mads,...).
Definition: Step.hpp:24
const std::shared_ptr< NOMAD::Barrier > getBarrier() const
MegaIteration(const Step *parentStep, size_t k, std::shared_ptr< Barrier > barrier, SuccessType success)
Constructor.
void init()
Helper for constructor.
std::shared_ptr< Barrier > _barrier