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_);
......@@ -150,7 +152,7 @@ void FixedPisaNsga2<CO, MO>::initialize() {
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;
}
......@@ -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.
......
......@@ -35,80 +35,72 @@
#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
{
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
#if hyper_opt == 1
for (int i = n - fr - 1; i >= 0; i--)
#else
#else
for (int i = n - 1; i >= 0; i--)
#endif
#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
{
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
#if hyper_opt == 1
for (int i = n - fr - 1; i >= 0; i--)
#else
#else
for (int i = n - 1; i >= 0; i--)
#endif
#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;
......@@ -119,14 +111,14 @@ int dominates2way(POINT p, POINT q)
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
{
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 hyper_opt == 0
if (fr > frmax)
{frmax = fr;
fs[fr].points = (POINT*) malloc( maxm * sizeof(POINT));
......@@ -135,7 +127,7 @@ void makeDominatedBit(FRONT ps, int p)
fs[fr].points[j].objectives = (OBJECTIVE*) malloc(maxn * sizeof(OBJECTIVE));
}
}
#endif
#endif
int z = ps.nPoints - 1 - p;
for (int i = 0; i < z; i++)
......@@ -163,39 +155,39 @@ void makeDominatedBit(FRONT ps, int p)
fs[fr].nPoints++;}
}
fr++;
}
}
double hv2(FRONT ps)
// returns the hypervolume of ps[0 ..] in 2D
// assumes that ps is sorted improving
{
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:
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.
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);
......@@ -284,22 +276,22 @@ Canada, July 2006.
}
avl_clear_tree(tree);
return hyperv;
}
}
double inclhv(POINT p)
// returns the inclusive hypervolume of p
{
double inclhv(POINT p)
// returns the inclusive hypervolume of p
{
double volume = 1;
for (int i = 0; i < n; i++)
volume *= fabs(p.objectives[i] - ref.objectives[i]);
return volume;
}
}
double exclhv(FRONT ps, int p)
// returns the exclusive hypervolume of ps[p] relative to ps[p+1 ..]
{
double exclhv(FRONT ps, int p)
// returns the exclusive hypervolume of ps[p] relative to ps[p+1 ..]
{
double volume = inclhv(ps.points[p]);
if (ps.nPoints > p + 1)
{
......@@ -308,27 +300,27 @@ double exclhv(FRONT ps, int p)
fr--;
}
return volume;
}
}
double hv(FRONT ps)
// returns the hypervolume of ps[0 ..]
{
#if hyper_opt > 0
double hv(FRONT ps)
// returns the hypervolume of ps[0 ..]
{
#if hyper_opt > 0
qsort(ps.points, ps.nPoints, sizeof(POINT), greater);
#endif
#endif
#if hyper_opt == 2
#if hyper_opt == 2
if (n == 2) return hv2(ps);
#elif hyper_opt == 3
#elif hyper_opt == 3
if (n == 3) return hv3_AVL(ps);
#endif
#endif
double volume = 0;
#if hyper_opt <= 1
#if hyper_opt <= 1
for (int i = 0; i < ps.nPoints; i++) volume += exclhv(ps, i);
#else
#else
n--;
for (int i = ps.nPoints - 1; i >= 0; i--)
// we can ditch dominated points here,
......@@ -336,26 +328,27 @@ double hv(FRONT ps)
volume += fabs(ps.points[i].objectives[n] - ref.objectives[n]) * exclhv(ps, i);
n++;
#endif
#endif
return volume;
}
}
double Hypervolume::FromFile(std::string file)
// processes each front from the file
{
double FromFile(std::string file, const std::vector<double>& referencePoint)
// processes each front from the file
{
FILECONTENTS *f = readFile(file.c_str());
// find the biggest fronts
for (int i = 0; i < f->nFronts; i++)
{if (f->fronts[i].nPoints > maxm) maxm = f->fronts[i].nPoints;
{
if (f->fronts[i].nPoints > maxm) maxm = f->fronts[i].nPoints;
if (f->fronts[i].n > maxn) maxn = f->fronts[i].n;
}
// allocate memory
#if hyper_opt == 0
#if hyper_opt == 0
fs = (FRONT*) malloc(sizeof(FRONT) * maxm);
#else
#else
// slicing (hyper_opt > 1) saves a level of recursion
int maxd = maxn - (hyper_opt / 2 + 1);
......@@ -373,7 +366,7 @@ double Hypervolume::FromFile(std::string file)
fs[i].points[j].objectives = (OBJECTIVE*)malloc(sizeof(OBJECTIVE) * (maxn - (i + 1) * (hyper_opt / 2)));
}
}
#endif
#endif
tree = avl_alloc_tree ((avl_compare_t) compare_tree_asc,
(avl_freeitem_t) free);
......@@ -385,18 +378,13 @@ double Hypervolume::FromFile(std::string file)
// initialise to zero (origin)
for (int i = 0; i < maxn; i++) ref.objectives[i] = 0;
/*
if (argc == 2)
{printf("No reference point provided: using the origin\n");
for (int i = 0; i < maxn; i++) ref.objectives[i] = 0;
}
else if (argc - 2 != maxn)
{printf("Your reference point should have %d values\n", maxn);
return 0;
if (referencePoint.empty()) {
printf("No reference point provided: using the origin\n");
} else if ((int)referencePoint.size() != maxn) {
printf("Your reference point should have %d values: using the origin\n", maxn);
} else {
for (int i = 0; i < maxn; i++) ref.objectives[i] = referencePoint[i];
}
else
for (int i = 2; i < argc; i++) ref.objectives[i - 2] = atof(argv[i]);
*/
for (int i = 0; i < f->nFronts; i++)
{
......@@ -405,14 +393,14 @@ double Hypervolume::FromFile(std::string file)
getrusage (RUSAGE_SELF, &ru_before);
n = f->fronts[i].n;
#if hyper_opt >= 3
#if hyper_opt >= 3
if (n == 2)
{qsort(f->fronts[i].points, f->fronts[i].nPoints, sizeof(POINT), greater);
//printf("hv(%d) = %1.10f\n", i+1, hv2(f->fronts[i]));
return hv2(f->fronts[i]);
}
else
#endif
#endif
//printf("hv(%d) = %1.10f\n", i+1, hv(f->fronts[i]));
return hv(f->fronts[i]);
......@@ -423,4 +411,9 @@ double Hypervolume::FromFile(std::string file)
}
return 0;
}
}
#undef MAXIMISING
#undef BEATS
#undef WORSE
\ No newline at end of file
......@@ -22,6 +22,9 @@
*/
#include <string>
#include <vector>
// To do:
// - can we sort less often or reduce/optimise dominance checks?
// - should we use FPL's data structure?
......@@ -36,7 +39,7 @@
namespace Hypervolume {
double FromFile(std::string file);
double FromFile(std::string file, const std::vector<double>& referencePoint);
//Room for more functions to compute volumes
//without accessing external files
}
......@@ -64,6 +64,7 @@ namespace {
MAXGENERATIONS,
EPSILON,
EXPECTEDHYPERVOL,
HYPERVOLREFERENCE,
CONVHVOLPROG,
ONEPILOTCONVERGE,
SOLSYNCH,
......@@ -114,6 +115,8 @@ OptimizeCmd::OptimizeCmd():
("EPSILON", "Tolerance of hypervolume criteria, default 0.001");
itsAttr[EXPECTEDHYPERVOL] = Attributes::makeReal
("EXPECTED_HYPERVOL", "The reference hypervolume, default 0");
itsAttr[HYPERVOLREFERENCE] = Attributes::makeRealArray
("HYPERVOLREFERENCE", "The reference point (real array) for the hypervolume, default empty (origin)");
itsAttr[CONVHVOLPROG] = Attributes::makeReal
("CONV_HVOL_PROG", "converge if change in hypervolume is smaller, default 0");
itsAttr[ONEPILOTCONVERGE] = Attributes::makeBool
......@@ -123,7 +126,7 @@ OptimizeCmd::OptimizeCmd():
itsAttr[GENEMUTATIONPROBABILITY] = Attributes::makeReal
("GENE_MUTATION_PROBABILITY", "Mutation probability of individual gene, default: 0.5");
itsAttr[MUTATIONPROBABILITY] = Attributes::makeReal
("MUTATION_PROBABILITY", "Mutation probability of genom, default: 0.5");
("MUTATION_PROBABILITY", "Mutation probability of genome, default: 0.5");
itsAttr[RECOMBINATIONPROBABILITY] = Attributes::makeReal
("RECOMBINATION_PROBABILITY", "Probability for genes to recombine, default: 0.5");
itsAttr[SIMBINCROSSOVERNU] = Attributes::makeReal
......@@ -269,6 +272,7 @@ void OptimizeCmd::execute() {
}
}
}
// sanity checks
if (Attributes::getString(itsAttr[INPUT]) == "") {
throw OpalException("OptimizeCmd::execute",
"The argument INPUT has to be provided");
......@@ -281,6 +285,11 @@ void OptimizeCmd::execute() {
throw OpalException("OptimizeCmd::execute",
"The argument MAXGENERATIONS has to be provided");
}
if (Attributes::getRealArray(itsAttr[HYPERVOLREFERENCE]).empty() == false &&
Attributes::getRealArray(itsAttr[HYPERVOLREFERENCE]).size() != objectivesstr.size()) {
throw OpalException("OptimizeCmd::execute",
"The hypervolume reference point should have the same dimension as the objectives");
}
if (Attributes::getString(itsAttr[SIMTMPDIR]) != "") {
fs::path dir(Attributes::getString(itsAttr[SIMTMPDIR]));
......@@ -405,7 +414,7 @@ void OptimizeCmd::execute() {
CmdArguments_t args(new CmdArguments(argv.size(), &argv[0]));
boost::shared_ptr<Comm_t> comm(new Comm_t(args, MPI_COMM_WORLD));
boost::scoped_ptr<pilot_t> pi(new pilot_t(args, comm, funcs, dvars, objectives, constraints));
boost::scoped_ptr<pilot_t> pi(new pilot_t(args, comm, funcs, dvars, objectives, constraints, Attributes::getRealArray(itsAttr[HYPERVOLREFERENCE])));
} catch (OptPilotException &e) {
std::cout << "Exception caught: " << e.what() << std::endl;
......
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