diff --git a/eo/src/eo b/eo/src/eo index d2df9d51..bf380700 100644 --- a/eo/src/eo +++ b/eo/src/eo @@ -80,7 +80,9 @@ #include #include #include - +#ifndef _MSC_VER +#include // CtrlC handling (using 2 global variables!) +#endif // Selection // the eoSelectOne's #include // also contains the eoSequentialSelect diff --git a/eo/src/eoCtrlCContinue.h b/eo/src/eoCtrlCContinue.h new file mode 100644 index 00000000..3a2adfcb --- /dev/null +++ b/eo/src/eoCtrlCContinue.h @@ -0,0 +1,92 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// eoCtrlCContinue.h +// (c) EEAAX 1996 - GeNeura Team, 1998 - Maarten Keijzer 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 eoCtrlCContinue_h +#define eoCtrlCContinue_h + +#include +#include + +// --- Global variables - but don't know what else to do - MS --- +static bool arret_demande = false; +static bool existCtrlCContinue = false; + +// +// The signal handler - installed in the eoCtrlCContinue Ctor +// +void signal_handler( int sig ) +// --------------------------- +{ + // --- On veut la paix, jusqu'a la fin --- + signal( SIGINT, SIG_IGN ); + signal( SIGQUIT, SIG_IGN ); + cerr << "Ctrl C entered ... closing down" << endl ; + arret_demande = true; +} + +// +// Permet de savoir si on a appuye sur Control-C +// (renvoie alors 1) +// +int AppliArretDemande() +// -------------------- +{ + return arret_demande; +} + + + +/** + Ctrl C handling: this eoContinue tells whether the user pressed Ctrl C +*/ +template< class EOT> +class eoCtrlCContinue: public eoContinue +{ +public: + + /// Ctor : installs the signal handler + eoCtrlCContinue() + { + // First checks that no other eoCtrlCContinue does exist + if (existCtrlCContinue) + throw runtime_error("A signal handler for Ctrl C is already defined!\n"); + signal( SIGINT, signal_handler ); + signal( SIGQUIT, signal_handler ); + existCtrlCContinue = true; + } + + /** Returns false when Ctrl C has been typed in + * reached */ + virtual bool operator() ( const eoPop& _vEO ) + { + if (arret_demande) + return false; + return true; + } + +}; + + +#endif diff --git a/eo/src/ga/eoBit.h b/eo/src/ga/eoBit.h index 668c9112..558903b1 100644 --- a/eo/src/ga/eoBit.h +++ b/eo/src/ga/eoBit.h @@ -64,7 +64,7 @@ template class eoBit: public eoVector eoVector(size, value) {} /// My class name. - string className() const + virtual string className() const { return "eoBit"; } @@ -73,7 +73,7 @@ template class eoBit: public eoVector * To print me on a stream. * @param os The ostream. */ - void printOn(ostream& os) const + virtual void printOn(ostream& os) const { EO::printOn(os); os << ' '; @@ -85,7 +85,7 @@ template class eoBit: public eoVector * To read me from a stream. * @param is The istream. */ - void readFrom(istream& is) + virtual void readFrom(istream& is) { EO::readFrom(is); unsigned s; diff --git a/eo/src/ga/eoBitOp.h b/eo/src/ga/eoBitOp.h index 816d5cbe..61117a46 100644 --- a/eo/src/ga/eoBitOp.h +++ b/eo/src/ga/eoBitOp.h @@ -69,7 +69,7 @@ template class eoOneBitFlip: public eoMonOp { public: /// The class name. - string className() const { return "eoOneBitFlip"; } + virtual string className() const { return "eoOneBitFlip"; } /** * Change one bit. @@ -99,7 +99,7 @@ template class eoDetBitFlip: public eoMonOp eoDetBitFlip(const unsigned& _num_bit = 1): num_bit(_num_bit) {} /// The class name. - string className() const { return "eoDetBitFlip"; } + virtual string className() const { return "eoDetBitFlip"; } /** * Change num_bit bits. @@ -135,7 +135,7 @@ template class eoBitMutation: public eoMonOp eoBitMutation(const double& _rate = 0.01): rate(_rate) {} /// The class name. - string className() const { return "eoBitMutation"; } + virtual string className() const { return "eoBitMutation"; } /** * Mutate a chromosome. @@ -168,7 +168,7 @@ template class eoBitInversion: public eoMonOp { public: /// The class name. - string className() const { return "eoBitInversion"; } + virtual string className() const { return "eoBitInversion"; } /** * Inverts a range of bits in a binary chromosome. @@ -196,7 +196,7 @@ template class eoBitNext: public eoMonOp { public: /// The class name. - string className() const { return "eoBitNext"; } + virtual string className() const { return "eoBitNext"; } /** * Change the bit string x to be x+1. @@ -230,7 +230,7 @@ template class eoBitPrev: public eoMonOp { public: /// The class name. - string className() const { return "eoBitPrev"; } + virtual string className() const { return "eoBitPrev"; } /** * Change the bit string x to be x-1. @@ -264,7 +264,7 @@ template class eo1PtBitXover: public eoQuadOp { public: /// The class name. - string className() const { return "eo1PtBitXover"; } + virtual string className() const { return "eo1PtBitXover"; } /** * 1-point crossover for binary chromosomes. @@ -302,7 +302,7 @@ template class eoUBitXover: public eoQuadOp runtime_error("UxOver --> invalid preference"); } /// The class name. - string className() const { return "eoUBitXover"; } + virtual string className() const { return "eoUBitXover"; } /** * Uniform crossover for binary chromosomes. @@ -348,7 +348,7 @@ template class eoNPtsBitXover: public eoQuadOp } /// The class name. - string className() const { return "eoNPtsBitXover"; } + virtual string className() const { return "eoNPtsBitXover"; } /** * n-point crossover for binary chromosomes. @@ -416,7 +416,7 @@ template class eoBitGxOver: public eoQuadOp } /// The class name - string className() const { return "eoBitGxOver"; } + virtual string className() const { return "eoBitGxOver"; } /** * Gene crossover for binary chromosomes. diff --git a/eo/test/t-eoGenOp.cpp b/eo/test/t-eoGenOp.cpp index b91ab576..d885b7cb 100644 --- a/eo/test/t-eoGenOp.cpp +++ b/eo/test/t-eoGenOp.cpp @@ -112,8 +112,8 @@ class one2threeOp : public eoGenOp // :-) void apply(eoPopulator& _plop) { EOT& eo = *_plop; // select the guy - ++_plop; // advance + ++_plop; // advance _plop.insert("v(" + eo.s + ", 1)"); ++_plop; _plop.insert("v(" + eo.s + ", 2)"); @@ -126,7 +126,7 @@ class one2threeOp : public eoGenOp // :-) class two2oneOp : public eoGenOp // :-) { - public: + public: unsigned max_production(void) { return 1; } void apply(eoPopulator& _plop) @@ -139,6 +139,29 @@ class two2oneOp : public eoGenOp // :-) virtual string className() {return "two2oneOp";} }; +class three2threeOp : public eoGenOp // :-) +{ + public: + unsigned max_production(void) { return 3; } + + void apply(eoPopulator& _plop) + { + EOT& eo1 = *_plop; // select 1st guy + EOT& eo2 = *++_plop; // select 2nd guy + EOT& eo3 = *++_plop; // select 3rd guy + EOT a = eo1; + EOT b = eo2; + EOT c = eo3; + cout << "les selectionnes: a=" << a << " et b=" << b << " et c=" << c << endl; + eo1.s = "323-1(" + a.s + ", " + b.s + ", " + c.s + ")"; + eo2.s = "323-2(" + a.s + ", " + b.s + ", " + c.s + ")"; + eo3.s = "323-3(" + a.s + ", " + b.s + ", " + c.s + ")"; + // oh right, and invalidate fitnesses + cout << "les enfants: a=" << eo1 << " et b=" << eo2 << " et c=" << eo3 << endl; + } + virtual string className() {return "three2threeOp";} +}; + // dummy intialization. Re-init if no pSize, resize first if pSize void init(eoPop & _pop, unsigned _pSize) @@ -191,6 +214,7 @@ int the_main(int argc, char **argv) // our own operator one2threeOp o2t; two2oneOp t2o; + three2threeOp t2t; // a selector @@ -212,12 +236,16 @@ int the_main(int argc, char **argv) // with one2three op eoSequentialOp sOp2; sOp2.add(o2t, 1); - sOp2.add(quad, 1); + // sOp2.add(quad, 1); + // with three2three op eoSequentialOp sOp3; - // sOp3.add(t2o, 1); - sOp3.add(bin, 1); - sOp3.add(quad, 1); + sOp3.add(t2t, 1); + +// eoSequentialOp sOp3; +// sOp3.add(t2o, 1); +// sOp3.add(bin, 1); +// sOp3.add(quad, 1); // try adding quads and bins to see what results you'll get // now a sequential selection that is a simple "addition" @@ -243,7 +271,7 @@ int the_main(int argc, char **argv) // To simulate SGA: first a prop between quadOp and quadClone eoProportionalOp pSGAOp; - pSGAOp.add(bin, 0.8); + pSGAOp.add(quad, 0.8); pSGAOp.add(quadclone, 0.2); // sequential selection between pSGAOp and mon eoSequentialOp virtualSGA; @@ -258,6 +286,7 @@ int the_main(int argc, char **argv) while (offspring.size() < pop.size()) { virtualSGA(popit); + cout << "SeqPopulator boucle et incremente\n"; ++popit; } } @@ -284,6 +313,7 @@ int the_main(int argc, char **argv) while (offspring.size() < 2*pop.size()) { virtualSGA(it_step3); + cout << "SelectPopulator boucle et incremente\n"; ++it_step3; } @@ -310,6 +340,36 @@ int the_main(int argc, char **argv) // ok, now print cout << "Apres Quad+Mon ds un eoSelectivePopulator\n" << pop << endl; + // On teste 1->3 + init(pop, pSize); + eoSelectivePopulator it_step5(pop, offspring, seqSelect); + while (offspring.size() < 2*pop.size()) + { + sOp2(it_step5); + ++it_step5; + } + + swap(pop, offspring); + offspring.clear(); + + // ok, now print + cout << "Apres 1->3 seul ds un eoSelectivePopulator\n" << pop << endl; + + // On teste 3->3 + init(pop, pSize); + eoSelectivePopulator it_step6(pop, offspring, seqSelect); + while (offspring.size() < 2*pop.size()) + { + sOp3(it_step6); + ++it_step6; + } + + swap(pop, offspring); + offspring.clear(); + + // ok, now print + cout << "Apres 3->3 seul ds un eoSelectivePopulator\n" << pop << endl; + return 1; } diff --git a/eo/tutorial/Lesson3/exercise3.1.cpp b/eo/tutorial/Lesson3/exercise3.1.cpp index a4d0609e..3bdab13d 100644 --- a/eo/tutorial/Lesson3/exercise3.1.cpp +++ b/eo/tutorial/Lesson3/exercise3.1.cpp @@ -238,7 +238,11 @@ void main_function(int argc, char **argv) eoCombinedContinue continuator(genCont); continuator.add(steadyCont); // continuator.add(fitCont); - + // Ctrl C signal handling: don't know if that works in MSC ... +#ifndef _MSC_VER + eoCtrlCContinue ctrlC; + continuator.add(ctrlC); +#endif // CHECKPOINT // but now you want to make many different things every generation diff --git a/eo/tutorial/Templates/crossover.tmpl b/eo/tutorial/Templates/crossover.tmpl deleted file mode 100644 index db1d054f..00000000 --- a/eo/tutorial/Templates/crossover.tmpl +++ /dev/null @@ -1,71 +0,0 @@ -Template for simple operators -============================= - -=========================================================================== -eoQuad : crossover operators that take 2 parents and modify both -====== - -template class eoMyDerivedQuadOp: public eoQuadOp -{ -public: - /** - * (Default) Constructor. - */ - eoMyDerivedQuadOp(paramType _anyParameter) : - anyParameter(_anyParameter) {} - - /// The class name. Used to display statistics - string className() const { return "eoMyDerivedQuadOp"; } - - /** - * eoQuad crossover - modifies both parents - * @param Indi1 The first parent - * @param Indi2 The first parent - */ - bool operator()(Indi& Indi1, Indi& Indi2) - { - // do whatever needs to be done - - // if at least one individual has been modified - return true; - // otherwise - // return false; - } - -protected: - paramType anyParameter -}; - -=========================================================================== -eoBin : crossover operators that take 2 parents and modify the first one -===== - -template class eoMyDerivedBinOp: public eoBinOp -{ -public: - /** - * (Default) Constructor. - */ - eoMyDerivedBinOp(paramType _anyParameter) : - anyParameter(_anyParameter) {} - - /// The class name. Used to display statistics - string className() const { return "eoMyDerivedBinOp"; } - - /** - * eoQuad crossover - modifies first parent only - * @param Indi1 The first parent - * @param Indi2 The first parent - const - */ - bool operator()(Indi& Indi1, const Indi& Indi2) - { - // do whatever needs to be done - // if Indi1 has been modified - return true; - // otherwise - // return false; - } - -protected: - paramType anyParameter -}; diff --git a/eo/tutorial/Templates/genop.tmpl b/eo/tutorial/Templates/genop.tmpl deleted file mode 100644 index 397f3125..00000000 --- a/eo/tutorial/Templates/genop.tmpl +++ /dev/null @@ -1,172 +0,0 @@ -Template for simple operators -============================= - -=========================================================================== -eoGen : general operator that takes any number of parents and generates -====== any number of offspring - -First, a GenOp that creates more offspring than there are parents ------------------------------------------------------------------ - -template class eoMyDerivedGrowGenOp: public eoGenOp -{ -public: - /** - * (Default) Constructor. - */ - eoMyDerivedGrowGenOp(paramType _anyParameter) : - anyParameter(_anyParameter) {} - - /// The class name. Used to display statistics - string className() const { return "eoMyDerivedGrowGenOp"; } - - /// The TOTAL number of offspring (including modified parents) - unsigned max_production(void) { return NbOffspring; } - - /** - * eoGrowGen operator - eventually modifies the parents - * BUT does generate more offspring - * - * @param _pop a POPULATOR (not a simple population) - */ - void apply(eoPopulator& _plop) - { - EOT& parent1 = *_plop; // select the first parent - ++_plop; // advance once for each selected parents - ... - EOT& parentN = *_plop; // select the last parent - // don't advance after the last one: _plop always - // points to the last that has already been treated - - // apply operator to the parents (modifying them AND generating - // new individuals ofs1, ofs2, ..., ofsN - _plop.insert(ofs1); - ++_plop; // advance after each insertion - ... - _plop.insert(ofsN); - // don't advance after the last one: _plop always - // points to the last that has already been treated - - // oh right, and invalidate fitnesses of modified parents - parent1.invalidate(); - ... - parentN.invalidate(); - } - - -protected: - paramType anyParameter -}; - -=========================================================================== -Now, a GenOp that creates less offspring than there are parents ---------------------------------------------------------------- -First version, get parents from populator, and erase some of them ------------------------------------------------------------------ - -template class eoMyDerivedShrink1GenOp: public eoGenOp -{ -public: - /** - * (Default) Constructor. - */ - eoMyDerivedShrink1GenOp(paramType _anyParameter) : - anyParameter(_anyParameter) {} - - /// The class name. Used to display statistics - string className() const { return "eoMyDerivedShrink1GenOp"; } - - /// The TOTAL number of offspring (here = nb of remaining modified parents) - unsigned max_production(void) { return NbLeftParents; } - - /** - * eoShrinkGen operator - modifies some of the parents - * and kills the rest - * - * @param _pop a POPULATOR (not a simple population) - */ - void apply(eoPopulator& _plop) - { - EOT& parent1 = *_plop; // select the first parent - ++_plop; // advance once for each selected parents - ... - EOT& parentN = *_plop; // select the last parent - // don't advance after the last one: _plop always - // points to the last that has already been treated - - // modify some of the parents (the first ones - // as erase removes backwards) - ... - - // then kill the ones that are now useless - _plop.erase(); // as many times as necessary - ... - // oh right, and invalidate fitnesses of remaining modified parents - parent1.invalidate(); - ... - parentK.invalidate(); - } - - -protected: - paramType anyParameter -}; - -=========================================================================== -Now, a GenOp that creates less offspring than there are parents ---------------------------------------------------------------- -Second version, get parents from an external selector (no erasing) ------------------------------------------------------------------- - -template class eoMyDerivedShrink2GenOp: public eoGenOp -{ -public: - /** - * (Default) Constructor. - */ - eoMyDerivedShrink2GenOp(eoSelectOne & _sel, paramType _anyParameter) : - sel(_sel), anyParameter(_anyParameter) {} - - /// The class name. Used to display statistics - string className() const { return "eoMyDerivedShrink2GenOp"; } - - /// The TOTAL number of offspring (here = nb of parents modified in place) - unsigned max_production(void) { return NbLeftParents; } - - /** - * eoShrinkGen operator - modifies some parents in the populator - * using extra "parents" selected from an external selector - * - * @param _pop a POPULATOR (not a simple population) - */ - void apply(eoPopulator& _plop) - { - EOT& parent1 = *_plop; // select the first parent - ++_plop; // advance once for each selected parents - ... - EOT& parentN = *_plop; // select the last parent you need - // don't advance after the last one: _plop always - // points to the last that has already been treated - - // get extra parents - use private selector - // _plop.source() is the eoPop used by _plop to get parents - // WARNING: you are not allowed to modify them (mandatory "const") - const EOT& parentN+1 = sel(_plop.source()); - ... - const EOT& parentM = sel(_plop.source()); - - // modify (in place) the "true" parents - // (i.e. parent1, ..., parentsN) - ... - - // invalidate fitnesses of modified parents - parent1.invalidate(); - ... - parentN.invalidate(); - } - - -protected: - eoSelectoIne & sel; - paramType anyParameter -};