some changes to start, new work on eoLottery
This commit is contained in:
parent
2cafee6bc6
commit
e20fb6c7aa
4 changed files with 66 additions and 29 deletions
|
|
@ -42,9 +42,14 @@
|
|||
#include <eoFitness.h> // what's the matter with you?
|
||||
#include <eoProblem.h> // what's the matter with you?
|
||||
|
||||
#include <eoLottery.h>
|
||||
|
||||
//#include <eoGA.h>
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#endif _eo_
|
||||
|
||||
// Local Variables:
|
||||
// mode:C++
|
||||
// End:
|
||||
|
|
|
|||
|
|
@ -15,6 +15,9 @@
|
|||
#include <eoUniform.h> // eoUniform
|
||||
#include <eoRnd.h> // eoRnd
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
/// eoBin: implementation of binary chromosome.
|
||||
/// based on STL's bit_vector (vector<bool>)
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
template <class F> class eoBin: public EO<F>, public bit_vector
|
||||
|
|
@ -79,7 +82,8 @@ template<class Chrom> class eoBinRandom: public eoMonOp<Chrom>
|
|||
/// The class name.
|
||||
string className() const { return "eoBinRandom"; }
|
||||
|
||||
///
|
||||
/// Randomizes a cromosome.
|
||||
/// @param chrom The cromosome to be randomize.
|
||||
void operator()(Chrom& chrom) const
|
||||
{
|
||||
eoUniform<float> uniform(0.0, 1.0);
|
||||
|
|
@ -99,12 +103,13 @@ template<class Chrom> class eoBinBitFlip: public eoMonOp<Chrom>
|
|||
/// The class name.
|
||||
string className() const { return "eoBinBitFlip"; }
|
||||
|
||||
///
|
||||
void eoBinBitFlip::operator()(Chrom& chrom) const
|
||||
/// Change one bit.
|
||||
/// @param chrom The cromosome which one bit is going to be changed.
|
||||
void operator()(Chrom& chrom) const
|
||||
{
|
||||
eoUniform<unsigned> uniform(0, chrom.size());
|
||||
unsigned pos = uniform();
|
||||
chrom[pos] = !chrom[pos];
|
||||
eoUniform<float> uniform(0.0, 1.0);
|
||||
for (unsigned i = 0; i < chrom.size(); i++)
|
||||
chrom[i] = (uniform() < 0.5) ? false : true;
|
||||
}
|
||||
};
|
||||
|
||||
|
|
@ -116,12 +121,14 @@ template<class Chrom> class eoBinMutation: public eoMonOp<Chrom>
|
|||
{
|
||||
public:
|
||||
/// (Default) Constructor.
|
||||
/// @param _rate Rate of mutation.
|
||||
eoBinMutation(const double& _rate = 0.01): rate(_rate), uniform(0.0, 1.0) {}
|
||||
|
||||
/// The class name.
|
||||
string className() const { return "eoBinMutation"; }
|
||||
|
||||
///
|
||||
/// Mutate a chromosome.
|
||||
/// @param chrom The chromosome to be mutated.
|
||||
void operator()(Chrom& chrom) const
|
||||
{
|
||||
for (unsigned i = 0; i < chrom.size(); i++)
|
||||
|
|
@ -144,7 +151,8 @@ template<class Chrom> class eoBinInversion: public eoMonOp<Chrom>
|
|||
/// The class name.
|
||||
string className() const { return "eoBinInversion"; }
|
||||
|
||||
///
|
||||
/// Inverts a range of bits in a binary chromosome.
|
||||
/// @param chrom The chromosome whos bits are going to be inverted (a range).
|
||||
void operator()(Chrom& chrom) const
|
||||
{
|
||||
eoUniform<unsigned> uniform(0, chrom.size() + 1);
|
||||
|
|
@ -167,7 +175,8 @@ template<class Chrom> class eoBinNext: public eoMonOp<Chrom>
|
|||
/// The class name.
|
||||
string className() const { return "eoBinNext"; }
|
||||
|
||||
///
|
||||
/// Change the bit string x to be x+1.
|
||||
/// @param chrom The chromosome to be added one.
|
||||
void operator()(Chrom& chrom) const
|
||||
{
|
||||
for (int i = chrom.size() - 1; i >= 0; i--)
|
||||
|
|
@ -194,7 +203,8 @@ template<class Chrom> class eoBinPrev: public eoMonOp<Chrom>
|
|||
/// The class name.
|
||||
string className() const { return "eoBinPrev"; }
|
||||
|
||||
///
|
||||
/// Change the bit string x to be x-1.
|
||||
/// @param chrom The chromosome to be substracted one.
|
||||
void operator()(Chrom& chrom) const
|
||||
{
|
||||
for (int i = chrom.size() - 1; i >= 0; i--)
|
||||
|
|
@ -206,11 +216,11 @@ template<class Chrom> class eoBinPrev: public eoMonOp<Chrom>
|
|||
else
|
||||
{
|
||||
chrom[i] = 1;
|
||||
continue;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoBinCrossover --> classic crossover
|
||||
//-----------------------------------------------------------------------------
|
||||
|
|
@ -221,6 +231,9 @@ template<class Chrom> class eoBinCrossover: public eoBinOp<Chrom>
|
|||
/// The class name.
|
||||
string className() const { return "eoBinCrossover"; }
|
||||
|
||||
/// 2-point crossover for binary chromosomes.
|
||||
/// @param chrom1 The first chromosome.
|
||||
/// @param chrom2 The first chromosome.
|
||||
void operator()(Chrom& chrom1, Chrom& chrom2) const
|
||||
{
|
||||
eoUniform<unsigned> uniform(1, min(chrom1.size(), chrom2.size()));
|
||||
|
|
@ -248,7 +261,9 @@ template<class Chrom> class eoBinNxOver: public eoBinOp<Chrom>
|
|||
/// The class name.
|
||||
string className() const { return "eoBinNxOver"; }
|
||||
|
||||
///
|
||||
/// n-point crossover for binary chromosomes.
|
||||
/// @param chrom1 The first chromosome.
|
||||
/// @param chrom2 The first chromosome.
|
||||
void operator()(Chrom& chrom1, Chrom& chrom2) const
|
||||
{
|
||||
unsigned max_size = min(chrom1.size(), chrom2.size());
|
||||
|
|
@ -312,7 +327,9 @@ template<class Chrom> class eoBinGxOver: public eoBinOp<Chrom>
|
|||
/// The class name
|
||||
string className() const { return "eoBinGxOver"; }
|
||||
|
||||
///
|
||||
/// Gene crossover for binary chromosomes.
|
||||
/// @param chrom1 The first chromosome.
|
||||
/// @param chrom2 The first chromosome.
|
||||
void operator()(Chrom& chrom1, Chrom& chrom2) const
|
||||
{
|
||||
unsigned max_genes = min(chrom1.size(), chrom2.size()) / gene_size;
|
||||
|
|
@ -353,8 +370,8 @@ template<class Chrom> class eoBinGxOver: public eoBinOp<Chrom>
|
|||
template<class Chrom> class eoBinUxOver: public eoBinOp<Chrom>
|
||||
{
|
||||
public:
|
||||
/// Constructor.
|
||||
eoBinUxOver(const float _rate): rate(_rate)
|
||||
/// (Default) Constructor.
|
||||
eoBinUxOver(const float _rate = 0.5): rate(_rate)
|
||||
{
|
||||
if (rate < 0 || rate > 1)
|
||||
{
|
||||
|
|
@ -366,7 +383,9 @@ template<class Chrom> class eoBinUxOver: public eoBinOp<Chrom>
|
|||
/// The class name.
|
||||
string className() const { return "eoBinUxOver"; }
|
||||
|
||||
///
|
||||
/// Uniform crossover for binary chromosomes.
|
||||
/// @param chrom1 The first chromosome.
|
||||
/// @param chrom2 The first chromosome.
|
||||
void operator()(Chrom& chrom1, Chrom& chrom2) const
|
||||
{
|
||||
unsigned min_size = min(chrom1.size(), chrom2.size());
|
||||
|
|
|
|||
|
|
@ -11,24 +11,24 @@
|
|||
class eoFitness: public eoPersistent
|
||||
{
|
||||
public:
|
||||
virtual operator float() const = 0;
|
||||
|
||||
virtual bool operator<(const eoFitness& other) const = 0;
|
||||
|
||||
bool operator>(const eoFitness& other) const
|
||||
|
||||
virtual bool operator>(const eoFitness& other) const
|
||||
{
|
||||
return !(*this < other || *this == other);
|
||||
}
|
||||
|
||||
bool operator==(const eoFitness& other) const
|
||||
|
||||
virtual bool operator==(const eoFitness& other) const
|
||||
{
|
||||
return !(other < *this || *this < other);
|
||||
}
|
||||
|
||||
bool operator!=(const eoFitness& other) const
|
||||
virtual bool operator!=(const eoFitness& other) const
|
||||
{
|
||||
return other < *this || *this < other;
|
||||
}
|
||||
|
||||
virtual operator float() const = 0;
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
|
|
|||
|
|
@ -16,15 +16,25 @@
|
|||
template<class Chrom> class eoLottery: public eoSelect<Chrom>
|
||||
{
|
||||
public:
|
||||
eoLottery(const float& rate = 1.0): eoLottery(rate) {}
|
||||
|
||||
void operator()(const eoPop<Chrom>& pop, eoPop<Chrom>& breeders)
|
||||
/// (Default) Constructor.
|
||||
eoLottery(const float& _rate = 1.0): rate(_rate) {}
|
||||
|
||||
///
|
||||
void operator()(const eoPop<Chrom>& pop, eoPop<Chrom>& breeders) const
|
||||
{
|
||||
// scores of chromosomes
|
||||
vector<float> score(pop.size());
|
||||
|
||||
// calculates accumulated scores for chromosomes
|
||||
|
||||
transform(pop.begin(), pop.end(), score.begin(), fitness);
|
||||
|
||||
for (unsigned i = 0; i < pop.size(); i++)
|
||||
score[i] = pop[i].fitness();
|
||||
|
||||
|
||||
|
||||
|
||||
float sum = accumulate(score.begin(), score.end(), MINFLOAT);
|
||||
transform(score.begin(), score.end(), score.begin(),
|
||||
bind2nd(divides<float>(), sum));
|
||||
|
|
@ -32,7 +42,7 @@ template<class Chrom> class eoLottery: public eoSelect<Chrom>
|
|||
|
||||
// generates random numbers
|
||||
vector<float> random(pop.size());
|
||||
generate(random.begin(), random.end(), Uniform<float>(0,1));
|
||||
generate(random.begin(), random.end(), eoUniform<float>(0,1));
|
||||
sort(random.begin(), random.end(), less<float>());
|
||||
|
||||
// selection of chromosomes
|
||||
|
|
@ -49,10 +59,13 @@ template<class Chrom> class eoLottery: public eoSelect<Chrom>
|
|||
if (score_index < pop.size())
|
||||
score_index++;
|
||||
else
|
||||
fill_n(back_insert_iterator<Pop>(breeders),
|
||||
fill_n(back_insert_iterator<eoPop<Chrom> >(breeders),
|
||||
num_chroms - breeders.size(), pop.back());
|
||||
} while (breeders.size() < num_chroms);
|
||||
}
|
||||
|
||||
private:
|
||||
float rate; // selection rate
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue