add selection

git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@381 331e1502-861f-0410-8da2-ba01fb791d7f
This commit is contained in:
liefooga 2007-06-26 12:15:06 +00:00
commit 7c219dbd48
7 changed files with 553 additions and 0 deletions

View file

@ -0,0 +1,83 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
//-----------------------------------------------------------------------------
// moeoDetTournamentSelect.h
// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007
/*
This library...
Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr
*/
//-----------------------------------------------------------------------------
#ifndef MOEODETTOURNAMENTSELECT_H_
#define MOEODETTOURNAMENTSELECT_H_
#include <comparator/moeoComparator.h>
#include <comparator/moeoFitnessThenDiversityComparator.h>
#include <selection/moeoSelectOne.h>
#include <selection/moeoSelectors.h>
/**
* Selection strategy that selects ONE individual by deterministic tournament.
*/
template < class MOEOT > class moeoDetTournamentSelect:public moeoSelectOne < MOEOT >
{
public:
/**
* Full Ctor.
* @param _comparator the comparator (used to compare 2 individuals)
* @param _tSize the number of individuals in the tournament (default: 2)
*/
moeoDetTournamentSelect (moeoComparator < MOEOT > & _comparator, unsigned int _tSize = 2) : comparator (_comparator), tSize (_tSize)
{
// consistency check
if (tSize < 2)
{
std::
cout << "Warning, Tournament size should be >= 2\nAdjusted to 2\n";
tSize = 2;
}
}
/**
* Ctor without comparator. A moeoFitnessThenDiversityComparator is used as default.
* @param _tSize the number of individuals in the tournament (default: 2)
*/
moeoDetTournamentSelect (unsigned int _tSize = 2) : comparator (defaultComparator), tSize (_tSize)
{
// consistency check
if (tSize < 2)
{
std::
cout << "Warning, Tournament size should be >= 2\nAdjusted to 2\n";
tSize = 2;
}
}
/**
* Apply the tournament to the given population
* @param _pop the population
*/
const MOEOT & operator() (const eoPop < MOEOT > &_pop)
{
// use the selector
return mo_deterministic_tournament (_pop, tSize, comparator);
}
protected:
/** the comparator (used to compare 2 individuals) */
moeoComparator < MOEOT > & comparator;
/** a fitness then diversity comparator can be used as default */
moeoFitnessThenDiversityComparator < MOEOT > defaultComparator;
/** the number of individuals in the tournament */
unsigned int tSize;
};
#endif /*MOEODETTOURNAMENTSELECT_H_ */

View file

@ -0,0 +1,43 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
//-----------------------------------------------------------------------------
// moeoRandomSelect.h
// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007
/*
This library...
Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr
*/
//-----------------------------------------------------------------------------
#ifndef MOEORANDOMSELECT_H_
#define MOEORANDOMSELECT_H_
#include <eoRandomSelect.h>
#include <selection/moeoSelectOne.h>
/**
* Selection strategy that selects only one element randomly from a whole population.
*/
template < class MOEOT > class moeoRandomSelect:public moeoSelectOne < MOEOT >, public eoRandomSelect <MOEOT >
{
public:
/**
* Ctor.
*/
moeoRandomSelect(){}
/**
* Return one individual at random by using an eoRandomSelect.
*/
const MOEOT & operator () (const eoPop < MOEOT > &_pop)
{
return eoRandomSelect < MOEOT >::operator ()(_pop);
}
};
#endif /*MOEORANDOMSELECT_H_ */

View file

@ -0,0 +1,62 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
//-----------------------------------------------------------------------------
// moeoRouletteSelect.h
// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007
/*
This library...
Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr
*/
//-----------------------------------------------------------------------------
#ifndef MOEOROULETTESELECT_H_
#define MOEOROULETTESELECT_H_
#include <selection/moeoSelectOne.h>
#include <selection/moeoSelectors.h>
/**
* Selection strategy that selects ONE individual by using roulette wheel process.
* @WARNING This selection only uses fitness values (and not diversity values).
*/
template < class MOEOT >
class moeoRouletteSelect:public moeoSelectOne < MOEOT >
{
public:
/**
* Ctor.
* @param _tSize the number of individuals in the tournament (default: 2)
*/
moeoRouletteSelect (unsigned int _tSize = 2) : tSize (_tSize)
{
// consistency check
if (tSize < 2)
{
std::
cout << "Warning, Tournament size should be >= 2\nAdjusted to 2\n";
tSize = 2;
}
}
/**
* Apply the tournament to the given population
* @param _pop the population
*/
const MOEOT & operator () (const eoPop < MOEOT > & _pop)
{
// use the selector
return mo_roulette_wheel(_pop,tSize);
}
protected:
/** size */
double & tSize;
};
#endif /*MOEOROULETTESELECT_H_ */

View file

