NOMAD Source  Version 4.0.0 Beta
MegaIteration.hpp
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------------*/
2 /* NOMAD - Nonlinear Optimization by Mesh Adaptive Direct Search - */
3 /* */
4 /* NOMAD - Version 4.0.0 has been created by */
5 /* Viviane Rochon Montplaisir - Polytechnique Montreal */
6 /* Christophe Tribes - Polytechnique Montreal */
7 /* */
8 /* The copyright of NOMAD - version 4.0.0 is owned by */
9 /* Charles Audet - Polytechnique Montreal */
10 /* Sebastien Le Digabel - Polytechnique Montreal */
11 /* Viviane Rochon Montplaisir - Polytechnique Montreal */
12 /* Christophe Tribes - Polytechnique Montreal */
13 /* */
14 /* NOMAD v4 has been funded by Rio Tinto, Hydro-Québec, NSERC (Natural */
15 /* Sciences and Engineering Research Council of Canada), InnovÉÉ (Innovation */
16 /* en Énergie Électrique) and IVADO (The Institute for Data Valorization) */
17 /* */
18 /* NOMAD v3 was created and developed by Charles Audet, Sebastien Le Digabel, */
19 /* Christophe Tribes and Viviane Rochon Montplaisir and was funded by AFOSR */
20 /* and Exxon Mobil. */
21 /* */
22 /* NOMAD v1 and v2 were created and developed by Mark Abramson, Charles Audet, */
23 /* Gilles Couture, and John E. Dennis Jr., and were funded by AFOSR and */
24 /* Exxon Mobil. */
25 /* */
26 /* Contact information: */
27 /* Polytechnique Montreal - GERAD */
28 /* C.P. 6079, Succ. Centre-ville, Montreal (Quebec) H3C 3A7 Canada */
29 /* e-mail: nomad@gerad.ca */
30 /* */
31 /* This program is free software: you can redistribute it and/or modify it */
32 /* under the terms of the GNU Lesser General Public License as published by */
33 /* the Free Software Foundation, either version 3 of the License, or (at your */
34 /* option) any later version. */
35 /* */
36 /* This program is distributed in the hope that it will be useful, but WITHOUT */
37 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
38 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License */
39 /* for more details. */
40 /* */
41 /* You should have received a copy of the GNU Lesser General Public License */
42 /* along with this program. If not, see <http://www.gnu.org/licenses/>. */
43 /* */
44 /* You can find information on the NOMAD software at www.gerad.ca/nomad */
45 /*---------------------------------------------------------------------------------*/
46 #ifndef __NOMAD400_MEGAITERATION__
47 #define __NOMAD400_MEGAITERATION__
48 
49 #include "../Algos/Iteration.hpp"
50 #include "../Algos/Step.hpp"
51 
52 #include "../nomad_nsbegin.hpp"
53 
54 /// Class to manage the iterations.
55 /**
56  A mega iteration of an algorithm:
57  * - generates a lot of points over multiple strategies (ex., Poll and Search for Mads).
58  * - evaluates points
59  * - performs post-processing
60 
61  The run and start functions of mega iteration must be implemented for each optimizer
62  that has several phases of points creation that we want to combine before launching evaluations (for example, ::MadsMegaIteration, ::NMMegaIteration, ...).
63 
64  \note As an hypothesis, the time load is taken by the evaluation,
65  which is parallelized over all evaluations simultaneously.
66  The iteration generation, including trial points generation,
67  has little time load, so they do not need to be parallelized.
68  It is also preferable to keep parallelization to the only place where
69  it matters the most to avoid errors. \n
70  There is no parallelization at the algorithmic level.
71  The algorithms are run in main thread(s) only; other threads are available for evaluations.
72 */
73 class MegaIteration: public Step
74 {
75 
76 
77 protected:
78  std::vector<std::shared_ptr<Iteration>> _iterList; ///< A collection of additional iterations: Help generate more eval points.
79 
80  /**
81  The barrier holds xFeas, xInf and hMax.
82  xFeas and xInf will be used as frame centers.
83  \note This barrier is in subspace.
84  */
85  std::shared_ptr<Barrier> _barrier;
86 
87 
88  size_t _k; ///< Main counter
89  size_t _nbIterRun; ///< Number of iterations run within this MegaIteration
90 
91  /**
92  Success type of this step. Initialized with the run of the previous
93  mega iteration, so that the update step knows what to do
94  (for example, enlarge or reduce the mesh).
95  At the end of run step during the mega iteration of an algorithm,
96  the success type is updated (MegaIteration::setSuccessType) with the latest
97  success type.
98  */
100 
101 
102 
103 public:
104  /// Constructor
105  /**
106  \param parentStep The parent step of this step -- \b IN.
107  \param k The main iteration counter -- \b IN.
108  \param barrier The barrier for constraints handling. Belongs to subproblem space. -- \b IN.
109  \param success Success type of the previous MegaIteration. -- \b IN.
110  */
111  explicit MegaIteration(const Step* parentStep,
112  size_t k,
113  std::shared_ptr<Barrier> barrier,
114  SuccessType success);
115 
116  // No Destructor needed - keep defaults.
117 
118 
119  /*---------*/
120  /* Get/Set */
121  /*---------*/
122 
123  size_t getNbIterations() const { return _iterList.size(); }
124  const std::shared_ptr<Iteration>& getIter(size_t i) const { return _iterList[i]; }
125 
126  size_t getK() const { return _k; }
127  void setK(const size_t k) { _k = k; }
128  size_t getNextK() const;
129 
130  // Barrier
131  const std::shared_ptr<Barrier>& getBarrier() const { return _barrier; }
132  void setBarrier(const std::shared_ptr<Barrier> &barrier) { _barrier = barrier; }
133 
134 
135  /**
136  Success is initialized with the success of the previous
137  MegaIteration. This is useful to know for the Update step, and some Search steps.
138  At the end of the MegaIteration, Success is updated with the latest SuccessType.
139  */
141  void setSuccessType(const SuccessType& success) { _megaIterationSuccess = success; }
142 
143  /// Compute the number of xFeas and xInf points to use to create iterations
144  void computeMaxXFeasXInf(size_t &maxXFeas, size_t &maxXInf);
145 
146  /*---------*/
147  /* Others */
148  /*---------*/
149 
150  virtual void read(std::istream& is);
151  virtual void display(std::ostream& os) const ;
152 
153 private:
154  /// Helper for constructor
155  void init();
156 
157 protected:
158 
159  virtual void startImp() override = 0;
160  virtual bool runImp() override = 0;
161 
162  /// Implementation of the end task.
163  /**
164  Perform callback check for user termination and clear the iteration list.
165  */
166  virtual void endImp() override;
167 
168 };
169 
170 
171 /**
172  Display useful values so that a new MegaIteration could be constructed using these values.
173  */
174 std::ostream& operator<<(std::ostream& os, const MegaIteration& megaIteration);
175 
176 /// Get an MegaIteration values from a stream
177 std::istream& operator>>(std::istream& is, MegaIteration& megaIteration);
178 
179 
180 
181 #include "../nomad_nsend.hpp"
182 
183 #endif // __NOMAD400_MEGAITERATION__
MegaIteration::getSuccessType
const SuccessType & getSuccessType() const
Definition: MegaIteration.hpp:140
MegaIteration
Class to manage the iterations.
Definition: MegaIteration.hpp:73
MegaIteration::runImp
virtual bool runImp() override=0
MegaIteration::startImp
virtual void startImp() override=0
operator>>
std::istream & operator>>(std::istream &is, MegaIteration &megaIteration)
Get an MegaIteration values from a stream.
MegaIteration::setBarrier
void setBarrier(const std::shared_ptr< Barrier > &barrier)
Definition: MegaIteration.hpp:132
MegaIteration::read
virtual void read(std::istream &is)
Step
Base class of all types of steps (Iteration, Termination, Initialization, Poll, Mads,...
Definition: Step.hpp:68
SuccessType
SuccessType
Success type of an iteration.
Definition: defines.hpp:148
MegaIteration::_iterList
std::vector< std::shared_ptr< Iteration > > _iterList
A collection of additional iterations: Help generate more eval points.
Definition: MegaIteration.hpp:78
MegaIteration::computeMaxXFeasXInf
void computeMaxXFeasXInf(size_t &maxXFeas, size_t &maxXInf)
Compute the number of xFeas and xInf points to use to create iterations.
operator<<
std::ostream & operator<<(std::ostream &os, const MegaIteration &megaIteration)
MegaIteration::_nbIterRun
size_t _nbIterRun
Number of iterations run within this MegaIteration.
Definition: MegaIteration.hpp:89
MegaIteration::getBarrier
const std::shared_ptr< Barrier > & getBarrier() const
Definition: MegaIteration.hpp:131
MegaIteration::setSuccessType
void setSuccessType(const SuccessType &success)
Definition: MegaIteration.hpp:141
MegaIteration::init
void init()
Helper for constructor.
MegaIteration::endImp
virtual void endImp() override
Implementation of the end task.
MegaIteration::display
virtual void display(std::ostream &os) const
MegaIteration::_barrier
std::shared_ptr< Barrier > _barrier
Definition: MegaIteration.hpp:85
MegaIteration::getK
size_t getK() const
Definition: MegaIteration.hpp:126
MegaIteration::_k
size_t _k
Main counter.
Definition: MegaIteration.hpp:88
MegaIteration::getNbIterations
size_t getNbIterations() const
Definition: MegaIteration.hpp:123
MegaIteration::MegaIteration
MegaIteration(const Step *parentStep, size_t k, std::shared_ptr< Barrier > barrier, SuccessType success)
Constructor.
MegaIteration::setK
void setK(const size_t k)
Definition: MegaIteration.hpp:127
MegaIteration::getNextK
size_t getNextK() const
MegaIteration::getIter
const std::shared_ptr< Iteration > & getIter(size_t i) const
Definition: MegaIteration.hpp:124
MegaIteration::_megaIterationSuccess
SuccessType _megaIterationSuccess
Definition: MegaIteration.hpp:99