Added changes mainly by Marc
This commit is contained in:
parent
449ed17ff8
commit
91f5ddbdaa
28 changed files with 2400 additions and 1595 deletions
|
|
@ -1,79 +1,96 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// eoInsertion.h
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef eoInsertion_h
|
||||
#define eoInsertion_h
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <eoPop.h> // eoPop
|
||||
#include <eoPopOps.h> // eoMerge
|
||||
|
||||
/******************************************************************************
|
||||
* eoInsertion: A replacement algorithm.
|
||||
* Creates a new population with all the breeders and the best individuals
|
||||
* from the original population.
|
||||
*****************************************************************************/
|
||||
|
||||
template<class Chrom> class eoInsertion: public eoMerge<Chrom>
|
||||
{
|
||||
public:
|
||||
/// (Default) Constructor.
|
||||
eoInsertion(const float& _rate = 1.0): eoMerge<Chrom>(_rate) {}
|
||||
|
||||
/**
|
||||
* Creates a new population based on breeders and original populations.
|
||||
* @param breeders The population of breeders.
|
||||
* @param pop The original population.
|
||||
*/
|
||||
/*void operator()(eoPop<Chrom>& breeders, eoPop<Chrom>& pop)
|
||||
{
|
||||
int new_size = static_cast<int>(pop.size() * rate());
|
||||
|
||||
if (new_size == breeders.size())
|
||||
{
|
||||
pop = breeders;
|
||||
}
|
||||
else if (new_size < breeders.size())
|
||||
{
|
||||
pop = breeders;
|
||||
sort(pop.begin(), pop.end());
|
||||
pop.erase(pop.begin(), pop.begin() - new_size + pop.size());
|
||||
}
|
||||
else
|
||||
{
|
||||
sort(pop.begin(), pop.end());
|
||||
pop.erase(pop.begin(),
|
||||
pop.begin() + breeders.size() + pop.size() - new_size);
|
||||
copy(breeders.begin(), breeders.end(),
|
||||
back_insert_iterator<eoPop<Chrom> >(pop));
|
||||
}
|
||||
}*/
|
||||
|
||||
void operator()(eoPop<Chrom>& breeders, eoPop<Chrom>& pop)
|
||||
{
|
||||
unsigned target = static_cast<unsigned>(rint(pop.size() * rate()));
|
||||
|
||||
pop.swap(breeders);
|
||||
|
||||
if (target < pop.size())
|
||||
{
|
||||
partial_sort(pop.begin(), pop.begin() + target, pop.end(),
|
||||
greater<Chrom>());
|
||||
pop.erase(pop.begin() + target, pop.end());
|
||||
}
|
||||
else
|
||||
{
|
||||
target = min(target - pop.size(), breeders.size());
|
||||
partial_sort(breeders.begin(), breeders.begin() + target,
|
||||
breeders.end(), greater<Chrom>());
|
||||
copy(breeders.begin(), breeders.begin() + target,
|
||||
back_insert_iterator<eoPop<Chrom> >(pop));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#endif eoInsertion_h
|
||||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoInsertion.h
|
||||
// Inserts new members into the population
|
||||
// (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 eoInsertion_h
|
||||
#define eoInsertion_h
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <iostream>
|
||||
|
||||
// EO includes
|
||||
#include <eoPop.h> // eoPop
|
||||
#include <eoMerge.h> // eoMerge
|
||||
|
||||
/******************************************************************************
|
||||
* eoInsertion: A replacement algorithm.
|
||||
* Creates a new population with all the breeders and the best individuals
|
||||
* from the original population.
|
||||
*****************************************************************************/
|
||||
|
||||
template<class Chrom> class eoInsertion: public eoMerge<Chrom>
|
||||
{
|
||||
public:
|
||||
/// (Default) Constructor.
|
||||
eoInsertion(const float& _rate = 1.0): eoMerge<Chrom>( _rate ) {}
|
||||
|
||||
/// Ctor from istream
|
||||
eoInsertion( istream& _is): eoBinPopOp<Chrom>( _is ) {};
|
||||
|
||||
/// Dtor
|
||||
virtual ~eoInsertion() {};
|
||||
|
||||
/**
|
||||
* Creates a new population based on breeders and original populations.
|
||||
* @param breeders The population of breeders. Should be sorted to work correctly
|
||||
* @param pop The original population.
|
||||
*/
|
||||
void operator()( eoPop<Chrom>& _breeders, eoPop<Chrom>& _pop)
|
||||
{
|
||||
unsigned target = static_cast<unsigned>(rint(_pop.size() * rate()));
|
||||
|
||||
_pop.swap(_breeders);
|
||||
|
||||
if (target < _pop.size())
|
||||
{
|
||||
partial_sort(_pop.begin(), _pop.begin() + target, _pop.end(),
|
||||
greater<Chrom>());
|
||||
_pop.erase(_pop.begin() + target, _pop.end());
|
||||
}
|
||||
else
|
||||
{
|
||||
target = min(target - _pop.size(), _breeders.size());
|
||||
partial_sort(_breeders.begin(), _breeders.begin() + target,
|
||||
_breeders.end(), greater<Chrom>());
|
||||
copy(_breeders.begin(), _breeders.begin() + target,
|
||||
back_insert_iterator<eoPop<Chrom> >(_pop));
|
||||
}
|
||||
};
|
||||
|
||||
/** @name Methods from eoObject */
|
||||
//@{
|
||||
/** readFrom and printOn inherited from eoMerge */
|
||||
|
||||
/** Inherited from eoObject. Returns the class name.
|
||||
@see eoObject
|
||||
*/
|
||||
virtual string className() const {return "eoInsertion";};
|
||||
//@}
|
||||
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#endif eoInsertion_h
|
||||
|
|
|
|||
Reference in a new issue