diff --git a/eo/src/MGE/VirusOp.h b/eo/src/MGE/VirusOp.h new file mode 100644 index 00000000..73038aa7 --- /dev/null +++ b/eo/src/MGE/VirusOp.h @@ -0,0 +1,106 @@ +/* + ViruOp.h + (c) GeNeura Team 2001, Marc Schoenauer 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 + Marc.Schoenauer@polytechnique.fr +CVS Info: $Date: 2001-05-10 12:16:00 $ $Header: /home/nojhan/dev/eodev/eodev_cvs/eo/src/MGE/Attic/VirusOp.h,v 1.1 2001-05-10 12:16:00 jmerelo Exp $ $Author: jmerelo $ +*/ + +#ifndef VirusOp_h +#define VirusOp_h + +//----------------------------------------------------------------------------- + +#include // ostream, istream +#include // bind2nd +#include // string + +#include +#include + +/** eoBitFlip --> changes 1 bit +\class eoBitBitFlip eoBitOp.h ga/eoBitOp.h +\ingroup bitstring +*/ + +template +class VirusBitFlip: public eoMonOp > { + public: + /// The class name. + virtual string className() const { return "VirusBitFlip"; }; + + /** + * Change one bit. + * @param chrom The cromosome which one bit is going to be changed. + */ + bool operator()(eoVirus& _chrom) { + unsigned i = eo::rng.random(_chrom.size()); + _chrom.virusBitSet(i, _chrom.virusBit(i) ? false : true ); + return true; + } +}; + +template +class VirusMutation: public eoMonOp > { + public: + /// The class name. + virtual string className() const { return "VirusMutation"; }; + + /** + * Change one bit. + * @param chrom The cromosome which one bit is going to be changed. + */ + bool operator()(eoVirus& _chrom) { + // Search for virus bits + vector bitsSet; + for ( unsigned i = 0; i < _chrom.size(); i ++ ) { + if ( _chrom.virusBit(i) ) { + bitsSet.push_back( i ); + } + } + if ( !bitsSet.size() ) { + return false; + } + unsigned flipSite = eo::rng.random(bitsSet.size()); + unsigned flipValue = bitsSet[ flipSite ]; + _chrom[flipValue] = _chrom[flipValue] ? false : true; + return true; + } +}; + +template +class VirusTransmission: public eoBinOp > { + public: + /// The class name. + virtual string className() const { return "VirusTransmission"; }; + + /** + * Change one bit. + * @param _chrom The "receptor" chromosome + * @param _chrom2 The "donor" chromosome + */ + bool operator()(eoVirus& _chrom,const eoVirus& _chrom2) { + // Search for virus bits + for ( unsigned i = 0; i < _chrom.size(); i ++ ) { + _chrom.virusBitSet(i, _chrom2.virusBit(i) ); + } + return true; + } +}; + +#endif //VirusOp_h diff --git a/eo/src/MGE/eoInitVirus.h b/eo/src/MGE/eoInitVirus.h new file mode 100644 index 00000000..3659f476 --- /dev/null +++ b/eo/src/MGE/eoInitVirus.h @@ -0,0 +1,64 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// eoInit.h +// (c) Maarten Keijzer 2000, GeNeura Team, 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 + Marc.Schoenauer@polytechnique.fr + mak@dhi.dk + */ +//----------------------------------------------------------------------------- + +#ifndef _eoInitVirus_H +#define _eoInitVirus_H + +#include + +#include +#include +#include +#include + +/** + Initializer for binary chromosome with MGE +*/ +template +class eoInitVirus: public eoInit< eoVirus > { +public: + + eoInitVirus(unsigned _combien, eoRndGenerator& _generator) + : combien(_combien), generator(_generator) {} + + virtual void operator()( eoVirus& chrom) + { + chrom.resize(combien); + chrom.virResize(combien); + std::generate(chrom.begin(), chrom.end(), generator); + for ( unsigned i = 0; i < combien; i ++ ) { + chrom.virusBitSet(i, generator() ); + } + chrom.invalidate(); + } + +private : + unsigned combien; + /// generic wrapper for eoFunctor (s), to make them have the function-pointer style copy semantics + eoSTLF generator; +}; + +#endif diff --git a/eo/src/MGE/eoVirus.h b/eo/src/MGE/eoVirus.h new file mode 100644 index 00000000..46e55c91 --- /dev/null +++ b/eo/src/MGE/eoVirus.h @@ -0,0 +1,112 @@ +/* + eoVirus.h + (c) GeNeura Team 2001, Marc Schoenauer 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 + Marc.Schoenauer@polytechnique.fr +CVS Info: $Date: 2001-05-10 12:16:00 $ $Header: /home/nojhan/dev/eodev/eodev_cvs/eo/src/MGE/Attic/eoVirus.h,v 1.1 2001-05-10 12:16:00 jmerelo Exp $ $Author: jmerelo $ +*/ + +#ifndef eoVirus_h +#define eoVirus_h + +//----------------------------------------------------------------------------- + +#include // ostream, istream +#include // bind2nd +#include // string + +#include + +/** +\defgroup bitstring + + Various functions for a bitstring representation +*/ + +/** eoBit: implementation of bitstring chromosome. +\class eoBit eoBit.h ga/eoBit.h +\ingroup bitstring + * based on STL's vector specialization. +*/ +template class eoVirus: public eoBit +{ + public: + + /** + * (Default) Constructor. + * @param size Size of the binary string. + */ + eoVirus(unsigned _size = 0, bool _value = false, bool _virValue = false): + eoBit(_size, _value), virus( _size, _virValue) {} + + /// My class name. + virtual string className() const { + return "eoVirus"; + } + + /// Access to virus features + void virResize( unsigned _i ) { + virus.resize(_i ); + } + + /// Access to virus features + bool virusBit( unsigned _i ) const { + return virus[_i]; + } + + /// Change virus features + void virusBitSet( unsigned _i, bool _bit ) { + virus[_i ] = _bit; + } + + /** + * To print me on a stream. + * @param os The ostream. + */ + virtual void printOn(ostream& os) const { + EO::printOn(os); + os << ' '; + os << size() << ' '; + copy(begin(), end(), ostream_iterator(os)); + cout << endl; + copy(virus.begin(), virus.end(), ostream_iterator(os)); + } + + /** + * To read me from a stream. + * @param is The istream. + */ + virtual void readFrom(istream& is){ + eoBit::readFrom(is); + unsigned s; + is >> s; + string bits; + is >> bits; + if (is) { + virus.resize(bits.size()); + transform(bits.begin(), bits.end(), virus.begin(), + bind2nd(equal_to(), '1')); + } + } + private: + vector virus; +}; + +//----------------------------------------------------------------------------- + +#endif //eoBit_h diff --git a/eo/src/ga/eoBitOp.h b/eo/src/ga/eoBitOp.h index 951c1c50..3e691280 100644 --- a/eo/src/ga/eoBitOp.h +++ b/eo/src/ga/eoBitOp.h @@ -21,33 +21,9 @@ Contact: todos@geneura.ugr.es, http://geneura.ugr.es Marc.Schoenauer@polytechnique.fr mak@dhi.dk + CVS Info: $Date: 2001-05-10 12:16:00 $ $Header: /home/nojhan/dev/eodev/eodev_cvs/eo/src/ga/eoBitOp.h,v 1.13 2001-05-10 12:16:00 jmerelo Exp $ $Author: jmerelo $ */ //----------------------------------------------------------------------------- -// MS 17/10/2000 -// Added the uniform crossover - which, for some reasons, had dissapeared! -// Added the eoDetBitFlip, which flips exactly num_bit bits -// Aslo added the above standard header - -// I also want to start the discussion about the "gene" crossover. -// I think the word "gene" is not appropriate: if real numbers are coded in -// binary format, then a "gene" is a bit, and that's it -// if you want to exchange real number per se, then use real coding -// -// Because all crossover operators here except that Gene crossover -// ARE generic, i.e. appky to any vertor of something. - -// Note that for mutations, if instead of -// chrom[i] = (chrom[i]) ? false : true; -// we were calling something like -// specific_mutate(chrom[i]) -// all mutation would also be generic ... except those eoBitNext and eoBitPrev - -// If anybody reads this and want to change that (I'm also testing to see -// if someone ever reads the headers :-), drop me a mail -// Marc (Marc.Schoenauer@polytechnique.fr) -//----------------------------------------------------------------------------- -// eoBitOp.h -//----------------------------------------------------------------------------- #ifndef eoBitOp_h #define eoBitOp_h diff --git a/eo/test/Makefile.am b/eo/test/Makefile.am index 66899e8e..ceac9a94 100644 --- a/eo/test/Makefile.am +++ b/eo/test/Makefile.am @@ -13,7 +13,7 @@ LDADDS = $(top_builddir)/src/utils/libeoutils.a $(top_builddir)/src/libeo.a CXXFLAGS = -g -Wall ############################################################################### -check_PROGRAMS = t-eoParetoFitness t-eoPareto t-eofitness t-eoRandom t-eobin t-eoStateAndParser t-eoCheckpointing t-eoSSGA \ +check_PROGRAMS = t-eoParetoFitness t-eoPareto t-eofitness t-eoRandom t-eobin t-eoVirus t-MGE t-MGE-control t-eoStateAndParser t-eoCheckpointing t-eoSSGA \ t-eoExternalEO t-eoSymreg t-eo t-eoReplacement t-eoSelect t-eoGenOp t-eoGA t-eoReal t-eoVector t-eoESAll TESTS=run_tests t-eoVector t-eoRandom t-eoSSGA t-eoPareto t-eoParetoFitness # removing temporarily t-eoESFull @@ -39,6 +39,24 @@ t_eobin_LDADD = $(LDADDS) ############################################################################### +t_eoVirus_SOURCES = t-eoVirus.cpp binary_value.h +t_eoVirus_DEPENDENCIES = $(DEPS) +t_eoVirus_LDADD = $(LDADDS) + +############################################################################### + +t_MGE_SOURCES = t-MGE.cpp binary_value.h +t_MGE_DEPENDENCIES = $(DEPS) +t_MGE_LDADD = $(LDADDS) + +############################################################################### + +t_MGE_control_SOURCES = t-MGE-control.cpp binary_value.h +t_MGE_control_DEPENDENCIES = $(DEPS) +t_MGE_control_LDADD = $(LDADDS) + +############################################################################### + t_eoStateAndParser_SOURCES = t-eoStateAndParser.cpp t_eoStateAndParser_DEPENDENCIES = $(DEPS) t_eoStateAndParser_LDFLAGS = -lm diff --git a/eo/test/t-MGE-control.cpp b/eo/test/t-MGE-control.cpp new file mode 100644 index 00000000..5e9fc58a --- /dev/null +++ b/eo/test/t-MGE-control.cpp @@ -0,0 +1,97 @@ +//----------------------------------------------------------------------------- +// t-eoMGE.cpp +//----------------------------------------------------------------------------- + +#ifndef __GNUG__ +// to avoid long name warnings +#pragma warning(disable:4786) +#endif // __GNUG__ + +#include +#include + +#include "binary_value.h" + +// Viri +#include +#include +#include + +//----------------------------------------------------------------------------- + +typedef eoVirus Chrom; + +//----------------------------------------------------------------------------- + +int main() +{ + const unsigned POP_SIZE = 100, CHROM_SIZE = 16; + unsigned i; + eoBooleanGenerator gen; + + // the populations: + eoPop pop; + + // Evaluation + eoEvalFuncPtr eval( binary_value ); + + eoInitVirus random(CHROM_SIZE, gen); + for (i = 0; i < POP_SIZE; ++i) { + Chrom chrom; + random(chrom); + eval(chrom); + pop.push_back(chrom); + } + + cout << "population:" << endl; + for (i = 0; i < pop.size(); ++i) + cout << "\t" << pop[i] << " " << pop[i].fitness() << endl; + + + // selection + eoDetTournamentSelect lottery( 3) ; + + // breeder + eoOneBitFlip bf; + eoUBitXover xover; + eoProportionalOp propSel; + eoGeneralBreeder breeder( lottery, propSel ); + propSel.add(bf, 0.75); + propSel.add(xover, 0.25); + + // Replace a single one + eoPlusReplacement replace; + + // Terminators + eoGenContinue continuator1(50); + eoFitContinue continuator2(65535.f); + eoCombinedContinue continuator(continuator1, continuator2); + eoCheckPoint checkpoint(continuator); + eoStdoutMonitor monitor; + checkpoint.add(monitor); + eoSecondMomentStats stats; + monitor.add(stats); + checkpoint.add(stats); + + // GA generation + eoEasyEA ea(checkpoint, eval, breeder, replace ); + + // evolution + try + { + ea(pop); + } + catch (exception& e) + { + cout << "exception: " << e.what() << endl;; + exit(EXIT_FAILURE); + } + + cout << "pop" << endl; + for (i = 0; i < pop.size(); ++i) + cout << "\t" << pop[i] << " " << pop[i].fitness() << endl; + + return 0; +} + +//----------------------------------------------------------------------------- diff --git a/eo/test/t-MGE.cpp b/eo/test/t-MGE.cpp new file mode 100644 index 00000000..21c1c57f --- /dev/null +++ b/eo/test/t-MGE.cpp @@ -0,0 +1,101 @@ +//----------------------------------------------------------------------------- +// t-eoMGE.cpp +//----------------------------------------------------------------------------- + +#ifndef __GNUG__ +// to avoid long name warnings +#pragma warning(disable:4786) +#endif // __GNUG__ + +#include +#include + +#include "binary_value.h" + +// Viri +#include +#include +#include + +//----------------------------------------------------------------------------- + +typedef eoVirus Chrom; + +//----------------------------------------------------------------------------- + +int main() +{ + const unsigned POP_SIZE = 100, CHROM_SIZE = 16; + unsigned i; + eoBooleanGenerator gen; + + // the populations: + eoPop pop; + + // Evaluation + eoEvalFuncPtr eval( binary_value ); + + eoInitVirus random(CHROM_SIZE, gen); + for (i = 0; i < POP_SIZE; ++i) { + Chrom chrom; + random(chrom); + eval(chrom); + pop.push_back(chrom); + } + + cout << "population:" << endl; + for (i = 0; i < pop.size(); ++i) + cout << "\t" << pop[i] << " " << pop[i].fitness() << endl; + + + // selection + eoDetTournamentSelect lottery( 3) ; + + // breeder + VirusMutation vm; + VirusTransmission vt; + VirusBitFlip vf; + eoUBitXover xover; + eoProportionalOp propSel; + eoGeneralBreeder breeder( lottery, propSel ); + propSel.add(vm, 0.25); + propSel.add(vf, 0.25); + propSel.add(vt, 0.25); + propSel.add(xover, 0.25); + + // Replace a single one + eoPlusReplacement replace; + + // Terminators + eoGenContinue continuator1(50); + eoFitContinue continuator2(65535.f); + eoCombinedContinue continuator(continuator1, continuator2); + eoCheckPoint checkpoint(continuator); + eoStdoutMonitor monitor; + checkpoint.add(monitor); + eoSecondMomentStats stats; + monitor.add(stats); + checkpoint.add(stats); + + // GA generation + eoEasyEA ea(checkpoint, eval, breeder, replace ); + + // evolution + try + { + ea(pop); + } + catch (exception& e) + { + cout << "exception: " << e.what() << endl;; + exit(EXIT_FAILURE); + } + + cout << "pop" << endl; + for (i = 0; i < pop.size(); ++i) + cout << "\t" << pop[i] << " " << pop[i].fitness() << endl; + + return 0; +} + +//-----------------------------------------------------------------------------