/** -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- ----------------------------------------------------------------------------- eoSharing.h (c) Maarten Keijzer, Marc Schoenauer, 2001 This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Contact: Marc.Schoenauer@inria.fr mkeijzer@dhi.dk */ //----------------------------------------------------------------------------- #ifndef eoSharing_h #define eoSharing_h #include #include /** Sharing is a perf2worth class that implements * Goldberg and Richardson's basic sharing */ /** A helper class for Sharing - to hold distances * * @ingroup Selectors * */ class dMatrix : public std::vector { public: // Ctor : sets size dMatrix(unsigned _s) : std::vector(_s*_s), rSize(_s) {} /** simple accessor */ double operator()(unsigned _i, unsigned _j) const { return this->operator[](_i*rSize + _j); } /** reference - to set values */ double & operator()(unsigned _i, unsigned _j) { return this->operator[](_i*rSize + _j); } /** just in case */ void printOn(std::ostream & _os) { unsigned index=0; for (unsigned i=0; ioperator[](index++) << " " ; _os << std::endl; } _os << std::endl; } private: unsigned rSize; // row size (== number of columns!) }; /** Sharing is a perf2worth class that implements * Goldberg and Richardson's basic sharing * see eoSharingSelect for how to use it * and test/t-eoSharing.cpp for a sample use of both * @ingroup Selectors */ template class eoSharing : public eoPerf2Worth { public: using eoPerf2Worth::value; /* Ctor requires a distance - cannot have a default distance! */ eoSharing(double _nicheSize, eoDistance & _dist) : eoPerf2Worth("Sharing"), nicheSize(_nicheSize), dist(_dist) {} /** Computes shared fitnesses */ void operator()(const eoPop& _pop) { unsigned i, j, pSize=_pop.size(); if (pSize <= 1) throw std::runtime_error("Apptempt to do sharing with population of size 1"); value().resize(pSize); std::vector sim(pSize); // to hold the similarities dMatrix distMatrix(pSize); // to hold the distances // compute the similarities (wrong name for distMatrix, I know) distMatrix(0,0)=1; for (i=1; inicheSize ? 0 : 1-(d/nicheSize) ); } } for (i=0; i & dist; // specific distance }; #endif