60 #include <boost/filesystem.hpp>
92 GENEMUTATIONPROBABILITY,
94 RECOMBINATIONPROBABILITY,
112 "The \"OPTIMIZE\" command initiates optimization.") {
114 (
"INPUT",
"Path to input file");
116 (
"OUTPUT",
"Name used in output file generation");
118 (
"OUTDIR",
"Name of directory used to store generation output files");
120 (
"OBJECTIVES",
"List of objectives to be used");
122 (
"DVARS",
"List of optimization variables to be used");
124 (
"CONSTRAINTS",
"List of constraints to be used");
126 (
"INITIALPOPULATION",
"Size of the initial population");
128 (
"STARTPOPULATION",
"Generation file (JSON format) to be started from (optional)",
"");
130 (
"NUM_MASTERS",
"Number of master nodes");
132 (
"NUM_COWORKERS",
"Number processors per worker");
134 (
"DUMP_DAT",
"Dump old generation data format with frequency (PISA only)");
136 (
"DUMP_FREQ",
"Dump generation data with frequency (PISA only)");
138 (
"DUMP_OFFSPRING",
"Dump offspring (instead of parent population), default: true");
140 (
"NUM_IND_GEN",
"Number of individuals in a generation (PISA only)");
142 (
"MAXGENERATIONS",
"Number of generations to run");
144 (
"EPSILON",
"Tolerance of hypervolume criteria, default 0.001");
146 (
"EXPECTED_HYPERVOL",
"The reference hypervolume, default 0");
148 (
"HYPERVOLREFERENCE",
"The reference point (real array) for the hypervolume, default empty (origin)");
150 (
"CONV_HVOL_PROG",
"Converge if change in hypervolume is smaller, default 0");
152 (
"ONE_PILOT_CONVERGE",
"default false");
154 (
"SOL_SYNCH",
"Solution exchange frequency, default 0");
156 (
"GENE_MUTATION_PROBABILITY",
"Mutation probability of individual gene, default: 0.5");
158 (
"MUTATION_PROBABILITY",
"Mutation probability of genome, default: 0.5");
160 (
"RECOMBINATION_PROBABILITY",
"Probability for genes to recombine, default: 0.5");
162 (
"SIMBIN_CROSSOVER_NU",
"Simulated binary crossover, default: 2.0");
164 (
"INITIAL_OPTIMIZATION",
"Optimize speed of initial generation, default: false");
166 (
"BIRTH_CONTROL",
"Enforce strict population sizes (or flexible to keep workers busy), default: false");
168 (
"SIMTMPDIR",
"Directory where simulations are run");
170 (
"TEMPLATEDIR",
"Directory where templates are stored");
172 (
"FIELDMAPDIR",
"Directory where field maps are stored");
174 (
"DISTDIR",
"Directory where distributions are stored",
"");
176 (
"CROSSOVER",
"Type of cross over.", {
"BLEND",
"NAIVEONEPOINT",
"NAIVEUNIFORM",
"SIMULATEDBINARY"},
"BLEND");
178 (
"MUTATION",
"Type of bit mutation.", {
"ONEBIT",
"INDEPENDENTBIT"},
"INDEPENDENTBIT");
180 (
"RESTART_FILE",
"H5 file to restart the OPAL simulations from (optional)",
"");
182 (
"RESTART_STEP",
"Restart from given H5 step (optional)",
199 namespace fs = boost::filesystem;
202 opal->setOptimizerFlag();
221 funcs.insert(std::pair<std::string, client::function::type>
225 funcs.insert(std::pair<std::string, client::function::type>
229 funcs.insert(std::pair<std::string, client::function::type>
230 (
"sddsVariableAt", ff));
233 funcs.insert(std::pair<std::string, client::function::type>
237 funcs.insert(std::pair<std::string, client::function::type>
238 (
"maxNormRadialPeak", ff));
241 funcs.insert(std::pair<std::string, client::function::type>
242 (
"numberOfPeaks", ff));
245 funcs.insert(std::pair<std::string, client::function::type>
246 (
"sumErrSqRadialPeak", ff));
249 funcs.insert(std::pair<std::string, client::function::type>
250 (
"probVariableWithID", ff));
252 std::string fname = inputfile.stem().native();
254 funcs.insert(std::pair<std::string, client::function::type>
255 (
"statVariableAt", ff));
258 funcs.insert(std::pair<std::string, client::function::type>
263 std::vector<std::string> arguments(opal->getArguments());
264 std::vector<char*> argv;
265 std::map<unsigned int, std::string> argumentMapper({
266 {INPUT,
"inputfile"},
269 {INITIALPOPULATION,
"initialPopulation"},
270 {STARTPOPULATION,
"start-population"},
271 {NUMMASTERS,
"num-masters"},
272 {NUMCOWORKERS,
"num-coworkers"},
273 {DUMPDAT,
"dump-dat"},
274 {DUMPFREQ,
"dump-freq"},
275 {DUMPOFFSPRING,
"dump-offspring"},
276 {NUMINDGEN,
"num-ind-gen"},
277 {MAXGENERATIONS,
"maxGenerations"},
278 {EPSILON,
"epsilon"},
279 {EXPECTEDHYPERVOL,
"expected-hypervol"},
280 {CONVHVOLPROG,
"conv-hvol-prog"},
281 {ONEPILOTCONVERGE,
"one-pilot-converge"},
282 {SOLSYNCH,
"sol-synch"},
283 {GENEMUTATIONPROBABILITY,
"gene-mutation-probability"},
284 {MUTATIONPROBABILITY,
"mutation-probability"},
285 {RECOMBINATIONPROBABILITY,
"recombination-probability"},
286 {SIMBINCROSSOVERNU,
"simbin-crossover-nu"},
287 {INITIALOPTIMIZATION,
"initial-optimization"},
288 {BIRTHCONTROL,
"birth-control"},
289 {RESTART_FILE,
"restartfile"},
290 {RESTART_STEP,
"restartstep"}
293 auto it = argumentMapper.end();
294 for (
unsigned int i = 0; i <
SIZE; ++ i) {
295 if ((it = argumentMapper.find(i)) != argumentMapper.end()) {
297 if (
type ==
"string") {
300 arguments.push_back(argument);
302 }
else if (
type ==
"real") {
305 size_t last = val.find_last_not_of(
'0');
306 if (val[last] !=
'.') ++ last;
307 val.erase (last, std::string::npos );
308 std::string argument =
"--" + (*it).second +
"=" + val;
309 arguments.push_back(argument);
311 }
else if (
type ==
"logical") {
314 arguments.push_back(argument);
322 "The argument INPUT has to be provided");
326 "The argument INITIALPOPULATION has to be provided");
330 "The argument MAXGENERATIONS has to be provided");
335 "The hypervolume reference point should have the same dimension as the objectives");
340 "No INITIAL_OPTIMIZATION possible when reading initial population from file (STARTPOPULATION)");
345 "No INITIAL_OPTIMIZATION possible with BIRTH_CONTROL");
350 if (dir.is_relative()) {
351 fs::path path = fs::path(std::string(getenv(
"PWD")));
356 if (!fs::exists(dir)) {
357 fs::create_directory(dir);
359 std::string argument =
"--simtmpdir=" + dir.native();
360 arguments.push_back(argument);
365 if (dir.is_relative()) {
366 fs::path path = fs::path(std::string(getenv(
"PWD")));
371 std::string argument =
"--templates=" + dir.native();
372 arguments.push_back(argument);
377 if (dir.is_relative()) {
378 fs::path path = fs::path(std::string(getenv(
"PWD")));
383 setenv(
"FIELDMAPS", dir.c_str(), 1);
388 if (dir.is_relative()) {
389 fs::path path = fs::path(std::string(getenv(
"PWD")));
394 setenv(
"DISTRIBUTIONS", dir.c_str(), 1);
399 for (
size_t i = 0; i < arguments.size(); ++ i) {
400 argv.push_back(
const_cast<char*
>(arguments[i].c_str()));
401 *
gmsg << arguments[i] <<
" ";
405 std::set<std::string> vars;
406 for (
const std::string &
name: dvarsstr) {
408 DVar* dvar =
dynamic_cast<DVar*
>(obj);
409 if (dvar ==
nullptr) {
411 "The design variable " +
name +
" is not known");
418 DVar_t tmp = boost::make_tuple(var, lowerbound, upperbound);
420 auto ret = vars.insert(var);
421 if (ret.second ==
false) {
423 "There is already a design variable with the variable " + var +
" defined");
426 std::set<std::string> objExpressions;
427 for (
const std::string &
name: objectivesstr) {
430 if (objective ==
nullptr) {
432 "The objective " +
name +
" is not known");
438 auto ret = objExpressions.insert(expr);
439 if (ret.second ==
false) {
441 "There is already a objective with the expression " + expr +
" defined");
444 std::set<std::string> constraintExpressions;
445 for (
const std::string &
name: constraintsstr) {
448 if (constraint ==
nullptr) {
450 "The constraint " +
name +
" is not known");
456 auto ret = constraintExpressions.insert(expr);
457 if (ret.second ==
false) {
459 "There is already a constraint with the expression " + expr +
" defined");
465 size_t pos = tmplFile.find_last_of(
"/");
466 if(pos != std::string::npos)
467 tmplFile = tmplFile.substr(pos+1);
468 pos = tmplFile.find(
".");
469 tmplFile = tmplFile.substr(0,pos);
472 std::ifstream infile(tmplFile.c_str());
474 std::map<std::string, short> dvarCheck;
475 for (
auto itr = dvars.begin(); itr != dvars.end(); ++ itr) {
476 dvarCheck.insert(std::make_pair(boost::get<0>(itr->second), 0));
479 while(infile.good()) {
481 std::getline(infile, line,
'\n');
484 for(
auto &check: dvarCheck) {
485 pos = line.find(
"_" + check.first +
"_");
486 if (pos != std::string::npos &&
487 dvarCheck.find(check.first) != dvarCheck.end()) {
488 dvarCheck.at(check.first) = 1;
494 for (
auto itr = dvarCheck.begin(); itr != dvarCheck.end(); ++ itr) {
495 if (itr->second == 0) {
497 "Couldn't find the design variable '" + itr->first +
"' in '" + tmplFile +
"'!");
507 this->
run(args, funcs, dvars, objectives, constraints);
510 std::cout <<
"Exception caught: " <<
e.what() <<
std::endl;
511 MPI_Abort(MPI_COMM_WORLD, -100);
531 std::map<std::string, CrossOver> map;
532 map[
"BLEND"] = CrossOver::Blend;
533 map[
"NAIVEONEPOINT"] = CrossOver::NaiveOnePoint;
534 map[
"NAIVEUNIFORM"] = CrossOver::NaiveUniform;
535 map[
"SIMULATEDBINARY"] = CrossOver::SimulatedBinary;
539 switch ( map[crossover] ) {
540 case CrossOver::Blend:
542 case CrossOver::NaiveOnePoint:
543 co = CrossOver::NaiveOnePoint;
545 case CrossOver::NaiveUniform:
546 co = CrossOver::NaiveUniform;
548 case CrossOver::SimulatedBinary:
549 co = CrossOver::SimulatedBinary;
553 "No cross over '" + crossover +
"' supported.");
560 std::map<std::string, Mutation> map;
561 map[
"INDEPENDENTBIT"] = Mutation::IndependentBit;
562 map[
"ONEBIT"] = Mutation::OneBit;
564 Mutation mut = Mutation::IndependentBit;
566 switch ( map[mutation] ) {
567 case Mutation::IndependentBit:
569 case Mutation::OneBit:
570 mut = Mutation::OneBit;
574 "No mutation '" + mutation +
"' supported.");
591 boost::shared_ptr<Comm_t> comm(
new Comm_t(args, MPI_COMM_WORLD));
592 if (comm->isWorker())
601 switch ( crossover + mutation ) {
602 case CrossOver::Blend + Mutation::IndependentBit:
607 boost::scoped_ptr<pilot_t>
pi(
new pilot_t(args, comm,
609 objectives, constraints,
611 true, userVariables));
614 case CrossOver::Blend + Mutation::OneBit:
619 boost::scoped_ptr<pilot_t>
pi(
new pilot_t(args, comm,
621 objectives, constraints,
623 true, userVariables));
626 case CrossOver::NaiveOnePoint + Mutation::IndependentBit:
631 boost::scoped_ptr<pilot_t>
pi(
new pilot_t(args, comm,
633 objectives, constraints,
635 true, userVariables));
638 case CrossOver::NaiveOnePoint + Mutation::OneBit:
643 boost::scoped_ptr<pilot_t>
pi(
new pilot_t(args, comm,
645 objectives, constraints,
647 true, userVariables));
650 case CrossOver::NaiveUniform + Mutation::IndependentBit:
655 boost::scoped_ptr<pilot_t>
pi(
new pilot_t(args, comm,
657 objectives, constraints,
659 true, userVariables));
662 case CrossOver::NaiveUniform + Mutation::OneBit:
667 boost::scoped_ptr<pilot_t>
pi(
new pilot_t(args, comm,
669 objectives, constraints,
671 true, userVariables));
674 case CrossOver::SimulatedBinary + Mutation::IndependentBit:
679 boost::scoped_ptr<pilot_t>
pi(
new pilot_t(args, comm,
681 objectives, constraints,
683 true, userVariables));
686 case CrossOver::SimulatedBinary + Mutation::OneBit:
691 boost::scoped_ptr<pilot_t>
pi(
new pilot_t(args, comm,
693 objectives, constraints,
695 true, userVariables));
700 "No such cross over and mutation combination supported.");
703 if (comm->isWorker())
T::PETE_Expr_t::PETE_Return_t min(const PETE_Expr< T > &expr, NDIndex< D > &loc)
Inform & endl(Inform &inf)
std::map< std::string, client::function::type > functionDictionary_t
boost::shared_ptr< CmdArguments > CmdArguments_t
boost::tuple< std::string, double, double > DVar_t
type of design variables
std::map< std::string, DVar_t > DVarContainer_t
std::pair< std::string, DVar_t > namedDVar_t
std::map< std::string, Expressions::Expr_t * > Named_t
type of an expressions with a name
std::pair< std::string, Expressions::Expr_t * > SingleNamed_t
Attribute makeBool(const std::string &name, const std::string &help)
Make logical attribute.
double getReal(const Attribute &attr)
Return real value.
Attribute makeStringArray(const std::string &name, const std::string &help)
Create a string array attribute.
Attribute makePredefinedString(const std::string &name, const std::string &help, const std::initializer_list< std::string > &predefinedStrings)
Make predefined string attribute.
Attribute makeReal(const std::string &name, const std::string &help)
Make real attribute.
bool getBool(const Attribute &attr)
Return logical value.
Attribute makeRealArray(const std::string &name, const std::string &help)
Create real array attribute.
std::vector< double > getRealArray(const Attribute &attr)
Get array value.
std::vector< std::string > getStringArray(const Attribute &attr)
Get string array value.
std::string getString(const Attribute &attr)
Get string value.
Attribute makeString(const std::string &name, const std::string &help)
Make string attribute.
constexpr double e
The value of.
boost::function< boost::tuple< double, bool >arguments_t)> type
double FromFile(std::string file, const std::vector< double > &referencePoint)
The base class for all OPAL actions.
The base class for all OPAL objects.
void registerOwnership(const AttributeHandler::OwnerType &itsClass) const
std::vector< Attribute > itsAttr
The object attributes.
std::map< std::string, std::string > getVariableData()
static void stashInstance()
static OpalData * getInstance()
static OpalData * popInstance()
std::string getExpression() const
std::string getVariable() const
double getUpperBound() const
double getLowerBound() const
std::string getExpression() const
Concrete implementation of an Opal simulation wrapper.
virtual OptimizeCmd * clone(const std::string &name)
Make clone.
CrossOver crossoverSelection(std::string crossover)
Mutation mutationSelection(std::string mutation)
void run(const CmdArguments_t &args, const functionDictionary_t &funcs, const DVarContainer_t &dvars, const Expressions::Named_t &objectives, const Expressions::Named_t &constraints)
virtual void execute()
Execute the command.
OptimizeCmd()
Exemplar constructor.
The base class for all OPAL exceptions.