Commit 6e19d56c authored by frey_m's avatar frey_m
Browse files

Merge branch '239-remove-requirement-of-data-file-for-sampler' into 'master'

Resolve "Remove requirement of data file for SAMPLER and OPTIMIZER"

Closes #239

See merge request !288
parents b1197220 402cb0b1
......@@ -113,7 +113,8 @@ public:
const Expressions::Named_t &obj,
const Expressions::Named_t &cons,
std::vector<double> hypervolRef = {},
bool isOptimizerRun = true)
bool isOptimizerRun = true,
const std::map<std::string, std::string> &userVariables = {})
: Poller(comm->mpiComm())
, comm_(comm)
, cmd_args_(args)
......@@ -123,7 +124,7 @@ public:
, hypervolRef_(hypervolRef)
{
if (isOptimizerRun)
setup(known_expr_funcs);
setup(known_expr_funcs, userVariables);
}
virtual ~Pilot()
......@@ -185,7 +186,8 @@ protected:
boost::scoped_ptr<Trace> job_trace_;
private:
void setup(functionDictionary_t known_expr_funcs) {
void setup(functionDictionary_t known_expr_funcs,
const std::map<std::string, std::string> &userVariables) {
global_rank_ = comm_->globalRank();
if(global_rank_ == 0) {
......@@ -210,7 +212,7 @@ private:
// here the control flow starts to diverge
if ( comm_->isOptimizer() ) { startOptimizer(); }
else if ( comm_->isWorker() ) { startWorker(); }
else if ( comm_->isWorker() ) { startWorker(userVariables); }
else if ( comm_->isPilot() ) { startPilot(); }
}
......@@ -267,7 +269,7 @@ protected:
}
virtual
void startWorker() {
void startWorker(const std::map<std::string, std::string> &userVariables) {
std::ostringstream os;
os << "\033[01;35m" << " " << global_rank_ << " (PID: " << getpid() << ") ▶ Worker"
......@@ -283,7 +285,7 @@ protected:
boost::scoped_ptr< Worker<Sim_t> > w(
new Worker<Sim_t>(objectives_, constraints_, simName,
comm_->getBundle(), cmd_args_));
comm_->getBundle(), cmd_args_, userVariables));
std::cout << "Stop Worker.." << std::endl;
}
......
......@@ -51,9 +51,11 @@ public:
std::string simName,
Comm::Bundle_t comms,
CmdArguments_t args,
const std::map<std::string, std::string> &userVariables,
bool isOptimizer = true)
: Poller(comms.worker)
, cmd_args_(args)
, userVariables_(userVariables)
{
objectives_ = objectives;
constraints_ = constraints;
......@@ -121,8 +123,8 @@ protected:
try {
SimPtr_t sim(new Sim_t(objectives_, constraints_,
params, simulation_name_, coworker_comm_,
cmd_args_));
params, simulation_name_, coworker_comm_,
cmd_args_, userVariables_));
sim->run();
} catch(OptPilotException &ex) {
......@@ -151,6 +153,8 @@ protected:
std::string simulation_name_;
CmdArguments_t cmd_args_;
const std::map<std::string, std::string> userVariables_;
/// notify coworkers of incoming broadcast
void notifyCoWorkers(int tag) {
......@@ -203,7 +207,8 @@ protected:
reqVarContainer_t requested_results;
try {
SimPtr_t sim(new Sim_t(objectives_, constraints_,
params, simulation_name_, coworker_comm_, cmd_args_));
params, simulation_name_, coworker_comm_, cmd_args_,
userVariables_));
// run simulation in a "blocking" fashion
sim->run();
......
......@@ -10,6 +10,7 @@
#include <vector>
#include <boost/algorithm/string.hpp>
#include "boost/filesystem.hpp"
#include "Util/OptPilotException.h"
......@@ -126,6 +127,12 @@ private:
*/
void fillDictionary() {
namespace fs = boost::filesystem;
fs::path pwd = fs::current_path();
if (!fs::exists(varDictionary_) || fs::is_empty(varDictionary_))
return;
std::ifstream infile;
infile.open(varDictionary_.c_str(), std::ifstream::in);
......
......@@ -746,72 +746,44 @@ void OpalData::update() {
}
}
std::vector<std::string> OpalData::getAllNames() {
std::vector<std::string> result;
for(ObjectDir::const_iterator index = p->mainDirectory.begin();
index != p->mainDirectory.end(); ++index) {
std::string tmpName = (*index).first;
if(!tmpName.empty()) result.push_back(tmpName);
//// DTA
//if (!tmpName.empty()) {
// Object *tmpObject = OPAL.find(tmpName);
// std::cerr << tmpObject->getCategory() << "\t" << tmpName << "\t";
// string bi = (tmpObject->isBuiltin()) ? "BUILT-IN" : "";
// std::cerr << bi << std::endl;
//}
//// /DTA
std::map<std::string, std::string> OpalData::getVariableData() {
std::map<std::string, std::string> udata;
std::vector<std::string> uvars = this->getVariableNames();
for (auto& uvar : uvars) {
Object *tmpObject = OpalData::getInstance()->find(uvar);
if (dynamic_cast<RealVariable*>(tmpObject)) {
RealVariable* variable = dynamic_cast<RealVariable*>(OpalData::getInstance()->find(uvar));
udata[uvar] = std::to_string(variable->getReal());
} else if (dynamic_cast<StringConstant*>(tmpObject)) {
StringConstant* variable = dynamic_cast<StringConstant*>(OpalData::getInstance()->find(uvar));
udata[uvar] = variable->getString();
} else {
throw OpalException("OpalData::getVariableData()",
"Type of '" + uvar + "' not supported. "
"Only support for REAL and STRING.");
}
}
return udata;
}
std::vector<std::string> OpalData::getVariableNames() {
std::vector<std::string> result;
// DTA
std::cout << "\nUser-defined variables:\n";
const OpalParser mp;
// FileStream *is;
// is = new FileStream("/home/research/dabell/projects/opal9/src/tmp/myExpr.txt");
// StringStream *ss;
// ss = new StringStream("myVar:=ALPHA+BETA;");
for(ObjectDir::const_iterator index = p->mainDirectory.begin();
index != p->mainDirectory.end(); ++index) {
std::string tmpName = (*index).first;
if(!tmpName.empty()) {
if (!tmpName.empty()) {
Object *tmpObject = OpalData::getInstance()->find(tmpName);
if(!tmpObject || tmpObject->isBuiltin()) continue;
if(tmpObject->getCategory() == "VARIABLE") {
std::cout << tmpName;
if(dynamic_cast<RealVariable *>(&*tmpObject)) {
RealVariable &variable = *dynamic_cast<RealVariable *>(OpalData::getInstance()->find(tmpName));
std::cout << "\te= " << variable.value().getBase().getImage();
std::cout << "\tr= " << variable.getReal();
std::cout << "\ta= " << variable.itsAttr[0];
//Attributes::setReal(variable.itsAttr[0],137.);
//std::cout << "\te= " << variable.value().getBase().getImage();
//std::cout << "\tx= " << variable.itsAttr[0];
if (tmpObject) {
if (!tmpObject || tmpObject->isBuiltin())
continue;
if (tmpObject->getCategory() == "VARIABLE") {
result.push_back(tmpName);
}
std::cout << std::endl;
}
// if(tmpName=="MYL"){
// std::cout << "myL noted" << std::endl;
// RealVariable& variable = *dynamic_cast<RealVariable*>(OPAL.find(tmpName));
// std::cout << tmpName;
// std::cout << "\te= " << variable.value().getBase().getImage();
// std::cout << "\tr= " << variable.getReal();
// std::cout << "\ta= " << variable.itsAttr[0] << std::endl;
// mp.run(is);
// std::cout << tmpName;
// std::cout << "\te= " << variable.value().getBase().getImage();
// std::cout << "\tr= " << variable.getReal();
// std::cout << "\ta= " << variable.itsAttr[0] << std::endl;
// mp.run(ss);
// }
}
}
std::cout << std::endl;
////TxAttributeSet data(variableName);
////const RealVariable& var = *dynamic_cast<RealVariable*>(OPAL.find(variableName));
////data.appendString("expression",variable.value().getBase().getImage());
////data.appendParam("value",variable.getReal());
//// /DTA
return result;
}
......
......@@ -151,7 +151,8 @@ public:
virtual void operator()(Object *) const;
};
std::vector<std::string> getAllNames();
std::map<std::string, std::string> getVariableData();
std::vector<std::string> getVariableNames();
bool isInOPALCyclMode();
bool isInOPALTMode();
......
......@@ -40,7 +40,8 @@
OpalSimulation::OpalSimulation(Expressions::Named_t objectives,
Expressions::Named_t constraints,
Param_t params, std::string name,
MPI_Comm comm, CmdArguments_t args)
MPI_Comm comm, CmdArguments_t args,
std::map<std::string, std::string> uvars)
: Simulation(args)
, objectives_(objectives)
, constraints_(constraints)
......@@ -120,17 +121,16 @@ OpalSimulation::OpalSimulation(Expressions::Named_t objectives,
std::string tmplFile = tmplDir + "/" + simulationName_ + ".tmpl";
// data file is assumed to be located in the root directory
std::string dataFile = simulationName_ + ".data";
fs::path pwd = fs::current_path();
if (!fs::exists(dataFile))
throw OptPilotException("OpalSimulation::OpalSimulation",
"The data file '" + dataFile + "' \n" +
" doesn't exist in directory '" + pwd.native() + "'");
if (!fs::exists(tmplFile))
throw OptPilotException("OpalSimulation::OpalSimulation",
"The template file '" + tmplFile + "' doesn't exit");
gs_.reset(new GenerateOpalSimulation(tmplFile, dataFile, userVariables_));
for (const auto& uvar : userVariables_) {
uvars[uvar.first] = uvar.second;
}
gs_.reset(new GenerateOpalSimulation(tmplFile, dataFile, uvars));
}
......
......@@ -40,11 +40,13 @@ public:
* @param[in] name of the simulation
* @param[in] comm MPI communicator used for running the simulation
* @param[in] args command line arguments passed to the framework
* @param[in] uvars defined in the input file (e.g. REAL a = 1.0)
*/
OpalSimulation(Expressions::Named_t objectives,
Expressions::Named_t constraints,
Param_t params, std::string name, MPI_Comm comm,
CmdArguments_t args);
CmdArguments_t args,
std::map<std::string, std::string> uvars);
virtual ~OpalSimulation();
......
......@@ -577,6 +577,9 @@ void OptimizeCmd::run(const CmdArguments_t& args,
CrossOver crossover = this->crossoverSelection(Attributes::getString(itsAttr[CROSSOVER]));
Mutation mutation = this->mutationSelection(Attributes::getString(itsAttr[MUTATION]));
std::map<std::string, std::string> userVariables = OpalData::getInstance()->getVariableData();
switch ( crossover + mutation ) {
case CrossOver::Blend + Mutation::IndependentBit:
{
......@@ -586,7 +589,8 @@ void OptimizeCmd::run(const CmdArguments_t& args,
boost::scoped_ptr<pilot_t> pi(new pilot_t(args, comm,
funcs, dvars,
objectives, constraints,
Attributes::getRealArray(itsAttr[HYPERVOLREFERENCE])));
Attributes::getRealArray(itsAttr[HYPERVOLREFERENCE]),
true, userVariables));
break;
}
case CrossOver::Blend + Mutation::OneBit:
......@@ -597,7 +601,8 @@ void OptimizeCmd::run(const CmdArguments_t& args,
boost::scoped_ptr<pilot_t> pi(new pilot_t(args, comm,
funcs, dvars,
objectives, constraints,
Attributes::getRealArray(itsAttr[HYPERVOLREFERENCE])));
Attributes::getRealArray(itsAttr[HYPERVOLREFERENCE]),
true, userVariables));
break;
}
case CrossOver::NaiveOnePoint + Mutation::IndependentBit:
......@@ -608,7 +613,8 @@ void OptimizeCmd::run(const CmdArguments_t& args,
boost::scoped_ptr<pilot_t> pi(new pilot_t(args, comm,
funcs, dvars,
objectives, constraints,
Attributes::getRealArray(itsAttr[HYPERVOLREFERENCE])));
Attributes::getRealArray(itsAttr[HYPERVOLREFERENCE]),
true, userVariables));
break;
}
case CrossOver::NaiveOnePoint + Mutation::OneBit:
......@@ -619,7 +625,8 @@ void OptimizeCmd::run(const CmdArguments_t& args,
boost::scoped_ptr<pilot_t> pi(new pilot_t(args, comm,
funcs, dvars,
objectives, constraints,
Attributes::getRealArray(itsAttr[HYPERVOLREFERENCE])));
Attributes::getRealArray(itsAttr[HYPERVOLREFERENCE]),
true, userVariables));
break;
}
case CrossOver::NaiveUniform + Mutation::IndependentBit:
......@@ -630,7 +637,8 @@ void OptimizeCmd::run(const CmdArguments_t& args,
boost::scoped_ptr<pilot_t> pi(new pilot_t(args, comm,
funcs, dvars,
objectives, constraints,
Attributes::getRealArray(itsAttr[HYPERVOLREFERENCE])));
Attributes::getRealArray(itsAttr[HYPERVOLREFERENCE]),
true, userVariables));
break;
}
case CrossOver::NaiveUniform + Mutation::OneBit:
......@@ -641,7 +649,8 @@ void OptimizeCmd::run(const CmdArguments_t& args,
boost::scoped_ptr<pilot_t> pi(new pilot_t(args, comm,
funcs, dvars,
objectives, constraints,
Attributes::getRealArray(itsAttr[HYPERVOLREFERENCE])));
Attributes::getRealArray(itsAttr[HYPERVOLREFERENCE]),
true, userVariables));
break;
}
case CrossOver::SimulatedBinary + Mutation::IndependentBit:
......@@ -652,7 +661,8 @@ void OptimizeCmd::run(const CmdArguments_t& args,
boost::scoped_ptr<pilot_t> pi(new pilot_t(args, comm,
funcs, dvars,
objectives, constraints,
Attributes::getRealArray(itsAttr[HYPERVOLREFERENCE])));
Attributes::getRealArray(itsAttr[HYPERVOLREFERENCE]),
true, userVariables));
break;
}
case CrossOver::SimulatedBinary + Mutation::OneBit:
......@@ -663,7 +673,8 @@ void OptimizeCmd::run(const CmdArguments_t& args,
boost::scoped_ptr<pilot_t> pi(new pilot_t(args, comm,
funcs, dvars,
objectives, constraints,
Attributes::getRealArray(itsAttr[HYPERVOLREFERENCE])));
Attributes::getRealArray(itsAttr[HYPERVOLREFERENCE]),
true, userVariables));
break;
}
default:
......
......@@ -438,6 +438,8 @@ void SampleCmd::execute() {
}
*gmsg << endl;
std::map<std::string, std::string> userVariables = opal->getVariableData();
Inform *origGmsg = gmsg;
gmsg = 0;
try {
......@@ -457,7 +459,7 @@ void SampleCmd::execute() {
boost::scoped_ptr<pilot_t> pi(new pilot_t(args, comm, funcs, dvars,
objectives, sampleMethods,
storeobjstr, filesToKeep));
storeobjstr, filesToKeep, userVariables));
if (comm->isWorker())
popEnvironment();
......
......@@ -46,7 +46,8 @@ public:
std::shared_ptr<SamplingMethod>
>& sampleMethods,
const std::vector<std::string> &storeobjstr,
const std::vector<std::string> &filesToKeep)
const std::vector<std::string> &filesToKeep,
const std::map<std::string, std::string> &userVariables)
: Pilot<Opt_t,
Sim_t,
SolPropagationGraph_t,
......@@ -57,7 +58,8 @@ public:
obj,
Expressions::Named_t(),
{},
false)
false,
{})
, sampleMethods_m(sampleMethods)
{
if (obj.size() == 0) {
......@@ -67,7 +69,7 @@ public:
};
}
this->setup(known_expr_funcs, storeobjstr, filesToKeep);
this->setup(known_expr_funcs, storeobjstr, filesToKeep, userVariables);
}
virtual ~SamplePilot()
......@@ -86,7 +88,8 @@ protected:
virtual
void setup(functionDictionary_t known_expr_funcs,
const std::vector<std::string> &storeobjstr,
const std::vector<std::string> &filesToKeep)
const std::vector<std::string> &filesToKeep,
const std::map<std::string, std::string> &userVariables)
{
this->global_rank_ = this->comm_->globalRank();
......@@ -96,7 +99,7 @@ protected:
// here the control flow starts to diverge
if ( this->comm_->isOptimizer() ) { startSampler(); }
else if ( this->comm_->isWorker() ) { startWorker(storeobjstr, filesToKeep); }
else if ( this->comm_->isWorker() ) { startWorker(storeobjstr, filesToKeep, userVariables); }
else if ( this->comm_->isPilot() ) { this->startPilot(); }
}
......@@ -118,7 +121,8 @@ protected:
using Pilot<Opt_t, Sim_t, SolPropagationGraph_t, Comm_t>::startWorker;
void startWorker(const std::vector<std::string> &storeobjstr,
const std::vector<std::string> &filesToKeep)
const std::vector<std::string> &filesToKeep,
const std::map<std::string, std::string> &userVariables)
{
std::ostringstream os;
os << "\033[01;35m" << " " << this->global_rank_ << " (PID: " << getpid() << ") ▶ Worker"
......@@ -135,7 +139,7 @@ protected:
boost::scoped_ptr< SampleWorker<Sim_t> > w(
new SampleWorker<Sim_t>(this->objectives_, this->constraints_, simName,
this->comm_->getBundle(), this->cmd_args_,
storeobjstr, filesToKeep));
storeobjstr, filesToKeep, userVariables));
std::cout << "Stop Worker.." << std::endl;
}
......
......@@ -27,8 +27,9 @@ public:
Comm::Bundle_t comms,
CmdArguments_t args,
const std::vector<std::string> &storeobjstr,
const std::vector<std::string> &filesToKeep)
: Worker<Sim_t>(objectives, constraints, simName, comms, args, false)
const std::vector<std::string> &filesToKeep,
const std::map<std::string, std::string> &userVariables)
: Worker<Sim_t>(objectives, constraints, simName, comms, args, userVariables, false)
, statVariablesToStore_m(storeobjstr)
, filesToKeep_m(filesToKeep)
{
......@@ -94,9 +95,10 @@ protected:
MPI_Bcast_params(params, this->leader_pid_, this->coworker_comm_);
try {
typename Worker<Sim_t>::SimPtr_t sim(new Sim_t(this->objectives_, this->constraints_,
params, this->simulation_name_, this->coworker_comm_,
this->cmd_args_));
typename Worker<Sim_t>::SimPtr_t sim(
new Sim_t(this->objectives_, this->constraints_,
params, this->simulation_name_, this->coworker_comm_,
this->cmd_args_, this->userVariables_));
sim->setFilename(job_id);
......@@ -138,11 +140,12 @@ protected:
reqVarContainer_t requested_results;
try {
typename Worker<Sim_t>::SimPtr_t sim(new Sim_t(this->objectives_,
this->constraints_,
params,
this->simulation_name_,
this->coworker_comm_,
this->cmd_args_));
this->constraints_,
params,
this->simulation_name_,
this->coworker_comm_,
this->cmd_args_,
this->userVariables_));
sim->setFilename(job_id);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment