EvolvingObjects
Selection operators

Selectors are operators that select a subset of a population. More...

Classes

class  eoDetSelect< EOT >
 eoDetSelect selects many individuals deterministically More...
class  eoDetTournamentSelect< EOT >
 eoDetTournamentSelect: a selection method that selects ONE individual by deterministic tournament More...
class  eoFitnessScalingSelect< EOT >
 eoFitnessScalingSelect: select an individual proportional to the linearly scaled fitness that is computed by the private eoLinearFitScaling object More...
class  eoLinearFitScaling< EOT >
 An instance of eoPerf2Worth COmputes the linearly scaled fitnesses with given selective pressure Pselect(Best) == pressure/sizePop Pselect(average) == 1.0/sizePop truncate negative values to 0 -. More...
class  eoLinearTopology< POT >
 One of the local best strategies for particle swarm optimization. More...
class  eoNeighborhood< POT >
 Abstract class for neighborboods. More...
class  eoPerf2Worth< EOT, WorthT >
 Base class to transform raw fitnesses into fitness for selection. More...
class  eoPerf2WorthCached< EOT, WorthT >
 Perf2Worth with fitness cache. More...
class  eoNoPerf2Worth< EOT >
 A dummy perf2worth, just in case you need it. More...
class  eoProportionalSelect< EOT >
 eoProportionalSelect: select an individual proportional to her stored fitness value More...
class  eoRandomSelect< EOT >
 This file contains straightforward selectors: eoRandomSelect returns an individual uniformly selected eoBestSelect always return the best individual eoSequentialSelect returns all individuals in turn. More...
class  eoBestSelect< EOT >
 eoBestSelect: a selection method that always return the best (mainly for testing purposes) More...
class  eoNoSelect< EOT >
 eoNoSelect: returns all individual in order WITHOUT USING FITNESS!!! looping back to the beginning when exhasuted More...
class  eoRanking< EOT >
 An instance of eoPerfFromWorth COmputes the ranked fitness: fitnesses range in [m,M] with m=2-pressure/popSize and M=pressure/popSize. More...
class  eoRankingSelect< EOT >
 eoRankingSelect: select an individual by roulette wheel on its rank is an eoRouletteWorthSelect, i.e. More...
class  eoRankMuSelect< EOT >
 Selects the "Mu" bests individuals. More...
class  eoRingTopology< POT >
 Static ring topology for particle swarm optimization. More...
class  eoSelect< EOT >
 eoSelect selects a number of individuals from the first argument and puts it in the second. More...
class  eoSelectFactory< EOT >
 EO Factory.An instance of the factory class to create selectors, that is, eoSelect objects. More...
