adding eoGeneration
This commit is contained in:
parent
c1481ac24f
commit
c1cddf3236
5 changed files with 91 additions and 100 deletions
|
|
@ -48,6 +48,7 @@
|
||||||
#include <eoInsertion.h>
|
#include <eoInsertion.h>
|
||||||
#include <eoInclusion.h>
|
#include <eoInclusion.h>
|
||||||
|
|
||||||
|
#include <eoGeneratio.h>
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
// to be continued ...
|
// to be continued ...
|
||||||
|
|
@ -55,7 +56,6 @@
|
||||||
|
|
||||||
#include <eoFitness.h> // what's the matter with you?
|
#include <eoFitness.h> // what's the matter with you?
|
||||||
#include <eoProblem.h> // what's the matter with you?
|
#include <eoProblem.h> // what's the matter with you?
|
||||||
// #include <eoGA.h>
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,8 +7,10 @@
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
#include <eoBin.h> // eoBin
|
#include <algorithm> // swap_ranges
|
||||||
#include <eoOp.h> // eoMonOp
|
#include <eoUniform.h> // eoUniform
|
||||||
|
#include <eoBin.h> // eoBin
|
||||||
|
#include <eoOp.h> // eoMonOp
|
||||||
|
|
||||||
|
|
||||||
/** @name BitWise Genetic operators
|
/** @name BitWise Genetic operators
|
||||||
|
|
@ -210,10 +212,7 @@ template<class Chrom> class eoBinNxOver: public eoBinOp<Chrom>
|
||||||
eoBinNxOver(const unsigned& _num_points = 2): num_points(_num_points)
|
eoBinNxOver(const unsigned& _num_points = 2): num_points(_num_points)
|
||||||
{
|
{
|
||||||
if (num_points < 1)
|
if (num_points < 1)
|
||||||
{
|
runtime_error("NxOver --> invalid number of points");
|
||||||
cerr << "NxOver --> invalid number of points " << num_points << endl;
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The class name.
|
/// The class name.
|
||||||
|
|
@ -272,15 +271,9 @@ template<class Chrom> class eoBinGxOver: public eoBinOp<Chrom>
|
||||||
gene_size(_gene_size), num_points(_num_points)
|
gene_size(_gene_size), num_points(_num_points)
|
||||||
{
|
{
|
||||||
if (gene_size < 1)
|
if (gene_size < 1)
|
||||||
{
|
runtime_error("GxOver --> invalid gene size");
|
||||||
cerr << "GxOver --> invalid gene size " << gene_size << endl;
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
if (num_points < 1)
|
if (num_points < 1)
|
||||||
{
|
runtime_error("GxOver --> invalid number of points");
|
||||||
cerr << "GxOver --> invalid number of points " << num_points << endl;
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The class name
|
/// The class name
|
||||||
|
|
@ -329,15 +322,12 @@ template<class Chrom> class eoBinGxOver: public eoBinOp<Chrom>
|
||||||
|
|
||||||
template<class Chrom> class eoBinUxOver: public eoBinOp<Chrom>
|
template<class Chrom> class eoBinUxOver: public eoBinOp<Chrom>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
/// (Default) Constructor.
|
/// (Default) Constructor.
|
||||||
eoBinUxOver(const float _rate = 0.5): rate(_rate)
|
eoBinUxOver(const float _rate = 0.5): rate(_rate)
|
||||||
{
|
{
|
||||||
if (rate < 0 || rate > 1)
|
if (rate < 0 || rate > 1)
|
||||||
{
|
runtime_error("UxOver --> invalid rate");
|
||||||
cerr << "UxOver --> invalid rate " << rate << endl;
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The class name.
|
/// The class name.
|
||||||
|
|
|
||||||
|
|
@ -7,12 +7,15 @@
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
#include <vector> // vector
|
#include <vector> // vector
|
||||||
|
#include <eoUniform.h> // eoUniform
|
||||||
|
#include <eoOp.h> // eoOp, eoMonOp, eoBinOp
|
||||||
|
#include <eoPop.h> // eoPop
|
||||||
|
#include <eoPopOps.h> // eoTransform
|
||||||
|
#include <eoOpSelector.h> // eoOpSelector
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
#include <eo> // eoTransform, eoUniform, eoOp, eoMonOp, eoBinOp, eoPop
|
|
||||||
|
|
||||||
/*****************************************************************************
|
/*****************************************************************************
|
||||||
* eoBreeder: transforms a population using genetic operators. *
|
* eoBreeder: transforms a population using genetic operators. *
|
||||||
* For every operator there is a rated to be applyed. *
|
* For every operator there is a rated to be applyed. *
|
||||||
|
|
@ -24,55 +27,48 @@ template<class Chrom> class eoBreeder: public eoTransform<Chrom>
|
||||||
/// Default constructor.
|
/// Default constructor.
|
||||||
eoBreeder( eoOpSelector<Chrom>& _opSel): opSel( _opSel ) {}
|
eoBreeder( eoOpSelector<Chrom>& _opSel): opSel( _opSel ) {}
|
||||||
|
|
||||||
|
|
||||||
/// Destructor.
|
/// Destructor.
|
||||||
virtual ~eoBreeder() { }
|
virtual ~eoBreeder() {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Transforms a population.
|
* Transforms a population.
|
||||||
* @param pop The population to be transformed.
|
* @param pop The population to be transformed.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
void operator()(eoPop<Chrom>& pop)
|
void operator()(eoPop<Chrom>& pop)
|
||||||
{
|
{
|
||||||
for (unsigned i = 0; i < pop.size(); i ++ ) {
|
for (unsigned i = 0; i < pop.size(); i++) {
|
||||||
eoOp<Chrom>* op = opSel.Op();
|
eoOp<Chrom>* op = opSel.Op();
|
||||||
switch (op->readArity()) {
|
switch (op->readArity()) {
|
||||||
case unary:
|
case unary:
|
||||||
{
|
{
|
||||||
eoMonOp<Chrom>* monop = static_cast<eoMonOp<Chrom>* >(op);
|
eoMonOp<Chrom>* monop = static_cast<eoMonOp<Chrom>* >(op);
|
||||||
(*monop)( pop[i] );
|
(*monop)( pop[i] );
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case binary:
|
case binary:
|
||||||
{
|
{
|
||||||
eoBinOp<Chrom>* binop =
|
eoBinOp<Chrom>* binop =
|
||||||
static_cast<eoBinOp<Chrom>* >(op);
|
static_cast<eoBinOp<Chrom>* >(op);
|
||||||
eoUniform<unsigned> u(0, pop.size() );
|
eoUniform<unsigned> u(0, pop.size() );
|
||||||
(*binop)(pop[i], pop[ u() ] );
|
(*binop)(pop[i], pop[ u() ] );
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case Nary:
|
case Nary:
|
||||||
{
|
{
|
||||||
eoNaryOp<Chrom>* Nop =
|
eoNaryOp<Chrom>* Nop =
|
||||||
static_cast<eoNaryOp<Chrom>* >(op);
|
static_cast<eoNaryOp<Chrom>* >(op);
|
||||||
eoUniform<unsigned> u(0, pop.size() );
|
eoUniform<unsigned> u(0, pop.size() );
|
||||||
eoPop<Chrom> tmpVec;
|
eoPop<Chrom> tmpVec;
|
||||||
tmpVec.push_back( pop[i] );
|
tmpVec.push_back( pop[i] );
|
||||||
for ( unsigned i = 0; i < u(); i ++ ) {
|
for ( unsigned i = 0; i < u(); i ++ ) {
|
||||||
tmpVec.push_back( pop[ u() ] );
|
tmpVec.push_back( pop[ u() ] );
|
||||||
}
|
|
||||||
(*Nop)( tmpVec );
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
{
|
|
||||||
throw runtime_error( "eoBreeder:operator() Not implemented yet!" );
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
(*Nop)( tmpVec );
|
||||||
};
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/// The class name.
|
/// The class name.
|
||||||
string classname() const { return "eoBreeder"; }
|
string classname() const { return "eoBreeder"; }
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,9 @@
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
// eoGA.h
|
// eoGeneration.h
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
#ifndef eoGA_h
|
#ifndef eoGeneration_h
|
||||||
#define eoGA_h
|
#define eoGeneration_h
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
@ -11,18 +11,21 @@
|
||||||
#include <eoPopOps.h> // eoSelect, eoTranform, eoMerge
|
#include <eoPopOps.h> // eoSelect, eoTranform, eoMerge
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
// eoGA
|
// eoGeneration
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
class eoGA
|
template<class Chrom> class eoGeneration: public eoTransform<Chrom>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
/// Constructor.
|
/// Constructor.
|
||||||
eoGA(eoSelect& _select, eoTranform& _transform, eoMerge& _replace)
|
eoGeneration(eoSelect& _select,
|
||||||
{
|
eoTranform& _transform,
|
||||||
}
|
eoMerge& _replace):
|
||||||
|
eoTransform<Chrom>() {}
|
||||||
|
|
||||||
///
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
void operator()(eoPop& pop)
|
void operator()(eoPop& pop)
|
||||||
{
|
{
|
||||||
eoPop breeders;
|
eoPop breeders;
|
||||||
|
|
@ -32,6 +35,9 @@ class eoGA
|
||||||
replace(breeders, pop);
|
replace(breeders, pop);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Class name.
|
||||||
|
string className() const { return "eoGeneration"; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
eoSelect& select;
|
eoSelect& select;
|
||||||
eoTranform& transform;
|
eoTranform& transform;
|
||||||
|
|
@ -40,4 +46,4 @@ class eoGA
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
#endif eoGA_h
|
#endif eoGeneration_h
|
||||||
|
|
@ -25,25 +25,24 @@ template<class EOT>
|
||||||
class eoTransform: public eoObject{
|
class eoTransform: public eoObject{
|
||||||
|
|
||||||
public:
|
public:
|
||||||
/** ctor
|
/** ctor */
|
||||||
*/
|
eoTransform() {};
|
||||||
eoTransform() {};
|
|
||||||
|
|
||||||
/// Dtor
|
/// Dtor
|
||||||
virtual ~eoTransform(){};
|
virtual ~eoTransform(){};
|
||||||
|
|
||||||
/// Pure virtual transformation function. Does something on the population
|
/// Pure virtual transformation function. Does something on the population
|
||||||
virtual void operator () ( eoPop<EOT>& _pop ) = 0;
|
virtual void operator () ( eoPop<EOT>& _pop ) = 0;
|
||||||
|
|
||||||
/** @name Methods from eoObject */
|
/** @name Methods from eoObject */
|
||||||
//@{
|
//@{
|
||||||
/** readFrom and printOn are not overriden
|
/** readFrom and printOn are not overriden
|
||||||
*/
|
*/
|
||||||
/** Inherited from eoObject. Returns the class name.
|
/** Inherited from eoObject. Returns the class name.
|
||||||
@see eoObject
|
@see eoObject
|
||||||
*/
|
*/
|
||||||
string className() const {return "eoTransform";};
|
string className() const {return "eoTransform";};
|
||||||
//@}
|
//@}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Reference in a new issue