Added the signal handling - see eoCtrlCContinue.h
I've disabled it in case of MSC as I don't know if this works there ... Also added a couple of "virtual" in the ga dir
This commit is contained in:
parent
4cb797544a
commit
ddc6650ce5
8 changed files with 180 additions and 265 deletions
|
|
@ -80,7 +80,9 @@
|
||||||
#include <eoGenContinue.h>
|
#include <eoGenContinue.h>
|
||||||
#include <eoSteadyFitContinue.h>
|
#include <eoSteadyFitContinue.h>
|
||||||
#include <eoFitContinue.h>
|
#include <eoFitContinue.h>
|
||||||
|
#ifndef _MSC_VER
|
||||||
|
#include <eoCtrlCContinue.h> // CtrlC handling (using 2 global variables!)
|
||||||
|
#endif
|
||||||
// Selection
|
// Selection
|
||||||
// the eoSelectOne's
|
// the eoSelectOne's
|
||||||
#include <eoRandomSelect.h> // also contains the eoSequentialSelect
|
#include <eoRandomSelect.h> // also contains the eoSequentialSelect
|
||||||
|
|
|
||||||
92
eo/src/eoCtrlCContinue.h
Normal file
92
eo/src/eoCtrlCContinue.h
Normal file
|
|
@ -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 <signal.h>
|
||||||
|
#include <eoContinue.h>
|
||||||
|
|
||||||
|
// --- 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<EOT>
|
||||||
|
{
|
||||||
|
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<EOT>& _vEO )
|
||||||
|
{
|
||||||
|
if (arret_demande)
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
@ -64,7 +64,7 @@ template <class FitT> class eoBit: public eoVector<FitT, bool>
|
||||||
eoVector<FitT, bool>(size, value) {}
|
eoVector<FitT, bool>(size, value) {}
|
||||||
|
|
||||||
/// My class name.
|
/// My class name.
|
||||||
string className() const
|
virtual string className() const
|
||||||
{
|
{
|
||||||
return "eoBit";
|
return "eoBit";
|
||||||
}
|
}
|
||||||
|
|
@ -73,7 +73,7 @@ template <class FitT> class eoBit: public eoVector<FitT, bool>
|
||||||
* To print me on a stream.
|
* To print me on a stream.
|
||||||
* @param os The ostream.
|
* @param os The ostream.
|
||||||
*/
|
*/
|
||||||
void printOn(ostream& os) const
|
virtual void printOn(ostream& os) const
|
||||||
{
|
{
|
||||||
EO<FitT>::printOn(os);
|
EO<FitT>::printOn(os);
|
||||||
os << ' ';
|
os << ' ';
|
||||||
|
|
@ -85,7 +85,7 @@ template <class FitT> class eoBit: public eoVector<FitT, bool>
|
||||||
* To read me from a stream.
|
* To read me from a stream.
|
||||||
* @param is The istream.
|
* @param is The istream.
|
||||||
*/
|
*/
|
||||||
void readFrom(istream& is)
|
virtual void readFrom(istream& is)
|
||||||
{
|
{
|
||||||
EO<FitT>::readFrom(is);
|
EO<FitT>::readFrom(is);
|
||||||
unsigned s;
|
unsigned s;
|
||||||
|
|
|
||||||
|
|
@ -69,7 +69,7 @@ template<class Chrom> class eoOneBitFlip: public eoMonOp<Chrom>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
/// The class name.
|
/// The class name.
|
||||||
string className() const { return "eoOneBitFlip"; }
|
virtual string className() const { return "eoOneBitFlip"; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Change one bit.
|
* Change one bit.
|
||||||
|
|
@ -99,7 +99,7 @@ template<class Chrom> class eoDetBitFlip: public eoMonOp<Chrom>
|
||||||
eoDetBitFlip(const unsigned& _num_bit = 1): num_bit(_num_bit) {}
|
eoDetBitFlip(const unsigned& _num_bit = 1): num_bit(_num_bit) {}
|
||||||
|
|
||||||
/// The class name.
|
/// The class name.
|
||||||
string className() const { return "eoDetBitFlip"; }
|
virtual string className() const { return "eoDetBitFlip"; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Change num_bit bits.
|
* Change num_bit bits.
|
||||||
|
|
@ -135,7 +135,7 @@ template<class Chrom> class eoBitMutation: public eoMonOp<Chrom>
|
||||||
eoBitMutation(const double& _rate = 0.01): rate(_rate) {}
|
eoBitMutation(const double& _rate = 0.01): rate(_rate) {}
|
||||||
|
|
||||||
/// The class name.
|
/// The class name.
|
||||||
string className() const { return "eoBitMutation"; }
|
virtual string className() const { return "eoBitMutation"; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Mutate a chromosome.
|
* Mutate a chromosome.
|
||||||
|
|
@ -168,7 +168,7 @@ template<class Chrom> class eoBitInversion: public eoMonOp<Chrom>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
/// The class name.
|
/// The class name.
|
||||||
string className() const { return "eoBitInversion"; }
|
virtual string className() const { return "eoBitInversion"; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Inverts a range of bits in a binary chromosome.
|
* Inverts a range of bits in a binary chromosome.
|
||||||
|
|
@ -196,7 +196,7 @@ template<class Chrom> class eoBitNext: public eoMonOp<Chrom>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
/// The class name.
|
/// The class name.
|
||||||
string className() const { return "eoBitNext"; }
|
virtual string className() const { return "eoBitNext"; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Change the bit string x to be x+1.
|
* Change the bit string x to be x+1.
|
||||||
|
|
@ -230,7 +230,7 @@ template<class Chrom> class eoBitPrev: public eoMonOp<Chrom>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
/// The class name.
|
/// The class name.
|
||||||
string className() const { return "eoBitPrev"; }
|
virtual string className() const { return "eoBitPrev"; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Change the bit string x to be x-1.
|
* Change the bit string x to be x-1.
|
||||||
|
|
@ -264,7 +264,7 @@ template<class Chrom> class eo1PtBitXover: public eoQuadOp<Chrom>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
/// The class name.
|
/// The class name.
|
||||||
string className() const { return "eo1PtBitXover"; }
|
virtual string className() const { return "eo1PtBitXover"; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 1-point crossover for binary chromosomes.
|
* 1-point crossover for binary chromosomes.
|
||||||
|
|
@ -302,7 +302,7 @@ template<class Chrom> class eoUBitXover: public eoQuadOp<Chrom>
|
||||||
runtime_error("UxOver --> invalid preference");
|
runtime_error("UxOver --> invalid preference");
|
||||||
}
|
}
|
||||||
/// The class name.
|
/// The class name.
|
||||||
string className() const { return "eoUBitXover"; }
|
virtual string className() const { return "eoUBitXover"; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Uniform crossover for binary chromosomes.
|
* Uniform crossover for binary chromosomes.
|
||||||
|
|
@ -348,7 +348,7 @@ template<class Chrom> class eoNPtsBitXover: public eoQuadOp<Chrom>
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The class name.
|
/// The class name.
|
||||||
string className() const { return "eoNPtsBitXover"; }
|
virtual string className() const { return "eoNPtsBitXover"; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* n-point crossover for binary chromosomes.
|
* n-point crossover for binary chromosomes.
|
||||||
|
|
@ -416,7 +416,7 @@ template<class Chrom> class eoBitGxOver: public eoQuadOp<Chrom>
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The class name
|
/// The class name
|
||||||
string className() const { return "eoBitGxOver"; }
|
virtual string className() const { return "eoBitGxOver"; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gene crossover for binary chromosomes.
|
* Gene crossover for binary chromosomes.
|
||||||
|
|
|
||||||
|
|
@ -112,8 +112,8 @@ class one2threeOp : public eoGenOp<EOT> // :-)
|
||||||
void apply(eoPopulator<EOT>& _plop)
|
void apply(eoPopulator<EOT>& _plop)
|
||||||
{
|
{
|
||||||
EOT& eo = *_plop; // select the guy
|
EOT& eo = *_plop; // select the guy
|
||||||
++_plop; // advance
|
|
||||||
|
|
||||||
|
++_plop; // advance
|
||||||
_plop.insert("v(" + eo.s + ", 1)");
|
_plop.insert("v(" + eo.s + ", 1)");
|
||||||
++_plop;
|
++_plop;
|
||||||
_plop.insert("v(" + eo.s + ", 2)");
|
_plop.insert("v(" + eo.s + ", 2)");
|
||||||
|
|
@ -139,6 +139,29 @@ class two2oneOp : public eoGenOp<EOT> // :-)
|
||||||
virtual string className() {return "two2oneOp";}
|
virtual string className() {return "two2oneOp";}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class three2threeOp : public eoGenOp<EOT> // :-)
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
unsigned max_production(void) { return 3; }
|
||||||
|
|
||||||
|
void apply(eoPopulator<EOT>& _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
|
// dummy intialization. Re-init if no pSize, resize first if pSize
|
||||||
void init(eoPop<Dummy> & _pop, unsigned _pSize)
|
void init(eoPop<Dummy> & _pop, unsigned _pSize)
|
||||||
|
|
@ -191,6 +214,7 @@ int the_main(int argc, char **argv)
|
||||||
// our own operator
|
// our own operator
|
||||||
one2threeOp o2t;
|
one2threeOp o2t;
|
||||||
two2oneOp t2o;
|
two2oneOp t2o;
|
||||||
|
three2threeOp t2t;
|
||||||
|
|
||||||
|
|
||||||
// a selector
|
// a selector
|
||||||
|
|
@ -212,12 +236,16 @@ int the_main(int argc, char **argv)
|
||||||
// with one2three op
|
// with one2three op
|
||||||
eoSequentialOp<EOT> sOp2;
|
eoSequentialOp<EOT> sOp2;
|
||||||
sOp2.add(o2t, 1);
|
sOp2.add(o2t, 1);
|
||||||
sOp2.add(quad, 1);
|
// sOp2.add(quad, 1);
|
||||||
|
|
||||||
|
// with three2three op
|
||||||
eoSequentialOp<EOT> sOp3;
|
eoSequentialOp<EOT> sOp3;
|
||||||
// sOp3.add(t2o, 1);
|
sOp3.add(t2t, 1);
|
||||||
sOp3.add(bin, 1);
|
|
||||||
sOp3.add(quad, 1);
|
// eoSequentialOp<EOT> 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
|
// try adding quads and bins to see what results you'll get
|
||||||
|
|
||||||
// now a sequential selection that is a simple "addition"
|
// 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
|
// To simulate SGA: first a prop between quadOp and quadClone
|
||||||
eoProportionalOp<EOT> pSGAOp;
|
eoProportionalOp<EOT> pSGAOp;
|
||||||
pSGAOp.add(bin, 0.8);
|
pSGAOp.add(quad, 0.8);
|
||||||
pSGAOp.add(quadclone, 0.2);
|
pSGAOp.add(quadclone, 0.2);
|
||||||
// sequential selection between pSGAOp and mon
|
// sequential selection between pSGAOp and mon
|
||||||
eoSequentialOp<EOT> virtualSGA;
|
eoSequentialOp<EOT> virtualSGA;
|
||||||
|
|
@ -258,6 +286,7 @@ int the_main(int argc, char **argv)
|
||||||
while (offspring.size() < pop.size())
|
while (offspring.size() < pop.size())
|
||||||
{
|
{
|
||||||
virtualSGA(popit);
|
virtualSGA(popit);
|
||||||
|
cout << "SeqPopulator boucle et incremente\n";
|
||||||
++popit;
|
++popit;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -284,6 +313,7 @@ int the_main(int argc, char **argv)
|
||||||
while (offspring.size() < 2*pop.size())
|
while (offspring.size() < 2*pop.size())
|
||||||
{
|
{
|
||||||
virtualSGA(it_step3);
|
virtualSGA(it_step3);
|
||||||
|
cout << "SelectPopulator boucle et incremente\n";
|
||||||
++it_step3;
|
++it_step3;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -310,6 +340,36 @@ int the_main(int argc, char **argv)
|
||||||
// ok, now print
|
// ok, now print
|
||||||
cout << "Apres Quad+Mon ds un eoSelectivePopulator\n" << pop << endl;
|
cout << "Apres Quad+Mon ds un eoSelectivePopulator\n" << pop << endl;
|
||||||
|
|
||||||
|
// On teste 1->3
|
||||||
|
init(pop, pSize);
|
||||||
|
eoSelectivePopulator<EOT> 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<EOT> 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;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -238,7 +238,11 @@ void main_function(int argc, char **argv)
|
||||||
eoCombinedContinue<Indi> continuator(genCont);
|
eoCombinedContinue<Indi> continuator(genCont);
|
||||||
continuator.add(steadyCont);
|
continuator.add(steadyCont);
|
||||||
// continuator.add(fitCont);
|
// continuator.add(fitCont);
|
||||||
|
// Ctrl C signal handling: don't know if that works in MSC ...
|
||||||
|
#ifndef _MSC_VER
|
||||||
|
eoCtrlCContinue<Indi> ctrlC;
|
||||||
|
continuator.add(ctrlC);
|
||||||
|
#endif
|
||||||
|
|
||||||
// CHECKPOINT
|
// CHECKPOINT
|
||||||
// but now you want to make many different things every generation
|
// but now you want to make many different things every generation
|
||||||
|
|
|
||||||
|
|
@ -1,71 +0,0 @@
|
||||||
Template for simple operators
|
|
||||||
=============================
|
|
||||||
|
|
||||||
===========================================================================
|
|
||||||
eoQuad : crossover operators that take 2 parents and modify both
|
|
||||||
======
|
|
||||||
|
|
||||||
template<class Indi> class eoMyDerivedQuadOp: public eoQuadOp<Indi>
|
|
||||||
{
|
|
||||||
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 Indi> class eoMyDerivedBinOp: public eoBinOp<Indi>
|
|
||||||
{
|
|
||||||
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
|
|
||||||
};
|
|
||||||
|
|
@ -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 Indi> class eoMyDerivedGrowGenOp: public eoGenOp<Indi>
|
|
||||||
{
|
|
||||||
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<EOT>& _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 Indi> class eoMyDerivedShrink1GenOp: public eoGenOp<Indi>
|
|
||||||
{
|
|
||||||
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<EOT>& _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 Indi> class eoMyDerivedShrink2GenOp: public eoGenOp<Indi>
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
/**
|
|
||||||
* (Default) Constructor.
|
|
||||||
*/
|
|
||||||
eoMyDerivedShrink2GenOp(eoSelectOne<EOT> & _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<EOT>& _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<EOT> 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<EOT> & sel;
|
|
||||||
paramType anyParameter
|
|
||||||
};
|
|
||||||
Reference in a new issue