197 namespace fs = std::filesystem;
200 opal->setOptimizerFlag();
219 funcs.insert(std::pair<std::string, client::function::type>
223 funcs.insert(std::pair<std::string, client::function::type>
227 funcs.insert(std::pair<std::string, client::function::type>
228 (
"sddsVariableAt", ff));
231 funcs.insert(std::pair<std::string, client::function::type>
235 funcs.insert(std::pair<std::string, client::function::type>
236 (
"maxNormRadialPeak", ff));
239 funcs.insert(std::pair<std::string, client::function::type>
240 (
"numberOfPeaks", ff));
243 funcs.insert(std::pair<std::string, client::function::type>
244 (
"sumErrSqRadialPeak", ff));
247 funcs.insert(std::pair<std::string, client::function::type>
248 (
"probVariableWithID", ff));
250 std::string fname = inputfile.stem().native();
252 funcs.insert(std::pair<std::string, client::function::type>
253 (
"statVariableAt", ff));
256 funcs.insert(std::pair<std::string, client::function::type>
261 std::vector<std::string> arguments(opal->getArguments());
262 std::vector<char*> argv;
263 std::map<unsigned int, std::string> argumentMapper({
264 {INPUT,
"inputfile"},
267 {INITIALPOPULATION,
"initialPopulation"},
268 {STARTPOPULATION,
"start-population"},
269 {NUMMASTERS,
"num-masters"},
270 {NUMCOWORKERS,
"num-coworkers"},
271 {DUMPDAT,
"dump-dat"},
272 {DUMPFREQ,
"dump-freq"},
273 {DUMPOFFSPRING,
"dump-offspring"},
274 {NUMINDGEN,
"num-ind-gen"},
275 {MAXGENERATIONS,
"maxGenerations"},
276 {EPSILON,
"epsilon"},
277 {EXPECTEDHYPERVOL,
"expected-hypervol"},
278 {CONVHVOLPROG,
"conv-hvol-prog"},
279 {ONEPILOTCONVERGE,
"one-pilot-converge"},
280 {SOLSYNCH,
"sol-synch"},
281 {GENEMUTATIONPROBABILITY,
"gene-mutation-probability"},
282 {MUTATIONPROBABILITY,
"mutation-probability"},
283 {RECOMBINATIONPROBABILITY,
"recombination-probability"},
284 {SIMBINCROSSOVERNU,
"simbin-crossover-nu"},
285 {INITIALOPTIMIZATION,
"initial-optimization"},
286 {BIRTHCONTROL,
"birth-control"},
287 {RESTART_FILE,
"restartfile"},
288 {RESTART_STEP,
"restartstep"}
291 auto it = argumentMapper.end();
292 for (
unsigned int i = 0; i <
SIZE; ++ i) {
293 if ((it = argumentMapper.find(i)) != argumentMapper.end()) {
295 if (
type ==
"string") {
298 arguments.push_back(argument);
300 }
else if (
type ==
"real") {
303 size_t last = val.find_last_not_of(
'0');
304 if (val[last] !=
'.') ++ last;
305 val.erase (last, std::string::npos );
306 std::string argument =
"--" + (*it).second +
"=" + val;
307 arguments.push_back(argument);
309 }
else if (
type ==
"logical") {
312 arguments.push_back(argument);
320 "The argument INPUT has to be provided");
324 "The argument INITIALPOPULATION has to be provided");
328 "The argument MAXGENERATIONS has to be provided");
333 "The hypervolume reference point should have the same dimension as the objectives");
338 "No INITIAL_OPTIMIZATION possible when reading initial population from file (STARTPOPULATION)");
343 "No INITIAL_OPTIMIZATION possible with BIRTH_CONTROL");
348 if (dir.is_relative()) {
349 fs::path path = fs::path(std::string(getenv(
"PWD")));
354 if (!fs::exists(dir)) {
355 fs::create_directory(dir);
357 std::string argument =
"--simtmpdir=" + dir.native();
358 arguments.push_back(argument);
363 if (dir.is_relative()) {
364 fs::path path = fs::path(std::string(getenv(
"PWD")));
369 std::string argument =
"--templates=" + dir.native();
370 arguments.push_back(argument);
375 if (dir.is_relative()) {
376 fs::path path = fs::path(std::string(getenv(
"PWD")));
381 setenv(
"FIELDMAPS", dir.c_str(), 1);
386 if (dir.is_relative()) {
387 fs::path path = fs::path(std::string(getenv(
"PWD")));
392 setenv(
"DISTRIBUTIONS", dir.c_str(), 1);
397 for (
size_t i = 0; i < arguments.size(); ++ i) {
398 argv.push_back(
const_cast<char*
>(arguments[i].c_str()));
399 *
gmsg << arguments[i] <<
" ";
403 std::set<std::string> vars;
404 for (
const std::string &
name: dvarsstr) {
406 DVar* dvar =
dynamic_cast<DVar*
>(obj);
407 if (dvar ==
nullptr) {
409 "The design variable " +
name +
" is not known");
416 DVar_t tmp = boost::make_tuple(var, lowerbound, upperbound);
418 auto ret = vars.insert(var);
419 if (ret.second ==
false) {
421 "There is already a design variable with the variable " + var +
" defined");
424 std::set<std::string> objExpressions;
425 for (
const std::string &
name: objectivesstr) {
428 if (objective ==
nullptr) {
430 "The objective " +
name +
" is not known");
436 auto ret = objExpressions.insert(expr);
437 if (ret.second ==
false) {
439 "There is already a objective with the expression " + expr +
" defined");
442 std::set<std::string> constraintExpressions;
443 for (
const std::string &
name: constraintsstr) {
446 if (constraint ==
nullptr) {
448 "The constraint " +
name +
" is not known");
454 auto ret = constraintExpressions.insert(expr);
455 if (ret.second ==
false) {
457 "There is already a constraint with the expression " + expr +
" defined");
463 size_t pos = tmplFile.find_last_of(
"/");
464 if(pos != std::string::npos)
465 tmplFile = tmplFile.substr(pos+1);
466 pos = tmplFile.find(
".");
467 tmplFile = tmplFile.substr(0,pos);
470 std::ifstream infile(tmplFile.c_str());
472 std::map<std::string, short> dvarCheck;
473 for (
auto itr = dvars.begin(); itr != dvars.end(); ++ itr) {
474 dvarCheck.insert(std::make_pair(boost::get<0>(itr->second), 0));
477 while(infile.good()) {
479 std::getline(infile, line,
'\n');
482 for(
auto &check: dvarCheck) {
483 pos = line.find(
"_" + check.first +
"_");
484 if (pos != std::string::npos &&
485 dvarCheck.find(check.first) != dvarCheck.end()) {
486 dvarCheck.at(check.first) = 1;
492 for (
auto itr = dvarCheck.begin(); itr != dvarCheck.end(); ++ itr) {
493 if (itr->second == 0) {
495 "Couldn't find the design variable '" + itr->first +
"' in '" + tmplFile +
"'!");
505 this->
run(args, funcs, dvars, objectives, constraints);
508 std::cout <<
"Exception caught: " << e.what() << std::endl;
509 MPI_Abort(MPI_COMM_WORLD, -100);