Compiled new stuff on VC++, changes to breeder

This commit is contained in:
jmerelo 1999-02-15 16:01:35 +00:00
commit e3bff7d45f
8 changed files with 83 additions and 68 deletions

View file

@ -33,7 +33,7 @@ template <class F> class eoBin: public eoVector<bool, F>
* Constructor. * Constructor.
* @param size Size of the binary string. * @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); generate(begin(), end(), rnd);
} }

View file

@ -7,8 +7,10 @@
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#include <vector> // vector #include <vector> // vector
#include <pair.h> // pair
using namespace std;
#include <eo> // eoTransform, eoUniform, eoOp, eoMonOp, eoBinOp, eoPop #include <eo> // eoTransform, eoUniform, eoOp, eoMonOp, eoBinOp, eoPop
/***************************************************************************** /*****************************************************************************
@ -20,73 +22,64 @@ template<class Chrom> class eoBreeder: public eoTransform<Chrom>
{ {
public: public:
/// Default constructor. /// Default constructor.
eoBreeder(): uniform(0.0, 1.0) {} eoBreeder( eoOpSelector<Chrom>& _opSel): opSel( _opSel ) {}
/**
* 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));
}
/// Destructor.
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)
{ {
unsigned i; for (unsigned i = 0; i < pop.size(); i ++ ) {
eoOp<Chrom>* op = opSel.Op();
for (Operators::const_iterator op = operators.begin(); switch (op->readArity()) {
op != operators.end(); case unary:
op++) {
switch (op->second->readArity()) eoMonOp<Chrom>* monop = static_cast<eoMonOp<Chrom>* >(op);
{ (*monop)( pop[i] );
case unary: break;
{ }
eoMonOp<Chrom>& monop = (eoMonOp<Chrom>&)*(op->second); case binary:
for (i = 0; i < pop.size(); i++) {
if (uniform() < op->first) eoBinOp<Chrom>* binop =
monop(pop[i]); static_cast<eoBinOp<Chrom>* >(op);
break; 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. /// The class name.
string classname() const { return "eoBreeder"; } string classname() const { return "eoBreeder"; }
private: private:
typedef vector<pair<float, eoOp<Chrom>*> > Operators; eoOpSelector<Chrom>& opSel;
eoUniform<float> uniform;
Operators operators;
}; };
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------

View file

@ -20,13 +20,10 @@ using namespace std;
#define MINFLOAT numeric_limits<float>::min() #define MINFLOAT numeric_limits<float>::min()
#define MAXDOUBLE numeric_limits<double>::max() #define MAXDOUBLE numeric_limits<double>::max()
#define MAXINT numeric_limits<int>::max() #define MAXINT numeric_limits<int>::max()
#define min _MIN
#define max _MAX
#else #else
#include <float.h> #include <float.h>
#include <limits.h> #include <limits.h>
#include <values.h> #include <values.h>
#ifndef MAXFLOAT #ifndef MAXFLOAT
#define MAXFLOAT (float)1e127 #define MAXFLOAT (float)1e127
#define MAXDOUBLE (double)1.79769313486231570e+308 #define MAXDOUBLE (double)1.79769313486231570e+308

View file

@ -19,7 +19,7 @@ template<class Chrom> class eoInclusion: public eoMerge<Chrom>
{ {
public: public:
/// (Default) Constructor. /// (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. * Creates a new population based on breeders and original populations.

View file

@ -148,6 +148,7 @@ public:
}; };
#include <eoPop.h>
/** eoNaryOp is the N-ary operator: genetic operator that takes /** eoNaryOp is the N-ary operator: genetic operator that takes
several EOs. It could be called an {\em orgy} operator several EOs. It could be called an {\em orgy} operator
*/ */
@ -168,7 +169,7 @@ public:
/** applies randomly operator, to the object. /** 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 /** @name Methods from eoObject
readFrom and printOn are directly inherited from eoObject. readFrom and printOn are directly inherited from eoObject.

View file

@ -47,7 +47,7 @@ public:
virtual deleteOp( ID _id ) = 0; virtual deleteOp( ID _id ) = 0;
/// Returns a genetic operator according to the established criteria /// Returns a genetic operator according to the established criteria
virtual const eoOp<EOT>& Op() = 0; virtual eoOp<EOT>* Op() = 0;
/// Methods inherited from eoObject /// Methods inherited from eoObject
//@{ //@{

View file

@ -26,11 +26,11 @@ Operators are represented as pairs (proportion,operator)
*/ */
template<class EOT> template<class EOT>
class eoProportionalOpSel: public eoOpSelector<EOT>, class eoProportionalOpSel: public eoOpSelector<EOT>,
public multimap<float,const eoOp<EOT>*,greater<float> > public multimap<float,eoOp<EOT>*,greater<float> >
{ {
public: public:
typedef multimap<float, const eoOp<EOT>*,greater<float> > MMF; typedef multimap<float, eoOp<EOT>*,greater<float> > MMF;
/// default ctor /// default ctor
eoProportionalOpSel() eoProportionalOpSel()
@ -79,7 +79,7 @@ public:
}; };
/// Returns a genetic operator according to the established criteria /// 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 // Check that all add up to one
float acc = 0; float acc = 0;
MMF::iterator i; MMF::iterator i;
@ -100,7 +100,7 @@ public:
} while ( (acc <= aRnd ) && (i++!=end() ) ); } while ( (acc <= aRnd ) && (i++!=end() ) );
if ( i == end() ) if ( i == end() )
throw runtime_error( "Operator not found in eoProportionalOpSelector" ); throw runtime_error( "Operator not found in eoProportionalOpSelector" );
return *(i->second); return i->second;
//return i->second; //return i->second;
} }

View file

@ -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> Project: "t_eoid"=.\t_eoid.dsp - Package Owner=<4>
Package=<5> 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> Project: "t_eoinsertion"=.\t_eoinsertion.dsp - Package Owner=<4>
Package=<5> Package=<5>