diff --git a/eo/src/ga/eoBin.h b/eo/src/ga/eoBin.h new file mode 100644 index 00000000..1d7ec419 --- /dev/null +++ b/eo/src/ga/eoBin.h @@ -0,0 +1,93 @@ +/* + eoBin.h + (c) GeNeura Team 1998 + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + Contact: todos@geneura.ugr.es, http://geneura.ugr.es +*/ + +#ifndef eoBin_h +#define eoBin_h + +//----------------------------------------------------------------------------- + +#include // ostream, istream +#include // bind2nd +#include // string +#include // EO + +/** eoBin: implementation of binary chromosome. + * based on STL's bit_vector (vector). +*/ +template class eoBin: public eoVector +{ + public: + + /** + * (Default) Constructor. + * @param size Size of the binary string. + */ + eoBin(unsigned size = 0, bool value = false): + eoVector(size, value) {} + + /** + * Constructor. + * @param size Size of the binary string. + */ + eoBin(unsigned size, const eoRnd& rnd): eoVector(size) + { + generate(begin(), end(), rnd); + } + + /** Constructor from istream. + @param is The istream to read from.*/ + eoBin(istream& _is):eoVector(_is){}; + + /// My class name. + string className() const + { + return "eoBin"; + } + + /** + * To print me on a stream. + * @param os The ostream. + */ + void printOn(ostream& os) const + { + copy(begin(), end(), ostream_iterator(os)); + } + + /** + * To read me from a stream. + * @param is The istream. + */ + void readFrom(istream& is) + { + string bits; + is >> bits; + if (is) + { + resize(bits.size()); + transform(bits.begin(), bits.end(), begin(), + bind2nd(equal_to(), '1')); + } + } +}; + +//----------------------------------------------------------------------------- + +#endif eoBin_h diff --git a/eo/src/ga/eoBitOp.h b/eo/src/ga/eoBitOp.h new file mode 100644 index 00000000..78337fbb --- /dev/null +++ b/eo/src/ga/eoBitOp.h @@ -0,0 +1,325 @@ +//----------------------------------------------------------------------------- +// eoBitOp.h +//----------------------------------------------------------------------------- + +#ifndef eoBitOp_h +#define eoBitOp_h + +//----------------------------------------------------------------------------- + +#include // swap_ranges +#include // eoUniform +#include // eoBin +#include // eoMonOp + + +/** @name BitWise Genetic operators + +Even as these operators might seem general, they are particular versions of genetic +operators useful only for binary operators. As any set of genetic operators, it must +have a factory that knows how to build them from a description +@author GeNeura Team +@version 0.1 +@see eoBin +@see eoBitOpFactory +*/ + +//@{ + +/** eoBinRandom --> mofify a chromosome in a random way */ + +template class eoBinRandom: public eoMonOp +{ + public: + /// The class name. + string className() const { return "eoBinRandom"; } + + /** + * Randomizes a cromosome. + * @param chrom The cromosome to be randomize. + */ + void operator()(Chrom& chrom) const + { + eoUniform uniform(0.0, 1.0); + for (unsigned i = 0; i < chrom.size(); i++) + chrom[i] = (uniform() < 0.5) ? false : true; + } +}; + + +/** eoBinBitFlip --> chages a bit */ + +template class eoBinBitFlip: public eoMonOp +{ + public: + /// The class name. + string className() const { return "eoBinBitFlip"; } + + /** + * Change one bit. + * @param chrom The cromosome which one bit is going to be changed. + */ + void operator()(Chrom& chrom) const + { + eoUniform uniform(0, chrom.size()); + unsigned i = uniform(); + chrom[i] = (chrom[i]) ? false : true; + } +}; + + +/** eoBinMutation --> classical mutation */ + +template class eoBinMutation: public eoMonOp +{ + public: + /** + * (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. + */ + void operator()(Chrom& chrom) const + { + for (unsigned i = 0; i < chrom.size(); i++) + if (uniform() < rate) + chrom[i] = !chrom[i]; + } + + private: + double rate; + mutable eoUniform uniform; +}; + + +/** eoBinInversion: inverts the bits of the chromosome between an interval */ + +template class eoBinInversion: public eoMonOp +{ + public: + /// 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). + */ + void operator()(Chrom& chrom) const + { + eoUniform uniform(0, chrom.size() + 1); + + unsigned u1 = uniform(), u2; + do u2 = uniform(); while (u1 == u2); + unsigned r1 = min(u1, u2), r2 = max(u1, u2); + + reverse(chrom.begin() + r1, chrom.begin() + r2); + } +}; + + +/** eoBinNext --> next binary value */ + +template class eoBinNext: public eoMonOp +{ + public: + /// 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. + */ + void operator()(Chrom& chrom) const + { + for (int i = chrom.size() - 1; i >= 0; i--) + if (chrom[i]) + { + chrom[i] = 0; + continue; + } + else + { + chrom[i] = 1; + break; + } + } +}; + + +/** eoBinPrev --> previos binary value */ + +template class eoBinPrev: public eoMonOp +{ + 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. + */ + void operator()(Chrom& chrom) const + { + for (int i = chrom.size() - 1; i >= 0; i--) + if (chrom[i]) + { + chrom[i] = 0; + break; + } + else + { + chrom[i] = 1; + continue; + } + } +}; + + +/** eoBinCrossover --> classic crossover */ + +template class eoBinCrossover: public eoQuadraticOp +{ + public: + /// The class name. + string className() const { return "eoBinCrossover"; } + + /** + * 2-point crossover for binary chromosomes. + * @param chrom1 The first chromosome. + * @param chrom2 The first chromosome. + */ + void operator()(Chrom& chrom1, Chrom& chrom2) const + { + eoUniform uniform(1, min(chrom1.size(), chrom2.size())); + swap_ranges(chrom1.begin(), chrom1.begin() + uniform(), chrom2.begin()); + } +}; + + +/** eoBinNxOver --> n-point crossover */ + +template class eoBinNxOver: public eoQuadraticOp +{ + public: + /// (Defualt) Constructor. + eoBinNxOver(const unsigned& _num_points = 2): num_points(_num_points) + { + if (num_points < 1) + runtime_error("NxOver --> invalid number of points"); + } + + /// The class name. + string className() const { return "eoBinNxOver"; } + + /** + * 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()); + unsigned max_points = min(max_size - 1, num_points); + + vector points(max_size, false); + eoUniform uniform(1, max_size); + + // select ranges of bits to swap + do { + unsigned bit = uniform(); + if (points[bit]) + continue; + else + { + points[bit] = true; + max_points--; + } + } while (max_points); + + + // swap bits between chromosomes + bool change = false; + for (unsigned bit = 1; bit < points.size(); bit++) + { + if (points[bit]) + change = !change; + + if (change) + swap(chrom1[bit], chrom2[bit]); + } + } + + private: + unsigned num_points; +}; + + +/** eoBinGxOver --> gene crossover */ + +template class eoBinGxOver: public eoQuadraticOp +{ + public: + /// Constructor. + eoBinGxOver(const unsigned _gene_size, const unsigned _num_points = 2): + gene_size(_gene_size), num_points(_num_points) + { + if (gene_size < 1) + runtime_error("GxOver --> invalid gene size"); + if (num_points < 1) + 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. + * @param chrom2 The first chromosome. + */ + void operator()(Chrom& chrom1, Chrom& chrom2) const + { + unsigned max_genes = min(chrom1.size(), chrom2.size()) / gene_size; + unsigned cut_genes = min(max_genes, num_points); + + vector points(max_genes, false); + eoUniform uniform(0, max_genes); + + // selects genes to swap + do { + unsigned bit = uniform(); + if (points[bit]) + continue; + else + { + points[bit] = true; + cut_genes--; + } + } while (cut_genes); + + // swaps genes + for (unsigned i = 0; i < points.size(); i++) + if (points[i]) + swap_ranges(chrom1.begin() + i * gene_size, + chrom1.begin() + i * gene_size + gene_size, + chrom2.begin() + i * gene_size); + } + + private: + unsigned gene_size; + unsigned num_points; +}; + + + +//----------------------------------------------------------------------------- +//@} +#endif eoBitOp_h + diff --git a/eo/src/ga/eoBitOpFactory.h b/eo/src/ga/eoBitOpFactory.h new file mode 100644 index 00000000..8d70c7e4 --- /dev/null +++ b/eo/src/ga/eoBitOpFactory.h @@ -0,0 +1,120 @@ +// eoBitOpFactory.h +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// eoOpFactory.h +// (c) GeNeura Team, 1998 +/* + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + Contact: todos@geneura.ugr.es, http://geneura.ugr.es + */ +//----------------------------------------------------------------------------- + +#ifndef _EOBITOPFACTORY_H +#define _EOBITOPFACTORY_H + +#include +#include + +//----------------------------------------------------------------------------- + +/** EO Factory. An instance of the factory class to create operators that act +on bitstring chromosomes. Only those chromosomes can instantiate the operators +that are created here +@see eoSelect*/ +template< class EOT> +class eoBitOpFactory: public eoOpFactory { + +public: + + /// @name ctors and dtors + //{@ + /// constructor + eoBitOpFactory( ) {}; + + /// destructor + virtual ~eoBitOpFactory() {}; + //@} + + /** Another factory method: creates an object from an istream, reading from + it whatever is needed to create the object. Usually, the format for the istream will be\\ + objectType parameter1 parameter2 ... parametern\\ + If there are problems, an exception is raised; it should be caught at the + upper level, because it might be something for that level\\ + At the same time, it catches exceptions thrown at a lower level, which will + indicate that whatever is in the stream is for this method to process + @param _is an stream from where a single line will be read + @throw runtime_exception if the object type is not known + */ + virtual eoOp* make(istream& _is) { + eoOp * opPtr = NULL; + try { + opPtr = eoOpFactory::make( _is ); + } catch ( const string& objectTypeStr ) { + if ( objectTypeStr == "eoBinRandom") { + opPtr = new eoBinRandom(); + } + if ( objectTypeStr == "eoBinBitFlip" ) { + opPtr = new eoBinBitFlip( ); + } + if ( objectTypeStr == "eoBinMutation" ) { + float rate; + _is >> rate; + opPtr = new eoBinMutation( rate ); + } + if ( objectTypeStr == "eoBinInversion" ) { + opPtr = new eoBinInversion( ); + } + if ( objectTypeStr == "eoBinNext" ) { + opPtr = new eoBinNext( ); + } + if ( objectTypeStr == "eoBinPrev" ) { + opPtr = new eoBinPrev( ); + } + if ( objectTypeStr == "eoBinNext" ) { + opPtr = new eoBinNext( ); + } + if ( objectTypeStr == "eoBinCrossover" ) { + opPtr = new eoBinCrossover( ); + } + if ( objectTypeStr == "eoBinNxOver" ) { + unsigned nPoints; + _is >> nPoints; + opPtr = new eoBinNxOver( nPoints ); + } + if ( objectTypeStr == "eoBinGxOver" ) { + unsigned geneSize, nPoints; + _is >> geneSize >> nPoints; + opPtr = new eoBinGxOver( geneSize, nPoints ); + } + if ( objectTypeStr == "eoBinUxOver" ) { + float rate; + _is >> rate; + opPtr = new eoBinUxOver( rate ); + } + if ( !opPtr ) { // to be caught by the upper level + throw objectTypeStr; + } + } + return opPtr; + }; + + +}; + + +#endif _EOBITOPFACTORY_H + diff --git a/eo/src/other/eoExternalEO.h b/eo/src/other/eoExternalEO.h new file mode 100644 index 00000000..8ca43e71 --- /dev/null +++ b/eo/src/other/eoExternalEO.h @@ -0,0 +1,67 @@ +/* -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + + ----------------------------------------------------------------------------- + eoExternalEO.h + * Definition of an object that allows an external struct + * to be inserted in EO + (c) Maarten Keijzer (mkeijzer@mad.scientist.com) and GeNeura Team, 1999, 2000 + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + Contact: todos@geneura.ugr.es, http://geneura.ugr.es + */ + +#ifndef eoExternalEO_h +#define eoExternalEO_h + +#include "EO.h" + +/** + * Definition of an object that allows an external struct + * to be inserted in EO +*/ +template +class eoExternalEO : public EO, virtual public External +{ + public : + + typedef External Type; + + eoExternalEO(void) : EO(), Base() {} + eoExternalEO(istream& is) : EO(), Base() { readFrom(is); } + + /** + * Read object.\\ + * @param _is a istream. + * @throw runtime_exception If a valid object can't be read. + */ + virtual void readFrom(istream& _is) + { + EO::readFrom(is); + throw runtime_excpetion("Reading not defined yet"); + } + + /** + * Write object. Called printOn since it prints the object _on_ a stream. + * @param _os A ostream. + */ + virtual void printOn(ostream& _os) const + { + EO::printOn(is); + throw runtime_excpetion("Writing not defined yet"); + } + +}; + diff --git a/eo/src/other/eoExternalOpFunctions.cpp b/eo/src/other/eoExternalOpFunctions.cpp new file mode 100644 index 00000000..34efcf41 --- /dev/null +++ b/eo/src/other/eoExternalOpFunctions.cpp @@ -0,0 +1,41 @@ +/* -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + + ----------------------------------------------------------------------------- + eoExternalOpFunc.h + Defines eoExternalInitOpFunc, eoExternalMonOpFunc, eoExternalBinOpFunc, eoExternalQuadOpFunc + that are used to wrap a function pointer to externally defined initialization + and 'genetic' operators + + (c) Maarten Keijzer (mkeijzer@mad.scientist.com) and GeNeura Team, 1999, 2000 + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + Contact: todos@geneura.ugr.es, http://geneura.ugr.es + */ + +#ifndef eoExternalOpFunc_h +#define eoExternalOpFunc_h + +#include "eoExternalEO.h" +#include "eoOps.h" +#include "eoRnd.h" + +template +class eoExternalInitFunc +{ + +public : + +}; \ No newline at end of file diff --git a/eo/src/other/eoString.h b/eo/src/other/eoString.h new file mode 100644 index 00000000..d1ba55b4 --- /dev/null +++ b/eo/src/other/eoString.h @@ -0,0 +1,148 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// eoString.h +// (c) GeNeura Team, 1998 +/* + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + Contact: todos@geneura.ugr.es, http://geneura.ugr.es + */ +//----------------------------------------------------------------------------- + +#ifndef _eoString_H +#define _eoString_H + +// STL libraries +#include +#include + +using namespace std; + +// EO headers +#include + +//----------------------------------------------------------------------------- +// eoString +//----------------------------------------------------------------------------- + +/** Adaptor that turns an STL string into an EO */ +template +class eoString: public eo1d, public string { +public: + + /// Canonical part of the objects: several ctors, copy ctor, dtor and assignment operator + //@{ + /// ctor + eoString( const string& _str ="" ) + : eo1d(), string( _str ) {}; + + + /** Ctor using a random number generator + @param _size Lineal length of the object + @param _rnd a random number generator, which returns a random value each time it´s called + */ + eoString( unsigned _size, eoRnd& _rnd ) + : eo1d(), string(){ + for ( unsigned i = 0; i < _size; i ++ ) { + *this += _rnd(); + } + }; + + /** Ctor from a stream + @param _s input stream + */ + eoString( istream & _s ) + : eo1d(){ + _s >> *this; + }; + + /// copy ctor + eoString( const eoString& _eoStr ) + :eo1d( static_cast & > ( _eoStr ) ), + string( _eoStr ){}; + + /// Assignment operator + const eoString& operator =( const eoString& _eoStr ) { + if ( this != & _eoStr ) { + eo1d::operator = ( _eoStr ); + string::operator = ( _eoStr ); + } + return *this; + } + + /// dtor + virtual ~eoString() {}; +//@} + + + /** methods that implement the eo1d protocol + @exception out_of_range if _i is larger than EO´s size + */ + virtual char getGene( unsigned _i ) const { + if ( _i >= length() ) + throw out_of_range( "out_of_range when reading gene"); + return (*this)[_i]; + }; + + /** methods that implement the eo1d protocol + @exception out_of_range if _i is larger than EO´s size + */ + virtual void setGene( unsigned _i, const char& _value ) { + if ( _i >= size() ) + throw out_of_range( "out_of_range when writing a gene"); + (*this)[_i] = _value; + }; + + /** Inserts a value after _i, displacing anything to the right + @exception out_of_range if _i is larger than EO´s size + */ + virtual void insertGene( unsigned _i, char _val ) { + if (_i <= this->size() ) { + string::iterator i = this->begin()+_i; + this->insert( i, _val ); + } else + throw out_of_range( "out_of_range when inserting gene"); + }; + + /** Eliminates the gene at position _i + @exception out_of_range if _i is larger than EO´s size + */ + virtual void deleteGene( unsigned _i ) { + if (_i < this->size() ) { + string::iterator i = this->begin()+_i; + this->erase( i ); + } else + throw out_of_range( "out_of_range when deleting gene"); + }; + + /// methods that implement the EO protocol + virtual unsigned length() const { return this->size(); }; + + /** @name Methods from eoObject + readFrom and printOn are directly inherited from eo1d + */ + //@{ + /** Inherited from eoObject + @see eoObject + */ + virtual string className() const {return "eoString";}; + //@} + + +}; + +#endif + diff --git a/eo/src/other/eoStringMutation.h b/eo/src/other/eoStringMutation.h new file mode 100644 index 00000000..5cebc8eb --- /dev/null +++ b/eo/src/other/eoStringMutation.h @@ -0,0 +1,78 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// eoStringMutation.h +// (c) GeNeura Team, 1999 +/* + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + Contact: todos@geneura.ugr.es, http://geneura.ugr.es + */ +//----------------------------------------------------------------------------- + +#ifndef _EOSTRINGMUTATION_H +#define _EOSRTINGMUTATION_H + +#include +// EO includes +#include +#include +#include + +/** Mutation of an eoString. + The eoString's genes are changed by adding or substracting 1 to +*/ + +template +class eoStringMutation: public eoMutation { + public: + + /// + eoStringMutation(const double _rate=0.0) : eoMutation< EOT >(_rate) {}; + + /// + virtual ~eoStringMutation() {}; + + /** @name Methods from eoObject + */ + //@{ + /** Inherited from eoObject + @see eoObject + */ + string className() const {return "eoStringMutation";}; + //@} + + + private: + +#ifdef _MSC_VER + typedef EOT::Type Type; +#else + typedef typename EOT::Type Type; +#endif + + /// applies operator to one gene in the EO. It increments or decrements the value of that gene by one. + virtual void applyAt( EOT& _eo, unsigned _i ) const { + eoUniform uniform( 0, 1 ); + if( rate < uniform() ) { + _eo.gene(_i) += ( uniform()>=0.5 )? (1) : (-1) ; + } + } + +}; + +//----------------------------------------------------------------------------- + +#endif