@ -0,0 +1,92 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
//-----------------------------------------------------------------------------
// moeoSelectFormPopAndArch.h
// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007
/*
This library...
Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr
*/
//-----------------------------------------------------------------------------
#ifndef MOEOSELECTONEFROMPOPANDARCH_H_
#define MOEOSELECTONEFROMPOPANDARCH_H_
#include <eoPop.h>
#include <utils/eoRNG.h>
#include <archive/moeoArchive.h>
#include <selection/moeoSelectOne.h>
#include <selection/moeoRandomSelect.h>
/**
* Elitist selection process that consists in choosing individuals in the archive as well as in the current population.
*/
template < class MOEOT >
class moeoSelectFromPopAndArch : public moeoSelectOne < MOEOT >
{
public:
/**
* Ctor
* @param _popSelectOne the population's selection operator
* @param _archSelectOne the archive's selection operator
* @param _arch the archive
* @param _ratioFromPop the ratio of selected individuals from the population
*/
moeoSelectFromPopAndArch (moeoSelectOne < MOEOT > & _popSelectOne, moeoSelectOne < MOEOT > _archSelectOne, moeoArchive < MOEOT > & _arch, double _ratioFromPop=0.5)
: popSelectOne(_popSelectOne), archSelectOne(_archSelectOne), arch(_arch), ratioFromPop(_ratioFromPop)
{}
/**
* Defaulr ctor - the archive's selection operator is a random selector
* @param _popSelectOne the population's selection operator
* @param _arch the archive
* @param _ratioFromPop the ratio of selected individuals from the population
*/
moeoSelectFromPopAndArch (moeoSelectOne < MOEOT > & _popSelectOne, moeoArchive < MOEOT > & _arch, double _ratioFromPop=0.5)
: popSelectOne(_popSelectOne), archSelectOne(randomSelectOne), arch(_arch), ratioFromPop(_ratioFromPop)
{}
/**
* The selection process
*/
virtual const MOEOT & operator () (const eoPop < MOEOT > & pop)
{
if (arch.size() > 0)
if (rng.flip(ratioFromPop))
return popSelectOne(pop);
else
return archSelectOne(arch);
else
return popSelectOne(pop);
}
/**
* Setups some population stats
*/
virtual void setup (const eoPop < MOEOT > & _pop)
{
popSelectOne.setup(_pop);
}
private:
/** The population's selection operator */
moeoSelectOne < MOEOT > & popSelectOne;
/** The archive's selection operator */
moeoSelectOne < MOEOT > & archSelectOne;
/** The archive */
moeoArchive < MOEOT > & arch;
/** The ratio of selected individuals from the population*/
double ratioFromPop;
/** A random selection operator (used as default for archSelectOne) */
moeoRandomSelect < MOEOT > randomSelectOne;
};
#endif /*MOEOSELECTONEFROMPOPANDARCH_H_*/

View file

@ -0,0 +1,24 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
//-----------------------------------------------------------------------------
// moeoSelectOne.h
// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007
/*
This library...
Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr
*/
//-----------------------------------------------------------------------------
#ifndef MOEOSELECTONE_H_
#define MOEOSELECTONE_H_
#include <eoSelectOne.h>
/**
* Selection strategy for multi-objective optimization that selects only one element from a whole population.
*/
template < class MOEOT >
class moeoSelectOne : public eoSelectOne < MOEOT > {};
#endif /*MOEOSELECTONE_H_*/

View file

