eoBreeder almost finished

This commit is contained in:
gustavo 1999-02-12 17:43:28 +00:00
commit 9303c4b53d
7 changed files with 136 additions and 116 deletions

View file

@ -4,7 +4,7 @@
# Created: 1993-05-16
# Public domain
# $Id: mkinstalldirs,v 1.1 1999-01-29 12:23:53 gustavo Exp $
# $Id: mkinstalldirs,v 5.1 1999-02-12 17:39:01 gustavo Exp $
errstatus=0

View file

@ -8,6 +8,7 @@
//-----------------------------------------------------------------------------
#include <eoData.h>
#include <eoObject.h>
#include <eoPrintable.h>
#include <eoPersistent.h>

View file

@ -24,17 +24,18 @@ have a factory that knows how to build them from a description
//@{
/**
eoBinRandom --> mofify a chromosome in a random way
*/
/** eoBinRandom --> mofify a chromosome in a random way */
template<class Chrom> class eoBinRandom: public eoMonOp<Chrom>
{
public:
/// The class name.
string className() const { return "eoBinRandom"; }
/// Randomizes a cromosome.
/// @param chrom The cromosome to be randomize.
/**
* Randomizes a cromosome.
* @param chrom The cromosome to be randomize.
*/
void operator()(Chrom& chrom) const
{
eoUniform<float> uniform(0.0, 1.0);
@ -44,9 +45,7 @@ template<class Chrom> class eoBinRandom: public eoMonOp<Chrom>
};
//-----------------------------------------------------------------------------
// eoBinBitFlip --> chages a bit
//-----------------------------------------------------------------------------
/** eoBinBitFlip --> chages a bit */
template<class Chrom> class eoBinBitFlip: public eoMonOp<Chrom>
{
@ -54,8 +53,10 @@ template<class Chrom> class eoBinBitFlip: public eoMonOp<Chrom>
/// The class name.
string className() const { return "eoBinBitFlip"; }
/// Change one bit.
/// @param chrom The cromosome which one bit is going to be changed.
/**
* Change one bit.
* @param chrom The cromosome which one bit is going to be changed.
*/
void operator()(Chrom& chrom) const
{
eoUniform<float> uniform(0.0, 1.0);
@ -64,22 +65,25 @@ template<class Chrom> class eoBinBitFlip: public eoMonOp<Chrom>
}
};
//-----------------------------------------------------------------------------
// eoBinMutation --> classical mutation
//-----------------------------------------------------------------------------
/** eoBinMutation --> classical mutation */
template<class Chrom> class eoBinMutation: public eoMonOp<Chrom>
{
public:
/// (Default) Constructor.
/// @param _rate Rate of mutation.
/**
* (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.
/**
* Mutate a chromosome.
* @param chrom The chromosome to be mutated.
*/
void operator()(Chrom& chrom) const
{
for (unsigned i = 0; i < chrom.size(); i++)
@ -92,9 +96,8 @@ template<class Chrom> class eoBinMutation: public eoMonOp<Chrom>
mutable eoUniform<float> uniform;
};
//-----------------------------------------------------------------------------
// eoBinInversion --> inverts the bits of the chromosome between an interval
//-----------------------------------------------------------------------------
/** eoBinInversion: inverts the bits of the chromosome between an interval */
template<class Chrom> class eoBinInversion: public eoMonOp<Chrom>
{
@ -102,8 +105,10 @@ 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).
/**
* 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);
@ -116,9 +121,8 @@ template<class Chrom> class eoBinInversion: public eoMonOp<Chrom>
}
};
//-----------------------------------------------------------------------------
// eoBinNext --> next binary value
//-----------------------------------------------------------------------------
/** eoBinNext --> next binary value */
template<class Chrom> class eoBinNext: public eoMonOp<Chrom>
{
@ -126,8 +130,10 @@ 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.
/**
* 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--)
@ -144,18 +150,19 @@ template<class Chrom> class eoBinNext: public eoMonOp<Chrom>
}
};
//-----------------------------------------------------------------------------
// eoBinPrev --> previos binary value
//-----------------------------------------------------------------------------
/** eoBinPrev --> previos binary value */
template<class Chrom> class eoBinPrev: public eoMonOp<Chrom>
{
public:
/// 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.
/**
* 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--)
@ -172,9 +179,8 @@ template<class Chrom> class eoBinPrev: public eoMonOp<Chrom>
}
};
//-----------------------------------------------------------------------------
// eoBinCrossover --> classic crossover
//-----------------------------------------------------------------------------
/** eoBinCrossover --> classic crossover */
template<class Chrom> class eoBinCrossover: public eoBinOp<Chrom>
{
@ -182,9 +188,11 @@ 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.
/**
* 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()));
@ -192,9 +200,8 @@ template<class Chrom> class eoBinCrossover: public eoBinOp<Chrom>
}
};
//-----------------------------------------------------------------------------
// eoBinNxOver --> n-point crossover
//-----------------------------------------------------------------------------
/** eoBinNxOver --> n-point crossover */
template<class Chrom> class eoBinNxOver: public eoBinOp<Chrom>
{
@ -212,9 +219,11 @@ 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.
/**
* 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());
@ -252,9 +261,8 @@ template<class Chrom> class eoBinNxOver: public eoBinOp<Chrom>
unsigned num_points;
};
//-----------------------------------------------------------------------------
// eoBinGxOver --> gene crossover
//-----------------------------------------------------------------------------
/** eoBinGxOver --> gene crossover */
template<class Chrom> class eoBinGxOver: public eoBinOp<Chrom>
{
@ -278,9 +286,11 @@ 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.
/**
* 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;
@ -314,9 +324,8 @@ template<class Chrom> class eoBinGxOver: public eoBinOp<Chrom>
unsigned num_points;
};
//-----------------------------------------------------------------------------
// eoBinUxOver --> uniform crossover
//-----------------------------------------------------------------------------
/** eoBinUxOver --> uniform crossover */
template<class Chrom> class eoBinUxOver: public eoBinOp<Chrom>
{
@ -334,9 +343,11 @@ 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.
/**
* 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());

View file

@ -22,59 +22,60 @@ template<class Chrom> class eoBreeder: public eoTransform<Chrom>
/// Default constructor.
eoBreeder(): uniform(0.0, 1.0) {}
/// Destructor.
~eoBreeder()
{
for (Operators::iterator op = operators.begin();
op != operators.end();
op++)
delete op->second;
}
/**
* Adds a genetic operator.
* @param operator The operator.
* @param rate Rate to apply the operator.
*/
void add(eoOp<Chrom>* operator, float rate = 1.0)
void add(eoOp<Chrom>& op, float rate = 1.0)
{
operators.push_back(pair<float, eoOp<Chrom> * >(operator, rate));
operators.push_back(pair<float, eoOp<Chrom>*>(rate, &op));
}
/**
* Transforms a population.
* @param pop The population to be transformed.
*/
void operator()(eoPop<Chrom>& pop) const
void operator()(eoPop<Chrom>& pop)
{
for (Operators::const_iterator op = operators.begin();
op != operators.end();
op++)
if (op->first < uniform())
switch (op->second->readArity())
{
case unary:
{
eoMonOp<Chrom>& monop =
dinamic_cast<eoMonOp<Chrom> >(*(op->second));
for_each(pop.begin(), pop.end(), monop);
break;
}
case binary:
{
eoBinOp<Chrom>& binop =
dinamic_cast<eoBinOp<Chrom> >(*(op->second));
for (unsigned i = 1; i < pop.size(); i += 2)
binop(pop[i - 1], pop[i]);
break;
}
default:
{
cerr << "eoBreeder:operator() Not implemented yet!" << endl;
exit(EXIT_FAILURE);
break;
}
switch (op->second->readArity())
{
case unary:
{
eoMonOp<Chrom>& monop = (eoMonOp<Chrom>&)*(op->second);
for (unsigned i = 0; i < pop.size(); i++)
if (uniform() < op->first)
monop(pop[i]);
break;
}
case binary:
{
vector<unsigned> pos(pop.size(), 1);
pos[0] = 0;
partial_sum(pos.begin(), pos.end(), pos.begin());
random_shuffle(pos.begin(), pos.end());
cout << "pos = ";
copy(pos.begin(), pos.end(),
ostream_iterator<unsigned>(cout, " "));
cout << endl;
eoBinOp<Chrom>& binop = (eoBinOp<Chrom>&)*(op->second);
for (unsigned i = 1; i < pop.size(); i += 2)
if (uniform() < op->first)
binop(pop[pos[i - 1]], pop[pos[i]]);
break;
}
default:
{
cerr << "eoBreeder:operator() Not implemented yet!" << endl;
exit(EXIT_FAILURE);
break;
}
}
}
/// The class name.

View file

@ -22,8 +22,11 @@ using namespace std;
#define MAXINT numeric_limits<int>::max()
#define min _MIN
#define max _MAX
#else
#include <limits.h>
#else
#include <float.h>
#include <limits.h>
#include <values.h>
#ifndef MAXFLOAT
#define MAXFLOAT (float)1e127
#define MAXDOUBLE (double)1.79769313486231570e+308
@ -38,4 +41,4 @@ using namespace std;
//-----------------------------------------------------------------------------
#endif npi_DATATYPES_H
#endif EODATA_H

View file

@ -11,39 +11,44 @@
/******************************************************************************
* eoInsertion: A replacement algorithm.
* Takes two populations: breeders and original populations. At the en of the
* process, the original population has chenge in the followin way:
* (1) the worst individuals haa been erased
* (2) the best individuals from the breeders has been added
* 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) {}
eoInsertion(const float& _rate = 1.0): eoMerge(_rate) {}
/**
* Creates a new population based on breeders and original population
* Creates a new population based on breeders and original populations.
* @param breeders The population of breeders.
* @param pop The original population.
*/
void operator()(const eoPop<Chrom>& breeders, eoPop<Chrom>& pop)
{
sort(pop.begin(), pop.end());
int new_size = static_cast<int>(pop.size() * rate());
cout << "new_size = " << new_size << endl;
if (rated() > 1)
pop.erase(pop.end() +
(int)(pop.size() * (rate() - 1) - breeders.size()),
pop.end());
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
{
cout << "eoInsertion no funciona con rate < 1"
exit(1);
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));
}
copy(breeders.begin(), breeders.end(),
back_insert_iterator<eoPop<Chrom> >(pop));
}
};

View file

@ -6,11 +6,10 @@
#define eoLottery_h
//-----------------------------------------------------------------------------
#include <functional>
#include <numeric> // accumulate
#include <eo> // eoPop eoSelect
#include <functional> //
#include <numeric> // accumulate
#include <eo> // eoPop eoSelect MINFLOAT
//-----------------------------------------------------------------------------
/// eoLottery: a selection method.