The BIG change of general operator interface

I also changed
  - the eoQuadratic into eoQuad (as dicussed with Maarten)
  - the eoBin into eoBit, with more appropriate names for the "binary"
    operators (that can be unary!) as no one protested when I posted on
    eodev list
This commit is contained in:
evomarc 2001-02-09 05:09:26 +00:00
commit 415b419671
60 changed files with 2034 additions and 940 deletions

View file

@ -5,4 +5,4 @@
###############################################################################
libeoincdir = $(includedir)/eo/ga
libeoinc_HEADERS = eoBin.h eoBitOp.h eoBitOpFactory.h
libeoinc_HEADERS = eoBit.h eoBitOp.h eoBitOpFactory.h

View file

@ -1,5 +1,5 @@
/*
eoBin.h
eoBit.h
(c) GeNeura Team 1998, Marc Schoenauer 2000
This library is free software; you can redistribute it and/or
@ -24,10 +24,14 @@
Added the calls to base class I/O routines that print the fitness
Left printing/reading of the size of the bitstring,
for backward compatibility, and as it is a general practice in EO
MS, Feb. 7, 2001
replaced all ...Bin... names with ...Bit... names - for bitstring
as it was ambiguous with bin...ary things
*/
#ifndef eoBin_h
#define eoBin_h
#ifndef eoBit_h
#define eoBit_h
//-----------------------------------------------------------------------------
@ -43,12 +47,12 @@
Various functions for a bitstring representation
*/
/** eoBin: implementation of binary chromosome.
\class eoBin eoBin.h ga/eoBin.h
/** eoBit: implementation of bitstring chromosome.
\class eoBit eoBit.h ga/eoBit.h
\ingroup bitstring
* based on STL's vector<bool> specialization.
*/
template <class FitT> class eoBin: public eoFixedLength<FitT, bool>
template <class FitT> class eoBit: public eoFixedLength<FitT, bool>
{
public:
@ -56,13 +60,13 @@ template <class FitT> class eoBin: public eoFixedLength<FitT, bool>
* (Default) Constructor.
* @param size Size of the binary string.
*/
eoBin(unsigned size = 0, bool value = false):
eoBit(unsigned size = 0, bool value = false):
eoFixedLength<FitT, bool>(size, value) {}
/// My class name.
string className() const
{
return "eoBin";
return "eoBit";
}
/**
@ -99,4 +103,4 @@ template <class FitT> class eoBin: public eoFixedLength<FitT, bool>
//-----------------------------------------------------------------------------
#endif //eoBin_h
#endif //eoBit_h

View file

@ -40,7 +40,7 @@
// chrom[i] = (chrom[i]) ? false : true;
// we were calling something like
// specific_mutate(chrom[i])
// all mutation would also be generic ... except those eoBinNext and eoBinPrev
// 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
@ -57,19 +57,19 @@
#include <algorithm> // swap_ranges
#include <utils/eoRNG.h>
#include <eoInit.h> // eoMonOp
#include <ga/eoBin.h>
#include <ga/eoBit.h>
/** eoBinBitFlip --> changes 1 bit
\class eoBinBitFlip eoBitOp.h ga/eoBitOp.h
/** eoBitFlip --> changes 1 bit
\class eoBitBitFlip eoBitOp.h ga/eoBitOp.h
\ingroup bitstring
*/
template<class Chrom> class eoBinBitFlip: public eoMonOp<Chrom>
template<class Chrom> class eoOneBitFlip: public eoMonOp<Chrom>
{
public:
/// The class name.
string className() const { return "eoBinBitFlip"; }
string className() const { return "eoOneBitFlip"; }
/**
* Change one bit.
@ -77,9 +77,9 @@ template<class Chrom> class eoBinBitFlip: public eoMonOp<Chrom>
*/
void operator()(Chrom& chrom)
{
chrom.invalidate();
unsigned i = rng.random(chrom.size());
unsigned i = eo::rng.random(chrom.size());
chrom[i] = (chrom[i]) ? false : true;
chrom.invalidate();
}
};
@ -94,7 +94,7 @@ template<class Chrom> class eoDetBitFlip: public eoMonOp<Chrom>
/**
* (Default) Constructor.
* @param _num_bit The number of bits to change
* default is one - equivalent to eoBinBitFlip then
* default is one - equivalent to eoOneBitFlip then
*/
eoDetBitFlip(const unsigned& _num_bit = 1): num_bit(_num_bit) {}
@ -107,35 +107,35 @@ template<class Chrom> class eoDetBitFlip: public eoMonOp<Chrom>
*/
void operator()(Chrom& chrom)
{
chrom.invalidate();
// does not check for duplicate: if someone volunteers ....
for (unsigned k=0; k<num_bit; k++)
{
unsigned i = rng.random(chrom.size());
unsigned i = eo::rng.random(chrom.size());
chrom[i] = (chrom[i]) ? false : true;
}
chrom.invalidate();
}
private:
unsigned num_bit;
};
/** eoBinMutation --> classical mutation
\class eoBinMutation eoBitOp.h ga/eoBitOp.h
/** eoBitMutation --> classical mutation
\class eoBitMutation eoBitOp.h ga/eoBitOp.h
\ingroup bitstring
*/
template<class Chrom> class eoBinMutation: public eoMonOp<Chrom>
template<class Chrom> class eoBitMutation: public eoMonOp<Chrom>
{
public:
/**
* (Default) Constructor.
* @param _rate Rate of mutation.
*/
eoBinMutation(const double& _rate = 0.01): rate(_rate) {}
eoBitMutation(const double& _rate = 0.01): rate(_rate) {}
/// The class name.
string className() const { return "eoBinMutation"; }
string className() const { return "eoBitMutation"; }
/**
* Mutate a chromosome.
@ -145,7 +145,7 @@ template<class Chrom> class eoBinMutation: public eoMonOp<Chrom>
{
bool changed_something = false;
for (unsigned i = 0; i < chrom.size(); i++)
if (rng.flip(rate))
if (eo::rng.flip(rate))
{
chrom[i] = !chrom[i];
changed_something = true;
@ -160,16 +160,16 @@ template<class Chrom> class eoBinMutation: public eoMonOp<Chrom>
};
/** eoBinInversion: inverts the bits of the chromosome between an interval
\class eoBinInversion eoBitOp.h ga/eoBitOp.h
/** eoBitInversion: inverts the bits of the chromosome between an interval
\class eoBitInversion eoBitOp.h ga/eoBitOp.h
\ingroup bitstring
*/
template<class Chrom> class eoBinInversion: public eoMonOp<Chrom>
template<class Chrom> class eoBitInversion: public eoMonOp<Chrom>
{
public:
/// The class name.
string className() const { return "eoBinInversion"; }
string className() const { return "eoBitInversion"; }
/**
* Inverts a range of bits in a binary chromosome.
@ -178,8 +178,8 @@ template<class Chrom> class eoBinInversion: public eoMonOp<Chrom>
void operator()(Chrom& chrom)
{
unsigned u1 = rng.random(chrom.size() + 1) , u2;
do u2 = rng.random(chrom.size() + 1); while (u1 == u2);
unsigned u1 = eo::rng.random(chrom.size() + 1) , u2;
do u2 = eo::rng.random(chrom.size() + 1); while (u1 == u2);
unsigned r1 = min(u1, u2), r2 = max(u1, u2);
reverse(chrom.begin() + r1, chrom.begin() + r2);
@ -188,16 +188,16 @@ template<class Chrom> class eoBinInversion: public eoMonOp<Chrom>
};
/** eoBinNext --> next binary value
\class eoBinNext eoBitOp.h ga/eoBitOp.h
/** eoBitNext --> next value when bitstring considered as binary value
\class eoBitNext eoBitOp.h ga/eoBitOp.h
\ingroup bitstring
*/
template<class Chrom> class eoBinNext: public eoMonOp<Chrom>
template<class Chrom> class eoBitNext: public eoMonOp<Chrom>
{
public:
/// The class name.
string className() const { return "eoBinNext"; }
string className() const { return "eoBitNext"; }
/**
* Change the bit string x to be x+1.
@ -222,16 +222,16 @@ template<class Chrom> class eoBinNext: public eoMonOp<Chrom>
};
/** eoBinPrev --> previous binary value
\class eoBinPrev eoBitOp.h ga/eoBitOp.h
/** eoBitPrev --> previous value when bitstring treated as binary value
\class eoBitPrev eoBitOp.h ga/eoBitOp.h
\ingroup bitstring
*/
template<class Chrom> class eoBinPrev: public eoMonOp<Chrom>
template<class Chrom> class eoBitPrev: public eoMonOp<Chrom>
{
public:
/// The class name.
string className() const { return "eoBinPrev"; }
string className() const { return "eoBitPrev"; }
/**
* Change the bit string x to be x-1.
@ -256,16 +256,16 @@ template<class Chrom> class eoBinPrev: public eoMonOp<Chrom>
};
/** eoBinCrossover --> classic 1-point crossover
\class eoBinCrossover eoBitOp.h ga/eoBitOp.h
/** eo1PtBitXover --> classic 1-point crossover
\class eo1PtBitCrossover eoBitOp.h ga/eoBitOp.h
\ingroup bitstring
*/
template<class Chrom> class eoBinCrossover: public eoQuadraticOp<Chrom>
template<class Chrom> class eo1PtBitXover: public eoQuadOp<Chrom>
{
public:
/// The class name.
string className() const { return "eoBinCrossover"; }
string className() const { return "eo1PtBitXover"; }
/**
* 1-point crossover for binary chromosomes.
@ -274,7 +274,7 @@ template<class Chrom> class eoBinCrossover: public eoQuadraticOp<Chrom>
*/
void operator()(Chrom& chrom1, Chrom& chrom2)
{
unsigned site = rng.random(min(chrom1.size(), chrom2.size()));
unsigned site = eo::rng.random(min(chrom1.size(), chrom2.size()));
if (!std::equal(chrom1.begin(), chrom1.begin()+site, chrom2.begin()))
{
@ -288,22 +288,22 @@ template<class Chrom> class eoBinCrossover: public eoQuadraticOp<Chrom>
};
/** eoBinUxOver --> classic Uniform crossover
\class eoBinNxOver eoBitOp.h ga/eoBitOp.h
/** eoUBitXover --> classic Uniform crossover
\class eoUBitXover eoBitOp.h ga/eoBitOp.h
\ingroup bitstring
*/
template<class Chrom> class eoBinUxOver: public eoQuadraticOp<Chrom>
template<class Chrom> class eoUBitXover: public eoQuadOp<Chrom>
{
public:
/// (Default) Constructor.
eoBinUxOver(const float& _preference = 0.5): preference(_preference)
eoUBitXover(const float& _preference = 0.5): preference(_preference)
{
if ( (_preference <= 0.0) || (_preference >= 1.0) )
runtime_error("UxOver --> invalid preference");
}
/// The class name.
string className() const { return "eoBinUxOver"; }
string className() const { return "eoUBitXover"; }
/**
* Uniform crossover for binary chromosomes.
@ -318,7 +318,7 @@ template<class Chrom> class eoBinUxOver: public eoQuadraticOp<Chrom>
bool changed = false;
for (unsigned int i=0; i<chrom1.size(); i++)
{
if (rng.flip(preference))
if (eo::rng.flip(preference))
{
bool tmp = chrom1[i];
chrom1[i]=chrom2[i];
@ -337,23 +337,23 @@ template<class Chrom> class eoBinUxOver: public eoQuadraticOp<Chrom>
};
/** eoBinNxOver --> n-point crossover
\class eoBinNxOver eoBitOp.h ga/eoBitOp.h
/** eoNPtsBitXover --> n-point crossover
\class eoNPtsBitXover eoBitOp.h ga/eoBitOp.h
\ingroup bitstring
*/
template<class Chrom> class eoBinNxOver: public eoQuadraticOp<Chrom>
template<class Chrom> class eoNPtsBitXover: public eoQuadOp<Chrom>
{
public:
/// (Default) Constructor.
eoBinNxOver(const unsigned& _num_points = 2): num_points(_num_points)
eoNPtsBitXover(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"; }
string className() const { return "eoNPtsBitXover"; }
/**
* n-point crossover for binary chromosomes.
@ -369,7 +369,7 @@ template<class Chrom> class eoBinNxOver: public eoQuadraticOp<Chrom>
// select ranges of bits to swap
do {
unsigned bit = rng.random(max_size) + 1;
unsigned bit = eo::rng.random(max_size) + 1;
if (points[bit])
continue;
else
@ -401,16 +401,18 @@ template<class Chrom> class eoBinNxOver: public eoQuadraticOp<Chrom>
/** eoBinGxOver --> gene crossover
\class eoBinGxOver eoBitOp.h ga/eoBitOp.h
/** eoBitGxOver --> Npts crossover when bistring considered
as a string of binary-encoded genes (exchanges genes)
Is anybody still using it apart from historians ??? :-)
\class eoBitGxOver eoBitOp.h ga/eoBitOp.h
\ingroup bitstring
*/
template<class Chrom> class eoBinGxOver: public eoQuadraticOp<Chrom>
template<class Chrom> class eoBitGxOver: public eoQuadOp<Chrom>
{
public:
/// Constructor.
eoBinGxOver(const unsigned _gene_size, const unsigned _num_points = 2):
eoBitGxOver(const unsigned _gene_size, const unsigned _num_points = 2):
gene_size(_gene_size), num_points(_num_points)
{
if (gene_size < 1)
@ -420,7 +422,7 @@ template<class Chrom> class eoBinGxOver: public eoQuadraticOp<Chrom>
}
/// The class name
string className() const { return "eoBinGxOver"; }
string className() const { return "eoBitGxOver"; }
/**
* Gene crossover for binary chromosomes.
@ -436,7 +438,7 @@ template<class Chrom> class eoBinGxOver: public eoQuadraticOp<Chrom>
// selects genes to swap
do {
unsigned bit = rng.random(max_genes);
unsigned bit = eo::rng.random(max_genes);
if (points[bit])
continue;
else

View file

@ -26,8 +26,8 @@
#ifndef _EOBITOPFACTORY_H
#define _EOBITOPFACTORY_H
#include <eoOpFactory.h>
#include <eoBitOp.h>
#include <eoFactory.h>
#include <ga/eoBitOp.h>
//-----------------------------------------------------------------------------
@ -36,7 +36,7 @@ on bitstring chromosomes. Only those chromosomes can instantiate the operators
that are created here
@see eoSelect*/
template< class EOT>
class eoBitOpFactory: public eoOpFactory<EOT>
class eoBitOpFactory: public eoFactory<EOT>
{
public:
@ -64,49 +64,97 @@ public:
{
eoOp<EOT> * opPtr = NULL;
try {
opPtr = eoOpFactory<EOT>::make( _is );
opPtr = eoFactory<EOT>::make( _is );
} catch ( const string& objectTypeStr ) {
if ( objectTypeStr == "eoBinRandom") {
opPtr = new eoBinRandom<EOT>();
}
if ( objectTypeStr == "eoBinBitFlip" ) {
opPtr = new eoBinBitFlip<EOT>( );
opPtr = new eoOneBitFlip<EOT>( );
}
// handles old operator names as well as new ones
if ( objectTypeStr == "eoOneBitFlip" ) {
opPtr = new eoOneBitFlip<EOT>( );
}
// Standard BitFilp Mutation
if ( objectTypeStr == "eoBinMutation" ) {
float rate;
_is >> rate;
opPtr = new eoBinMutation<EOT>( rate );
opPtr = new eoBitMutation<EOT>( rate );
}
if ( objectTypeStr == "eoBitMutation" ) {
float rate;
_is >> rate;
opPtr = new eoBitMutation<EOT>( rate );
}
// Bit inversion
if ( objectTypeStr == "eoBinInversion" ) {
opPtr = new eoBinInversion<EOT>( );
opPtr = new eoBitInversion<EOT>( );
}
if ( objectTypeStr == "eoBitInversion" ) {
opPtr = new eoBitInversion<EOT>( );
}
// Next binary value
if ( objectTypeStr == "eoBinNext" ) {
opPtr = new eoBinNext<EOT>( );
opPtr = new eoBitNext<EOT>( );
}
if ( objectTypeStr == "eoBitNext" ) {
opPtr = new eoBitNext<EOT>( );
}
// Previous binary value
if ( objectTypeStr == "eoBinPrev" ) {
opPtr = new eoBinPrev<EOT>( );
opPtr = new eoBitPrev<EOT>( );
}
if ( objectTypeStr == "eoBinNext" ) {
opPtr = new eoBinNext<EOT>( );
if ( objectTypeStr == "eoBitPrev" ) {
opPtr = new eoBitPrev<EOT>( );
}
// 1 point Xover
if ( objectTypeStr == "eoBinCrossover" ) {
opPtr = new eoBinCrossover<EOT>( );
opPtr = new eo1PtBitXover<EOT>( );
}
if ( objectTypeStr == "eo1PtBitXover" ) {
opPtr = new eo1PtBitXover<EOT>( );
}
// Npts Xover
if ( objectTypeStr == "eoBinNxOver" ) {
unsigned nPoints;
_is >> nPoints;
opPtr = new eoBinNxOver<EOT>( nPoints );
opPtr = new eoNPtsBitXover<EOT>( nPoints );
}
if ( objectTypeStr == "eoNPtsBitXover" ) {
unsigned nPoints;
_is >> nPoints;
opPtr = new eoNPtsBitXover<EOT>( nPoints );
}
// Gene Xover (obsolete)
if ( objectTypeStr == "eoBinGxOver" ) {
unsigned geneSize, nPoints;
_is >> geneSize >> nPoints;
opPtr = new eoBinGxOver<EOT>( geneSize, nPoints );
opPtr = new eoBitGxOver<EOT>( geneSize, nPoints );
}
if ( objectTypeStr == "eoBitGxOver" ) {
unsigned geneSize, nPoints;
_is >> geneSize >> nPoints;
opPtr = new eoBitGxOver<EOT>( geneSize, nPoints );
}
// Uniform Xover
if ( objectTypeStr == "eoBinUxOver" ) {
float rate;
_is >> rate;
opPtr = new eoBinUxOver<EOT>( rate );
opPtr = new eoUBitXover<EOT>( rate );
}
if ( objectTypeStr == "eoUBitXover" ) {
float rate;
_is >> rate;
opPtr = new eoUBitXover<EOT>( rate );
}
// nothing read!
if ( !opPtr ) { // to be caught by the upper level
throw objectTypeStr;
}