Compiled new stuff on VC++, changes to breeder
This commit is contained in:
parent
1eaeece266
commit
e3bff7d45f
8 changed files with 83 additions and 68 deletions
|
|
@ -33,7 +33,7 @@ template <class F> class eoBin: public eoVector<bool, F>
|
|||
* Constructor.
|
||||
* @param size Size of the binary string.
|
||||
*/
|
||||
eoBin(const unsigned& size, const eoRnd<Type>& rnd): eoVector<bool,F>(size)
|
||||
eoBin(const unsigned& size, const eoRnd<bool>& rnd): eoVector<bool,F>(size)
|
||||
{
|
||||
generate(begin(), end(), rnd);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,8 +7,10 @@
|
|||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <vector> // vector
|
||||
#include <pair.h> // pair
|
||||
#include <vector> // vector
|
||||
|
||||
using namespace std;
|
||||
|
||||
#include <eo> // eoTransform, eoUniform, eoOp, eoMonOp, eoBinOp, eoPop
|
||||
|
||||
/*****************************************************************************
|
||||
|
|
@ -20,73 +22,64 @@ template<class Chrom> class eoBreeder: public eoTransform<Chrom>
|
|||
{
|
||||
public:
|
||||
/// Default constructor.
|
||||
eoBreeder(): uniform(0.0, 1.0) {}
|
||||
|
||||
/**
|
||||
* Adds a genetic operator.
|
||||
* @param operator The operator.
|
||||
* @param rate Rate to apply the operator.
|
||||
*/
|
||||
void add(eoOp<Chrom>& op, float rate = 1.0)
|
||||
{
|
||||
operators.push_back(pair<float, eoOp<Chrom>*>(rate, &op));
|
||||
}
|
||||
eoBreeder( eoOpSelector<Chrom>& _opSel): opSel( _opSel ) {}
|
||||
|
||||
|
||||
/// Destructor.
|
||||
virtual ~eoBreeder() { }
|
||||
|
||||
/**
|
||||
* Transforms a population.
|
||||
* @param pop The population to be transformed.
|
||||
*/
|
||||
void operator()(eoPop<Chrom>& pop)
|
||||
|
||||
void operator()(eoPop<Chrom>& pop)
|
||||
{
|
||||
unsigned i;
|
||||
|
||||
for (Operators::const_iterator op = operators.begin();
|
||||
op != operators.end();
|
||||
op++)
|
||||
switch (op->second->readArity())
|
||||
{
|
||||
case unary:
|
||||
{
|
||||
eoMonOp<Chrom>& monop = (eoMonOp<Chrom>&)*(op->second);
|
||||
for (i = 0; i < pop.size(); i++)
|
||||
if (uniform() < op->first)
|
||||
monop(pop[i]);
|
||||
break;
|
||||
for (unsigned i = 0; i < pop.size(); i ++ ) {
|
||||
eoOp<Chrom>* op = opSel.Op();
|
||||
switch (op->readArity()) {
|
||||
case unary:
|
||||
{
|
||||
eoMonOp<Chrom>* monop = static_cast<eoMonOp<Chrom>* >(op);
|
||||
(*monop)( pop[i] );
|
||||
break;
|
||||
}
|
||||
case binary:
|
||||
{
|
||||
eoBinOp<Chrom>* binop =
|
||||
static_cast<eoBinOp<Chrom>* >(op);
|
||||
eoUniform<unsigned> u(0, pop.size() );
|
||||
(*binop)(pop[i], pop[ u() ] );
|
||||
break;
|
||||
}
|
||||
case Nary:
|
||||
{
|
||||
eoNaryOp<Chrom>* Nop =
|
||||
static_cast<eoNaryOp<Chrom>* >(op);
|
||||
eoUniform<unsigned> u(0, pop.size() );
|
||||
eoPop<Chrom> tmpVec;
|
||||
tmpVec.push_back( pop[i] );
|
||||
for ( unsigned i = 0; i < u(); i ++ ) {
|
||||
tmpVec.push_back( pop[ u() ] );
|
||||
}
|
||||
(*Nop)( tmpVec );
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
throw runtime_error( "eoBreeder:operator() Not implemented yet!" );
|
||||
break;
|
||||
}
|
||||
}
|
||||
case binary:
|
||||
{
|
||||
vector<unsigned> pos(pop.size());
|
||||
iota(pos.begin(), pos.end(), 0);
|
||||
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 (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.
|
||||
string classname() const { return "eoBreeder"; }
|
||||
|
||||
private:
|
||||
typedef vector<pair<float, eoOp<Chrom>*> > Operators;
|
||||
|
||||
eoUniform<float> uniform;
|
||||
Operators operators;
|
||||
eoOpSelector<Chrom>& opSel;
|
||||
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
|
|
|||
|
|
@ -20,13 +20,10 @@ using namespace std;
|
|||
#define MINFLOAT numeric_limits<float>::min()
|
||||
#define MAXDOUBLE numeric_limits<double>::max()
|
||||
#define MAXINT numeric_limits<int>::max()
|
||||
#define min _MIN
|
||||
#define max _MAX
|
||||
#else
|
||||
#include <float.h>
|
||||
#include <limits.h>
|
||||
#include <values.h>
|
||||
|
||||
#ifndef MAXFLOAT
|
||||
#define MAXFLOAT (float)1e127
|
||||
#define MAXDOUBLE (double)1.79769313486231570e+308
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ template<class Chrom> class eoInclusion: public eoMerge<Chrom>
|
|||
{
|
||||
public:
|
||||
/// (Default) Constructor.
|
||||
eoInclusion(const float& _rate = 1.0): eoMerge(_rate) {}
|
||||
eoInclusion(const float& _rate = 1.0): eoMerge<Chrom>(_rate) {}
|
||||
|
||||
/**
|
||||
* Creates a new population based on breeders and original populations.
|
||||
|
|
|
|||
|
|
@ -148,6 +148,7 @@ public:
|
|||
|
||||
};
|
||||
|
||||
#include <eoPop.h>
|
||||
/** eoNaryOp is the N-ary operator: genetic operator that takes
|
||||
several EOs. It could be called an {\em orgy} operator
|
||||
*/
|
||||
|
|
@ -168,7 +169,7 @@ public:
|
|||
|
||||
/** applies randomly operator, to the object.
|
||||
*/
|
||||
// virtual void operator()( EOPop<EOType> & _eop) const = 0;
|
||||
virtual void operator()( eoPop<EOType> & _eop) const = 0;
|
||||
|
||||
/** @name Methods from eoObject
|
||||
readFrom and printOn are directly inherited from eoObject.
|
||||
|
|
|
|||
|
|
@ -47,7 +47,7 @@ public:
|
|||
virtual deleteOp( ID _id ) = 0;
|
||||
|
||||
/// Returns a genetic operator according to the established criteria
|
||||
virtual const eoOp<EOT>& Op() = 0;
|
||||
virtual eoOp<EOT>* Op() = 0;
|
||||
|
||||
/// Methods inherited from eoObject
|
||||
//@{
|
||||
|
|
|
|||
|
|
@ -26,11 +26,11 @@ Operators are represented as pairs (proportion,operator)
|
|||
*/
|
||||
template<class EOT>
|
||||
class eoProportionalOpSel: public eoOpSelector<EOT>,
|
||||
public multimap<float,const eoOp<EOT>*,greater<float> >
|
||||
public multimap<float,eoOp<EOT>*,greater<float> >
|
||||
{
|
||||
public:
|
||||
|
||||
typedef multimap<float, const eoOp<EOT>*,greater<float> > MMF;
|
||||
typedef multimap<float, eoOp<EOT>*,greater<float> > MMF;
|
||||
|
||||
/// default ctor
|
||||
eoProportionalOpSel()
|
||||
|
|
@ -79,7 +79,7 @@ public:
|
|||
};
|
||||
|
||||
/// Returns a genetic operator according to the established criteria
|
||||
virtual const eoOp<EOT>& Op() {
|
||||
virtual eoOp<EOT>* Op() {
|
||||
// Check that all add up to one
|
||||
float acc = 0;
|
||||
MMF::iterator i;
|
||||
|
|
@ -100,7 +100,7 @@ public:
|
|||
} while ( (acc <= aRnd ) && (i++!=end() ) );
|
||||
if ( i == end() )
|
||||
throw runtime_error( "Operator not found in eoProportionalOpSelector" );
|
||||
return *(i->second);
|
||||
return i->second;
|
||||
//return i->second;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -51,6 +51,18 @@ Package=<4>
|
|||
|
||||
###############################################################################
|
||||
|
||||
Project: "t_eobreeder"=.\t_eobreeder.dsp - Package Owner=<4>
|
||||
|
||||
Package=<5>
|
||||
{{{
|
||||
}}}
|
||||
|
||||
Package=<4>
|
||||
{{{
|
||||
}}}
|
||||
|
||||
###############################################################################
|
||||
|
||||
Project: "t_eoid"=.\t_eoid.dsp - Package Owner=<4>
|
||||
|
||||
Package=<5>
|
||||
|
|
@ -63,6 +75,18 @@ Package=<4>
|
|||
|
||||
###############################################################################
|
||||
|
||||
Project: "t_eoinclusion"=.\t_eoinclusion.dsp - Package Owner=<4>
|
||||
|
||||
Package=<5>
|
||||
{{{
|
||||
}}}
|
||||
|
||||
Package=<4>
|
||||
{{{
|
||||
}}}
|
||||
|
||||
###############################################################################
|
||||
|
||||
Project: "t_eoinsertion"=.\t_eoinsertion.dsp - Package Owner=<4>
|
||||
|
||||
Package=<5>
|
||||
|
|
|
|||
Reference in a new issue