class  eoSelectFromWorth< EOT, WorthType >
 selects one element from a population (is an eoSelectOne) but the selection is based on a std::vector of Worth that is different from the fitnesses (e.g. More...
class  eoDetTournamentWorthSelect< EOT, WorthT >
 An instance of eoSelectPerf2Worth that does selection from the Worthes using a ... More...
class  eoStochTournamentWorthSelect< EOT, WorthT >
 An instance of eoSelectPerf2Worth that does selection from the Worthes using a ... More...
class  eoRouletteWorthSelect< EOT, WorthT >
 An instance of eoSelectPerf2Worth that does selection from the Worthes using a ... More...
class  eoSelectMany< EOT >
 eoSelectMany selects many individuals using eoSelectOne as it's mechanism. More...
class  eoSelectNumber< EOT >
 eoSelectNumber selects many individuals using eoSelectOne as it's mechanism. More...
class  eoSelectOne< EOT, WorthT >
 eoSelectOne selects only one element from a whole population. More...
class  eoSelectPerc< EOT >
 eoSelectPerc selects many individuals using eoSelectOne as it's mechanism. More...
class  eoSequentialSelect< EOT >
 Contains the following classes: More...
class  eoEliteSequentialSelect< EOT >
 All Individuals in order. More...
class  dMatrix
 Sharing is a perf2worth class that implements Goldberg and Richardson's basic sharing. More...
class  eoSharing< EOT >
 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. More...
class  eoSharingSelect< EOT >
 eoSharingSelect: select an individual by roulette wheel on its SHARED fitness. More...
class  eoSocialNeighborhood< POT >
 Derivated from eoNeighborhood. More...
class  eoStarTopology< POT >
 Topology dedicated to "globest best" strategy for particle swarm optimization. More...
class  eoStochasticUniversalSelect< EOT >
 eoStochasticUniversalSelect: select an individual proportional to her stored fitness value, but in contrast with eoStochasticUniversalSelect, get rid of most finite sampling effects by doing all selections in one go, using a single random number. More...
class  eoStochTournamentSelect< EOT >
 eoStochTournamentSelect: a selection method that selects ONE individual by binary stochastic tournament More...
class  eoTopology< POT >
 Defines the interface of a swarm topology. More...
class  eoTruncatedSelectMany< EOT >
 eoTruncatedSelectMany selects many individuals using eoSelectOne as it's mechanism. More...
class  eoTruncatedSelectOne< EOT >
 eoTruncatedSelectOne selects one individual using eoSelectOne as it's mechanism. More...
class  eoTruncSelect< EOT >
 eoTruncSelect selects individuals after truncating the population using eoSelectOne as it's mechanism. More...

Functions

template<class EOT >
bool minimizing_fitness ()
double scale_fitness (const std::pair< double, double > &_minmax, double _value)
template<class It >
double sum_fitness (It begin, It end)
template<class EOT >
double sum_fitness (const eoPop< EOT > &_pop)
template<class EOT >
double sum_fitness (const eoPop< EOT > &_pop, std::pair< double, double > &_minmax)
template<class It >
It roulette_wheel (It _begin, It _end, double total, eoRng &_gen=rng)
template<class EOT >
const EOT & roulette_wheel (const eoPop< EOT > &_pop, double total, eoRng &_gen=rng)
template<class EOT >
EOT & roulette_wheel (eoPop< EOT > &_pop, double total, eoRng &_gen=rng)
template<class It >
It deterministic_tournament (It _begin, It _end, unsigned _t_size, eoRng &_gen=rng)
template<class EOT >
const EOT & deterministic_tournament (const eoPop< EOT > &_pop, unsigned _t_size, eoRng &_gen=rng)
template<class EOT >
EOT & deterministic_tournament (eoPop< EOT > &_pop, unsigned _t_size, eoRng &_gen=rng)
template<class It >
It inverse_deterministic_tournament (It _begin, It _end, unsigned _t_size, eoRng &_gen=rng)
template<class EOT >
const EOT & inverse_deterministic_tournament (const eoPop< EOT > &_pop, unsigned _t_size, eoRng &_gen=rng)
template<class EOT >
EOT & inverse_deterministic_tournament (eoPop< EOT > &_pop, unsigned _t_size, eoRng &_gen=rng)
template<class It >
It stochastic_tournament (It _begin, It _end, double _t_rate, eoRng &_gen=rng)
template<class EOT >
const EOT & stochastic_tournament (const eoPop< EOT > &_pop, double _t_rate, eoRng &_gen=rng)
template<class EOT >
EOT & stochastic_tournament (eoPop< EOT > &_pop, double _t_rate, eoRng &_gen=rng)
template<class It >
It inverse_stochastic_tournament (It _begin, It _end, double _t_rate, eoRng &_gen=rng)
template<class EOT >
const EOT & inverse_stochastic_tournament (const eoPop< EOT > &_pop, double _t_rate, eoRng &_gen=rng)
template<class EOT >
EOT & inverse_stochastic_tournament (eoPop< EOT > &_pop, double _t_rate, eoRng &_gen=rng)

Detailed Description

Selectors are operators that select a subset of a population.

Example:

// to avoid long name warnings
#ifdef _MSC_VER
#pragma warning(disable:4786)
#endif

#include <cstring>
#include <stdexcept>

#include <eo>

//-----------------------------------------------------------------------------

struct Dummy : public EO<double>
{
    typedef double Type;
  void printOn(std::ostream & _os) const
  {
      _os << " - ";
      EO<double>::printOn(_os);
  }
};

bool operator==(const Dummy & _d1, const Dummy & _d2)
{
  return _d1.fitness() == _d2.fitness();
}

struct eoDummyPop : public eoPop<Dummy>
{
public :
    eoDummyPop(int s=0) { resize(s); }
};

// helper - DOES NOT WORK if different individuals have same fitness!!!
template <class EOT>
unsigned isInPop(EOT & _indi, eoPop<EOT> & _pop)
{
  for (unsigned i=0; i<_pop.size(); i++)
    if (_pop[i] == _indi)
      return i;
  return _pop.size();
}

unsigned int pSize;             // global variable, bouh!
std::string fitnessType;                // yes, a global variable :-)
eoDummyPop parentsOrg;

template <class EOT>
void testSelectMany(eoSelect<EOT> & _select, std::string _name)
{
    unsigned i;
  std::cout << "\n\n" << fitnessType + _name << std::endl;
  std::cout << "===============\n";

    eoDummyPop parents(parentsOrg);
    eoDummyPop offspring(0);

    // do the selection
    _select(parents, offspring);

    // compute stats
    std::vector<unsigned> nb(parents.size(), 0);
    for (i=0; i<offspring.size();  i++)
      {
        unsigned trouve = isInPop<Dummy>(offspring[i], parents);
        if (trouve == parents.size()) // pas trouve
          throw std::runtime_error("Pas trouve ds parents");
        nb[trouve]++;
       }
    // dump to file so you can plot using gnuplot - dir name is hardcoded!
    std::string fName = "ResSelect/" + fitnessType + _name + ".select";
    std::ofstream os(fName.c_str());
    for (i=0; i<parents.size();  i++)
      {
        std::cout << i << " -> " << ( (double)nb[i])/offspring.size() << std::endl;
        os << i << " " << ( (double)nb[i])/offspring.size() << std::endl;
      }

}

template <class EOT>
void testSelectOne(eoSelectOne<EOT> & _select, eoHowMany & _offspringRate,
                   eoHowMany & _fertileRate, std::string _name)
{
  eoTruncatedSelectOne<EOT> truncSelect(_select, _fertileRate);
  eoSelectMany<EOT> percSelect(truncSelect, _offspringRate);
  testSelectMany<EOT>(percSelect, _name);
}


//-----------------------------------------------------------------------------

int the_main(int argc, char **argv)
{
  eoParser parser(argc, argv);
  eoValueParam<unsigned> parentSizeParam = parser.createParam(unsigned(10), "parentSize", "Parent size",'P');
    pSize = parentSizeParam.value(); // global variable

//   eoValueParam<double> offsrpringRateParam = parser.createParam<double>(1.0, "offsrpringRate", "Offsrpring rate",'O');
//     double oRate = offsrpringRateParam.value();
  eoValueParam<eoHowMany> offsrpringRateParam = parser.createParam(eoHowMany(1.0), "offsrpringRate", "Offsrpring rate (% or absolute)",'O');
    eoHowMany oRate = offsrpringRateParam.value();

  eoValueParam<eoHowMany> fertileRateParam = parser.createParam(eoHowMany(1.0), "fertileRate", "Fertility rate (% or absolute)",'F');
    eoHowMany fRate = fertileRateParam.value();

eoValueParam<unsigned> tournamentSizeParam = parser.createParam(unsigned(2), "tournamentSize", "Deterministic tournament size",'T');
    unsigned int tSize = tournamentSizeParam.value();

  eoValueParam<double> tournamentRateParam = parser.createParam(1.0, "tournamentRate", "Stochastic tournament rate",'t');
    double tRate = tournamentRateParam.value();

  eoValueParam<double> rankingPressureParam = parser.createParam(2.0, "rankingPressure", "Selective pressure for the ranking selection",'p');
    double rankingPressure = rankingPressureParam.value();

  eoValueParam<double> rankingExponentParam = parser.createParam(1.0, "rankingExponent", "Exponent for the ranking selection",'e');
    double rankingExponent = rankingExponentParam.value();

  eoValueParam<std::string> fitTypeParam = parser.createParam(std::string("linear"), "fitType", "Type of fitness (linear, exp, log, super",'f');
    fitnessType = fitTypeParam.value();

    if (parser.userNeedsHelp())
      {
        parser.printHelp(std::cout);
        exit(0);
      }

    // hard-coded directory name ...
    system("mkdir ResSelect");
    std::cout << "Testing the Selections\nParents size = " << pSize
         << ", offspring rate = " << oRate;
    std::cout << " and putting rsulting files in dir ResSelect" << std::endl;

    // initialize parent population
    parentsOrg.resize(pSize);
    if (fitnessType == std::string("linear"))
      for (unsigned i=0; i<pSize; i++)
        parentsOrg[i].fitness(i);
    else if (fitnessType == std::string("exp"))
      for (unsigned i=0; i<pSize; i++)
        parentsOrg[i].fitness(exp((double)i));
    else if (fitnessType == std::string("log"))
      for (unsigned i=0; i<pSize; i++)
        parentsOrg[i].fitness(log(i+1.));
    else if (fitnessType == std::string("super"))
      {
        for (unsigned i=0; i<pSize-1; i++)
          parentsOrg[i].fitness(i);
        parentsOrg[pSize-1].fitness(10*pSize);
      }
    else
      throw std::runtime_error("Invalid fitness Type"+fitnessType);

    std::cout << "Initial parents (odd)\n" << parentsOrg << std::endl;

  // random seed
    eoValueParam<uint32_t>& seedParam = parser.createParam(uint32_t(0), "seed",
                                                           "Random number seed", 'S');
    if (seedParam.value() == 0)
        seedParam.value() = time(0);
    rng.reseed(seedParam.value());

    char fileName[1024];

// the selection procedures under test
    //    eoDetSelect<Dummy> detSelect(oRate);
    //    testSelectMany(detSelect, "detSelect");

    // Roulette
     eoProportionalSelect<Dummy> propSelect;
     testSelectOne<Dummy>(propSelect, oRate, fRate, "PropSelect");

    // Linear ranking using the perf2Worth construct
    eoRankingSelect<Dummy> newRankingSelect(rankingPressure);
    sprintf(fileName,"LinRank_%g",rankingPressure);
    testSelectOne<Dummy>(newRankingSelect, oRate, fRate, fileName);

    // Exponential ranking using the perf2Worth construct
    std::cout << "rankingExponent " << rankingExponent << std::endl;
    eoRankingSelect<Dummy> expRankingSelect(rankingPressure,rankingExponent);
    sprintf(fileName,"ExpRank_%g_%g",rankingPressure, rankingExponent);
    testSelectOne<Dummy>(expRankingSelect, oRate, fRate, fileName);

    // Det tournament
    eoDetTournamentSelect<Dummy> detTourSelect(tSize);
    sprintf(fileName,"DetTour_%d",tSize);
    testSelectOne<Dummy>(detTourSelect, oRate, fRate, fileName);

    // Stoch tournament
    eoStochTournamentSelect<Dummy> stochTourSelect(tRate);
    sprintf(fileName,"StochTour_%g",tRate);
    testSelectOne<Dummy>(stochTourSelect, oRate, fRate, fileName);

    // Fitness scaling
    eoFitnessScalingSelect<Dummy> newFitScaleSelect(rankingPressure);
    sprintf(fileName,"LinFitScale_%g",rankingPressure);
    testSelectOne<Dummy>(newFitScaleSelect, oRate, fRate, fileName);

    // Sequential selections
    eoSequentialSelect<Dummy> seqSel(false);
    strcpy(fileName,"Sequential");
    testSelectOne<Dummy>(seqSel, oRate, fRate, fileName);

    eoEliteSequentialSelect<Dummy> eliteSeqSel;
    strcpy(fileName,"EliteSequential");
    testSelectOne<Dummy>(eliteSeqSel, oRate, fRate, fileName);

    return 1;
}

int main(int argc, char **argv)
{
    try
    {
        the_main(argc, argv);
    }
    catch(std::exception& e)
    {
        std::cout << "Exception: " << e.what() << std::endl;
        return 1;
    }
}
 All Classes Namespaces Files Functions Variables Typedefs Friends