From fa1a7fd6954a49f948a21a0d738a29e8017012e5 Mon Sep 17 00:00:00 2001 From: liefooga Date: Tue, 26 Jun 2007 12:15:36 +0000 Subject: [PATCH] add utils git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@382 331e1502-861f-0410-8da2-ba01fb791d7f --- .../moeoArchiveObjectiveVectorSavingUpdater.h | 94 +++++++++++++++++++ .../src/utils/moeoArchiveUpdater.h | 54 +++++++++++ .../src/utils/moeoBinaryMetricSavingUpdater.h | 90 ++++++++++++++++++ .../utils/moeoConvertPopToObjectiveVectors.h | 44 +++++++++ 4 files changed, 282 insertions(+) create mode 100644 branches/paradiseo-moeo-1.0/src/utils/moeoArchiveObjectiveVectorSavingUpdater.h create mode 100644 branches/paradiseo-moeo-1.0/src/utils/moeoArchiveUpdater.h create mode 100644 branches/paradiseo-moeo-1.0/src/utils/moeoBinaryMetricSavingUpdater.h create mode 100644 branches/paradiseo-moeo-1.0/src/utils/moeoConvertPopToObjectiveVectors.h diff --git a/branches/paradiseo-moeo-1.0/src/utils/moeoArchiveObjectiveVectorSavingUpdater.h b/branches/paradiseo-moeo-1.0/src/utils/moeoArchiveObjectiveVectorSavingUpdater.h new file mode 100644 index 000000000..3c6035e16 --- /dev/null +++ b/branches/paradiseo-moeo-1.0/src/utils/moeoArchiveObjectiveVectorSavingUpdater.h @@ -0,0 +1,94 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// moeoArchiveObjectiveVectorSavingUpdater.h +// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007 +/* + This library... + + Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr + */ +//----------------------------------------------------------------------------- + +#ifndef MOEOARCHIVEOBJECTIVEVECTORSAVINGUPDATER_H_ +#define MOEOARCHIVEOBJECTIVEVECTORSAVINGUPDATER_H_ + +#include +#include +#include +#include +#include + +#define MAX_BUFFER_SIZE 1000 + +/** + * This class allows to save the objective vectors of the solutions contained in an archive into a file at each generation. + */ +template < class MOEOT > +class moeoArchiveObjectiveVectorSavingUpdater : public eoUpdater +{ +public: + + /** + * Ctor + * @param _arch local archive + * @param _filename target filename + * @param _count put this variable to true if you want a new file to be created each time () is called and to false if you only want the file to be updated + * @param _id own ID + */ + moeoArchiveObjectiveVectorSavingUpdater (moeoArchive & _arch, const std::string & _filename, bool _count = false, int _id = -1) : + arch(_arch), filename(_filename), count(_count), counter(0), id(_id) + {} + + + /** + * Saves the fitness of the archive's members into the file + */ + void operator()() { + char buff[MAX_BUFFER_SIZE]; + if (count) + { + if (id == -1) + { + sprintf (buff, "%s.%u", filename.c_str(), counter ++); + } + else + { + sprintf (buff, "%s.%u.%u", filename.c_str(), id, counter ++); + } + } + else + { + if (id == -1) + { + sprintf (buff, "%s", filename.c_str()); + } + else + { + sprintf (buff, "%s.%u", filename.c_str(), id); + } + counter ++; + } + std::ofstream f(buff); + for (unsigned int i = 0; i < arch.size (); i++) + f << arch[i].objectiveVector() << std::endl; + f.close (); + } + + +private: + + /** local archive */ + moeoArchive & arch; + /** target filename */ + std::string filename; + /** this variable is set to true if a new file have to be created each time () is called and to false if the file only HAVE to be updated */ + bool count; + /** counter */ + unsigned int counter; + /** own ID */ + int id; + +}; + +#endif /*MOEOARCHIVEOBJECTIVEVECTORSAVINGUPDATER_H_*/ diff --git a/branches/paradiseo-moeo-1.0/src/utils/moeoArchiveUpdater.h b/branches/paradiseo-moeo-1.0/src/utils/moeoArchiveUpdater.h new file mode 100644 index 000000000..6596940c2 --- /dev/null +++ b/branches/paradiseo-moeo-1.0/src/utils/moeoArchiveUpdater.h @@ -0,0 +1,54 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// moeoArchiveUpdater.h +// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007 +/* + This library... + + Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr + */ +//----------------------------------------------------------------------------- + +#ifndef MOEOARCHIVEUPDATER_H_ +#define MOEOARCHIVEUPDATER_H_ + +#include +#include +#include + +/** + * This class allows to update the archive at each generation with newly found non-dominated solutions. + */ +template < class MOEOT > +class moeoArchiveUpdater : public eoUpdater +{ +public: + + /** + * Ctor + * @param _arch an archive of non-dominated solutions + * @param _pop the main population + */ + moeoArchiveUpdater(moeoArchive < MOEOT > & _arch, const eoPop < MOEOT > & _pop) : arch(_arch), pop(_pop) + {} + + + /** + * Updates the archive with newly found non-dominated solutions contained in the main population + */ + void operator()() { + arch.update(pop); + } + + +private: + + /** the archive of non-dominated solutions */ + moeoArchive < MOEOT > & arch; + /** the main population */ + const eoPop < MOEOT > & pop; + +}; + +#endif /*MOEOARCHIVEUPDATER_H_*/ diff --git a/branches/paradiseo-moeo-1.0/src/utils/moeoBinaryMetricSavingUpdater.h b/branches/paradiseo-moeo-1.0/src/utils/moeoBinaryMetricSavingUpdater.h new file mode 100644 index 000000000..237ea4909 --- /dev/null +++ b/branches/paradiseo-moeo-1.0/src/utils/moeoBinaryMetricSavingUpdater.h @@ -0,0 +1,90 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// moeoBinaryMetricSavingUpdater.h +// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007 +/* + This library... + + Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr + */ +//----------------------------------------------------------------------------- + +#ifndef MOEOBINARYMETRICSAVINGUPDATER_H_ +#define MOEOBINARYMETRICSAVINGUPDATER_H_ + +#include +#include +#include +#include +#include +#include + +/** + * This class allows to save the progression of a binary metric comparing the objective vectors of the current population (or archive) + * with the objective vectors of the population (or archive) of the generation (n-1) into a file + */ +template < class MOEOT > +class moeoBinaryMetricSavingUpdater : public eoUpdater +{ +public: + + /** The objective vector type of a solution */ + typedef typename MOEOT::ObjectiveVector ObjectiveVector; + + + /** + * Ctor + * @param _metric the binary metric comparing two Pareto sets + * @param _pop the main population + * @param _filename the target filename + */ + moeoBinaryMetricSavingUpdater (moeoVectorVsVectorBinaryMetric < ObjectiveVector, double > & _metric, const eoPop < MOEOT > & _pop, std::string _filename) : + metric(_metric), pop(_pop), filename(_filename), counter(1) + {} + + + /** + * Saves the metric's value for the current generation + */ + void operator()() { + if (pop.size()) { + if (firstGen) { + firstGen = false; + } + else { + // creation of the two Pareto sets + std::vector < ObjectiveVector > from; + std::vector < ObjectiveVector > to; + for (unsigned int i=0; i & metric; + /** main population */ + const eoPop < MOEOT > & pop; + /** (n-1) population */ + eoPop< MOEOT > oldPop; + /** target filename */ + std::string filename; + /** is it the first generation ? */ + bool firstGen; + /** counter */ + unsigned int counter; + +}; + +#endif /*MOEOBINARYMETRICSAVINGUPDATER_H_*/ diff --git a/branches/paradiseo-moeo-1.0/src/utils/moeoConvertPopToObjectiveVectors.h b/branches/paradiseo-moeo-1.0/src/utils/moeoConvertPopToObjectiveVectors.h new file mode 100644 index 000000000..ca25cd7a5 --- /dev/null +++ b/branches/paradiseo-moeo-1.0/src/utils/moeoConvertPopToObjectiveVectors.h @@ -0,0 +1,44 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// moeoConvertPopToObjectiveVectors.h +// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007 +/* + This library... + + Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr + */ +//----------------------------------------------------------------------------- + +#ifndef MOEOPOPTOOBJECTIVEVECTORS_H_ +#define MOEOPOPTOOBJECTIVEVECTORS_H_ + +#include +#include + +/** + * Functor allowing to get a vector of objective vectors from a population + */ +template < class MOEOT, class ObjectiveVector = typename MOEOT::ObjectiveVector > +class moeoConvertPopToObjectiveVectors : public eoUF < const eoPop < MOEOT >, const std::vector < ObjectiveVector > > +{ +public: + + /** + * Returns a vector of the objective vectors from the population _pop + * @param _pop the population + */ + const std::vector < ObjectiveVector > operator()(const eoPop < MOEOT > _pop) + { + std::vector < ObjectiveVector > result; + result.resize(_pop.size()); + for (unsigned int i=0; i<_pop.size(); i++) + { + result.push_back(_pop[i].objectiveVector()); + } + return result; + } + +}; + +#endif /*MOEOPOPTOOBJECTIVEVECTORS_H_*/