dded Uniform Crossover (!) and "deterministic" bit-filp mutation

This commit is contained in:
evomarc 2000-10-24 03:58:02 +00:00
commit 5e33a2e50b

View file

@ -1,3 +1,50 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
//-----------------------------------------------------------------------------
// eoBitOp.h
// (c) GeNeura Team, 2000 - EEAAX 2000 - 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
*/
//-----------------------------------------------------------------------------
// 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 eoBinNext and eoBinPrev
// 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
//-----------------------------------------------------------------------------
@ -12,7 +59,8 @@
#include <eoInit.h> // eoMonOp
#include <ga/eoBin.h>
/** eoBinBitFlip --> changes a bit
/** eoBinBitFlip --> changes 1 bit
\class eoBinBitFlip eoBitOp.h ga/eoBitOp.h
\ingroup bitstring
*/
@ -35,6 +83,42 @@ template<class Chrom> class eoBinBitFlip: public eoMonOp<Chrom>
}
};
/** eoDetBitFlip --> changes exactly k bits
\class eoDetBitFlip eoBitOp.h ga/eoBitOp.h
\ingroup bitstring
*/
template<class Chrom> class eoDetBitFlip: public eoMonOp<Chrom>
{
public:
/**
* (Default) Constructor.
* @param _num_bit The number of bits to change
* default is one - equivalent to eoBinBitFlip then
*/
eoDetBitFlip(const unsigned& _num_bit = 1): num_bit(_num_bit) {}
/// The class name.
string className() const { return "eoDetBitFlip"; }
/**
* Change num_bit bits.
* @param chrom The cromosome which one bit is going to be changed.
*/
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());
chrom[i] = (chrom[i]) ? false : true;
}
}
private:
unsigned num_bit;
};
/** eoBinMutation --> classical mutation
\class eoBinMutation eoBitOp.h ga/eoBitOp.h
@ -172,7 +256,7 @@ template<class Chrom> class eoBinPrev: public eoMonOp<Chrom>
};
/** eoBinCrossover --> classic 2-point crossover
/** eoBinCrossover --> classic 1-point crossover
\class eoBinCrossover eoBitOp.h ga/eoBitOp.h
\ingroup bitstring
*/
@ -184,7 +268,7 @@ template<class Chrom> class eoBinCrossover: public eoQuadraticOp<Chrom>
string className() const { return "eoBinCrossover"; }
/**
* 2-point crossover for binary chromosomes.
* 1-point crossover for binary chromosomes.
* @param chrom1 The first chromosome.
* @param chrom2 The first chromosome.
*/
@ -204,6 +288,55 @@ template<class Chrom> class eoBinCrossover: public eoQuadraticOp<Chrom>
};
/** eoBinUxOver --> classic Uniform crossover
\class eoBinNxOver eoBitOp.h ga/eoBitOp.h
\ingroup bitstring
*/
template<class Chrom> class eoBinUxOver: public eoQuadraticOp<Chrom>
{
public:
/// (Default) Constructor.
eoBinUxOver(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"; }
/**
* Uniform crossover for binary chromosomes.
* @param chrom1 The first chromosome.
* @param chrom2 The first chromosome.
* @runtime_error if sizes don't match
*/
void operator()(Chrom& chrom1, Chrom& chrom2)
{
if ( chrom1.size() != chrom2.size())
runtime_error("UxOver --> chromosomes sizes don't match" );
bool changed = false;
for (unsigned int i=0; i<chrom1.size(); i++)
{
if (rng.flip(preference))
{
bool tmp = chrom1[i];
chrom1[i]=chrom2[i];
chrom2[i] = tmp;
changed = true;
}
}
if (changed)
{
chrom1.invalidate();
chrom2.invalidate();
}
}
private:
float preference;
};
/** eoBinNxOver --> n-point crossover
\class eoBinNxOver eoBitOp.h ga/eoBitOp.h
\ingroup bitstring
@ -212,7 +345,7 @@ template<class Chrom> class eoBinCrossover: public eoQuadraticOp<Chrom>
template<class Chrom> class eoBinNxOver: public eoQuadraticOp<Chrom>
{
public:
/// (Defualt) Constructor.
/// (Default) Constructor.
eoBinNxOver(const unsigned& _num_points = 2): num_points(_num_points)
{
if (num_points < 1)