Commit 42e6658e authored by snuverink_j's avatar snuverink_j
Browse files

fix 236: add option to add reference point for hypervolume calculation

parent dec39823
......@@ -61,11 +61,13 @@ public:
* @param[in] dim number of objectives
* @param[in] comms available to the optimizer
* @param[in] args the user passed on the command line
* @param[in] hypervolRef hypervolume reference point
*/
FixedPisaNsga2(Expressions::Named_t objectives,
Expressions::Named_t constraints,
DVarContainer_t dvars, size_t dim, Comm::Bundle_t comms,
CmdArguments_t args);
CmdArguments_t args,
std::vector<double> hypervolRef);
~FixedPisaNsga2();
......@@ -175,7 +177,7 @@ private:
DVarContainer_t dvars_m;
/// command line arguments specified by the user
CmdArguments_t args_;
CmdArguments_t args_m;
/// size of initial population
size_t alpha_m;
......@@ -205,6 +207,9 @@ private:
double conv_hvol_progress_;
double hvol_progress_;
/// hypervolume reference point
std::vector<double> hvol_ref_m;
/// file header for result files contains this parameter description
std::string file_param_descr_;
......
......@@ -32,15 +32,17 @@ FixedPisaNsga2<CO, MO>::FixedPisaNsga2(
Expressions::Named_t constraints,
DVarContainer_t dvars,
size_t dim, Comm::Bundle_t comms,
CmdArguments_t args)
CmdArguments_t args,
std::vector<double> hypervolRef)
: Optimizer(comms.opt)
, statistics_(new Statistics<size_t>("individuals"))
, comms_(comms)
, objectives_m(objectives)
, constraints_m(constraints)
, dvars_m(dvars)
, args_(args)
, args_m(args)
, dim_m(dim)
, hvol_ref_m(hypervolRef)
{
my_local_pid_ = 0;
MPI_Comm_rank(comms_.opt, &my_local_pid_);
......@@ -48,10 +50,10 @@ FixedPisaNsga2<CO, MO>::FixedPisaNsga2(
//FIXME: proper rand gen initialization (use boost?!)
srand(time(NULL) + comms_.island_id);
dump_freq_ = args->getArg<int>("dump-freq", 1, false);
dump_freq_ = args->getArg<int>("dump-freq", 1, false);
maxGenerations_m = args->getArg<int>("maxGenerations", true);
resultFile_m = args->getArg<std::string>("outfile", "-th_generation.dat", false);
resultDir_m = args->getArg<std::string>("outdir", "generations", false);
resultFile_m = args->getArg<std::string>("outfile", "-th_generation.dat", false);
resultDir_m = args->getArg<std::string>("outdir", "generations", false);
// create output directory if it does not exists
struct stat dirInfo;
......@@ -86,7 +88,7 @@ FixedPisaNsga2<CO, MO>::FixedPisaNsga2(
DVarContainer_t::iterator itr;
std::vector<std::string> dNames;
for(itr = dvars_m.begin(); itr != dvars_m.end(); itr++) {
std::string dName = boost::get<VAR_NAME>(itr->second);
file_param_descr_ += '%' + dName + ',';
......@@ -144,13 +146,13 @@ void FixedPisaNsga2<CO, MO>::initialize() {
// start poll loop
run_clock_start_ = boost::chrono::system_clock::now();
last_clock_ = boost::chrono::system_clock::now();
last_clock_ = boost::chrono::system_clock::now();
run();
bool compHyvol = (objectives_m.size() > (hyper_opt / 2 + 1));
if (compHyvol)
current_hvol_ =
variator_m->population()->computeHypervolume(comms_.island_id);
variator_m->population()->computeHypervolume(comms_.island_id, hvol_ref_m);
boost::chrono::duration<double> total =
boost::chrono::system_clock::now() - run_clock_start_;
......@@ -262,9 +264,8 @@ bool FixedPisaNsga2<CO, MO>::onMessage(MPI_Status status, size_t length) {
ind->objectives.clear();
//XXX: check order of genes
reqVarContainer_t::iterator itr;
std::map<std::string, double> vars;
for(itr = res.begin(); itr != res.end(); itr++) {
reqVarContainer_t::iterator itr = res.begin();
for(; itr != res.end(); ++itr) {
// mark invalid if expression could not be evaluated or constraint does not hold
if(!itr->second.is_valid || (itr->second.value.size() > 1 && !itr->second.value[0])) {
std::ostringstream dump;
......@@ -321,7 +322,7 @@ void FixedPisaNsga2<CO, MO>::postPoll() {
bool compHyvol = (objectives_m.size() > (hyper_opt / 2 + 1));
if (compHyvol) {
double hvol =
variator_m->population()->computeHypervolume(comms_.island_id);
variator_m->population()->computeHypervolume(comms_.island_id, hvol_ref_m);
hvol_progress_ = fabs(current_hvol_ - hvol) / current_hvol_;
current_hvol_ = hvol;
}
......@@ -337,7 +338,7 @@ void FixedPisaNsga2<CO, MO>::postPoll() {
stats << "dt = " << dt.count() << "s, total = " << total.count()
<< "s" << std::endl;
if (compHyvol)
stats << "Hypervolume = " << current_hvol_ << std::endl;
stats << "Hypervolume = " << current_hvol_ << std::endl;
stats << "__________________________________________" << std::endl;
progress_->log(stats);
......@@ -370,7 +371,7 @@ void FixedPisaNsga2<CO, MO>::exchangeSolutionStates() {
typedef typename FixedPisaNsga2::Individual_t individual;
size_t num_masters = args_->getArg<size_t>("num-masters", 1, false);
size_t num_masters = args_m->getArg<size_t>("num-masters", 1, false);
if(num_masters <= 1 ||
exchangeSolStateFreq_m == 0 ||
......@@ -660,7 +661,7 @@ template< template <class> class CO, template <class> class MO >
void FixedPisaNsga2<CO, MO>::dumpPopulationToFile() {
// only dump old data format if the user requests it
if(! args_->getArg<bool>("dump-dat", false, false)) return;
if(! args_m->getArg<bool>("dump-dat", false, false)) return;
typedef typename FixedPisaNsga2::Individual_t individual;
boost::shared_ptr<individual> temp;
......
......@@ -162,7 +162,9 @@ public:
}
double computeHypervolume(size_t island_id) {
double computeHypervolume(size_t island_id, const std::vector<double>& referencePoint) {
// protection check
if (individuals.empty() == true) return -1;
std::ofstream file;
std::ostringstream filename;
......@@ -186,7 +188,7 @@ public:
file.flush();
file.close();
hypervolume_ = Hypervolume::FromFile(filename.str());
hypervolume_ = Hypervolume::FromFile(filename.str(), referencePoint);
return hypervolume_;
}
......
......@@ -113,13 +113,15 @@ public:
functionDictionary_t known_expr_funcs,
const DVarContainer_t &dvar,
const Expressions::Named_t &obj,
const Expressions::Named_t &cons)
const Expressions::Named_t &cons,
std::vector<double> hypervolRef = {})
: Poller(comm->mpiComm())
, comm_(comm)
, cmd_args_(args)
, objectives_(obj)
, constraints_(cons)
, dvars_(dvar)
, hypervolRef_(hypervolRef)
{
setup(known_expr_funcs);
}
......@@ -157,9 +159,11 @@ protected:
bool has_opt_converged_;
bool continue_polling_;
Expressions::Named_t objectives_;
Expressions::Named_t constraints_;
DVarContainer_t dvars_;
Expressions::Named_t objectives_; ///< objectives
Expressions::Named_t constraints_; ///< constraints
DVarContainer_t dvars_; ///< design variables
std::vector<double> hypervolRef_; ///< hypervolume reference point
// keep track of state of all workers
std::vector<bool> is_worker_idle_;
......@@ -248,7 +252,7 @@ protected:
boost::scoped_ptr<Opt_t> opt(
new Opt_t(objectives_, constraints_, dvars_, objectives_.size(),
comm_->getBundle(), cmd_args_));
comm_->getBundle(), cmd_args_, hypervolRef_));
opt->initialize();
std::cout << "Stop Opt.." << std::endl;
......
......@@ -15,7 +15,7 @@
/**
* \class CmdArguments
* \brief Parsing commando line arguments
* \brief Parsing command line arguments
*
* In order to have a flexible framework, each component implementation gets
* access to all command line arguments.
......
/*
This program is free software (software libre); you can redistribute
it and/or modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, you can obtain a copy of the GNU
General Public License at:
http://www.gnu.org/copyleft/gpl.html
or by writing to:
Free Software Foundation, Inc., 59 Temple Place,
Suite 330, Boston, MA 02111-1307 USA
----------------------------------------------------------------------
This program is free software (software libre); you can redistribute
it and/or modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, you can obtain a copy of the GNU
General Public License at:
http://www.gnu.org/copyleft/gpl.html
or by writing to:
Free Software Foundation, Inc., 59 Temple Place,
Suite 330, Boston, MA 02111-1307 USA
----------------------------------------------------------------------
*/
......@@ -35,392 +35,385 @@
#include <sys/time.h>
#include <sys/resource.h>
#include "wfg.h"
#include "avl.h"
#include <string>
#include "hypervolume.h"
#define MAXIMISING true
#if MAXIMISING
#define BEATS(x,y) (x > y)
//#define BEATSEQ(x,y) (x >= y)
#else
#define BEATS(x,y) (x < y)
//#define BEATSEQ(x,y) (x <= y)
#endif
#define WORSE(x,y) (BEATS(y,x) ? (x) : (y))
//#define BETTER(x,y) (BEATS(y,x) ? (y) : (x))
namespace Hypervolume {
int n; // the number of objectives
POINT ref; // the reference point
int n; // the number of objectives
POINT ref; // the reference point
FRONT *fs; // memory management stuff
int fr = 0; // current depth
int frmax = -1; // max depth malloced so far (for hyper_opt = 0)
int maxm = 0; // identify the biggest fronts in the file
int maxn = 0;
FRONT *fs; // memory management stuff
int fr = 0; // current depth
int frmax = -1; // max depth malloced so far (for hyper_opt = 0)
int maxm = 0; // identify the biggest fronts in the file
int maxn = 0;
static avl_tree_t *tree;
double hv(FRONT);
static avl_tree_t *tree;
double hv(FRONT);
using namespace Hypervolume;
static int compare_tree_asc( const void *p1, const void *p2)
{
static int compare_tree_asc( const void *p1, const void *p2)
{
const double x1= *((const double *)p1+1);
const double x2= *((const double *)p2+1);
if (x1 != x2) return (x1 > x2) ? -1 : 1;
else return 0;
}
}
int greater(const void *v1, const void *v2)
// this sorts points improving in the last objective
{
POINT p = *(POINT*)v1;
POINT q = *(POINT*)v2;
#if hyper_opt == 1
for (int i = n - fr - 1; i >= 0; i--)
#else
for (int i = n - 1; i >= 0; i--)
#endif
if BEATS(p.objectives[i],q.objectives[i]) return 1;
else
if BEATS(q.objectives[i],p.objectives[i]) return -1;
return 0;
}
int dominates2way(POINT p, POINT q)
// returns -1 if p dominates q, 1 if q dominates p, 2 if p == q, 0 otherwise
{
// domination could be checked in either order
#if hyper_opt == 1
for (int i = n - fr - 1; i >= 0; i--)
#else
for (int i = n - 1; i >= 0; i--)
#endif
if BEATS(p.objectives[i],q.objectives[i])
{for (int j = i - 1; j >= 0; j--)
if BEATS(q.objectives[j],p.objectives[j]) return 0;
return -1;}
else
if BEATS(q.objectives[i],p.objectives[i])
{for (int j = i - 1; j >= 0; j--)
if BEATS(p.objectives[j],q.objectives[j]) return 0;
return 1;}
return 2;
}
void makeDominatedBit(FRONT ps, int p)
// creates the front ps[p+1 ..] in fs[fr], with each point bounded by ps[p] and dominated points removed
{
// when hyper_opt = 0 each new frame is allocated as needed, because the worst-case needs #frames = #points
#if hyper_opt == 0
if (fr > frmax)
{frmax = fr;
fs[fr].points = (POINT*) malloc( maxm * sizeof(POINT));
for (int j = 0; j < maxm; j++)
{
fs[fr].points[j].objectives = (OBJECTIVE*) malloc(maxn * sizeof(OBJECTIVE));
}
}
#endif
int z = ps.nPoints - 1 - p;
for (int i = 0; i < z; i++)
for (int j = 0; j < n; j++)
fs[fr].points[i].objectives[j] = WORSE(ps.points[p].objectives[j],ps.points[p + 1 + i].objectives[j]);
POINT t;
fs[fr].nPoints = 1;
for (int i = 1; i < z; i++)
{int j = 0;
bool keep = true;
while (j < fs[fr].nPoints && keep)
switch (dominates2way(fs[fr].points[i], fs[fr].points[j]))
{case -1: t = fs[fr].points[j];
fs[fr].nPoints--;
fs[fr].points[j] = fs[fr].points[fs[fr].nPoints];
fs[fr].points[fs[fr].nPoints] = t;
break;
case 0: j++; break;
// case 2: printf("Identical points!\n");
default: keep = false;
}
if (keep) {t = fs[fr].points[fs[fr].nPoints];
fs[fr].points[fs[fr].nPoints] = fs[fr].points[i];
fs[fr].points[i] = t;
fs[fr].nPoints++;}
}
fr++;
}
double hv2(FRONT ps)
// returns the hypervolume of ps[0 ..] in 2D
// assumes that ps is sorted improving
{
double volume = fabs((ps.points[0].objectives[0] - ref.objectives[0]) *
(ps.points[0].objectives[1] - ref.objectives[1]));
for (int i = 1; i < ps.nPoints; i++)
volume += fabs((ps.points[i].objectives[0] - ref.objectives[0]) *
(ps.points[i].objectives[1] - ps.points[i - 1].objectives[1]));
return volume;
}
int greater(const void *v1, const void *v2)
// this sorts points improving in the last objective
{
POINT p = *(POINT*)v1;
POINT q = *(POINT*)v2;
#if hyper_opt == 1
for (int i = n - fr - 1; i >= 0; i--)
#else
for (int i = n - 1; i >= 0; i--)
#endif
if BEATS(p.objectives[i],q.objectives[i]) return 1;
else
if BEATS(q.objectives[i],p.objectives[i]) return -1;
return 0;
}
double hv3_AVL(FRONT ps)
/* hv3_AVL: 3D algorithm code taken from version hv-1.2 available at
http://iridia.ulb.ac.be/~manuel/hypervolume and proposed by:
Carlos M. Fonseca, Lus Paquete, and Manuel Lpez-Ibez. An improved
dimension-sweep algorithm for the hypervolume indicator. In IEEE
Congress on Evolutionary Computation, pages 1157-1163, Vancouver,
Canada, July 2006.
int dominates2way(POINT p, POINT q)
// returns -1 if p dominates q, 1 if q dominates p, 2 if p == q, 0 otherwise
{
// domination could be checked in either order
#if hyper_opt == 1
for (int i = n - fr - 1; i >= 0; i--)
#else
for (int i = n - 1; i >= 0; i--)
#endif
if BEATS(p.objectives[i],q.objectives[i])
{for (int j = i - 1; j >= 0; j--)
if BEATS(q.objectives[j],p.objectives[j]) return 0;
return -1;}
else
if BEATS(q.objectives[i],p.objectives[i])
{for (int j = i - 1; j >= 0; j--)
if BEATS(p.objectives[j],q.objectives[j]) return 0;
return 1;}
return 2;
}
void makeDominatedBit(FRONT ps, int p)
// creates the front ps[p+1 ..] in fs[fr], with each point bounded by ps[p] and dominated points removed
{
// when hyper_opt = 0 each new frame is allocated as needed, because the worst-case needs #frames = #points
#if hyper_opt == 0
if (fr > frmax)
{frmax = fr;
fs[fr].points = (POINT*) malloc( maxm * sizeof(POINT));
for (int j = 0; j < maxm; j++)
{
fs[fr].points[j].objectives = (OBJECTIVE*) malloc(maxn * sizeof(OBJECTIVE));
}
}
#endif
Copyright (c) 2009
Carlos M. Fonseca <cmfonsec@ualg.pt>
Manuel Lopez-Ibanez <manuel.lopez-ibanez@ulb.ac.be>
Luis Paquete <paquete@dei.uc.pt>
*/
int z = ps.nPoints - 1 - p;
for (int i = 0; i < z; i++)
for (int j = 0; j < n; j++)
fs[fr].points[i].objectives[j] = WORSE(ps.points[p].objectives[j],ps.points[p + 1 + i].objectives[j]);
POINT t;
fs[fr].nPoints = 1;
for (int i = 1; i < z; i++)
{int j = 0;
bool keep = true;
while (j < fs[fr].nPoints && keep)
switch (dominates2way(fs[fr].points[i], fs[fr].points[j]))
{case -1: t = fs[fr].points[j];
fs[fr].nPoints--;
fs[fr].points[j] = fs[fr].points[fs[fr].nPoints];
fs[fr].points[fs[fr].nPoints] = t;
break;
case 0: j++; break;
// case 2: printf("Identical points!\n");
default: keep = false;
}
if (keep) {t = fs[fr].points[fs[fr].nPoints];
fs[fr].points[fs[fr].nPoints] = fs[fr].points[i];
fs[fr].points[i] = t;
fs[fr].nPoints++;}
}
fr++;
}
double hv2(FRONT ps)
// returns the hypervolume of ps[0 ..] in 2D
// assumes that ps is sorted improving
{
double volume = fabs((ps.points[0].objectives[0] - ref.objectives[0]) *
(ps.points[0].objectives[1] - ref.objectives[1]));
for (int i = 1; i < ps.nPoints; i++)
volume += fabs((ps.points[i].objectives[0] - ref.objectives[0]) *
(ps.points[i].objectives[1] - ps.points[i - 1].objectives[1]));
return volume;
}
double hv3_AVL(FRONT ps)
/* hv3_AVL: 3D algorithm code taken from version hv-1.2 available at
http://iridia.ulb.ac.be/~manuel/hypervolume and proposed by:
Carlos M. Fonseca, Lus Paquete, and Manuel Lpez-Ibez. An improved
dimension-sweep algorithm for the hypervolume indicator. In IEEE
Congress on Evolutionary Computation, pages 1157-1163, Vancouver,
Canada, July 2006.
Copyright (c) 2009
Carlos M. Fonseca <cmfonsec@ualg.pt>
Manuel Lopez-Ibanez <manuel.lopez-ibanez@ulb.ac.be>
Luis Paquete <paquete@dei.uc.pt>
*/
// returns the hypervolume of ps[0 ..] in 3D
// assumes that ps is sorted improving
{
// returns the hypervolume of ps[0 ..] in 3D
// assumes that ps is sorted improving
{
avl_init_node(ps.points[ps.nPoints-1].tnode,ps.points[ps.nPoints-1].objectives);
avl_insert_top(tree,ps.points[ps.nPoints-1].tnode);
double hypera = (ref.objectives[0] - ps.points[ps.nPoints-1].objectives[0]) *
(ref.objectives[1] - ps.points[ps.nPoints-1].objectives[1]);
(ref.objectives[1] - ps.points[ps.nPoints-1].objectives[1]);
double height;
if (ps.nPoints == 1)
height = ref.objectives[2] - ps.points[ps.nPoints-1].objectives[2];
height = ref.objectives[2] - ps.points[ps.nPoints-1].objectives[2];
else
height = ps.points[ps.nPoints-2].objectives[2] - ps.points[ps.nPoints-1].objectives[2];
height = ps.points[ps.nPoints-2].objectives[2] - ps.points[ps.nPoints-1].objectives[2];
double hyperv = hypera * height;
for (int i = ps.nPoints - 2; i >= 0; i--) {
if (i == 0)
height = ref.objectives[2] - ps.points[i].objectives[2];
else
height = ps.points[i-1].objectives[2] - ps.points[i].objectives[2];
// search tree for point q to the right of current point
const double * prv_ip, * nxt_ip;
avl_node_t *tnode;
avl_init_node(ps.points[i].tnode, ps.points[i].objectives);
if (avl_search_closest(tree, ps.points[i].objectives, &tnode) <= 0) {
nxt_ip = (double *)(tnode->item);
tnode = tnode->prev;
} else {
nxt_ip = (tnode->next!=NULL)
? (double *)(tnode->next->item)
: ref.objectives;
}
// if p is not dominated
if (nxt_ip[0] > ps.points[i].objectives[0]) {
// insert p in tree
avl_insert_after(tree, tnode, ps.points[i].tnode);
if (tnode !=NULL) {
prv_ip = (double *)(tnode->item);
if (prv_ip[0] > ps.points[i].objectives[0]) {
const double * cur_ip;
tnode = ps.points[i].tnode->prev;
// cur_ip = point dominated by pp with highest [0]-coordinate
cur_ip = (double *)(tnode->item);
// for each point in s in tree dominated by p
while (tnode->prev) {
prv_ip = (double *)(tnode->prev->item);
// decrease area by contribution of s
hypera -= (prv_ip[1] - cur_ip[1])*(nxt_ip[0] - cur_ip[0]);
if (prv_ip[0] < ps.points[i].objectives[0])
break; // prv is not dominated by pp
cur_ip = prv_ip;