NOMAD Source  Version 4.0.0 Beta
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
NMInitialization.cpp
Go to the documentation of this file.
1 
2 #include "../../Algos/CacheInterface.hpp"
3 #include "../../Algos/EvcInterface.hpp"
4 
5 #include "../../Algos/NelderMead/NMInitialization.hpp"
6 
7 
8 void NOMAD::NMInitialization::init()
9 {
10  _name = "NM Initialization";
11 
12  _nmStopReason = NOMAD::AlgoStopReasons<NOMAD::NMStopType>::get( _stopReasons );
13 }
14 
15 
16 bool NOMAD::NMInitialization::run()
17 {
18  bool doContinue = ! _stopReasons->checkTerminate();
19 
20  if (doContinue)
21  {
22  // For a standalone NM, evaluate the trial points generated during start (simplex is created later)
23  // Otherwise, there are no trial points available
24  evalTrialPoints(this);
25  doContinue = ! _stopReasons->checkTerminate();
26  if ( ! doContinue )
27  _nmStopReason->set(NOMAD::NMStopType::INITIAL_FAILED);
28 
29  }
30  return doContinue;
31 }
32 
33 void NOMAD::NMInitialization::start()
34 {
35  defaultStart();
36 
37  if ( ! _stopReasons->checkTerminate() )
38  {
39  // If needed, generate trial points and put them in cache to form simplex
40  // 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.
41  auto nm_opt = _runParams->getAttributeValue<bool>("NM_OPTIMIZATION");
42  if ( nm_opt && ! checkCacheCanFormSimplex() )
43  {
44  generateTrialPoints();
45  }
46  }
47 
48 }
49 
50 bool NOMAD::NMInitialization::checkCacheCanFormSimplex()
51 {
52  size_t n = _pbParams->getAttributeValue<size_t>("DIMENSION");
53  if ( NOMAD::CacheBase::getInstance()->size() < n+1 )
54  return false;
55  // TODO
56  return false;
57 
58 }
59 
60 // Generate trial points to form a simplex
61 void NOMAD::NMInitialization::generateTrialPoints()
62 {
63 
64  auto fixedVariable = getSubFixedVariable();
65 
66  NOMAD::Point x0 = _pbParams->getAttributeValue<NOMAD::Point>("X0");
67  size_t n = _pbParams->getAttributeValue<size_t>("DIMENSION");
68 
69  if (!x0.isComplete() || x0.size() != n)
70  {
71  std::string err = "Initialization: evalY0: Invalid X0 " + x0.display();
72  size_t cacheSize = NOMAD::CacheBase::getInstance()->size();
73  if (cacheSize > 0)
74  {
75  err += ". Hint: Try not setting X0 so that the cache is used (";
76  err += std::to_string(cacheSize) + " points)";
77  }
78  else
79  {
80  err += ". Cache is empty.";
81  }
82  throw NOMAD::Exception(__FILE__, __LINE__, err);
83  }
84 
85  NOMAD::EvalPoint evalPoint_x0(x0);
86  insertTrialPoint(evalPoint_x0);
87  AddOutputInfo("Using X0: " + evalPoint_x0.display());
88 
89  // Method to generate simplex points using X0 adapted from fminsearch (matlab)
90  const NOMAD::Double usualDelta = 0.05; // x0 + 5 percent
91  const NOMAD::Double zeroDelta = 0.00025; //
92  for ( size_t j = 0 ; j < n ; j++ )
93  {
94  NOMAD::EvalPoint trialPoint(x0);
95  if ( trialPoint[j] != 0 )
96  trialPoint[j] *= (1 + usualDelta );
97  else
98  trialPoint[j] = zeroDelta;
99 
100  insertTrialPoint(trialPoint);
101  }
102 
103  NOMAD::OutputQueue::Flush();
104 }
105