Commit 9c617167 authored by snuverink_j's avatar snuverink_j
Browse files

optimizer: add comments, refactoring and spacing

parent 6ea516ce
...@@ -83,20 +83,20 @@ protected: ...@@ -83,20 +83,20 @@ protected:
/// Write the variator config file /// Write the variator config file
void writeVariatorCfg(); void writeVariatorCfg();
// implementing poller hooks ///@{ implementing poller hooks
bool onMessage(MPI_Status status, size_t length); virtual bool onMessage(MPI_Status status, size_t length);
void postPoll(); virtual void postPoll();
void setupPoll() {} virtual void setupPoll() {}
void prePoll() { virtual void prePoll() {
// std::ostringstream debug; // std::ostringstream debug;
// debug << "IN PRE POLL: "; // debug << "IN PRE POLL: ";
// debug << getStateString(curState_m) << std::endl; // debug << getStateString(curState_m) << std::endl;
// progress_->log(debug); // progress_->log(debug);
} }
void onStop() {} virtual void onStop() {}
///@}
// helper sending evaluation requests to the pilot /// helper sending evaluation requests to the pilot
void dispatch_forward_solves(); void dispatch_forward_solves();
...@@ -140,16 +140,16 @@ private: ...@@ -140,16 +140,16 @@ private:
Variator_t; Variator_t;
boost::scoped_ptr<Variator_t> variator_m; boost::scoped_ptr<Variator_t> variator_m;
std::vector<unsigned int> pp_all; std::vector<unsigned int> pp_all; ///< IDs of population
std::vector<unsigned int> parent_queue_; std::vector<unsigned int> parent_queue_; ///< IDs that will make new offspring
std::set<unsigned int> archive_; // std::set<unsigned int> archive_;
std::set<unsigned int> to_selector_; std::set<unsigned int> to_selector_; ///< Successfully run IDs to go into population
// to compute the front // to compute the front
std::vector<int> copies; std::vector<int> copies; ///< number of individuals in the n-th front
std::vector<double> dist; std::vector<double> dist;
std::vector< std::vector<int> > front; std::vector< std::vector<int> > front; ///< individuals in each front
std::map<size_t, double> fitness_; std::map<size_t, double> fitness_; ///< map between id and fitness (sum of front number and dist)
/// communicator bundle for the optimizer /// communicator bundle for the optimizer
Comm::Bundle_t comms_; Comm::Bundle_t comms_;
......
...@@ -253,7 +253,7 @@ bool FixedPisaNsga2<CO, MO>::onMessage(MPI_Status status, size_t length) { ...@@ -253,7 +253,7 @@ bool FixedPisaNsga2<CO, MO>::onMessage(MPI_Status status, size_t length) {
job_trace_->log(dump); job_trace_->log(dump);
std::cout << "NON-EXISTING JOB with ID = " << jid << std::endl; std::cout << "NON-EXISTING JOB with ID = " << jid << std::endl;
throw OptPilotException("FixedPisaNsga2::onMessage", throw OptPilotException("FixedPisaNsga2::onMessage",
"non-existing job"); "non-existing job");
} }
boost::shared_ptr<individual> ind = it->second; boost::shared_ptr<individual> ind = it->second;
...@@ -624,7 +624,7 @@ void FixedPisaNsga2<CO, MO>::runStateMachine() { ...@@ -624,7 +624,7 @@ void FixedPisaNsga2<CO, MO>::runStateMachine() {
typedef typename FixedPisaNsga2::Individual_t individual; typedef typename FixedPisaNsga2::Individual_t individual;
typename std::map<unsigned int, boost::shared_ptr<individual> > typename std::map<unsigned int, boost::shared_ptr<individual> >
::iterator it; ::iterator it;
for(it = variator_m->population()->begin(); for(it = variator_m->population()->begin();
it != variator_m->population()->end(); it++) { it != variator_m->population()->end(); it++) {
//archive_.insert(it->first); //archive_.insert(it->first);
pp_all.push_back(it->first); pp_all.push_back(it->first);
...@@ -687,12 +687,13 @@ void FixedPisaNsga2<CO, MO>::dumpPopulationToFile() { ...@@ -687,12 +687,13 @@ void FixedPisaNsga2<CO, MO>::dumpPopulationToFile() {
typename std::map<unsigned int, boost::shared_ptr<individual> >::iterator it; typename std::map<unsigned int, boost::shared_ptr<individual> >::iterator it;
it = variator_m->population()->begin(); it = variator_m->population()->begin();
// find maximum length of ID
auto maxID = it->first; auto maxID = it->first;
for(it ++; it != variator_m->population()->end(); it++) { for(it ++; it != variator_m->population()->end(); it++) {
if (it->first > maxID) if (it->first > maxID)
maxID = it->first; maxID = it->first;
} }
size_t numDigits = std::to_string(maxID).length(); const size_t numDigits = std::to_string(maxID).length();
size_t next = 0, last = 0; size_t next = 0, last = 0;
std::string delimiter = ","; std::string delimiter = ",";
next = file_param_descr_.find(delimiter, last); next = file_param_descr_.find(delimiter, last);
...@@ -711,7 +712,7 @@ void FixedPisaNsga2<CO, MO>::dumpPopulationToFile() { ...@@ -711,7 +712,7 @@ void FixedPisaNsga2<CO, MO>::dumpPopulationToFile() {
file.precision(6); file.precision(6);
file << std::scientific; file << std::scientific;
for(it = variator_m->population()->begin(); for(it = variator_m->population()->begin();
it != variator_m->population()->end(); it++) { it != variator_m->population()->end(); it++) {
file << std::setw(numDigits + 1) << it->first << " "; file << std::setw(numDigits + 1) << it->first << " ";
...@@ -862,38 +863,35 @@ void FixedPisaNsga2<CO, MO>::calcFitnesses() ...@@ -862,38 +863,35 @@ void FixedPisaNsga2<CO, MO>::calcFitnesses()
int i, j, l; int i, j, l;
int size = pp_all.size(); int size = pp_all.size();
std::vector<int> d(size); std::vector<int> d(size,0); // 1 if dominated by another ind, 0 if not
std::vector<int> f(size); std::vector<int> f(size,1); // 1 if not yet in a front, -1 if in a front
/* initialize fitness and strength values */ /* initialize fitness and strength values */
for (i = 0; i < size; i++) { for (i = 0; i < size; i++) {
fitness_.insert(std::pair<size_t, double>(pp_all[i], 0.0)); fitness_.insert(std::pair<size_t, double>(pp_all[i], 0.0));
d[i] = 1;
f[i] = 1;
copies[i] = 0; copies[i] = 0;
} }
/* calculate strength values */ /* calculate strength values */
int num = size; int num = size; // number of individuals not yet in a front
for (l = 0; l < size; l++) { for (l = 0; l < size; l++) { // loop over fronts, there can be maximally `size` fronts
/* find next front */ /* find next front */
for (i = 0; i < size; i++) { for (i = 0; i < size; i++) {
d[i] = 0; d[i] = 0; // reset dominate flag
if (f[i] != -1) { if (f[i] == -1) continue; // don't consider if already in a front
for (j = 0; j < i && d[i] == 0; j++) for (j = 0; j < size && j != i; j++) {
if (f[j] != -1) if (f[j] == -1) continue;
if (dominates(pp_all[j], pp_all[i])) if (dominates(pp_all[j], pp_all[i])) {
d[i] = 1; d[i] = 1;
for(j = i+1; j < size && d[i] == 0; j++) break;
if (f[j] != -1) }
if (dominates(pp_all[j], pp_all[i]))
d[i] = 1;
} }
} }
/* extract front */ /* extract front */
for (i = 0; i < size; i++) { for (i = 0; i < size; i++) {
if (f[i] != -1 && d[i] == 0) { if (f[i] != -1 && d[i] == 0) {
// add to front
fitness_[pp_all[i]] = l; fitness_[pp_all[i]] = l;
f[i] = -1; f[i] = -1;
num--; num--;
...@@ -982,18 +980,19 @@ void FixedPisaNsga2<CO, MO>::environmentalSelection() { ...@@ -982,18 +980,19 @@ void FixedPisaNsga2<CO, MO>::environmentalSelection() {
for (int i = 0; i < size; i++) for (int i = 0; i < size; i++)
fitness_[pp_all[i]] += 1.0 / dist[i]; fitness_[pp_all[i]] += 1.0 / dist[i];
// get alpha_m fittest individuals
for (size_t i = 0; i < alpha_m; i++) { for (size_t i = 0; i < alpha_m; i++) {
int min = i; int min = i;
for (int j = i + 1; j < size; j++) { for (int j = i + 1; j < size; j++) {
if (fitness_[pp_all[j]] < fitness_[pp_all[min]]) if (fitness_[pp_all[j]] < fitness_[pp_all[min]])
min = j; min = j;
} }
// swap
unsigned int p_min = pp_all[min]; unsigned int p_min = pp_all[min];
pp_all[min] = pp_all[i]; pp_all[min] = pp_all[i];
pp_all[i] = p_min; pp_all[i] = p_min;
} }
// erase others
pp_all.erase(pp_all.begin() + alpha_m, pp_all.end()); pp_all.erase(pp_all.begin() + alpha_m, pp_all.end());
} }
...@@ -1003,12 +1002,12 @@ template< template <class> class CO, template <class> class MO > ...@@ -1003,12 +1002,12 @@ template< template <class> class CO, template <class> class MO >
void FixedPisaNsga2<CO, MO>::matingSelection() { void FixedPisaNsga2<CO, MO>::matingSelection() {
//FIXME: //FIXME:
int tournament = 2; int tournament = 1; ///< number of opponents
for (size_t i = 0; i < selector_mu_; i++) { for (size_t i = 0; i < selector_mu_; i++) {
int winner = irand(pp_all.size()); int winner = irand(pp_all.size());
for (int j = 1; j < tournament; j++) { for (int j = 0; j < tournament; j++) {
int opponent = irand(pp_all.size()); int opponent = irand(pp_all.size());
if (fitness_[pp_all[opponent]] < fitness_[pp_all[winner]] if (fitness_[pp_all[opponent]] < fitness_[pp_all[winner]]
|| winner == opponent) { || winner == opponent) {
......
...@@ -153,7 +153,7 @@ public: ...@@ -153,7 +153,7 @@ public:
if (viableA == true && viableB == true) { if (viableA == true && viableB == true) {
break; break;
} }
std::cout << "Individual not viable, I try again: iter= " << iter << std::endl; std::cout << "Individual not viable, I try again: iter= " << iter << std::endl;
iter++; iter++;
// if maximum number of tries then create new individual(s) // if maximum number of tries then create new individual(s)
if (iter > 100) { if (iter > 100) {
......
...@@ -81,13 +81,13 @@ namespace Hypervolume { ...@@ -81,13 +81,13 @@ namespace Hypervolume {
POINT p = *(POINT*)v1; POINT p = *(POINT*)v1;
POINT q = *(POINT*)v2; POINT q = *(POINT*)v2;
#if hyper_opt == 1 #if hyper_opt == 1
for (int i = n - fr - 1; i >= 0; i--) for (int i = n - fr - 1; i >= 0; i--) {
#else #else
for (int i = n - 1; i >= 0; i--) for (int i = n - 1; i >= 0; i--) {
#endif #endif
if BEATS(p.objectives[i],q.objectives[i]) return 1; if BEATS(p.objectives[i],q.objectives[i]) return 1;
else else if BEATS(q.objectives[i],p.objectives[i]) return -1;
if BEATS(q.objectives[i],p.objectives[i]) return -1; }
return 0; return 0;
} }
......
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