diff --git a/eo/src/eo b/eo/src/eo index 9f21e4dd..f7addb71 100644 --- a/eo/src/eo +++ b/eo/src/eo @@ -48,6 +48,7 @@ #include #include +#include //----------------------------------------------------------------------------- // to be continued ... @@ -55,7 +56,6 @@ #include // what's the matter with you? #include // what's the matter with you? -// #include //----------------------------------------------------------------------------- diff --git a/eo/src/eoBitOp.h b/eo/src/eoBitOp.h index 50b0bba5..d69fe85f 100644 --- a/eo/src/eoBitOp.h +++ b/eo/src/eoBitOp.h @@ -7,8 +7,10 @@ //----------------------------------------------------------------------------- -#include // eoBin -#include // eoMonOp +#include // swap_ranges +#include // eoUniform +#include // eoBin +#include // eoMonOp /** @name BitWise Genetic operators @@ -210,12 +212,9 @@ template class eoBinNxOver: public eoBinOp eoBinNxOver(const unsigned& _num_points = 2): num_points(_num_points) { if (num_points < 1) - { - cerr << "NxOver --> invalid number of points " << num_points << endl; - exit(EXIT_FAILURE); - } + runtime_error("NxOver --> invalid number of points"); } - + /// The class name. string className() const { return "eoBinNxOver"; } @@ -272,20 +271,14 @@ template class eoBinGxOver: public eoBinOp gene_size(_gene_size), num_points(_num_points) { if (gene_size < 1) - { - cerr << "GxOver --> invalid gene size " << gene_size << endl; - exit(EXIT_FAILURE); - } + runtime_error("GxOver --> invalid gene size"); if (num_points < 1) - { - cerr << "GxOver --> invalid number of points " << num_points << endl; - exit(EXIT_FAILURE); - } + runtime_error("GxOver --> invalid number of points"); } /// The class name string className() const { return "eoBinGxOver"; } - + /** * Gene crossover for binary chromosomes. * @param chrom1 The first chromosome. @@ -310,7 +303,7 @@ template class eoBinGxOver: public eoBinOp cut_genes--; } } while (cut_genes); - + // swaps genes for (unsigned i = 0; i < points.size(); i++) if (points[i]) @@ -318,7 +311,7 @@ template class eoBinGxOver: public eoBinOp chrom1.begin() + i * gene_size + gene_size, chrom2.begin() + i * gene_size); } - + private: unsigned gene_size; unsigned num_points; @@ -326,23 +319,20 @@ template class eoBinGxOver: public eoBinOp /** eoBinUxOver --> uniform crossover */ - + template class eoBinUxOver: public eoBinOp { - public: +public: /// (Default) Constructor. eoBinUxOver(const float _rate = 0.5): rate(_rate) { if (rate < 0 || rate > 1) - { - cerr << "UxOver --> invalid rate " << rate << endl; - exit(EXIT_FAILURE); - } + runtime_error("UxOver --> invalid rate"); } /// The class name. string className() const { return "eoBinUxOver"; } - + /** * Uniform crossover for binary chromosomes. * @param chrom1 The first chromosome. diff --git a/eo/src/eoBreeder.h b/eo/src/eoBreeder.h index 8fa445dc..75cda90e 100644 --- a/eo/src/eoBreeder.h +++ b/eo/src/eoBreeder.h @@ -7,11 +7,14 @@ //----------------------------------------------------------------------------- -#include // vector - +#include // vector +#include // eoUniform +#include // eoOp, eoMonOp, eoBinOp +#include // eoPop +#include // eoTransform +#include // eoOpSelector + using namespace std; - -#include // eoTransform, eoUniform, eoOp, eoMonOp, eoBinOp, eoPop /***************************************************************************** * eoBreeder: transforms a population using genetic operators. * @@ -24,55 +27,48 @@ template class eoBreeder: public eoTransform /// Default constructor. eoBreeder( eoOpSelector& _opSel): opSel( _opSel ) {} - /// Destructor. - virtual ~eoBreeder() { } + virtual ~eoBreeder() {} /** * Transforms a population. * @param pop The population to be transformed. */ - void operator()(eoPop& pop) { - for (unsigned i = 0; i < pop.size(); i ++ ) { - eoOp* op = opSel.Op(); - switch (op->readArity()) { - case unary: - { - eoMonOp* monop = static_cast* >(op); - (*monop)( pop[i] ); - break; - } - case binary: - { - eoBinOp* binop = - static_cast* >(op); - eoUniform u(0, pop.size() ); - (*binop)(pop[i], pop[ u() ] ); - break; - } - case Nary: - { - eoNaryOp* Nop = - static_cast* >(op); - eoUniform u(0, pop.size() ); - eoPop 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; - } + for (unsigned i = 0; i < pop.size(); i++) { + eoOp* op = opSel.Op(); + switch (op->readArity()) { + case unary: + { + eoMonOp* monop = static_cast* >(op); + (*monop)( pop[i] ); + break; + } + case binary: + { + eoBinOp* binop = + static_cast* >(op); + eoUniform u(0, pop.size() ); + (*binop)(pop[i], pop[ u() ] ); + break; + } + case Nary: + { + eoNaryOp* Nop = + static_cast* >(op); + eoUniform u(0, pop.size() ); + eoPop tmpVec; + tmpVec.push_back( pop[i] ); + for ( unsigned i = 0; i < u(); i ++ ) { + tmpVec.push_back( pop[ u() ] ); } - } - }; + (*Nop)( tmpVec ); + break; + } + } + } + }; /// The class name. string classname() const { return "eoBreeder"; } diff --git a/eo/src/eoGA.h b/eo/src/eoGeneration.h similarity index 68% rename from eo/src/eoGA.h rename to eo/src/eoGeneration.h index 5fee041f..80aa24ff 100644 --- a/eo/src/eoGA.h +++ b/eo/src/eoGeneration.h @@ -1,9 +1,9 @@ //----------------------------------------------------------------------------- -// eoGA.h +// eoGeneration.h //----------------------------------------------------------------------------- -#ifndef eoGA_h -#define eoGA_h +#ifndef eoGeneration_h +#define eoGeneration_h //----------------------------------------------------------------------------- @@ -11,18 +11,21 @@ #include // eoSelect, eoTranform, eoMerge //----------------------------------------------------------------------------- -// eoGA +// eoGeneration //----------------------------------------------------------------------------- -class eoGA +template class eoGeneration: public eoTransform { public: /// Constructor. - eoGA(eoSelect& _select, eoTranform& _transform, eoMerge& _replace) - { - } + eoGeneration(eoSelect& _select, + eoTranform& _transform, + eoMerge& _replace): + eoTransform() {} - /// + /** + * + */ void operator()(eoPop& pop) { eoPop breeders; @@ -32,6 +35,9 @@ class eoGA replace(breeders, pop); } + /// Class name. + string className() const { return "eoGeneration"; } + private: eoSelect& select; eoTranform& transform; @@ -40,4 +46,4 @@ class eoGA //----------------------------------------------------------------------------- -#endif eoGA_h +#endif eoGeneration_h diff --git a/eo/src/eoPopOps.h b/eo/src/eoPopOps.h index cc0a10f9..f97e7899 100644 --- a/eo/src/eoPopOps.h +++ b/eo/src/eoPopOps.h @@ -25,26 +25,25 @@ template class eoTransform: public eoObject{ public: - /** ctor - */ - eoTransform() {}; - - /// Dtor - virtual ~eoTransform(){}; - - /// Pure virtual transformation function. Does something on the population - virtual void operator () ( eoPop& _pop ) = 0; - - /** @name Methods from eoObject */ - //@{ - /** readFrom and printOn are not overriden - */ - /** Inherited from eoObject. Returns the class name. - @see eoObject - */ - string className() const {return "eoTransform";}; - //@} - + /** ctor */ + eoTransform() {}; + + /// Dtor + virtual ~eoTransform(){}; + + /// Pure virtual transformation function. Does something on the population + virtual void operator () ( eoPop& _pop ) = 0; + + /** @name Methods from eoObject */ + //@{ + /** readFrom and printOn are not overriden + */ + /** Inherited from eoObject. Returns the class name. + @see eoObject + */ + string className() const {return "eoTransform";}; + //@} + }; /** eoSelect usually takes elements from one population, with or without