Commit a20cc2db authored by kraus's avatar kraus
Browse files

Merge branch 'master' of gitlab.psi.ch:OPAL/src

parents e19111ce 5cfa1007
......@@ -22,16 +22,16 @@ template <class T> struct BlendCrossover
// value
const double alpha = 0.5;
for(size_t i = 0; i < ind1->genes.size(); i++) {
for(size_t i = 0; i < ind1->genes_m.size(); i++) {
double ming = std::min(ind1->genes[i], ind2->genes[i]);
double maxg = std::max(ind1->genes[i], ind2->genes[i]);
double ming = std::min(ind1->genes_m[i], ind2->genes_m[i]);
double maxg = std::max(ind1->genes_m[i], ind2->genes_m[i]);
double gamma1 = (1 + 2 * alpha) *
static_cast<double>(rand() / (RAND_MAX + 1.0)) - alpha;
double gamma2 = (1 + 2 * alpha) *
static_cast<double>(rand() / (RAND_MAX + 1.0)) - alpha;
ind1->genes[i] = (1 - gamma1) * ming + gamma1 * maxg;
ind2->genes[i] = (1 - gamma2) * ming + gamma2 * maxg;
ind1->genes_m[i] = (1 - gamma1) * ming + gamma1 * maxg;
ind2->genes_m[i] = (1 - gamma2) * ming + gamma2 * maxg;
}
}
};
\ No newline at end of file
......@@ -210,12 +210,12 @@ bool FixedPisaNsga2<CO, MO>::onMessage(MPI_Status status, size_t length) {
for (individual ind : new_states) {
// only insert individual if not already in population
if(variator_m->population()->isRepresentedInPopulation(ind.genes))
if(variator_m->population()->isRepresentedInPopulation(ind.genes_m))
continue;
boost::shared_ptr<individual> new_ind(new individual);
new_ind->genes = ind.genes;
new_ind->objectives = ind.objectives;
new_ind->genes_m = ind.genes_m;
new_ind->objectives_m = ind.objectives_m;
//XXX: can we pass more than lambda_m files to selector?
unsigned int id =
......@@ -260,7 +260,7 @@ bool FixedPisaNsga2<CO, MO>::onMessage(MPI_Status status, size_t length) {
reqVarContainer_t res;
MPI_Recv_reqvars(res, status.MPI_SOURCE, comms_.opt);
ind->objectives.clear();
ind->objectives_m.clear();
//XXX: check order of genes
reqVarContainer_t::iterator itr = res.begin();
......@@ -269,7 +269,7 @@ bool FixedPisaNsga2<CO, MO>::onMessage(MPI_Status status, size_t length) {
if(!itr->second.is_valid || (itr->second.value.size() > 1 && !itr->second.value[0])) {
std::ostringstream dump;
if (!itr->second.is_valid) {
dump << "invalid individual, objective or constraint\"" << itr->first
dump << "invalid individual, objective or constraint \"" << itr->first
<< "\" failed to be evaluated correctly"
<< std::endl;
} else {
......@@ -285,7 +285,7 @@ bool FixedPisaNsga2<CO, MO>::onMessage(MPI_Status status, size_t length) {
} else {
// update objective value for valid objective
if(itr->second.value.size() == 1)
ind->objectives.push_back(itr->second.value[0]);
ind->objectives_m.push_back(itr->second.value[0]);
}
}
......@@ -388,8 +388,8 @@ void FixedPisaNsga2<CO, MO>::exchangeSolutionStates() {
itr != variator_m->population()->end(); itr++) {
individual ind;
ind.genes = std::vector<double>(itr->second->genes);
ind.objectives = std::vector<double>(itr->second->objectives);
ind.genes_m = std::vector<double>(itr->second->genes_m);
ind.objectives_m = std::vector<double>(itr->second->objectives_m);
population.push_back(ind);
}
......@@ -465,10 +465,10 @@ void FixedPisaNsga2<CO, MO>::dispatch_forward_solves() {
params.insert(
std::pair<std::string, double>
(boost::get<VAR_NAME>(itr->second),
ind->genes[i]));
ind->genes_m[i]));
}
size_t jid = static_cast<size_t>(ind->id);
size_t jid = static_cast<size_t>(ind->id_m);
int pilot_rank = comms_.master_local_pid;
// now send the request to the pilot
......@@ -711,11 +711,11 @@ void FixedPisaNsga2<CO, MO>::dumpPopulationToFile() {
file << std::setw(numDigits + 1) << it->first << " ";
temp = it->second;
for(size_t i=0; i<temp->objectives.size(); i++)
file << std::setw(14) << temp->objectives[i] << " ";
for(size_t i=0; i<temp->objectives_m.size(); i++)
file << std::setw(14) << temp->objectives_m[i] << " ";
file << " ";
for(size_t i=0; i<temp->genes.size(); i++)
file << std::setw(14) << temp->genes[i] << " ";
for(size_t i=0; i<temp->genes_m.size(); i++)
file << std::setw(14) << temp->genes_m[i] << " ";
file << std::endl;
}
......@@ -739,7 +739,7 @@ void FixedPisaNsga2<CO, MO>::dumpPopulationToJSON() {
file << "{" << std::endl;
file << "\t" << "\"name\": " << "\"opt-pilot\"," << std::endl;
file << "\t" << "\"dvar-bounds\": {" << std::endl;
DVarContainer_t::iterator itr = dvars_m.begin();
for ( bounds_t::iterator it = dVarBounds_m.begin();
......@@ -747,13 +747,13 @@ void FixedPisaNsga2<CO, MO>::dumpPopulationToJSON() {
{
file << "\t\t\"" << boost::get<VAR_NAME>(itr->second) << "\": "
<< "[ " << it->first << ", " << it->second << " ]";
if ( it != dVarBounds_m.end() - 1 )
file << ",";
file << "\n";
}
file << "\t}\n\t," << std::endl;
file << "\t" << "\"constraints\": [" << std::endl;
for ( Expressions::Named_t::iterator it = constraints_m.begin();
it != constraints_m.end(); ++it )
......@@ -763,13 +763,13 @@ void FixedPisaNsga2<CO, MO>::dumpPopulationToJSON() {
s.erase(std::remove(s.begin(), s.end(), '"'), s.end());
file << "\t\t\"" << s << "\"";
if ( it != std::prev(constraints_m.end(), 1) )
file << ",";
file << "\n";
}
file << "\t]\n\t," << std::endl;
file << "\t" << "\"solutions\": " << "[" << std::endl;
typename std::map<unsigned int, boost::shared_ptr<individual> >::iterator it;
......@@ -785,12 +785,12 @@ void FixedPisaNsga2<CO, MO>::dumpPopulationToJSON() {
Expressions::Named_t::iterator expr_it;
expr_it = objectives_m.begin();
temp = it->second;
file << "\t\t\"obj\":\n" << "\t\t{\n";
for(size_t i=0; i < temp->objectives.size(); i++, expr_it++) {
for(size_t i=0; i < temp->objectives_m.size(); i++, expr_it++) {
file << "\t" << "\t" << "\t" << "\"" << expr_it->first << "\": "
<< temp->objectives[i];
if( i + 1 != temp->objectives.size())
<< temp->objectives_m[i];
if( i + 1 != temp->objectives_m.size())
file << ",";
file << std::endl;
}
......@@ -798,8 +798,8 @@ void FixedPisaNsga2<CO, MO>::dumpPopulationToJSON() {
size_t i = 0;
for(itr = dvars_m.begin(); itr != dvars_m.end(); ++i, ++itr) {
file << "\t\t\t\"" << boost::get<VAR_NAME>(itr->second) << "\": "
<< temp->genes[i];
if ( i + 1 != temp->genes.size())
<< temp->genes_m[i];
if ( i + 1 != temp->genes_m.size())
file << ",";
file << "\n";
}
......@@ -933,7 +933,7 @@ void FixedPisaNsga2<CO, MO>::calcDistances()
size_t idx1 = pp_all[front[l][i]];
boost::shared_ptr<individual> ind1 =
variator_m->population()->get_individual(idx1);
double obj_min = ind1->objectives[d];
double obj_min = ind1->objectives_m[d];
for (j = i + 1; j < copies[l]; j++) {
......@@ -943,8 +943,8 @@ void FixedPisaNsga2<CO, MO>::calcDistances()
// variator_m->population()->get_individual(idx1);
boost::shared_ptr<individual> ind2 =
variator_m->population()->get_individual(idx2);
// double obj1 = ind1->objectives[d];
double obj2 = ind2->objectives[d];
// double obj1 = ind1->objectives_m[d];
double obj2 = ind2->objectives_m[d];
if ( obj2 < obj_min ) {
min = j;
......@@ -967,8 +967,8 @@ void FixedPisaNsga2<CO, MO>::calcDistances()
variator_m->population()->get_individual(idx1);
boost::shared_ptr<individual> ind2 =
variator_m->population()->get_individual(idx2);
double obj1 = ind1->objectives[d];
double obj2 = ind2->objectives[d];
double obj1 = ind1->objectives_m[d];
double obj2 = ind2->objectives_m[d];
dist[front[l][i]] += obj1 - obj2;
}
......@@ -1038,11 +1038,11 @@ int FixedPisaNsga2<CO, MO>::dominates(unsigned int p_ind_a, unsigned int p_ind_b
boost::shared_ptr<individual> ind2 =
variator_m->population()->get_individual(p_ind_b);
for (size_t i = 0; i < ind1->objectives.size()/* && !a_is_worse*/; i++) {
if (ind1->objectives[i] > ind2->objectives[i]) a_is_worse = 1;
if (ind1->objectives[i] < ind2->objectives[i]) b_is_worse = 1;
// a_is_worse = ind1->objectives[i] > ind2->objectives[i];
// equal = (ind1->objectives[i] == ind2->objectives[i]) && equal;
for (size_t i = 0; i < ind1->objectives_m.size()/* && !a_is_worse*/; i++) {
if (ind1->objectives_m[i] > ind2->objectives_m[i]) a_is_worse = 1;
if (ind1->objectives_m[i] < ind2->objectives_m[i]) b_is_worse = 1;
// a_is_worse = ind1->objectives_m[i] > ind2->objectives_m[i];
// equal = (ind1->objectives_m[i] == ind2->objectives_m[i]) && equal;
}
return (b_is_worse && !a_is_worse);
......@@ -1079,4 +1079,4 @@ std::string FixedPisaNsga2<CO, MO>::getStateString(PisaState_t state) const {
default:
return "";
}
}
}
\ No newline at end of file
......@@ -9,7 +9,7 @@ template <class T> struct IndependentBitMutation
const double probability =
args->getArg<double>("gene-mutation-probability", 0.5);
for(size_t i = 0; i < ind->genes.size(); i++) {
for(size_t i = 0; i < ind->genes_m.size(); i++) {
double rval = static_cast<double>(rand() / (RAND_MAX + 1.0));
if(rval < probability) {
ind->new_gene(i);
......
......@@ -42,8 +42,8 @@ public:
, names_m(names)
, constraints_m(constraints)
{
genes.resize(bounds_m.size(), 0.0);
objectives.resize(bounds_m.size(), 0.0);
genes_m.resize(bounds_m.size(), 0.0);
objectives_m.resize(bounds_m.size(), 0.0);
// names should be equal length to bounds
if (names_m.size() != bounds_m.size()) {
......@@ -67,20 +67,20 @@ public:
/// copy another individual
Individual(boost::shared_ptr<Individual> individual) {
genes = genes_t(individual->genes);
objectives = objectives_t(individual->objectives);
genes_m = genes_t(individual->genes_m);
objectives_m = objectives_t(individual->objectives_m);
bounds_m = bounds_t(individual->bounds_m);
names_m = names_t(individual->names_m);
constraints_m = constraints_t(individual->constraints_m);
id = individual->id;
id_m = individual->id_m;
}
/// serialization of structure
template<class Archive>
void serialize(Archive & ar, const unsigned int version) {
ar & genes;
ar & objectives;
ar & id;
ar & genes_m;
ar & objectives_m;
ar & id_m;
}
/// initialize the gene with index gene_idx with a new random value
......@@ -89,8 +89,8 @@ public:
double max = std::max(bounds_m[gene_idx].first, bounds_m[gene_idx].second);
double min = std::min(bounds_m[gene_idx].first, bounds_m[gene_idx].second);
double delta = std::abs(max - min);
genes[gene_idx] = rand() / (RAND_MAX + 1.0) * delta + min;
return genes[gene_idx];
genes_m[gene_idx] = rand() / (RAND_MAX + 1.0) * delta + min;
return genes_m[gene_idx];
}
/// test if individual within bounds and constraints
bool viable() {
......@@ -98,17 +98,17 @@ public:
}
/// genes of an individual
genes_t genes;
genes_t genes_m;
/// values of objectives of an individual
objectives_t objectives;
objectives_t objectives_m;
/// id
unsigned int id = 0;
unsigned int id_m = 0;
private:
/// check bounds
bool checkBounds() {
for (size_t i=0; i < bounds_m.size(); i++) {
double value = genes[i];
double value = genes_m[i];
double max = std::max(bounds_m[i].first, bounds_m[i].second);
double min = std::min(bounds_m[i].first, bounds_m[i].second);
bool is_valid = (value >= min && value<= max);
......@@ -134,7 +134,7 @@ private:
exit(1);
}
size_t gene_idx = std::distance(names_m.begin(),it);
double value = genes[gene_idx];
double value = genes_m[gene_idx];
variable_dictionary.insert(std::pair<std::string, double>(req_var, value));
}
Expressions::Result_t result =
......
......@@ -8,16 +8,16 @@ template <class T> struct NaiveOnePointCrossover
typedef typename T::genes_t genes_t;
genes_t genes_ind2;
genes_ind2 = ind2->genes;
genes_ind2 = ind2->genes_m;
// determine crossover position u.a.r.
size_t position = static_cast<size_t>(
((double) ind1->genes.size() * (double) rand() / (RAND_MAX + 1.0))
((double) ind1->genes_m.size() * (double) rand() / (RAND_MAX + 1.0))
);
for(size_t i = position; i < ind1->genes.size(); i++) {
ind2->genes[i] = ind1->genes[i];
ind1->genes[i] = genes_ind2[i];
for(size_t i = position; i < ind1->genes_m.size(); i++) {
ind2->genes_m[i] = ind1->genes_m[i];
ind1->genes_m[i] = genes_ind2[i];
}
}
};
......@@ -7,13 +7,13 @@ template <class T> struct NaiveUniformCrossover
void crossover(boost::shared_ptr<T> ind1, boost::shared_ptr<T> ind2,
CmdArguments_t args) {
genes_t genes_ind2 = ind2->genes;
Individual::genes_t genes_ind2 = ind2->genes_m;
for(int i = 0; i < ind1->genes.size(); i++) {
for(std::size_t i = 0; i < ind1->genes_m.size(); i++) {
int choose = (int) (2.0 * (double) rand() / (RAND_MAX + 1.0));
if(choose == 1) {
ind2->genes[i] = ind1->genes[i];
ind1->genes[i] = genes_ind2[i];
ind2->genes_m[i] = ind1->genes_m[i];
ind1->genes_m[i] = genes_ind2[i];
}
}
}
......
......@@ -6,7 +6,7 @@ template <class T> struct OneBitMutation
{
void mutate(boost::shared_ptr<T> ind, CmdArguments_t args) {
int range = ind->genes.size();
int range = ind->genes_m.size();
int position = static_cast<int>((rand() / (RAND_MAX + 1.0)) * range);
ind->new_gene(position);
}
......
......@@ -51,7 +51,7 @@ public:
unsigned int id = getFreeID();
stagingArea.insert(ind_t(id, ind));
ind->id = id;
ind->id_m = id;
//std::cout << "+++ staging id = " << id << "\xd";
return id;
}
......@@ -149,7 +149,7 @@ public:
bool isRepresentedInPopulation(genes_t ind_genes) {
for(ind_t ind : individuals) {
if( ind_genes == ind.second->genes )
if( ind_genes == ind.second->genes_m )
return true;
}
......@@ -172,9 +172,9 @@ public:
for(it = individuals.begin(); it != individuals.end(); it++) {
individual temp = it->second;
for(size_t i=0; i<temp->objectives.size(); i++)
file << temp->objectives[i] << " ";
if (temp->objectives.size() > 0)
for(size_t i=0; i<temp->objectives_m.size(); i++)
file << temp->objectives_m[i] << " ";
if (temp->objectives_m.size() > 0)
file << std::endl;
}
......
......@@ -20,7 +20,7 @@ template <class T> struct SimulatedBinaryCrossover
} catch(OptPilotException &e)
{}
for(int i = 0; i < ind1->genes.size(); i++) {
for(std::size_t i = 0; i < ind1->genes_m.size(); i++) {
double ui = (double) rand() / (RAND_MAX + 1.0);
double beta_qi = 0.0;
......@@ -30,11 +30,11 @@ template <class T> struct SimulatedBinaryCrossover
beta_qi = pow(1.0/(2 * (1.0 - ui)), 1.0/(nu_c + 1.0));
}
double ming = min(ind1->genes[i], ind2->genes[i]);
double maxg = max(ind1->genes[i], ind2->genes[i]);
double ming = std::min(ind1->genes_m[i], ind2->genes_m[i]);
double maxg = std::max(ind1->genes_m[i], ind2->genes_m[i]);
ind1->genes[i] = 0.5 * ((1 + beta_qi) * ming + (1 - beta_qi) * maxg);
ind2->genes[i] = 0.5 * ((1 - beta_qi) * ming + (1 + beta_qi) * maxg);
ind1->genes_m[i] = 0.5 * ((1 + beta_qi) * ming + (1 - beta_qi) * maxg);
ind2->genes_m[i] = 0.5 * ((1 - beta_qi) * ming + (1 + beta_qi) * maxg);
}
}
};
......@@ -115,7 +115,8 @@ public:
const DVarContainer_t &dvar,
const Expressions::Named_t &obj,
const Expressions::Named_t &cons,
std::vector<double> hypervolRef = {})
std::vector<double> hypervolRef = {},
bool isOptimizerRun = true)
: Poller(comm->mpiComm())
, comm_(comm)
, cmd_args_(args)
......@@ -124,7 +125,8 @@ public:
, dvars_(dvar)
, hypervolRef_(hypervolRef)
{
setup(known_expr_funcs);
if (isOptimizerRun)
setup(known_expr_funcs);
}
~Pilot()
......@@ -201,7 +203,7 @@ private:
}
MPI_Barrier(MPI_COMM_WORLD);
parseInputFile(known_expr_funcs);
parseInputFile(known_expr_funcs, true);
// here the control flow starts to diverge
if ( comm_->isOptimizer() ) { startOptimizer(); }
......@@ -211,7 +213,7 @@ private:
protected:
void parseInputFile(functionDictionary_t known_expr_funcs) {
void parseInputFile(functionDictionary_t known_expr_funcs, bool isOptimizationRun) {
try {
input_file_ = cmd_args_->getArg<std::string>("inputfile", true);
......@@ -221,7 +223,7 @@ protected:
MPI_Abort(comm_m, -101);
}
if(objectives_.size() == 0 || dvars_.size() == 0) {
if((isOptimizationRun && objectives_.size() == 0) || dvars_.size() == 0) {
throw OptPilotException("Pilot::Pilot()",
"No objectives or dvars specified");
}
......@@ -231,8 +233,10 @@ protected:
os << "\033[01;35m";
os << " ✔ " << objectives_.size()
<< " objectives" << std::endl;
os << " ✔ " << constraints_.size()
<< " constraints" << std::endl;
if (isOptimizationRun) {
os << " ✔ " << constraints_.size()
<< " constraints" << std::endl;
}
os << " ✔ " << dvars_.size()
<< " dvars" << std::endl;
os << "\e[0m";
......
......@@ -105,4 +105,4 @@ protected:
}
};
#endif
#endif
\ No newline at end of file
......@@ -31,9 +31,11 @@ template <class Sim_t>
class Worker : protected Poller {
public:
Worker(Expressions::Named_t constraints,
std::string simName, Comm::Bundle_t comms, CmdArguments_t args)
std::string simName,
Comm::Bundle_t comms,
CmdArguments_t args)
: Poller(comms.worker)
, cmd_args_(args)
{
......@@ -42,13 +44,17 @@ public:
pilot_rank_ = comms.master_local_pid;
is_idle_ = true;
coworker_comm_ = comms.coworkers;
leader_pid_ = 0;
MPI_Comm_size(coworker_comm_, &num_coworkers_);
}
Worker(Expressions::Named_t objectives, Expressions::Named_t constraints,
std::string simName, Comm::Bundle_t comms, CmdArguments_t args)
Worker(Expressions::Named_t objectives,
Expressions::Named_t constraints,
std::string simName,
Comm::Bundle_t comms,
CmdArguments_t args,
bool isOptimizer = true)
: Poller(comms.worker)
, cmd_args_(args)
{
......@@ -60,9 +66,11 @@ public:
coworker_comm_ = comms.coworkers;
leader_pid_ = 0;
MPI_Comm_size(coworker_comm_, &num_coworkers_);
if (!isOptimizer) return;
int my_local_pid = 0;
MPI_Comm_rank(coworker_comm_, &my_local_pid);
MPI_Comm_size(coworker_comm_, &num_coworkers_);
// distinction between leader and coworkers
if(my_local_pid == leader_pid_)
......@@ -170,7 +178,6 @@ protected:
{}
void onStop() {
if(num_coworkers_ > 1)
notifyCoWorkers(MPI_STOP_TAG);
}
......@@ -204,6 +211,7 @@ protected:
// run simulation in a "blocking" fashion
sim->run();
sim->collectResults();
sim->cleanUp();
requested_results = sim->getResults();
} catch(OptPilotException &ex) {
std::cout << "Exception while running simulation: "
......@@ -231,4 +239,4 @@ protected:
}
};
#endif
#endif
\ No newline at end of file
......@@ -45,7 +45,7 @@ namespace {
std::pair<double, double>(lower_bound_, upper_bound_));
names.push_back("dvar"+std::to_string(i));
}
if (constraint.empty() == false)
if (constraint.empty() == false)
constraints.insert(std::pair<std::string,Expressions::Expr_t*>
("constraint0",new Expressions::Expr_t(constraint)));
......@@ -61,12 +61,12 @@ namespace {
size_t num_genes = 1;
boost::shared_ptr<Individual> ind = createIndividual(num_genes);
double gene = ind->genes[0];
double gene = ind->genes_m[0];
EXPECT_LE(lower_bound_, gene) << "gene should respect lower bound";
EXPECT_GE(upper_bound_, gene) << "gene should respect upper bound";
size_t my_size = ind->genes.size();
size_t my_size = ind->genes_m.size();
EXPECT_EQ(static_cast<size_t>(num_genes), my_size)
<< "individual should only have " << num_genes << " gene(s)";
......@@ -77,7 +77,7 @@ namespace {
size_t num_genes = 12;
boost::shared_ptr<Individual> ind = createIndividual(num_genes);
size_t my_size = ind->genes.size();
size_t my_size = ind->genes_m.size();
EXPECT_EQ(static_cast<size_t>(num_genes), my_size)
<< "individual should only have " << num_genes << " gene(s)";
......@@ -87,7 +87,7 @@ namespace {
size_t num_genes = 1;
boost::shared_ptr<Individual> ind = createIndividual(num_genes);
double gene = ind->genes[0];
double gene = ind->genes_m[0];
double new_gene = ind->new_gene(0);
EXPECT_NE(gene, new_gene) << "new gene should be different";
......@@ -97,13 +97,13 @@ namespace {
size_t num_genes = 2;
double half = (lower_bound_ + upper_bound_) / 2.;
std::string constraint = "(dvar0 + dvar1)/2. <=" + std::to_string(half);
std::string constraint = "(dvar0 + dvar1)/2. <=" + std::to_string(half);
// create several individuals to test
for (int i=0; i<10; i++) {
boost::shared_ptr<Individual> ind = createIndividual(num_genes,constraint);
double gene0 = ind->genes[0];
double gene1 = ind->genes[1];
double gene0 = ind->genes_m[0];
double gene1 = ind->genes_m[1];
EXPECT_LE((gene0+gene1)/2, half) << "constraint should be respected";
}
}
......@@ -112,4 +112,4 @@ namespace {
int main(int argc, char **argv) {