From 750d72313f02b4ac084973667a614bcb376f1e46 Mon Sep 17 00:00:00 2001 From: liefooga Date: Thu, 21 Jun 2007 14:26:30 +0000 Subject: [PATCH] add distance git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@342 331e1502-861f-0410-8da2-ba01fb791d7f --- .../src/distance/moeoDistance.h | 227 ++++++++++++++++++ .../src/distance/moeoDistanceMatrix.h | 75 ++++++ 2 files changed, 302 insertions(+) create mode 100644 branches/paradiseo-moeo-1.0/src/distance/moeoDistance.h create mode 100644 branches/paradiseo-moeo-1.0/src/distance/moeoDistanceMatrix.h diff --git a/branches/paradiseo-moeo-1.0/src/distance/moeoDistance.h b/branches/paradiseo-moeo-1.0/src/distance/moeoDistance.h new file mode 100644 index 000000000..c15e886f8 --- /dev/null +++ b/branches/paradiseo-moeo-1.0/src/distance/moeoDistance.h @@ -0,0 +1,227 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// moeoDistance.h +// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007 +/* + This library... + + Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr + */ +//----------------------------------------------------------------------------- + +#ifndef MOEODISTANCE_H_ +#define MOEODISTANCE_H_ + +#include +#include +#include + +/** + * The base class for distance computation. + */ +template < class MOEOT , class Type > +class moeoDistance : public eoBF < const MOEOT &, const MOEOT &, const Type > +{ +public: + + + /** + * Nothing to do + * @param _pop the population + */ + virtual void setup(const eoPop < MOEOT > & _pop) + {} + + /** + * Nothing to do + * @param _min lower bound + * @param _max upper bound + * @param _obj the objective index + */ + virtual void setup(double _min, double _max, unsigned _obj) + {} + + + /** + * Nothing to do + * @param _realInterval the eoRealInterval object + * @param _obj the objective index + */ + virtual void setup(eoRealInterval _realInterval, unsigned _obj) + {} + +}; + + +/** + * The base class for double distance computation with normalized objective values (i.e. between 0 and 1). + */ +template < class MOEOT , class Type = double > +class moeoNormalizedDistance : public moeoDistance < MOEOT , Type > +{ +public: + + /** the objective vector type of the solutions */ + typedef typename MOEOT::ObjectiveVector ObjectiveVector; + + + /** + * Default ctr + */ + moeoNormalizedDistance() + { + bounds.resize(ObjectiveVector::Traits::nObjectives()); + // initialize bounds in case someone does not want to use them + for (unsigned i=0; i & _pop) + { + double min, max; + for (unsigned i=0; i bounds; + +}; + + +/** + * A class allowing to compute an euclidian distance between two solutions in the objective space with normalized objective values (i.e. between 0 and 1). + * A distance value then lies between 0 and sqrt(nObjectives). + */ +template < class MOEOT > +class moeoEuclideanDistance : public moeoNormalizedDistance < MOEOT > +{ +public: + + /** the objective vector type of the solutions */ + typedef typename MOEOT::ObjectiveVector ObjectiveVector; + + + /** + * Returns the euclidian distance between _moeo1 and _moeo2 in the objective space + * @param _moeo1 the first solution + * @param _moeo2 the second solution + */ + const double operator()(const MOEOT & _moeo1, const MOEOT & _moeo2) + { + double result = 0.0; + double tmp1, tmp2; + for (unsigned i=0; i :: bounds; + +}; + +/** + * A class allowing to compute the Manhattan distance between two solutions in the objective space normalized objective values (i.e. between 0 and 1). + * A distance value then lies between 0 and nObjectives. + */ +template < class MOEOT > +class moeoManhattanDistance : public moeoNormalizedDistance < MOEOT > +{ +public: + + /** the objective vector type of the solutions */ + typedef typename MOEOT::ObjectiveVector ObjectiveVector; + + + /** + * Returns the Manhattan distance between _moeo1 and _moeo2 in the objective space + * @param _moeo1 the first solution + * @param _moeo2 the second solution + */ + const double operator()(const MOEOT & _moeo1, const MOEOT & _moeo2) + { + double result = 0.0; + double tmp1, tmp2; + for (unsigned i=0; i :: bounds; + +}; + +#endif /*MOEODISTANCE_H_*/ diff --git a/branches/paradiseo-moeo-1.0/src/distance/moeoDistanceMatrix.h b/branches/paradiseo-moeo-1.0/src/distance/moeoDistanceMatrix.h new file mode 100644 index 000000000..a3279b163 --- /dev/null +++ b/branches/paradiseo-moeo-1.0/src/distance/moeoDistanceMatrix.h @@ -0,0 +1,75 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// moeoDistanceMatrix.h +// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007 +/* + This library... + + Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr + */ +//----------------------------------------------------------------------------- + +#ifndef MOEODISTANCEMATRIX_H_ +#define MOEODISTANCEMATRIX_H_ + +#include +#include + +/** + * A matrix to compute distances between every pair of individuals contained in a population. + */ +template < class MOEOT , class Type > +class moeoDistanceMatrix : public eoUF < const eoPop < MOEOT > &, void > , public std::vector< std::vector < Type > > +{ +public: + + using std::vector< std::vector < Type > > :: size; + using std::vector< std::vector < Type > > :: operator[]; + + + /** + * Ctor + * @param _size size for every dimension of the matrix + * @param _distance the distance to use + */ + moeoDistanceMatrix (unsigned _size, moeoDistance < MOEOT , Type > & _distance) : distance(_distance) + { + this->resize(_size); + for(unsigned i=0; i<_size; i++) + { + this->operator[](i).resize(_size); + } + } + + + /** + * Sets the distance between every pair of individuals contained in the population _pop + * @param _pop the population + */ + void operator()(const eoPop < MOEOT > & _pop) + { + // 1 - setup the bounds (if necessary) + distance.setup(_pop); + // 2 - compute distances + this->operator[](0).operator[](0) = Type(); + for (unsigned i=0; ioperator[](i).operator[](i) = Type(); + for (unsigned j=0; joperator[](i).operator[](j) = distance(_pop[i], _pop[j]); + this->operator[](j).operator[](i) = this->operator[](i).operator[](j); + } + } + } + + +private: + + /** the distance to use */ + moeoDistance < MOEOT , Type > & distance; + +}; + +#endif /*MOEODISTANCEMATRIX_H_*/