29#include "OPALconfig.h"
35#include <boost/property_tree/json_parser.hpp>
36#include <boost/property_tree/ptree.hpp>
48 "We shouldn't get here!");
53 std::shared_ptr<SamplingMethod>
69 std::string resultFile = args->
getArg<std::string>(
"outfile",
"output",
false);
70 std::string resultDir = args->
getArg<std::string>(
"outdir",
"samples",
false);
73 std::ostringstream filename;
74 filename << resultDir <<
"/" << resultFile
75 <<
"_samples_" <<
comms_.island_id <<
".json";
78 if ( !std::filesystem::exists(resultDir) ) {
79 std::filesystem::create_directory(resultDir);
82 DVarContainer_t::iterator itr;
85 std::pair<double, double>
86 (boost::get<LOWER_BOUND>(itr->second),
87 boost::get<UPPER_BOUND>(itr->second)));
101 int nMasters =
args_->getArg<
int>(
"num-masters",
true);
105 "More masters than samples.");
109 int rest =
nSamples_m - nMasters * nLocSamples;
111 if (
comms_.island_id < rest )
117 if (
comms_.island_id < rest ) {
120 gid = (nLocSamples + 1) * rest + (
comms_.island_id - rest) * nLocSamples;
135 unsigned int jid =
static_cast<unsigned int>(length);
136 typename std::map<size_t, std::shared_ptr<Individual_t> >
::iterator it;
140 std::cout <<
"NON-EXISTING JOB with ID = " << jid << std::endl;
146 std::shared_ptr<Individual_t> ind = it->second;
151 ind->objectives.clear();
153 reqVarContainer_t::iterator itr = res.begin();
154 for(; itr != res.end(); ++itr) {
156 if(!itr->second.is_valid || (itr->second.value.size() > 1 && !itr->second.value[0])) {
157 ind->objectives.push_back(std::numeric_limits<double>::infinity());
160 if(itr->second.value.size() == 1)
161 ind->objectives.push_back(itr->second.value[0]);
174 std::cout <<
"(Sampler) Error: unexpected MPI_TAG: "
175 << status.MPI_TAG << std::endl;
194 std::vector<std::string> dNames;
196 DVarContainer_t::iterator itr;
198 std::string dName = boost::get<VAR_NAME>(itr->second);
199 dNames.push_back(dName);
202 std::shared_ptr<Individual_t> ind = std::shared_ptr<Individual_t>(
new Individual_t(dNames));
207 std::string dName = boost::get<VAR_NAME>(itr->second);
208 int i = ind->getIndex(dName);
218 namespace pt = boost::property_tree;
222 tree.put(
"name",
"sampler");
223 tree.put(OPAL_PROJECT_NAME
" version", OPAL_PROJECT_VERSION);
227 DVarContainer_t::iterator itr =
dvars_m.begin();
231 std::string dvar = boost::get<VAR_NAME>(itr->second);
232 bounds <<
"[ " << it->first <<
", " << it->second <<
" ]";
233 tree.put(
"dvar-bounds." + dvar,
bounds.str());
247 namespace pt = boost::property_tree;
254 if ( tree.get_optional<std::string>(
"samples") ) {
258 samples = tree.get_child(
"samples");
259 tree.erase(
"samples");
266 std::string
id = std::to_string(ind.
id);
268 DVarContainer_t::iterator itr;
270 std::string
name = boost::get<VAR_NAME>(itr->second);
272 samples.put(
id +
".dvar." +
name, ind.
genes[i]);
275 Expressions::Named_t::iterator expr_it;
278 for(
size_t i=0; i < ind.
objectives.size(); i++, expr_it++) {
279 std::string
name = expr_it->first;
282 if (
name ==
"dummy" )
289 tree.add_child(
"samples", samples);
290 boost::property_tree::write_json(
jsonFname_m, tree);
326 MPI_Isend(&dummy, 1, MPI_INT,
comms_.master_local_pid,
347 DVarContainer_t::iterator itr;
350 std::string dName = boost::get<VAR_NAME>(itr->second);
351 int i = ind->getIndex(dName);
353 std::pair<std::string, double>
354 (dName, ind->genes[i]));
357 size_t jid =
static_cast<size_t>(ind->id);
358 int pilot_rank =
comms_.master_local_pid;
369 std::pair<
size_t, std::shared_ptr<Individual_t> >(jid, ind));
void bounds(const PETE_Expr< T1 > &expr, Vektor< T2, D > &minval, Vektor< T2, D > &maxval)
#define MPI_OPT_CONVERGED_TAG
optimizer notifies pilot that optimization has converged (EXIT)
void MPI_Send_params(Param_t params, size_t pid, MPI_Comm comm)
void MPI_Recv_reqvars(reqVarContainer_t &reqvars, size_t pid, MPI_Comm comm)
std::map< std::string, DVar_t > DVarContainer_t
std::map< std::string, reqVarInfo_t > reqVarContainer_t
namedVariableCollection_t Param_t
std::shared_ptr< CmdArguments > CmdArguments_t
std::map< std::string, Expressions::Expr_t * > Named_t
type of an expressions with a name
std::string getGitRevision()
bundles all communicators for a specific role/pid
genes_t genes
genes of an individual
int getIndex(std::string name)
objectives_t objectives
values of objectives of an individual
void addIndividualToJSON(const std::shared_ptr< Individual_t > &ind)
SampleIndividual Individual_t
bool onMessage(MPI_Status status, size_t length)
void postPoll()
executed after handling (if any) new request
void dispatch_forward_solves()
std::list< Individual_t > individualsToDump_m
Expressions::Named_t objectives_m
objectives
virtual void initialize()
Initialization and start algorithm.
std::size_t jsonDumpFreq_m
Dumps id, design variables and bound.
Comm::Bundle_t comms_
communicator bundle for the optimizer
std::queue< std::shared_ptr< Individual_t > > individuals_m
std::map< std::string, std::shared_ptr< SamplingMethod > > sampleMethods_m
DVarContainer_t dvars_m
design variables
void dumpIndividualsToJSON()
CmdArguments_t args_
command line arguments specified by the user
std::map< size_t, std::shared_ptr< Individual_t > > jobmapping_m
mapping from unique job ID to individual
int act_sample_m
current generation
Sampler(Expressions::Named_t objectives, Expressions::Named_t constraints, DVarContainer_t dvars, size_t dim, Comm::Bundle_t comms, CmdArguments_t args, std::vector< double > hypervolRef, int nrWorkerGroups)
void createNewIndividual()
bounds_t dVarBounds_m
bounds on each specified gene
T getArg(const std::string name, bool isFatal=false)