@ -0,0 +1,158 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
//-----------------------------------------------------------------------------
// moeoSelectors.h
// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007
/*
This library...
Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr
*/
//-----------------------------------------------------------------------------
#ifndef MOEOSELECTORS_H_
#define MOEOSELECTORS_H_
#include <comparator/moeoComparator.h>
template <class It,class MOEOT>
It mo_deterministic_tournament(It _begin, It _end, unsigned int _t_size,moeoComparator<MOEOT>& _comparator ,eoRng& _gen = rng)
{
It best = _begin + _gen.random(_end - _begin);
for (unsigned int i = 0; i < _t_size - 1; ++i)
{
It competitor = _begin + _gen.random(_end - _begin);
// compare the two individuals by using the comparator
if (_comparator(*best, *competitor))
// best "better" than competitor
best=competitor;
}
return best;
}
template <class MOEOT>
const MOEOT& mo_deterministic_tournament(const eoPop<MOEOT>& _pop, unsigned int _t_size,moeoComparator<MOEOT>& _comparator, eoRng& _gen = rng)
{
return *mo_deterministic_tournament(_pop.begin(), _pop.end(),_t_size,_comparator, _gen);
}
template <class MOEOT>
MOEOT& mo_deterministic_tournament(eoPop<MOEOT>& _pop, unsigned int _t_size,moeoComparator<MOEOT>& _comparator,eoRng& _gen = rng)
{
return *mo_deterministic_tournament(_pop.begin(), _pop.end(), _t_size,_comparator, _gen);
}
template <class It,class MOEOT>
It mo_stochastic_tournament(It _begin, It _end, double _t_rate,moeoComparator<MOEOT>& _comparator ,eoRng& _gen = rng)
{
It i1 = _begin + _gen.random(_end - _begin);
It i2 = _begin + _gen.random(_end - _begin);
bool return_better = _gen.flip(_t_rate);
if (_comparator(*i1, *i2))
{
if (return_better) return i2;
// else
return i1;
}
else
{
if (return_better) return i1;
// else
}
// else
return i2;
}
template <class MOEOT>
const MOEOT& mo_stochastic_tournament(const eoPop<MOEOT>& _pop, double _t_rate,moeoComparator<MOEOT>& _comparator, eoRng& _gen = rng)
{
return *mo_stochastic_tournament(_pop.begin(), _pop.end(), _t_rate,_comparator, _gen);
}
template <class MOEOT>
MOEOT& mo_stochastic_tournament(eoPop<MOEOT>& _pop, double _t_rate, eoRng& _gen = rng)
{
return *mo_stochastic_tournament(_pop.begin(), _pop.end(), _t_rate, _gen);
}
template <class It>
It mo_roulette_wheel(It _begin, It _end, double total, eoRng& _gen = rng)
{
float roulette = _gen.uniform(total);
if (roulette == 0.0) // covers the case where total==0.0
return _begin + _gen.random(_end - _begin); // uniform choice
It i = _begin;
while (roulette > 0.0)
{
roulette -= static_cast<double>(*(i++));
}
return --i;
}
template <class MOEOT>
const MOEOT& mo_roulette_wheel(const eoPop<MOEOT>& _pop, double total, eoRng& _gen = rng)
{
float roulette = _gen.uniform(total);
if (roulette == 0.0) // covers the case where total==0.0
return _pop[_gen.random(_pop.size())]; // uniform choice
typename eoPop<MOEOT>::const_iterator i = _pop.begin();
while (roulette > 0.0)
{
roulette -= static_cast<double>((i++)->fitness());
}
return *--i;
}
template <class MOEOT>
MOEOT& mo_roulette_wheel(eoPop<MOEOT>& _pop, double total, eoRng& _gen = rng)
{
float roulette = _gen.uniform(total);
if (roulette == 0.0) // covers the case where total==0.0
return _pop[_gen.random(_pop.size())]; // uniform choice
typename eoPop<MOEOT>::iterator i = _pop.begin();
while (roulette > 0.0)
{
// fitness only
roulette -= static_cast<double>((i++)->fitness());
}
return *--i;
}
#endif /*MOEOSELECTORS_H_*/

View file

@ -0,0 +1,91 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
//-----------------------------------------------------------------------------
// moeoStochTournamentSelect.h
// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007
/*
This library...
Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr
*/
//-----------------------------------------------------------------------------
#ifndef MOEOSTOCHTOURNAMENTSELECT_H_
#define MOEOSTOCHTOURNAMENTSELECT_H_
#include <comparator/moeoComparator.h>
#include <comparator/moeoFitnessThenDiversityComparator.h>
#include <selection/moeoSelectOne.h>
#include <selection/moeoSelectors.h>
/**
* Selection strategy that selects ONE individual by stochastic tournament.
*/
template < class MOEOT > class moeoStochTournamentSelect:public moeoSelectOne <MOEOT>
{
public:
/**
* Full Ctor
* @param _comparator the comparator (used to compare 2 individuals)
* @param _tRate the tournament rate
*/
moeoStochTournamentSelect (moeoComparator < MOEOT > & _comparator, double _tRate = 1.0) : comparator (_comparator), tRate (_tRate)
{
// consistency checks
if (tRate < 0.5)
{
std::cerr << "Warning, Tournament rate should be > 0.5\nAdjusted to 0.55\n";
tRate = 0.55;
}
if (tRate > 1)
{
std::cerr << "Warning, Tournament rate should be < 1\nAdjusted to 1\n";
tRate = 1;
}
}
/**
* Ctor without comparator. A moeoFitnessThenDiversityComparator is used as default.
* @param _tRate the tournament rate
*/
moeoStochTournamentSelect (double _tRate = 1.0) : comparator (defaultComparator), tRate (_tRate)
{
// consistency checks
if (tRate < 0.5)
{
std::cerr << "Warning, Tournament rate should be > 0.5\nAdjusted to 0.55\n";
tRate = 0.55;
}
if (tRate > 1)
{
std::cerr << "Warning, Tournament rate should be < 1\nAdjusted to 1\n";
tRate = 1;
}
}
/**
* Apply the tournament to the given population
* @param _pop the population
*/
const MOEOT & operator() (const eoPop < MOEOT > &_pop)
{
// use the selector
return mo_stochastic_tournament(_pop,tRate,comparator);
}
protected:
/** the comparator (used to compare 2 individuals) */
moeoComparator < MOEOT > & comparator;
/** a fitness then diversity comparator can be used as default */
moeoFitnessThenDiversityComparator < MOEOT > defaultComparator;
/** the tournament rate */
double tRate;
};
#endif /*MOEOSTOCHTOURNAMENTSELECT_H_ */