This repository has been archived on 2026-03-28. You can view files and clone it, but you cannot make any changes to its state, such as pushing and creating new issues, pull requests or comments.
eodev/eo/src/eoMerge.h
evomarc 6acdcb6d9b THe big eoReplacement update:
The interface for eoReplacement is now eoPop<EOT>&, eoPop<EOT>&
(i.e. no const) and the result must be in the first argument in the end.

Hence it is possible to do SSGA and all intermediate replacmeent procedures

The classes derived from eoMergeReduce.h are now in a separate file
The SSGA-like replcaement procedures are in eoReduceMerge.h
A more general replacement can be found in eoSurviveAndDie.h
(it could be made a littel more general - still open for upgrades).

Also some accessors have been added to the eoPop (best and worse individuals)

And include file eo has been updated
2000-12-19 18:41:19 +00:00

134 lines
3.7 KiB
C++

// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
//-----------------------------------------------------------------------------
// eoMerge.h
// Base class for elitist-merging classes
// (c) GeNeura Team, 1998
/*
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: todos@geneura.ugr.es, http://geneura.ugr.es
*/
//-----------------------------------------------------------------------------
#ifndef eoMerge_h
#define eoMerge_h
//-----------------------------------------------------------------------------
#include <stdexcept>
// EO includes
#include <eoPop.h> // eoPop
#include <eoFunctor.h> // eoMerge
/**
* eoMerge: Base class for elitist replacement algorithms.
* Merges the old population (first argument), with the new generation
*
* Its signature is exactly
* that of the selection base eoSelect, but its purpose is to merge the
* two populations into one (the second argument).
* Note that the algorithms assume that the second argument denotes the
* next generation.
*/
template<class Chrom> class eoMerge: public eoBF<const eoPop<Chrom>&, eoPop<Chrom>&, void>
{};
/**
Straightforward elitism class, specify the number of individuals to copy
into new geneneration or the rate w.r.t. pop size
*/
template <class EOT> class eoElitism : public eoMerge<EOT>
{
public :
eoElitism(double _rate, bool _interpret_as_rate = true):
rate(0), howmany(0)
{
if (_interpret_as_rate)
{
if ( (_rate<0) || (_rate>1) )
throw std::logic_error("eoElitism: rate shoud be in [0,1]");
rate = _rate;
}
else
{
if (_rate<0)
throw std::logic_error("Negative number of offspring in eoElitism!");
howmany = (unsigned int)_rate;
if (howmany != _rate)
cout << "Warning: Number of guys to merge in eoElitism was rounded";
}
}
void operator()(const eoPop<EOT>& _pop, eoPop<EOT>& _offspring)
{
if ((howmany == 0) && (rate == 0.0))
return;
unsigned howmanyLocal;
if (howmany == 0) // rate is specified
howmanyLocal = (unsigned int) (rate * _pop.size());
else
howmanyLocal = howmany;
if (howmanyLocal > _pop.size())
throw std::logic_error("Elite larger than population");
vector<const EOT*> result;
_pop.nth_element(howmanyLocal, result);
for (size_t i = 0; i < result.size(); ++i)
{
_offspring.push_back(*result[i]);
}
}
private :
double rate;
unsigned howmany;
};
/**
No elite
*/
template <class EOT> class eoNoElitism : public eoElitism<EOT>
{
public :
eoNoElitism() : eoElitism<EOT>(0) {}
};
/**
Very elitist class, copies entire population into next gen
*/
template <class EOT> class eoPlus : public eoMerge<EOT>
{
public :
void operator()(const eoPop<EOT>& _pop, eoPop<EOT>& _offspring)
{
_offspring.reserve(_offspring.size() + _pop.size());
for (size_t i = 0; i < _pop.size(); ++i)
{
_offspring.push_back(_pop[i]);
}
}
private :
};
//-----------------------------------------------------------------------------
#endif