NOMAD Source  Version 4.0.0 Beta
CacheParameters.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 "../Param/CacheParameters.hpp"
48 #include "../Util/fileutils.hpp"
49 
50 /*----------------------------------------*/
51 /* initializations (private) */
52 /*----------------------------------------*/
53 void NOMAD::CacheParameters::init()
54 {
55  _typeName = "Cache";
56 
57  try
58  {
59  #include "../Attribute/cacheAttributesDefinition.hpp"
60  registerAttributes( _definition );
61  }
62  catch ( NOMAD::Exception & e)
63  {
64  std::string errorMsg = "Attribute registration failed: ";
65  errorMsg += e.what();
66  throw NOMAD::Exception(__FILE__,__LINE__, errorMsg);
67  }
68 }
69 
70 /*----------------------------------------*/
71 /* check the parameters */
72 /*----------------------------------------*/
73 void NOMAD::CacheParameters::checkAndComply( std::shared_ptr<NOMAD::RunParameters> runParams )
74 {
75  checkInfo();
76 
77  if (!toBeChecked())
78  {
79  // Early out
80  return;
81  }
82 
83  /*----------------------------*/
84  /* Update cache file names */
85  /*----------------------------*/
86  auto problemDir = runParams->getAttributeValue<std::string>("PROBLEM_DIR",false);
87  std::string cacheFileName = getAttributeValueProtected<std::string>("CACHE_FILE",false);
88  if (!cacheFileName.empty())
89  {
90  NOMAD::completeFileName(cacheFileName, problemDir);
91  setAttributeValue("CACHE_FILE", cacheFileName);
92  }
93 
94  // Hot restart needs a cache file
95  bool hotRestartRead = runParams->getAttributeValue<bool>("HOT_RESTART_READ_FILES", false);
96  bool hotRestartWrite = runParams->getAttributeValue<bool>("HOT_RESTART_WRITE_FILES", false);
97  if (hotRestartRead || hotRestartWrite)
98  {
99  if (cacheFileName.empty())
100  {
101  cacheFileName = "cache.txt";
102  std::cerr << "Warning: " << ((hotRestartWrite) ? "HOT_RESTART_WRITE_FILES" : "HOT_RESTART_READ_FILES") << " is set. CACHE_FILE set to \"" << cacheFileName << "\"" << std::endl;
103 
104  NOMAD::completeFileName(cacheFileName, problemDir);
105  setAttributeValue("CACHE_FILE", cacheFileName);
106  }
107  }
108 
109  _toBeChecked = false;
110 
111 }
112 // End checkAndComply()
113 
114 
_definition
_definition
Definition: cacheAttributesDefinition.hpp:7
completeFileName
void completeFileName(std::string &filename, const std::string &problemDir, bool addSeed=false, int seed=0)
Definition: fileutils.cpp:298