NOMAD Source  Version 4.0.0 Beta
NMInitialization.cpp
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 
47 #include "../../Algos/EvcInterface.hpp"
48 #include "../../Algos/NelderMead/NMInitialization.hpp"
49 #include "../../Algos/SubproblemManager.hpp"
50 
51 
52 void NOMAD::NMInitialization::init()
53 {
54  _name = getAlgoName() + "Initialization";
55 
56  _nmStopReason = NOMAD::AlgoStopReasons<NOMAD::NMStopType>::get( _stopReasons );
57 }
58 
59 
60 bool NOMAD::NMInitialization::runImp()
61 {
62  bool doContinue = ! _stopReasons->checkTerminate();
63 
64  if (doContinue)
65  {
66  // For a standalone NM, evaluate the trial points generated during start (simplex is created later)
67  // Otherwise, there are no trial points available
68  evalTrialPoints(this);
69  doContinue = ! _stopReasons->checkTerminate();
70  if ( ! doContinue )
71  _nmStopReason->set(NOMAD::NMStopType::INITIAL_FAILED);
72 
73  }
74  return doContinue;
75 }
76 
77 void NOMAD::NMInitialization::startImp()
78 {
79 
80  if ( ! _stopReasons->checkTerminate() )
81  {
82  // If needed, generate trial points and put them in cache to form simplex
83  // For a standalone optimization (NM_OPTIMIZATION true), initial trial points must be generated to form a valid simplex around x0. Otherwise, the cache will be used to construct the simplex.
84  auto nm_opt = _runParams->getAttributeValue<bool>("NM_OPTIMIZATION");
85  if ( nm_opt && ! checkCacheCanFormSimplex() )
86  {
87  generateTrialPoints();
88  }
89  }
90 
91 }
92 
93 
94 void NOMAD::NMInitialization::endImp()
95 {
96  // Construct _barrier member with evaluated _trialPoints for future use
97  // _trialPoints are already updated with Evals.
98  if (_trialPoints.size() > 0)
99  {
100  std::vector<NOMAD::EvalPoint> evalPointList;
101  std::copy(_trialPoints.begin(), _trialPoints.end(),
102  std::back_inserter(evalPointList));
103  auto hMax = _runParams->getAttributeValue<NOMAD::Double>("H_MAX_0");
104  _barrier = std::make_shared<NOMAD::Barrier>(hMax, NOMAD::SubproblemManager::getSubFixedVariable(this), NOMAD::EvcInterface::getEvaluatorControl()->getEvalType(), evalPointList);
105  }
106 }
107 
108 
109 bool NOMAD::NMInitialization::checkCacheCanFormSimplex()
110 {
111  // Complete this function: see Issue #393
112  size_t n = _pbParams->getAttributeValue<size_t>("DIMENSION");
113  if ( NOMAD::CacheBase::getInstance()->size() < n+1 )
114  return false;
115  return false;
116 
117 }
118 
119 // Generate trial points to form a simplex
120 void NOMAD::NMInitialization::generateTrialPoints()
121 {
122  NOMAD::Point x0 = _pbParams->getAttributeValue<NOMAD::Point>("X0");
123  size_t n = _pbParams->getAttributeValue<size_t>("DIMENSION");
124 
125  if (!x0.isComplete() || x0.size() != n)
126  {
127  std::string err = "Initialization: evalY0: Invalid X0 " + x0.display();
128  size_t cacheSize = NOMAD::CacheBase::getInstance()->size();
129  if (cacheSize > 0)
130  {
131  err += ". Hint: Try not setting X0 so that the cache is used (";
132  err += std::to_string(cacheSize) + " points)";
133  }
134  else
135  {
136  err += ". Cache is empty.";
137  }
138  throw NOMAD::Exception(__FILE__, __LINE__, err);
139  }
140 
141  NOMAD::EvalPoint evalPoint_x0(x0);
142  insertTrialPoint(evalPoint_x0);
144  AddOutputInfo("Using X0: " + evalPoint_x0.display());
146 
147  // Method to generate simplex points using X0 adapted from fminsearch (matlab)
148  const NOMAD::Double usualDelta = 0.05; // x0 + 5 percent
149  const NOMAD::Double zeroDelta = 0.00025; //
150  for ( size_t j = 0 ; j < n ; j++ )
151  {
152  NOMAD::EvalPoint trialPoint(x0);
153  if ( trialPoint[j] != 0 )
154  trialPoint[j] *= (1 + usualDelta );
155  else
156  trialPoint[j] = zeroDelta;
157 
158  insertTrialPoint(trialPoint);
159  }
160 
162  NOMAD::OutputQueue::Flush();
164 }
OUTPUT_INFO_END
#define OUTPUT_INFO_END
Definition: OutputQueue.hpp:151
OUTPUT_INFO_START
#define OUTPUT_INFO_START
Definition: OutputQueue.hpp:148