-
Notifications
You must be signed in to change notification settings - Fork 24
/
AllParameters.cpp
350 lines (301 loc) · 15.7 KB
/
AllParameters.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*---------------------------------------------------------------------------------*/
/* NOMAD - Nonlinear Optimization by Mesh Adaptive Direct Search - */
/* */
/* NOMAD - Version 4 has been created and developed by */
/* Viviane Rochon Montplaisir - Polytechnique Montreal */
/* Christophe Tribes - Polytechnique Montreal */
/* */
/* The copyright of NOMAD - version 4 is owned by */
/* Charles Audet - Polytechnique Montreal */
/* Sebastien Le Digabel - Polytechnique Montreal */
/* Viviane Rochon Montplaisir - Polytechnique Montreal */
/* Christophe Tribes - Polytechnique Montreal */
/* */
/* NOMAD 4 has been funded by Rio Tinto, Hydro-Québec, Huawei-Canada, */
/* NSERC (Natural Sciences and Engineering Research Council of Canada), */
/* InnovÉÉ (Innovation en Énergie Électrique) and IVADO (The Institute */
/* for Data Valorization) */
/* */
/* NOMAD v3 was created and developed by Charles Audet, Sebastien Le Digabel, */
/* Christophe Tribes and Viviane Rochon Montplaisir and was funded by AFOSR */
/* and Exxon Mobil. */
/* */
/* NOMAD v1 and v2 were created and developed by Mark Abramson, Charles Audet, */
/* Gilles Couture, and John E. Dennis Jr., and were funded by AFOSR and */
/* Exxon Mobil. */
/* */
/* Contact information: */
/* Polytechnique Montreal - GERAD */
/* C.P. 6079, Succ. Centre-ville, Montreal (Quebec) H3C 3A7 Canada */
/* e-mail: nomad@gerad.ca */
/* */
/* This program is free software: you can redistribute it and/or modify it */
/* under the terms of the GNU Lesser General Public License as published by */
/* the Free Software Foundation, either version 3 of the License, or (at your */
/* option) any later version. */
/* */
/* This program is distributed in the hope that it will be useful, but WITHOUT */
/* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
/* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License */
/* for more details. */
/* */
/* You should have received a copy of the GNU Lesser General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/* You can find information on the NOMAD software at www.gerad.ca/nomad */
/*---------------------------------------------------------------------------------*/
#include "../Param/AllParameters.hpp"
#include "../Type/EvalSortType.hpp"
#include "../Util/fileutils.hpp"
// Do we need to call checkAndComply() ?
bool NOMAD::AllParameters::toBeChecked() const
{
bool check = (!_pbParams || _pbParams->toBeChecked())
|| (!_evalParams || _evalParams->toBeChecked())
|| (!_evaluatorControlGlobalParams || _evaluatorControlGlobalParams->toBeChecked())
|| (!_evaluatorControlParams || _evaluatorControlParams->toBeChecked())
|| (!_runParams || _runParams->toBeChecked())
|| (!_cacheParams || _cacheParams->toBeChecked())
|| (!_dispParams || _dispParams->toBeChecked());
return check;
}
void NOMAD::AllParameters::resetToDefaultValues() noexcept
{
_runParams->resetToDefaultValues();
_pbParams->resetToDefaultValues();
_cacheParams->resetToDefaultValues();
_dispParams->resetToDefaultValues();
_evalParams->resetToDefaultValues();
_evaluatorControlGlobalParams->resetToDefaultValues();
_evaluatorControlParams->resetToDefaultValues();
}
/*----------------------------------------------------------------*/
/* read a parameters file and interpret attributes */
/*----------------------------------------------------------------*/
void NOMAD::AllParameters::read(const std::string ¶mFile, bool overwrite , bool resetAllEntries )
{
// Read all entries
NOMAD::Parameters::readParamFileAndSetEntries(paramFile, overwrite ,resetAllEntries );
// Read entries to detect deprecated entries explicitly set
_deprecatedParams->readAndDetectExplicitlySet();
// Read entries and set attribute values for each type of parameters
_runParams->readEntries();
_pbParams->readEntries(false, NOMAD::dirname(paramFile));
_evalParams->readEntries();
_evaluatorControlGlobalParams->readEntries();
_evaluatorControlParams->readEntries();
_cacheParams->readEntries();
_dispParams->readEntries();
}
/*----------------------------------------------------------------*/
/* read a parameter line, interpret it, and update the links */
/*----------------------------------------------------------------*/
void NOMAD::AllParameters::readParamLine(const std::string &line)
{
// Note: Here we have the line, but we do not know which parameter type
// it applies to. For now, the workaround is to create a temporary
// ParameterEntry and to extract the parameter name from it.
// This could be cleaner, since we will create another ParameterEntry
// in readParamLine().
auto pe = std::make_unique<NOMAD::ParameterEntry>(line);
std::string name = pe->getName();
bool overwrite = true;
if ( _cacheParams->isRegisteredAttribute(name) )
{
_cacheParams->readParamLine(line, overwrite);
}
else if ( _dispParams->isRegisteredAttribute(name) )
{
_dispParams->readParamLine(line, overwrite);
}
else if (_evalParams->isRegisteredAttribute(name))
{
_evalParams->readParamLine(line, overwrite);
}
else if ( _evaluatorControlParams->isRegisteredAttribute(name) )
{
_evaluatorControlParams->readParamLine(line, overwrite);
}
else if ( _evaluatorControlGlobalParams->isRegisteredAttribute(name) )
{
_evaluatorControlGlobalParams->readParamLine(line, overwrite);
}
else if ( _pbParams->isRegisteredAttribute(name) )
{
_pbParams->readParamLine(line, overwrite);
}
else if ( _runParams->isRegisteredAttribute(name) )
{
_runParams->readParamLine(line, overwrite);
}
else
{
std::string err = "Unknown parameter: " + name;
std::cout << err << std::endl;
}
}
void NOMAD::AllParameters::eraseAllEntries()
{
NOMAD::Parameters::eraseAllEntries();
}
bool NOMAD::AllParameters::isAlgoCompatible(const NOMAD::AllParameters& allP_tmp) const
{
return _pbParams->isAlgoCompatible(allP_tmp.getPbParams().get()) &&
_dispParams->isAlgoCompatible(allP_tmp.getDispParams().get()) &&
_runParams->isAlgoCompatible(allP_tmp.getRunParams().get()) &&
_evaluatorControlGlobalParams->isAlgoCompatible(allP_tmp.getEvaluatorControlGlobalParams().get()) &&
_evaluatorControlParams->isAlgoCompatible(allP_tmp.getEvaluatorControlParams().get()) &&
_evalParams->isAlgoCompatible(allP_tmp.getEvalParams().get()) ;
}
/*----------------------------------------------------------------*/
/* read a parameters file and interpret attributes */
/*----------------------------------------------------------------*/
void NOMAD::AllParameters::displayHelp(const std::string &helpSubject , bool devHelp , std::ostream & os )
{
std::ostringstream ossBasic,ossAdvanced;
_evalParams->displayHelp(helpSubject, devHelp, ossBasic , ossAdvanced);
_evaluatorControlGlobalParams->displayHelp(helpSubject,devHelp, ossBasic , ossAdvanced);
_evaluatorControlParams->displayHelp(helpSubject,devHelp, ossBasic , ossAdvanced);
_runParams->displayHelp(helpSubject,devHelp, ossBasic , ossAdvanced);
_pbParams->displayHelp(helpSubject,devHelp, ossBasic , ossAdvanced);
_cacheParams->displayHelp(helpSubject,devHelp,ossBasic , ossAdvanced);
_dispParams->displayHelp(helpSubject,devHelp, ossBasic , ossAdvanced);
if ( !devHelp )
{
if (ossBasic.str().empty() && ossAdvanced.str().empty())
{
if (helpSubject == "_____") // Empty subject
{
os << "The nomad -h command is used to get help on a specific subject." << std::endl;
os << "A keyword for the subject must be provided:"<< std::endl ;
os << " nomad -h keyword" << std::endl << std::endl;
os << "The subject can relate to BASIC or ADVANCED parameters to define " << std::endl ;
os << "an optimization problem, select an algorithm and its settings or" << std::endl ;
os << "manage the outputs." << std::endl << std::endl;
os << "For example, to obtain help on problem bounds: " << std::endl ;
os << " nomad -h bounds OR nomad -h bound." << std::endl << std::endl;
os << "To obtain help on mesh stoping criterions or mesh initialization: " << std::endl ;
os << " nomad -h mesh." << std::endl << std::endl;
os << "Please note that the help will scan all subjects that mention " << std::endl;
os << "the keyword. Hence, for a given keyword, help on several " << std::endl;
os << "parameters is provided." << std::endl << std::endl;
os << "The keyword selection is case insensitive." << std::endl << std::endl;
}
else
os << "No help found for " << helpSubject << std::endl << std::endl;
}
if (!ossBasic.str().empty())
{
os << "-------------------------------------------------------------------------------" << std::endl;
os << "-------------------------------- BASIC PARAMETERS -----------------------------" << std::endl;
os << "-------------------------------------------------------------------------------" << std::endl;
os << std::endl << ossBasic.str() << std::endl << std::endl;
}
if (!ossAdvanced.str().empty())
{
os << "-------------------------------------------------------------------------------" << std::endl;
os << "------------------------------ ADVANCED PARAMETERS ----------------------------" << std::endl;
os << "-------------------------------------------------------------------------------" << std::endl;
os << std::endl << ossAdvanced.str() << std::endl << std::endl;
}
}
else
{
if ( ossBasic.str().empty() )
{
if (helpSubject == "_____") // Empty subject
{
os << "Provide a subject to obtain help: nomad -d keyword" << std::endl << std::endl;
}
else
{
os << "No help found for " << helpSubject << std::endl << std::endl;
}
}
else
{
os << "-------------------------------------------------------------------------------" << std::endl;
os << "----------------------------- DEVELOPER PARAMETERS ----------------------------" << std::endl;
os << "-------------------------------------------------------------------------------" << std::endl;
os << ossBasic.str() << std::endl << std::endl;
}
}
}
void NOMAD::AllParameters::displayCSVDoc(std::ostream & os )
{
std::map<std::string,std::string> csvdoc;
_pbParams->insertCSVDoc(csvdoc);
_evaluatorControlGlobalParams->insertCSVDoc(csvdoc);
_runParams->insertCSVDoc(csvdoc);
_evaluatorControlParams->insertCSVDoc(csvdoc);
_evalParams->insertCSVDoc(csvdoc);
_cacheParams->insertCSVDoc(csvdoc);
_dispParams->insertCSVDoc(csvdoc);
for (const auto & singleEntry : csvdoc )
{
os << singleEntry.first << "," << singleEntry.second << std::endl;
}
}
bool NOMAD::AllParameters::mayUseSurrogate() const
{
if (toBeChecked())
{
throw NOMAD::Exception(__FILE__,__LINE__,"Parameters are not checked");
}
bool sortWithSurrogate = (_evaluatorControlParams->getAttributeValue<NOMAD::EvalSortType>("EVAL_QUEUE_SORT") == NOMAD::EvalSortType::SURROGATE);
bool vnsUseSurrogate = _runParams->getAttributeValue<bool>("VNS_MADS_SEARCH") && _runParams->getAttributeValue<bool>("VNS_MADS_SEARCH_WITH_SURROGATE");
return sortWithSurrogate || vnsUseSurrogate;
}
/*----------------------------------------*/
/* check the parameters */
/*----------------------------------------*/
void NOMAD::AllParameters::checkAndComply()
{
if (!toBeChecked())
{
// Early out
return;
}
_pbParams->checkAndComply();
_evaluatorControlGlobalParams->checkAndComply(_pbParams);
_runParams->checkAndComply(_evaluatorControlGlobalParams, _pbParams);
_evaluatorControlParams->checkAndComply(_evaluatorControlGlobalParams, _runParams);
_evalParams->checkAndComply(_runParams, _pbParams, _evaluatorControlGlobalParams, _evaluatorControlParams);
_cacheParams->checkAndComply(_runParams);
_dispParams->checkAndComply(_runParams, _pbParams);
}
// End checkAndComply()
std::string NOMAD::AllParameters::getSetAttributeAsString() const
{
std::string str = _runParams->getSetAttributeAsString()
+ _pbParams->getSetAttributeAsString()
+ _evalParams->getSetAttributeAsString()
+ _evaluatorControlGlobalParams->getSetAttributeAsString()
+ _evaluatorControlParams->getSetAttributeAsString()
+ _cacheParams->getSetAttributeAsString()
+ _dispParams->getSetAttributeAsString() ;
if ( str.empty() )
str = "All attributes have default value";
return str;
}
void NOMAD::AllParameters::display(std::ostream & os, bool flagHelp )
{
if (toBeChecked())
{
std::cout << "Warning: AllParameters::display(): Parameters are not checked." << std::endl;
}
os << "----- RUN PARAMETERS -----" << std::endl;
_runParams->display(os,flagHelp);
os << "----- PROBLEM PARAMETERS -----" << std::endl;
_pbParams->display(os,flagHelp);
os << "----- EVAL PARAMETERS -----" << std::endl;
_evalParams->display(os,flagHelp);
os << "----- EVALUATOR CONTROL PARAMETERS (GLOBAL) -----" << std::endl;
_evaluatorControlGlobalParams->display(os,flagHelp);
os << "----- EVALUATOR CONTROL PARAMETERS (BY MAIN THREAD)-----" << std::endl;
_evaluatorControlParams->display(os,flagHelp);
os << "----- CACHE PARAMETERS -----" << std::endl;
_cacheParams->display(os,flagHelp);
os << "----- DISPLAY PARAMETERS -----" << std::endl;
_dispParams->display(os,flagHelp);
}