Added changes mainly by Marc
This commit is contained in:
parent
449ed17ff8
commit
91f5ddbdaa
28 changed files with 2400 additions and 1595 deletions
|
|
@ -11,13 +11,14 @@ libeoincdir = $(includedir)/eo
|
|||
libeoinc_HEADERS = EO.h eo eo1d.h eo2d.h eo2dVector.h eoAged.h eoAlgo.h\
|
||||
eoAtomBitFlip.h eoAtomCreep.h eoAtomMutation.h eoAtomMutator.h \
|
||||
eoAtomRandom.h eoBin.h eoBitOp.h eoBitOpFactory.h eoBreeder.h\
|
||||
eoData.h eoDup.h eoESChrom.h eoESFullChrom.h eoESFullMut.h eoEasyEA.h\
|
||||
eoEvalFunc.h eoEvalFuncPtr.h eoEvaluator.h eoFitTerm.h eoFitness.h\
|
||||
eoData.h eoDetTournament.h eoDup.h eoESChrom.h eoESFullChrom.h eoESFullMut.h eoEasyEA.h\
|
||||
eoEvalFunc.h eoEvalFuncPtr.h eoFitTerm.h eoFitness.h\
|
||||
eoGenTerm.h eoGeneration.h eoID.h eoInclusion.h eoInsertion.h\
|
||||
eoKill.h eoLottery.h eoMultiMonOp.h eoMutation.h eoNegExp.h\
|
||||
eoKill.h eoLottery.h eoMerge.h eoMultiBinOp.h eoMultiMonOp.h eoMutation.h eoNegExp.h\
|
||||
eoNonUniform.h eoNormal.h eoObject.h eoOp.h eoOpSelector.h\
|
||||
eoParser.h eoPersistent.h eoPop.h eoPopOps.h eoPrintable.h\
|
||||
eoProblem.h eoProportionalOpSel.h eoRNG.h eoRnd.h eoString.h\
|
||||
eoTerm.h eoTranspose.h eoUniform.h eoVector.h eoXOver2.h
|
||||
eoStochTournament.h eoTerm.h eoTranspose.h eoUniform.h \
|
||||
eoUniformSelect.h eoUniformXOver.h eoVector.h eoXOver2.h
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -48,21 +48,23 @@
|
|||
|
||||
#include <eoOp.h>
|
||||
#include <eoMultiMonOp.h>
|
||||
#include <eoMultiBinOp.h>
|
||||
|
||||
#include <eoDup.h>
|
||||
#include <eoKill.h>
|
||||
#include <eoTranspose.h>
|
||||
#include <eoXOver2.h>
|
||||
#include <eoUniformXOver.h>
|
||||
#include <eoMutation.h>
|
||||
|
||||
#include <eoPop.h>
|
||||
#include <eoPopOps.h>
|
||||
#include <eoMerge.h>
|
||||
#include <eoBitOp.h>
|
||||
|
||||
// Evaluation functions
|
||||
#include <eoEvalFunc.h>
|
||||
#include <eoEvalFuncPtr.h>
|
||||
#include <eoEvaluator.h>
|
||||
|
||||
// Terminators
|
||||
#include <eoTerm.h>
|
||||
|
|
@ -70,6 +72,9 @@
|
|||
#include <eoFitTerm.h>
|
||||
|
||||
// Selection and reproduction stuff
|
||||
#include <eoUniformSelect.h>
|
||||
#include <eoDetTournament.h>
|
||||
#include <eoStochTournament.h>
|
||||
#include <eoLottery.h>
|
||||
#include <eoBreeder.h>
|
||||
#include <eoInsertion.h>
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoAtomCreep.h
|
||||
// eoAtomBitFlip.h
|
||||
// Increments or decrements by one a single element
|
||||
// (c) GeNeura Team, 1998
|
||||
/*
|
||||
|
|
@ -22,8 +22,8 @@
|
|||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
#ifndef _EOATOMCREEP_H
|
||||
#define _EOATOMCREEP_H
|
||||
#ifndef _EOATOMBITFLIP_H
|
||||
#define _EOATOMBITFLIP_H
|
||||
|
||||
/** Flips a single bit
|
||||
*/
|
||||
|
|
|
|||
651
eo/src/eoBitOp.h
651
eo/src/eoBitOp.h
|
|
@ -1,16 +1,16 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// eoBitOp.h
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef eoBitOp_h
|
||||
#define eoBitOp_h
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <algorithm> // swap_ranges
|
||||
#include <eoUniform.h> // eoUniform
|
||||
#include <eoBin.h> // eoBin
|
||||
#include <eoOp.h> // eoMonOp
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoBitOp.h
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef eoBitOp_h
|
||||
#define eoBitOp_h
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <algorithm> // swap_ranges
|
||||
#include <eoUniform.h> // eoUniform
|
||||
#include <eoBin.h> // eoBin
|
||||
#include <eoOp.h> // eoMonOp
|
||||
|
||||
|
||||
/** @name BitWise Genetic operators
|
||||
|
|
@ -24,334 +24,301 @@ have a factory that knows how to build them from a description
|
|||
@see eoBitOpFactory
|
||||
*/
|
||||
|
||||
//@{
|
||||
//@{
|
||||
|
||||
/** eoBinRandom --> mofify a chromosome in a random way */
|
||||
|
||||
template<class Chrom> class eoBinRandom: public eoMonOp<Chrom>
|
||||
{
|
||||
public:
|
||||
/// The class name.
|
||||
string className() const { return "eoBinRandom"; }
|
||||
|
||||
/**
|
||||
* Randomizes a cromosome.
|
||||
* @param chrom The cromosome to be randomize.
|
||||
*/
|
||||
void operator()(Chrom& chrom) const
|
||||
{
|
||||
eoUniform<float> uniform(0.0, 1.0);
|
||||
for (unsigned i = 0; i < chrom.size(); i++)
|
||||
chrom[i] = (uniform() < 0.5) ? false : true;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/** eoBinBitFlip --> chages a bit */
|
||||
|
||||
template<class Chrom> class eoBinBitFlip: public eoMonOp<Chrom>
|
||||
{
|
||||
public:
|
||||
/// The class name.
|
||||
string className() const { return "eoBinBitFlip"; }
|
||||
|
||||
/**
|
||||
* Change one bit.
|
||||
* @param chrom The cromosome which one bit is going to be changed.
|
||||
*/
|
||||
void operator()(Chrom& chrom) const
|
||||
{
|
||||
eoUniform<int> uniform(0, chrom.size());
|
||||
unsigned i = uniform();
|
||||
chrom[i] = (chrom[i]) ? false : true;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/** eoBinMutation --> classical mutation */
|
||||
|
||||
template<class Chrom> class eoBinMutation: public eoMonOp<Chrom>
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* (Default) Constructor.
|
||||
* @param _rate Rate of mutation.
|
||||
*/
|
||||
eoBinMutation(const double& _rate = 0.01): rate(_rate), uniform(0.0, 1.0) {}
|
||||
|
||||
/// The class name.
|
||||
string className() const { return "eoBinMutation"; }
|
||||
|
||||
/**
|
||||
* Mutate a chromosome.
|
||||
* @param chrom The chromosome to be mutated.
|
||||
*/
|
||||
void operator()(Chrom& chrom) const
|
||||
{
|
||||
for (unsigned i = 0; i < chrom.size(); i++)
|
||||
if (uniform() < rate)
|
||||
chrom[i] = !chrom[i];
|
||||
}
|
||||
|
||||
private:
|
||||
double rate;
|
||||
mutable eoUniform<float> uniform;
|
||||
};
|
||||
|
||||
|
||||
/** eoBinInversion: inverts the bits of the chromosome between an interval */
|
||||
|
||||
template<class Chrom> class eoBinInversion: public eoMonOp<Chrom>
|
||||
{
|
||||
public:
|
||||
/// The class name.
|
||||
string className() const { return "eoBinInversion"; }
|
||||
|
||||
/**
|
||||
* Inverts a range of bits in a binary chromosome.
|
||||
* @param chrom The chromosome whos bits are going to be inverted (a range).
|
||||
*/
|
||||
void operator()(Chrom& chrom) const
|
||||
{
|
||||
eoUniform<unsigned> uniform(0, chrom.size() + 1);
|
||||
|
||||
unsigned u1 = uniform(), u2;
|
||||
do u2 = uniform(); while (u1 == u2);
|
||||
unsigned r1 = min(u1, u2), r2 = max(u1, u2);
|
||||
|
||||
reverse(chrom.begin() + r1, chrom.begin() + r2);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/** eoBinNext --> next binary value */
|
||||
|
||||
template<class Chrom> class eoBinNext: public eoMonOp<Chrom>
|
||||
{
|
||||
public:
|
||||
/// The class name.
|
||||
string className() const { return "eoBinNext"; }
|
||||
|
||||
/**
|
||||
* Change the bit string x to be x+1.
|
||||
* @param chrom The chromosome to be added one.
|
||||
*/
|
||||
void operator()(Chrom& chrom) const
|
||||
{
|
||||
for (int i = chrom.size() - 1; i >= 0; i--)
|
||||
if (chrom[i])
|
||||
{
|
||||
chrom[i] = 0;
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
chrom[i] = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/** eoBinPrev --> previos binary value */
|
||||
|
||||
template<class Chrom> class eoBinPrev: public eoMonOp<Chrom>
|
||||
{
|
||||
public:
|
||||
/// The class name.
|
||||
string className() const { return "eoBinPrev"; }
|
||||
|
||||
/**
|
||||
* Change the bit string x to be x-1.
|
||||
* @param chrom The chromosome to be substracted one.
|
||||
*/
|
||||
void operator()(Chrom& chrom) const
|
||||
{
|
||||
for (int i = chrom.size() - 1; i >= 0; i--)
|
||||
if (chrom[i])
|
||||
{
|
||||
chrom[i] = 0;
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
chrom[i] = 1;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/** eoBinCrossover --> classic crossover */
|
||||
|
||||
template<class Chrom> class eoBinCrossover: public eoBinOp<Chrom>
|
||||
{
|
||||
public:
|
||||
/// The class name.
|
||||
string className() const { return "eoBinCrossover"; }
|
||||
|
||||
/**
|
||||
* 2-point crossover for binary chromosomes.
|
||||
* @param chrom1 The first chromosome.
|
||||
* @param chrom2 The first chromosome.
|
||||
*/
|
||||
void operator()(Chrom& chrom1, Chrom& chrom2) const
|
||||
{
|
||||
eoUniform<unsigned> uniform(1, min(chrom1.size(), chrom2.size()));
|
||||
swap_ranges(chrom1.begin(), chrom1.begin() + uniform(), chrom2.begin());
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/** eoBinNxOver --> n-point crossover */
|
||||
|
||||
template<class Chrom> class eoBinNxOver: public eoBinOp<Chrom>
|
||||
{
|
||||
public:
|
||||
/// (Defualt) Constructor.
|
||||
eoBinNxOver(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"; }
|
||||
|
||||
/**
|
||||
* n-point crossover for binary chromosomes.
|
||||
* @param chrom1 The first chromosome.
|
||||
* @param chrom2 The first chromosome.
|
||||
*/
|
||||
void operator()(Chrom& chrom1, Chrom& chrom2) const
|
||||
{
|
||||
unsigned max_size = min(chrom1.size(), chrom2.size());
|
||||
unsigned max_points = min(max_size - 1, num_points);
|
||||
|
||||
vector<bool> points(max_size, false);
|
||||
eoUniform<unsigned> uniform(1, max_size);
|
||||
|
||||
// select ranges of bits to swap
|
||||
do {
|
||||
unsigned bit = uniform();
|
||||
if (points[bit])
|
||||
continue;
|
||||
else
|
||||
{
|
||||
points[bit] = true;
|
||||
max_points--;
|
||||
}
|
||||
} while (max_points);
|
||||
|
||||
|
||||
// swap bits between chromosomes
|
||||
bool change = false;
|
||||
for (unsigned bit = 1; bit < points.size(); bit++)
|
||||
{
|
||||
if (points[bit])
|
||||
change = !change;
|
||||
|
||||
if (change)
|
||||
swap(chrom1[bit], chrom2[bit]);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
unsigned num_points;
|
||||
};
|
||||
|
||||
|
||||
/** eoBinGxOver --> gene crossover */
|
||||
|
||||
template<class Chrom> class eoBinGxOver: public eoBinOp<Chrom>
|
||||
{
|
||||
public:
|
||||
/// Constructor.
|
||||
eoBinGxOver(const unsigned _gene_size, const unsigned _num_points = 2):
|
||||
gene_size(_gene_size), num_points(_num_points)
|
||||
{
|
||||
if (gene_size < 1)
|
||||
runtime_error("GxOver --> invalid gene size");
|
||||
if (num_points < 1)
|
||||
runtime_error("GxOver --> invalid number of points");
|
||||
}
|
||||
|
||||
/// The class name
|
||||
string className() const { return "eoBinGxOver"; }
|
||||
|
||||
/**
|
||||
* Gene crossover for binary chromosomes.
|
||||
* @param chrom1 The first chromosome.
|
||||
* @param chrom2 The first chromosome.
|
||||
*/
|
||||
void operator()(Chrom& chrom1, Chrom& chrom2) const
|
||||
{
|
||||
unsigned max_genes = min(chrom1.size(), chrom2.size()) / gene_size;
|
||||
unsigned cut_genes = min(max_genes, num_points);
|
||||
|
||||
vector<bool> points(max_genes, false);
|
||||
eoUniform<unsigned> uniform(0, max_genes);
|
||||
|
||||
// selects genes to swap
|
||||
do {
|
||||
unsigned bit = uniform();
|
||||
if (points[bit])
|
||||
continue;
|
||||
else
|
||||
{
|
||||
points[bit] = true;
|
||||
cut_genes--;
|
||||
}
|
||||
} while (cut_genes);
|
||||
|
||||
// swaps genes
|
||||
for (unsigned i = 0; i < points.size(); i++)
|
||||
if (points[i])
|
||||
swap_ranges(chrom1.begin() + i * gene_size,
|
||||
chrom1.begin() + i * gene_size + gene_size,
|
||||
chrom2.begin() + i * gene_size);
|
||||
}
|
||||
|
||||
private:
|
||||
unsigned gene_size;
|
||||
unsigned num_points;
|
||||
};
|
||||
|
||||
|
||||
/** eoBinRandom --> mofify a chromosome in a random way */
|
||||
|
||||
template<class Chrom> class eoBinRandom: public eoMonOp<Chrom>
|
||||
{
|
||||
public:
|
||||
/// The class name.
|
||||
string className() const { return "eoBinRandom"; }
|
||||
|
||||
/**
|
||||
* Randomizes a cromosome.
|
||||
* @param chrom The cromosome to be randomize.
|
||||
*/
|
||||
void operator()(Chrom& chrom) const
|
||||
{
|
||||
eoUniform<float> uniform(0.0, 1.0);
|
||||
for (unsigned i = 0; i < chrom.size(); i++)
|
||||
chrom[i] = (uniform() < 0.5) ? false : true;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/** eoBinBitFlip --> chages a bit */
|
||||
|
||||
template<class Chrom> class eoBinBitFlip: public eoMonOp<Chrom>
|
||||
{
|
||||
public:
|
||||
/// The class name.
|
||||
string className() const { return "eoBinBitFlip"; }
|
||||
|
||||
/**
|
||||
* Change one bit.
|
||||
* @param chrom The cromosome which one bit is going to be changed.
|
||||
*/
|
||||
void operator()(Chrom& chrom) const
|
||||
{
|
||||
eoUniform<int> uniform(0, chrom.size());
|
||||
unsigned i = uniform();
|
||||
chrom[i] = (chrom[i]) ? false : true;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/** eoBinMutation --> classical mutation */
|
||||
|
||||
template<class Chrom> class eoBinMutation: public eoMonOp<Chrom>
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* (Default) Constructor.
|
||||
* @param _rate Rate of mutation.
|
||||
*/
|
||||
eoBinMutation(const double& _rate = 0.01): rate(_rate), uniform(0.0, 1.0) {}
|
||||
|
||||
/// The class name.
|
||||
string className() const { return "eoBinMutation"; }
|
||||
|
||||
/**
|
||||
* Mutate a chromosome.
|
||||
* @param chrom The chromosome to be mutated.
|
||||
*/
|
||||
void operator()(Chrom& chrom) const
|
||||
{
|
||||
for (unsigned i = 0; i < chrom.size(); i++)
|
||||
if (uniform() < rate)
|
||||
chrom[i] = !chrom[i];
|
||||
}
|
||||
|
||||
private:
|
||||
double rate;
|
||||
mutable eoUniform<float> uniform;
|
||||
};
|
||||
|
||||
|
||||
/** eoBinInversion: inverts the bits of the chromosome between an interval */
|
||||
|
||||
template<class Chrom> class eoBinInversion: public eoMonOp<Chrom>
|
||||
{
|
||||
public:
|
||||
/// The class name.
|
||||
string className() const { return "eoBinInversion"; }
|
||||
|
||||
/**
|
||||
* Inverts a range of bits in a binary chromosome.
|
||||
* @param chrom The chromosome whos bits are going to be inverted (a range).
|
||||
*/
|
||||
void operator()(Chrom& chrom) const
|
||||
{
|
||||
eoUniform<unsigned> uniform(0, chrom.size() + 1);
|
||||
|
||||
unsigned u1 = uniform(), u2;
|
||||
do u2 = uniform(); while (u1 == u2);
|
||||
unsigned r1 = min(u1, u2), r2 = max(u1, u2);
|
||||
|
||||
reverse(chrom.begin() + r1, chrom.begin() + r2);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/** eoBinNext --> next binary value */
|
||||
|
||||
template<class Chrom> class eoBinNext: public eoMonOp<Chrom>
|
||||
{
|
||||
public:
|
||||
/// The class name.
|
||||
string className() const { return "eoBinNext"; }
|
||||
|
||||
/**
|
||||
* Change the bit string x to be x+1.
|
||||
* @param chrom The chromosome to be added one.
|
||||
*/
|
||||
void operator()(Chrom& chrom) const
|
||||
{
|
||||
for (int i = chrom.size() - 1; i >= 0; i--)
|
||||
if (chrom[i])
|
||||
{
|
||||
chrom[i] = 0;
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
chrom[i] = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/** eoBinPrev --> previos binary value */
|
||||
|
||||
template<class Chrom> class eoBinPrev: public eoMonOp<Chrom>
|
||||
{
|
||||
public:
|
||||
/// The class name.
|
||||
string className() const { return "eoBinPrev"; }
|
||||
|
||||
/**
|
||||
* Change the bit string x to be x-1.
|
||||
* @param chrom The chromosome to be substracted one.
|
||||
*/
|
||||
void operator()(Chrom& chrom) const
|
||||
{
|
||||
for (int i = chrom.size() - 1; i >= 0; i--)
|
||||
if (chrom[i])
|
||||
{
|
||||
chrom[i] = 0;
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
chrom[i] = 1;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/** eoBinCrossover --> classic crossover */
|
||||
|
||||
template<class Chrom> class eoBinCrossover: public eoBinOp<Chrom>
|
||||
{
|
||||
public:
|
||||
/// The class name.
|
||||
string className() const { return "eoBinCrossover"; }
|
||||
|
||||
/**
|
||||
* 2-point crossover for binary chromosomes.
|
||||
* @param chrom1 The first chromosome.
|
||||
* @param chrom2 The first chromosome.
|
||||
*/
|
||||
void operator()(Chrom& chrom1, Chrom& chrom2) const
|
||||
{
|
||||
eoUniform<unsigned> uniform(1, min(chrom1.size(), chrom2.size()));
|
||||
swap_ranges(chrom1.begin(), chrom1.begin() + uniform(), chrom2.begin());
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/** eoBinNxOver --> n-point crossover */
|
||||
|
||||
template<class Chrom> class eoBinNxOver: public eoBinOp<Chrom>
|
||||
{
|
||||
public:
|
||||
/// (Defualt) Constructor.
|
||||
eoBinNxOver(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"; }
|
||||
|
||||
/**
|
||||
* n-point crossover for binary chromosomes.
|
||||
* @param chrom1 The first chromosome.
|
||||
* @param chrom2 The first chromosome.
|
||||
*/
|
||||
void operator()(Chrom& chrom1, Chrom& chrom2) const
|
||||
{
|
||||
unsigned max_size = min(chrom1.size(), chrom2.size());
|
||||
unsigned max_points = min(max_size - 1, num_points);
|
||||
|
||||
vector<bool> points(max_size, false);
|
||||
eoUniform<unsigned> uniform(1, max_size);
|
||||
|
||||
// select ranges of bits to swap
|
||||
do {
|
||||
unsigned bit = uniform();
|
||||
if (points[bit])
|
||||
continue;
|
||||
else
|
||||
{
|
||||
points[bit] = true;
|
||||
max_points--;
|
||||
}
|
||||
} while (max_points);
|
||||
|
||||
|
||||
// swap bits between chromosomes
|
||||
bool change = false;
|
||||
for (unsigned bit = 1; bit < points.size(); bit++)
|
||||
{
|
||||
if (points[bit])
|
||||
change = !change;
|
||||
|
||||
if (change)
|
||||
swap(chrom1[bit], chrom2[bit]);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
unsigned num_points;
|
||||
};
|
||||
|
||||
|
||||
/** eoBinGxOver --> gene crossover */
|
||||
|
||||
template<class Chrom> class eoBinGxOver: public eoBinOp<Chrom>
|
||||
{
|
||||
public:
|
||||
/// Constructor.
|
||||
eoBinGxOver(const unsigned _gene_size, const unsigned _num_points = 2):
|
||||
gene_size(_gene_size), num_points(_num_points)
|
||||
{
|
||||
if (gene_size < 1)
|
||||
runtime_error("GxOver --> invalid gene size");
|
||||
if (num_points < 1)
|
||||
runtime_error("GxOver --> invalid number of points");
|
||||
}
|
||||
|
||||
/// The class name
|
||||
string className() const { return "eoBinGxOver"; }
|
||||
|
||||
/**
|
||||
* Gene crossover for binary chromosomes.
|
||||
* @param chrom1 The first chromosome.
|
||||
* @param chrom2 The first chromosome.
|
||||
*/
|
||||
void operator()(Chrom& chrom1, Chrom& chrom2) const
|
||||
{
|
||||
unsigned max_genes = min(chrom1.size(), chrom2.size()) / gene_size;
|
||||
unsigned cut_genes = min(max_genes, num_points);
|
||||
|
||||
vector<bool> points(max_genes, false);
|
||||
eoUniform<unsigned> uniform(0, max_genes);
|
||||
|
||||
// selects genes to swap
|
||||
do {
|
||||
unsigned bit = uniform();
|
||||
if (points[bit])
|
||||
continue;
|
||||
else
|
||||
{
|
||||
points[bit] = true;
|
||||
cut_genes--;
|
||||
}
|
||||
} while (cut_genes);
|
||||
|
||||
// swaps genes
|
||||
for (unsigned i = 0; i < points.size(); i++)
|
||||
if (points[i])
|
||||
swap_ranges(chrom1.begin() + i * gene_size,
|
||||
chrom1.begin() + i * gene_size + gene_size,
|
||||
chrom2.begin() + i * gene_size);
|
||||
}
|
||||
|
||||
private:
|
||||
unsigned gene_size;
|
||||
unsigned num_points;
|
||||
};
|
||||
|
||||
|
||||
/** eoBinUxOver --> uniform crossover */
|
||||
|
||||
template<class Chrom> class eoBinUxOver: public eoBinOp<Chrom>
|
||||
{
|
||||
public:
|
||||
/// (Default) Constructor.
|
||||
eoBinUxOver(const float _rate = 0.5): rate(_rate)
|
||||
{
|
||||
if (rate < 0 || rate > 1)
|
||||
runtime_error("UxOver --> invalid rate");
|
||||
}
|
||||
|
||||
/// The class name.
|
||||
string className() const { return "eoBinUxOver"; }
|
||||
|
||||
/**
|
||||
* Uniform crossover for binary chromosomes.
|
||||
* @param chrom1 The first chromosome.
|
||||
* @param chrom2 The first chromosome.
|
||||
*/
|
||||
void operator()(Chrom& chrom1, Chrom& chrom2) const
|
||||
{
|
||||
unsigned min_size = min(chrom1.size(), chrom2.size());
|
||||
eoUniform<float> uniform(0, 1);
|
||||
|
||||
for (unsigned bit = 0; bit < min_size; bit++)
|
||||
if (uniform() < rate)
|
||||
swap(chrom1[bit], chrom2[bit]);
|
||||
}
|
||||
|
||||
public:
|
||||
float rate;
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
//@}
|
||||
#endif eoBitOp_h
|
||||
//-----------------------------------------------------------------------------
|
||||
//@}
|
||||
#endif eoBitOp_h
|
||||
|
|
|
|||
|
|
@ -1,82 +1,103 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// eoBreeder.h
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef eoBreeder_h
|
||||
#define eoBreeder_h
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <vector> // vector
|
||||
#include <eoUniform.h> // eoUniform
|
||||
#include <eoOp.h> // eoOp, eoMonOp, eoBinOp
|
||||
#include <eoPop.h> // eoPop
|
||||
#include <eoPopOps.h> // eoTransform
|
||||
#include <eoOpSelector.h> // eoOpSelector
|
||||
|
||||
using namespace std;
|
||||
|
||||
/*****************************************************************************
|
||||
* eoBreeder: transforms a population using genetic operators. *
|
||||
* For every operator there is a rated to be applyed. *
|
||||
*****************************************************************************/
|
||||
|
||||
template<class Chrom> class eoBreeder: public eoTransform<Chrom>
|
||||
{
|
||||
public:
|
||||
/// Default constructor.
|
||||
eoBreeder( eoOpSelector<Chrom>& _opSel): opSel( _opSel ) {}
|
||||
|
||||
/// Destructor.
|
||||
virtual ~eoBreeder() {}
|
||||
|
||||
/**
|
||||
* Transforms a population.
|
||||
* @param pop The population to be transformed.
|
||||
*/
|
||||
void operator()(eoPop<Chrom>& pop)
|
||||
{
|
||||
for (unsigned i = 0; i < pop.size(); i++) {
|
||||
eoOp<Chrom>* op = opSel.Op();
|
||||
switch (op->readArity()) {
|
||||
case unary:
|
||||
{
|
||||
eoMonOp<Chrom>* monop = static_cast<eoMonOp<Chrom>* >(op);
|
||||
(*monop)( pop[i] );
|
||||
break;
|
||||
}
|
||||
case binary:
|
||||
{
|
||||
eoBinOp<Chrom>* binop = static_cast<eoBinOp<Chrom>* >(op);
|
||||
eoUniform<unsigned> u(0, pop.size() );
|
||||
(*binop)(pop[i], pop[ u() ] );
|
||||
break;
|
||||
}
|
||||
case Nary:
|
||||
{
|
||||
eoNaryOp<Chrom>* Nop = static_cast<eoNaryOp<Chrom>* >(op);
|
||||
eoUniform<unsigned> u(0, pop.size() );
|
||||
eoPop<Chrom> inVec, outVec;
|
||||
inVec.push_back( pop[i] );
|
||||
unsigned numberOfOperands = u();
|
||||
for ( unsigned i = 0; i < numberOfOperands; i ++ ) {
|
||||
inVec.push_back( pop[ u() ] );
|
||||
}
|
||||
(*Nop)( inVec, outVec );
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/// The class name.
|
||||
string classname() const { return "eoBreeder"; }
|
||||
|
||||
private:
|
||||
eoOpSelector<Chrom>& opSel;
|
||||
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#endif eoBreeder_h
|
||||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoBreeder.h
|
||||
// Takes two populations and mixes them
|
||||
// (c) GeNeura Team, 1998
|
||||
/*
|
||||
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
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef eoBreeder_h
|
||||
#define eoBreeder_h
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <vector> // vector
|
||||
#include <eoUniform.h> // eoUniform
|
||||
#include <eoOp.h> // eoOp, eoMonOp, eoBinOp
|
||||
#include <eoPop.h> // eoPop
|
||||
#include <eoPopOps.h> // eoTransform
|
||||
#include <eoOpSelector.h> // eoOpSelector
|
||||
|
||||
using namespace std;
|
||||
|
||||
/*****************************************************************************
|
||||
* eoBreeder: transforms a population using genetic operators. *
|
||||
* For every operator there is a rated to be applyed. *
|
||||
*****************************************************************************/
|
||||
|
||||
template<class Chrom> class eoBreeder: public eoMonPopOp<Chrom>
|
||||
{
|
||||
public:
|
||||
/// Default constructor.
|
||||
eoBreeder( eoOpSelector<Chrom>& _opSel): opSel( _opSel ) {}
|
||||
|
||||
/// Destructor.
|
||||
virtual ~eoBreeder() {}
|
||||
|
||||
/**
|
||||
* Transforms a population.
|
||||
* @param pop The population to be transformed.
|
||||
*/
|
||||
void operator()(eoPop<Chrom>& pop)
|
||||
{
|
||||
for (unsigned i = 0; i < pop.size(); i++) {
|
||||
eoOp<Chrom>* op = opSel.Op();
|
||||
switch (op->readArity()) {
|
||||
case unary:
|
||||
{
|
||||
eoMonOp<Chrom>* monop = static_cast<eoMonOp<Chrom>* >(op);
|
||||
(*monop)( pop[i] );
|
||||
break;
|
||||
}
|
||||
case binary:
|
||||
{
|
||||
eoBinOp<Chrom>* binop = static_cast<eoBinOp<Chrom>* >(op);
|
||||
eoUniform<unsigned> u(0, pop.size() );
|
||||
(*binop)(pop[i], pop[ u() ] );
|
||||
break;
|
||||
}
|
||||
case Nary:
|
||||
{
|
||||
eoNaryOp<Chrom>* Nop = static_cast<eoNaryOp<Chrom>* >(op);
|
||||
eoUniform<unsigned> u(0, pop.size() );
|
||||
eoPop<Chrom> inVec, outVec;
|
||||
inVec.push_back( pop[i] );
|
||||
unsigned numberOfOperands = u();
|
||||
for ( unsigned i = 0; i < numberOfOperands; i ++ ) {
|
||||
inVec.push_back( pop[ u() ] );
|
||||
}
|
||||
(*Nop)( inVec, outVec );
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/// The class name.
|
||||
string classname() const { return "eoBreeder"; }
|
||||
|
||||
private:
|
||||
eoOpSelector<Chrom>& opSel;
|
||||
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#endif eoBreeder_h
|
||||
|
|
|
|||
78
eo/src/eoDetTournament.h
Normal file
78
eo/src/eoDetTournament.h
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoDetTournament.h
|
||||
// (c) GeNeura Team, 1998 - EEAAX 1999
|
||||
/*
|
||||
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
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef eoDetTournament_h
|
||||
#define eoDetTournament_h
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <functional> //
|
||||
#include <numeric> // accumulate
|
||||
#include <eoPopOps.h> // eoPop eoSelect MINFLOAT
|
||||
#include <eoRNG.h>
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
/** eoDetTournament: a selection method that selects ONE individual by
|
||||
deterministic tournament
|
||||
-MS- 24/10/99 */
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
template <class EOT> class eoDetTournament: public eoSelectOne<EOT>
|
||||
{
|
||||
public:
|
||||
/// (Default) Constructor.
|
||||
eoDetTournament(unsigned _Tsize = 2 ):eoSelectOne(), Tsize(_Tsize) {
|
||||
// consistency check
|
||||
if (Tsize < 2) {
|
||||
cout << "Warning, Tournament size shoudl be >= 2\nAdjusted\n";
|
||||
Tsize = 2;
|
||||
}
|
||||
}
|
||||
|
||||
/** DANGER: if you want to be able to minimize as well as maximizem
|
||||
DON'T cast the fitness to a float, use the EOT comparator! */
|
||||
virtual const EOT& operator()(const eoPop<EOT>& pop) {
|
||||
unsigned best = rng.random(pop.size()); // random individual
|
||||
|
||||
for (unsigned i = 0; i<Tsize-1; i++) {
|
||||
unsigned tmp = rng.random(pop.size());
|
||||
if (pop[best] < pop[tmp])
|
||||
best = tmp;
|
||||
}
|
||||
return pop[best] ;
|
||||
}
|
||||
|
||||
/** Inherited from eoObject
|
||||
@see eoObject
|
||||
*/
|
||||
string className() const {return "eoDetTournament";};
|
||||
|
||||
private:
|
||||
unsigned Tsize;
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#endif eoDetTournament_h
|
||||
|
|
@ -119,9 +119,9 @@ class eoESFullChrom : public eoVector<double, fitT> {
|
|||
cout << "WARNING, Number of Standard Deviations > Number of Object Variables\nAdjusted!\n";
|
||||
num_sigma = num_genes;
|
||||
// modify the Param value - so .status is OK
|
||||
char sloc[20];
|
||||
sprintf(sloc, "%d", num_genes);
|
||||
parser.setParamValue("--NbSigma", sloc);
|
||||
ostrstream sloc;
|
||||
sloc << num_genes;
|
||||
parser.setParamValue("--NbSigma", sloc.str());
|
||||
}
|
||||
// adjust the sizes!!!
|
||||
resize(num_genes);
|
||||
|
|
|
|||
|
|
@ -1,243 +1,243 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoESMute.h : ES mutation
|
||||
// (c) GeNeura Team, 1998 for the EO part
|
||||
// Th. Baeck 1994 and EEAAX 1999 for the ES part
|
||||
/*
|
||||
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
|
||||
http://eeaax.cmap.polytchnique.fr/
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
|
||||
#ifndef _EOESMUT_H
|
||||
#define _EOESMUT_H
|
||||
|
||||
#include <eoParser.h>
|
||||
#include <eoRNG.h>
|
||||
#include <cmath> // for exp
|
||||
|
||||
#include <eoESFullChrom.h>
|
||||
#include <eoOp.h>
|
||||
|
||||
const double ES_SIGEPS = 1.0e-40; /* ES lower bound for sigma values */
|
||||
// should not be a parameter ...
|
||||
|
||||
/** ES-style mutation in the large: Obviously, valid only for eoESInd
|
||||
*/
|
||||
template <class fitT>
|
||||
class eoESMutate: public eoMonOp< eoESFullChrom<fitT> > {
|
||||
public:
|
||||
///
|
||||
eoESMutate( double _TauLcl, double _TauGlb, double _TauBeta )
|
||||
: eoMonOp< eoESFullChrom<fitT> >( ), TauLcl(_TauLcl), TauGlb(_TauGlb),
|
||||
TauBeta(_TauBeta) {};
|
||||
|
||||
/* The parser constructor
|
||||
*/
|
||||
eoESMutate(Parser & parser, unsigned _stdDevLength, unsigned _size, bool _correlated ):
|
||||
eoMonOp< eoESFullChrom<fitT> >( ) {
|
||||
parser.AddTitle("Parameters of ES mutation (before renormalization)");
|
||||
try { // we know that there is at least 1 std dev.
|
||||
if (_stdDevLength == 1) {
|
||||
TauLcl = parser.getInt("-Ml", "--TauLcl", "1",
|
||||
"TauLcl, Mutation rate for the only Std Dev." );
|
||||
// different normalization in that case -- Thomas Baeck
|
||||
TauLcl /= sqrt((double) _size);
|
||||
}
|
||||
else { /* more than 1 std dev */
|
||||
TauLcl = parser.getFloat("-Ml", "--TauLcl", "1",
|
||||
"Local mutation rate for Std Dev." );
|
||||
TauGlb = parser.getFloat("-Mg", "--TauGlb", "1",
|
||||
"Global mutation rate for Std Dev." );
|
||||
// renormalization
|
||||
TauLcl /= sqrt( 2.0 * sqrt( (double)_size ) );
|
||||
TauGlb /= sqrt( 2.0 * ( (double) _size ) );
|
||||
|
||||
if ( _correlated ) { // Correlated Mutations
|
||||
TauBeta = parser.getFloat("-Mb", "--TauBeta", "0.0873",
|
||||
"Mutation rate for corr. coeff." );
|
||||
// rotation angles: no normalization
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (exception & e)
|
||||
{
|
||||
cout << e.what() << endl;
|
||||
parser.printHelp();
|
||||
exit(1);
|
||||
}
|
||||
};
|
||||
|
||||
/// needed virtual dtor
|
||||
virtual ~eoESMutate() {};
|
||||
|
||||
// virtual separation depending wether correlated mutations are present
|
||||
virtual void operator() ( eoESFullChrom<fitT> & _eo ) const {
|
||||
if (_eo.CorCffLength())
|
||||
CorrelatedMutation(_eo);
|
||||
else
|
||||
StandardMutation(_eo);
|
||||
}
|
||||
|
||||
/** @name Methods from eoObject
|
||||
readFrom and printOn are directly inherited from eoObject
|
||||
*/
|
||||
//@{
|
||||
/** Inherited from eoObject
|
||||
@see eoObject
|
||||
*/
|
||||
virtual string className() const {return "eoESMutate";};
|
||||
|
||||
private:
|
||||
/// mutations - standard et correlated
|
||||
// =========
|
||||
/*
|
||||
* Standard mutation of object variables and standard
|
||||
* deviations in ESs.
|
||||
* If there are fewer different standard deviations available
|
||||
* than the dimension of the objective function requires, the
|
||||
* last standard deviation is responsible for ALL remaining
|
||||
* object variables.
|
||||
* Schwefel 1977: Numerische Optimierung von Computer-Modellen
|
||||
* mittels der Evolutionsstrategie, pp. 165 ff.
|
||||
*/
|
||||
|
||||
virtual void StandardMutation( eoESFullChrom<fitT> & _eo ) const {
|
||||
unsigned i,k;
|
||||
double Glb, StdLoc;
|
||||
|
||||
if (_eo.StdDevLength() == 1) { /* single StdDev -> No global factor */
|
||||
StdLoc = _eo.getStdDev(0);
|
||||
StdLoc *= exp(TauLcl*rng.normal());
|
||||
if (StdLoc < ES_SIGEPS)
|
||||
StdLoc = ES_SIGEPS;
|
||||
_eo.setStdDev(0, StdLoc);
|
||||
_eo.setGene( 0, _eo.getGene(0) + StdLoc*rng.normal());
|
||||
i = 1;
|
||||
}
|
||||
else { /* more than one std dev. */
|
||||
Glb = exp(TauGlb*rng.normal());
|
||||
for (i = 0; i < _eo.length() && i < _eo.StdDevLength(); i++) {
|
||||
StdLoc = _eo.getStdDev(i);
|
||||
StdLoc *= Glb * exp(TauLcl*rng.normal());
|
||||
if (StdLoc < ES_SIGEPS)
|
||||
StdLoc = ES_SIGEPS;
|
||||
_eo.setStdDev(i, StdLoc);
|
||||
_eo.setGene( i, _eo.getGene(i) + StdLoc*rng.normal());
|
||||
}
|
||||
}
|
||||
// last object variables: same STdDev than the preceding one
|
||||
for (k = i; k < _eo.length(); k++) {
|
||||
_eo.setGene( k, _eo.getGene(k) + StdLoc*rng.normal() );
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Correlated mutations in ESs, according to the following
|
||||
* sources:
|
||||
* H.-P. Schwefel: Internal Report of KFA Juelich, KFA-STE-IB-3/80
|
||||
* p. 43, 1980
|
||||
* G. Rudolph: Globale Optimierung mit parallelen Evolutions-
|
||||
* strategien, Diploma Thesis, University of Dortmund, 1990
|
||||
*/
|
||||
|
||||
// Code from Thomas Baeck
|
||||
|
||||
virtual void CorrelatedMutation( eoESFullChrom<fitT> & _eo ) const {
|
||||
|
||||
|
||||
int i, k, n1, n2, nq;
|
||||
|
||||
double d1, d2, S, C, Glb;
|
||||
double tmp;
|
||||
/*
|
||||
* First: mutate standard deviations (as above).
|
||||
*/
|
||||
|
||||
Glb = exp(TauGlb*rng.normal());
|
||||
for (i = 0; i < _eo.StdDevLength(); i++) {
|
||||
tmp = _eo.getStdDev(i);
|
||||
_eo.setStdDev( i, tmp*Glb*exp(TauLcl*rng.normal()) );
|
||||
}
|
||||
|
||||
/*
|
||||
* Mutate rotation angles.
|
||||
*/
|
||||
|
||||
for (i = 0; i < _eo.CorCffLength(); i++) {
|
||||
tmp = _eo.getCorCff(i);
|
||||
tmp += TauBeta*rng.normal();
|
||||
// danger of VERY long loops --MS--
|
||||
// while (CorCff[i] > M_PI)
|
||||
// CorCff[i] -= 2.0 * M_PI;
|
||||
// while (CorCff[i] < - M_PI)
|
||||
// CorCff[i] += 2.0 * M_PI;
|
||||
if ( fabs(tmp) > M_PI ) {
|
||||
tmp -= M_PI * (int) (tmp/M_PI) ;
|
||||
}
|
||||
_eo.setCorCff(i, tmp);
|
||||
}
|
||||
|
||||
/*
|
||||
* Perform correlated mutations.
|
||||
*/
|
||||
vector<double> VarStp(_eo.size());
|
||||
for (i = 0; i < _eo.size() && i < _eo.StdDevLength(); i++)
|
||||
VarStp[i] = _eo.getStdDev(i)*rng.normal();
|
||||
for (k = i; k < _eo.size(); k++)
|
||||
VarStp[k] = _eo.getStdDev(i-1)*rng.normal();
|
||||
nq = _eo.CorCffLength() - 1;
|
||||
for (k = _eo.size()-_eo.StdDevLength(); k < _eo.size()-1; k++) {
|
||||
n1 = _eo.size() - k - 1;
|
||||
n2 = _eo.size() - 1;
|
||||
for (i = 0; i < k; i++) {
|
||||
d1 = VarStp[n1];
|
||||
d2 = VarStp[n2];
|
||||
S = sin( _eo.getCorCff(nq) );
|
||||
C = cos( _eo.getCorCff(nq) );
|
||||
VarStp[n2] = d1 * S + d2 * C;
|
||||
VarStp[n1] = d1 * C - d2 * S;
|
||||
n2--;
|
||||
nq--;
|
||||
}
|
||||
}
|
||||
for (i = 0; i < _eo.size(); i++)
|
||||
_eo[i] += VarStp[i];
|
||||
|
||||
}
|
||||
// the data
|
||||
//=========
|
||||
double TauLcl; /* Local factor for mutation of std deviations */
|
||||
double TauGlb; /* Global factor for mutation of std deviations */
|
||||
double TauBeta; /* Factor for mutation of correlation parameters */
|
||||
};
|
||||
|
||||
/*
|
||||
* Correlated mutations in ESs, according to the following
|
||||
* sources:
|
||||
* H.-P. Schwefel: Internal Report of KFA Juelich, KFA-STE-IB-3/80
|
||||
* p. 43, 1980
|
||||
* G. Rudolph: Globale Optimierung mit parallelen Evolutions-
|
||||
* strategien, Diploma Thesis, University of Dortmund, 1990
|
||||
*/
|
||||
// Not yet implemented!
|
||||
|
||||
#endif
|
||||
|
||||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoESMute.h : ES mutation
|
||||
// (c) GeNeura Team, 1998 for the EO part
|
||||
// Th. Baeck 1994 and EEAAX 1999 for the ES part
|
||||
/*
|
||||
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
|
||||
http://eeaax.cmap.polytchnique.fr/
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
|
||||
#ifndef _EOESMUT_H
|
||||
#define _EOESMUT_H
|
||||
|
||||
#include <eoParser.h>
|
||||
#include <eoRNG.h>
|
||||
#include <cmath> // for exp
|
||||
|
||||
#include <eoESFullChrom.h>
|
||||
#include <eoOp.h>
|
||||
|
||||
const double ES_SIGEPS = 1.0e-40; /* ES lower bound for sigma values */
|
||||
// should not be a parameter ...
|
||||
|
||||
/** ES-style mutation in the large: Obviously, valid only for eoESInd
|
||||
*/
|
||||
template <class fitT>
|
||||
class eoESMutate: public eoMonOp< eoESFullChrom<fitT> > {
|
||||
public:
|
||||
///
|
||||
eoESMutate( double _TauLcl, double _TauGlb, double _TauBeta )
|
||||
: eoMonOp< eoESFullChrom<fitT> >( ), TauLcl(_TauLcl), TauGlb(_TauGlb),
|
||||
TauBeta(_TauBeta) {};
|
||||
|
||||
/* The parser constructor
|
||||
*/
|
||||
eoESMutate(Parser & parser, unsigned _stdDevLength, unsigned _size, bool _correlated ):
|
||||
eoMonOp< eoESFullChrom<fitT> >( ) {
|
||||
parser.AddTitle("Parameters of ES mutation (before renormalization)");
|
||||
try { // we know that there is at least 1 std dev.
|
||||
if (_stdDevLength == 1) {
|
||||
TauLcl = parser.getInt("-Ml", "--TauLcl", "1",
|
||||
"TauLcl, Mutation rate for the only Std Dev." );
|
||||
// different normalization in that case -- Thomas Baeck
|
||||
TauLcl /= sqrt((double) _size);
|
||||
}
|
||||
else { /* more than 1 std dev */
|
||||
TauLcl = parser.getFloat("-Ml", "--TauLcl", "1",
|
||||
"Local mutation rate for Std Dev." );
|
||||
TauGlb = parser.getFloat("-Mg", "--TauGlb", "1",
|
||||
"Global mutation rate for Std Dev." );
|
||||
// renormalization
|
||||
TauLcl /= sqrt( 2.0 * sqrt( (double)_size ) );
|
||||
TauGlb /= sqrt( 2.0 * ( (double) _size ) );
|
||||
|
||||
if ( _correlated ) { // Correlated Mutations
|
||||
TauBeta = parser.getFloat("-Mb", "--TauBeta", "0.0873",
|
||||
"Mutation rate for corr. coeff." );
|
||||
// rotation angles: no normalization
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (exception & e)
|
||||
{
|
||||
cout << e.what() << endl;
|
||||
parser.printHelp();
|
||||
exit(1);
|
||||
}
|
||||
};
|
||||
|
||||
/// needed virtual dtor
|
||||
virtual ~eoESMutate() {};
|
||||
|
||||
// virtual separation depending wether correlated mutations are present
|
||||
virtual void operator() ( eoESFullChrom<fitT> & _eo ) const {
|
||||
if (_eo.CorCffLength())
|
||||
CorrelatedMutation(_eo);
|
||||
else
|
||||
StandardMutation(_eo);
|
||||
}
|
||||
|
||||
/** @name Methods from eoObject
|
||||
readFrom and printOn are directly inherited from eoObject
|
||||
*/
|
||||
//@{
|
||||
/** Inherited from eoObject
|
||||
@see eoObject
|
||||
*/
|
||||
virtual string className() const {return "eoESMutate";};
|
||||
|
||||
private:
|
||||
/// mutations - standard et correlated
|
||||
// =========
|
||||
/*
|
||||
* Standard mutation of object variables and standard
|
||||
* deviations in ESs.
|
||||
* If there are fewer different standard deviations available
|
||||
* than the dimension of the objective function requires, the
|
||||
* last standard deviation is responsible for ALL remaining
|
||||
* object variables.
|
||||
* Schwefel 1977: Numerische Optimierung von Computer-Modellen
|
||||
* mittels der Evolutionsstrategie, pp. 165 ff.
|
||||
*/
|
||||
|
||||
virtual void StandardMutation( eoESFullChrom<fitT> & _eo ) const {
|
||||
unsigned i,k;
|
||||
double Glb, StdLoc;
|
||||
|
||||
if (_eo.StdDevLength() == 1) { /* single StdDev -> No global factor */
|
||||
StdLoc = _eo.getStdDev(0);
|
||||
StdLoc *= exp(TauLcl*rng.normal());
|
||||
if (StdLoc < ES_SIGEPS)
|
||||
StdLoc = ES_SIGEPS;
|
||||
_eo.setStdDev(0, StdLoc);
|
||||
_eo.setGene( 0, _eo.getGene(0) + StdLoc*rng.normal());
|
||||
i = 1;
|
||||
}
|
||||
else { /* more than one std dev. */
|
||||
Glb = exp(TauGlb*rng.normal());
|
||||
for (i = 0; i < _eo.length() && i < _eo.StdDevLength(); i++) {
|
||||
StdLoc = _eo.getStdDev(i);
|
||||
StdLoc *= Glb * exp(TauLcl*rng.normal());
|
||||
if (StdLoc < ES_SIGEPS)
|
||||
StdLoc = ES_SIGEPS;
|
||||
_eo.setStdDev(i, StdLoc);
|
||||
_eo.setGene( i, _eo.getGene(i) + StdLoc*rng.normal());
|
||||
}
|
||||
}
|
||||
// last object variables: same STdDev than the preceding one
|
||||
for (k = i; k < _eo.length(); k++) {
|
||||
_eo.setGene( k, _eo.getGene(k) + StdLoc*rng.normal() );
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Correlated mutations in ESs, according to the following
|
||||
* sources:
|
||||
* H.-P. Schwefel: Internal Report of KFA Juelich, KFA-STE-IB-3/80
|
||||
* p. 43, 1980
|
||||
* G. Rudolph: Globale Optimierung mit parallelen Evolutions-
|
||||
* strategien, Diploma Thesis, University of Dortmund, 1990
|
||||
*/
|
||||
|
||||
// Code from Thomas Baeck
|
||||
|
||||
virtual void CorrelatedMutation( eoESFullChrom<fitT> & _eo ) const {
|
||||
|
||||
|
||||
int i, k, n1, n2, nq;
|
||||
|
||||
double d1, d2, S, C, Glb;
|
||||
double tmp;
|
||||
/*
|
||||
* First: mutate standard deviations (as above).
|
||||
*/
|
||||
|
||||
Glb = exp(TauGlb*rng.normal());
|
||||
for (i = 0; i < _eo.StdDevLength(); i++) {
|
||||
tmp = _eo.getStdDev(i);
|
||||
_eo.setStdDev( i, tmp*Glb*exp(TauLcl*rng.normal()) );
|
||||
}
|
||||
|
||||
/*
|
||||
* Mutate rotation angles.
|
||||
*/
|
||||
|
||||
for (i = 0; i < _eo.CorCffLength(); i++) {
|
||||
tmp = _eo.getCorCff(i);
|
||||
tmp += TauBeta*rng.normal();
|
||||
// danger of VERY long loops --MS--
|
||||
// while (CorCff[i] > M_PI)
|
||||
// CorCff[i] -= 2.0 * M_PI;
|
||||
// while (CorCff[i] < - M_PI)
|
||||
// CorCff[i] += 2.0 * M_PI;
|
||||
if ( fabs(tmp) > M_PI ) {
|
||||
tmp -= M_PI * (int) (tmp/M_PI) ;
|
||||
}
|
||||
_eo.setCorCff(i, tmp);
|
||||
}
|
||||
|
||||
/*
|
||||
* Perform correlated mutations.
|
||||
*/
|
||||
vector<double> VarStp(_eo.size());
|
||||
for (i = 0; i < _eo.size() && i < _eo.StdDevLength(); i++)
|
||||
VarStp[i] = _eo.getStdDev(i)*rng.normal();
|
||||
for (k = i; k < _eo.size(); k++)
|
||||
VarStp[k] = _eo.getStdDev(i-1)*rng.normal();
|
||||
nq = _eo.CorCffLength() - 1;
|
||||
for (k = _eo.size()-_eo.StdDevLength(); k < _eo.size()-1; k++) {
|
||||
n1 = _eo.size() - k - 1;
|
||||
n2 = _eo.size() - 1;
|
||||
for (i = 0; i < k; i++) {
|
||||
d1 = VarStp[n1];
|
||||
d2 = VarStp[n2];
|
||||
S = sin( _eo.getCorCff(nq) );
|
||||
C = cos( _eo.getCorCff(nq) );
|
||||
VarStp[n2] = d1 * S + d2 * C;
|
||||
VarStp[n1] = d1 * C - d2 * S;
|
||||
n2--;
|
||||
nq--;
|
||||
}
|
||||
}
|
||||
for (i = 0; i < _eo.size(); i++)
|
||||
_eo[i] += VarStp[i];
|
||||
|
||||
}
|
||||
// the data
|
||||
//=========
|
||||
double TauLcl; /* Local factor for mutation of std deviations */
|
||||
double TauGlb; /* Global factor for mutation of std deviations */
|
||||
double TauBeta; /* Factor for mutation of correlation parameters */
|
||||
};
|
||||
|
||||
/*
|
||||
* Correlated mutations in ESs, according to the following
|
||||
* sources:
|
||||
* H.-P. Schwefel: Internal Report of KFA Juelich, KFA-STE-IB-3/80
|
||||
* p. 43, 1980
|
||||
* G. Rudolph: Globale Optimierung mit parallelen Evolutions-
|
||||
* strategien, Diploma Thesis, University of Dortmund, 1990
|
||||
*/
|
||||
// Not yet implemented!
|
||||
|
||||
#endif
|
||||
|
||||
|
|
|
|||
|
|
@ -41,9 +41,9 @@ template<class Chrom> class eoEasyEA: public eoAlgo<Chrom>
|
|||
{
|
||||
public:
|
||||
/// Constructor.
|
||||
eoEasyEA(eoSelect<Chrom>& _select,
|
||||
eoTransform<Chrom>& _transform,
|
||||
eoMerge<Chrom>& _replace,
|
||||
eoEasyEA(eoBinPopOp<Chrom>& _select,
|
||||
eoMonPopOp<Chrom>& _transform,
|
||||
eoBinPopOp<Chrom>& _replace,
|
||||
eoEvalFunc<Chrom>& _evaluator,
|
||||
eoTerm<Chrom>& _terminator)
|
||||
:step(_select, _transform, _replace, _evaluator),
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ template<class EOT> struct eoEvalFunc {
|
|||
typedef typename EOT::Fitness EOFitT;
|
||||
#endif
|
||||
|
||||
/// Effectively applies the evaluation function to an EO or urEO
|
||||
/// Effectively applies the evaluation function to an EO
|
||||
virtual void operator() ( EOT & _eo ) const = 0;
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -1,72 +0,0 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoEvaluator.h
|
||||
// (c) GeNeura Team, 1998
|
||||
/*
|
||||
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
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef _EOEVALUATOR_H
|
||||
#define _EOEVALUATOR_H
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
#include <eoPopOps.h>
|
||||
#include <eoEvalFunc.h>
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
/** Evaluator takes a vector of EOs and evaluates its fitness
|
||||
* returning void. Template instances must be of fitness and EO type
|
||||
*/
|
||||
template<class EOT>
|
||||
class eoEvaluator: public eoTransform<EOT>{
|
||||
public:
|
||||
/// ctor
|
||||
eoEvaluator( const eoEvalFunc< EOT> & _ef )
|
||||
: eoTransform<EOT>(), repEF( _ef ){};
|
||||
|
||||
/// Needed virtual destructor
|
||||
virtual ~eoEvaluator() {};
|
||||
|
||||
/* Sets the evaluation function
|
||||
virtual void EF( const eoEvalFunc< EOT> & _ef ) { repEF= _ef;};*/
|
||||
|
||||
/// Gets the evaluation function
|
||||
virtual const eoEvalFunc< EOT>& EF() { return repEF;};
|
||||
|
||||
/** This is the actual function operator(); it is left without implementation.
|
||||
It takes a vector of pointers to eo
|
||||
* @param _vEO is a vector of pointers to eo, that will be evaluated
|
||||
*/
|
||||
|
||||
virtual void operator() ( EOT& _eot ) const = 0;
|
||||
|
||||
///@name eoObject methods
|
||||
//@{
|
||||
/** Return the class id */
|
||||
virtual string className() const { return "eoEvaluator"; }
|
||||
|
||||
/** Read and print are left without implementation */
|
||||
//@}
|
||||
|
||||
private:
|
||||
const eoEvalFunc< EOT> & repEF;
|
||||
};
|
||||
//@}
|
||||
|
||||
#endif
|
||||
|
|
@ -1,80 +1,80 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoOp.h
|
||||
// (c) GeNeura Team, 1998
|
||||
/*
|
||||
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
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef eoGeneration_h
|
||||
#define eoGeneration_h
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <eoAlgo.h> // eoPop
|
||||
#include <eoEvalFunc.h>
|
||||
#include <eoPopOps.h> // eoSelect, eoTranform, eoMerge
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoGeneration
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
template<class Chrom> class eoGeneration: public eoAlgo<Chrom>
|
||||
{
|
||||
public:
|
||||
/// Constructor.
|
||||
eoGeneration(eoSelect<Chrom>& _select,
|
||||
eoTransform<Chrom>& _transform,
|
||||
eoMerge<Chrom>& _replace,
|
||||
eoEvalFunc<Chrom>& _evaluator):
|
||||
select(_select), transform(_transform),
|
||||
replace(_replace), evaluator( _evaluator) {};
|
||||
|
||||
/// Copy Constructor.
|
||||
eoGeneration(eoGeneration<Chrom>& _gen):
|
||||
select(_gen.select), transform(_gen.transform),
|
||||
replace(_gen.replace), evaluator( _gen.evaluator ) {};
|
||||
|
||||
|
||||
/// Apply one generation of evolution to the population.
|
||||
virtual void operator()(eoPop<Chrom>& pop) {
|
||||
eoPop<Chrom> breeders;
|
||||
select(pop, breeders);
|
||||
transform(breeders);
|
||||
eoPop<Chrom>::iterator i;
|
||||
// Can't use foreach here since foreach takes the
|
||||
// parameter by reference
|
||||
for ( i = breeders.begin(); i != breeders.end(); i++)
|
||||
evaluator(*i);
|
||||
replace(breeders, pop);
|
||||
}
|
||||
|
||||
/// Class name.
|
||||
string className() const { return "eoGeneration"; }
|
||||
|
||||
private:
|
||||
eoSelect<Chrom>& select;
|
||||
eoTransform<Chrom>& transform;
|
||||
eoMerge<Chrom>& replace;
|
||||
eoEvalFunc<Chrom>& evaluator;
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#endif eoGeneration_h
|
||||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoOp.h
|
||||
// (c) GeNeura Team, 1998
|
||||
/*
|
||||
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
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef eoGeneration_h
|
||||
#define eoGeneration_h
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <eoAlgo.h> // eoPop
|
||||
#include <eoEvalFunc.h>
|
||||
#include <eoPopOps.h> // eoSelect, eoTranform, eoMerge
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoGeneration
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
template<class Chrom> class eoGeneration: public eoAlgo<Chrom>
|
||||
{
|
||||
public:
|
||||
/// Constructor.
|
||||
eoGeneration(eoBinPopOp<Chrom>& _select,
|
||||
eoMonPopOp<Chrom>& _transform,
|
||||
eoBinPopOp<Chrom>& _replace,
|
||||
eoEvalFunc<Chrom>& _evaluator):
|
||||
select(_select), transform(_transform),
|
||||
replace(_replace), evaluator( _evaluator) {};
|
||||
|
||||
/// Copy Constructor.
|
||||
eoGeneration(eoGeneration<Chrom>& _gen):
|
||||
select(_gen.select), transform(_gen.transform),
|
||||
replace(_gen.replace), evaluator( _gen.evaluator ) {};
|
||||
|
||||
|
||||
/// Apply one generation of evolution to the population.
|
||||
virtual void operator()(eoPop<Chrom>& pop) {
|
||||
eoPop<Chrom> breeders;
|
||||
select(pop, breeders);
|
||||
transform(breeders);
|
||||
eoPop<Chrom>::iterator i;
|
||||
// Can't use foreach here since foreach takes the
|
||||
// parameter by reference
|
||||
for ( i = breeders.begin(); i != breeders.end(); i++)
|
||||
evaluator(*i);
|
||||
replace(breeders, pop);
|
||||
}
|
||||
|
||||
/// Class name.
|
||||
string className() const { return "eoGeneration"; }
|
||||
|
||||
private:
|
||||
eoBinPopOp<Chrom>& select;
|
||||
eoMonPopOp<Chrom>& transform;
|
||||
eoBinPopOp<Chrom>& replace;
|
||||
eoEvalFunc<Chrom>& evaluator;
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#endif eoGeneration_h
|
||||
|
|
|
|||
|
|
@ -7,8 +7,11 @@
|
|||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <eoPop.h> // eoPop
|
||||
#include <eoPopOps.h> // eoMerge
|
||||
#include <iostream>
|
||||
|
||||
// EO includes
|
||||
#include <eoPop.h>
|
||||
#include <eoMerge.h>
|
||||
|
||||
/*****************************************************************************
|
||||
* eoInclusion: A replacement algorithm. *
|
||||
|
|
@ -20,7 +23,13 @@ template<class Chrom> class eoInclusion: public eoMerge<Chrom>
|
|||
{
|
||||
public:
|
||||
/// (Default) Constructor.
|
||||
eoInclusion(const float& _rate = 1.0): eoMerge<Chrom>(_rate) {}
|
||||
eoInclusion(const float& _rate = 1.0): eoMerge<Chrom>( _rate ) {}
|
||||
|
||||
/// Ctor from istream
|
||||
eoInclusion( istream& _is): eoBinPopOp<Chrom>( _is ) {};
|
||||
|
||||
/// Dtor
|
||||
virtual ~eoInclusion() {};
|
||||
|
||||
/**
|
||||
* Creates a new population based on breeders and original populations.
|
||||
|
|
@ -38,6 +47,16 @@ template<class Chrom> class eoInclusion: public eoMerge<Chrom>
|
|||
greater<Chrom>());
|
||||
pop.erase(pop.begin() + target, pop.end());
|
||||
}
|
||||
|
||||
/** @name Methods from eoObject */
|
||||
//@{
|
||||
/** readFrom and printOn inherited from eoMerge */
|
||||
|
||||
/** Inherited from eoObject. Returns the class name.
|
||||
@see eoObject
|
||||
*/
|
||||
virtual string className() const {return "eoMerge";};
|
||||
//@}
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
|
|
|||
|
|
@ -1,79 +1,96 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// eoInsertion.h
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef eoInsertion_h
|
||||
#define eoInsertion_h
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <eoPop.h> // eoPop
|
||||
#include <eoPopOps.h> // eoMerge
|
||||
|
||||
/******************************************************************************
|
||||
* eoInsertion: A replacement algorithm.
|
||||
* Creates a new population with all the breeders and the best individuals
|
||||
* from the original population.
|
||||
*****************************************************************************/
|
||||
|
||||
template<class Chrom> class eoInsertion: public eoMerge<Chrom>
|
||||
{
|
||||
public:
|
||||
/// (Default) Constructor.
|
||||
eoInsertion(const float& _rate = 1.0): eoMerge<Chrom>(_rate) {}
|
||||
|
||||
/**
|
||||
* Creates a new population based on breeders and original populations.
|
||||
* @param breeders The population of breeders.
|
||||
* @param pop The original population.
|
||||
*/
|
||||
/*void operator()(eoPop<Chrom>& breeders, eoPop<Chrom>& pop)
|
||||
{
|
||||
int new_size = static_cast<int>(pop.size() * rate());
|
||||
|
||||
if (new_size == breeders.size())
|
||||
{
|
||||
pop = breeders;
|
||||
}
|
||||
else if (new_size < breeders.size())
|
||||
{
|
||||
pop = breeders;
|
||||
sort(pop.begin(), pop.end());
|
||||
pop.erase(pop.begin(), pop.begin() - new_size + pop.size());
|
||||
}
|
||||
else
|
||||
{
|
||||
sort(pop.begin(), pop.end());
|
||||
pop.erase(pop.begin(),
|
||||
pop.begin() + breeders.size() + pop.size() - new_size);
|
||||
copy(breeders.begin(), breeders.end(),
|
||||
back_insert_iterator<eoPop<Chrom> >(pop));
|
||||
}
|
||||
}*/
|
||||
|
||||
void operator()(eoPop<Chrom>& breeders, eoPop<Chrom>& pop)
|
||||
{
|
||||
unsigned target = static_cast<unsigned>(rint(pop.size() * rate()));
|
||||
|
||||
pop.swap(breeders);
|
||||
|
||||
if (target < pop.size())
|
||||
{
|
||||
partial_sort(pop.begin(), pop.begin() + target, pop.end(),
|
||||
greater<Chrom>());
|
||||
pop.erase(pop.begin() + target, pop.end());
|
||||
}
|
||||
else
|
||||
{
|
||||
target = min(target - pop.size(), breeders.size());
|
||||
partial_sort(breeders.begin(), breeders.begin() + target,
|
||||
breeders.end(), greater<Chrom>());
|
||||
copy(breeders.begin(), breeders.begin() + target,
|
||||
back_insert_iterator<eoPop<Chrom> >(pop));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#endif eoInsertion_h
|
||||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoInsertion.h
|
||||
// Inserts new members into the population
|
||||
// (c) GeNeura Team, 1998
|
||||
/*
|
||||
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
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef eoInsertion_h
|
||||
#define eoInsertion_h
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <iostream>
|
||||
|
||||
// EO includes
|
||||
#include <eoPop.h> // eoPop
|
||||
#include <eoMerge.h> // eoMerge
|
||||
|
||||
/******************************************************************************
|
||||
* eoInsertion: A replacement algorithm.
|
||||
* Creates a new population with all the breeders and the best individuals
|
||||
* from the original population.
|
||||
*****************************************************************************/
|
||||
|
||||
template<class Chrom> class eoInsertion: public eoMerge<Chrom>
|
||||
{
|
||||
public:
|
||||
/// (Default) Constructor.
|
||||
eoInsertion(const float& _rate = 1.0): eoMerge<Chrom>( _rate ) {}
|
||||
|
||||
/// Ctor from istream
|
||||
eoInsertion( istream& _is): eoBinPopOp<Chrom>( _is ) {};
|
||||
|
||||
/// Dtor
|
||||
virtual ~eoInsertion() {};
|
||||
|
||||
/**
|
||||
* Creates a new population based on breeders and original populations.
|
||||
* @param breeders The population of breeders. Should be sorted to work correctly
|
||||
* @param pop The original population.
|
||||
*/
|
||||
void operator()( eoPop<Chrom>& _breeders, eoPop<Chrom>& _pop)
|
||||
{
|
||||
unsigned target = static_cast<unsigned>(rint(_pop.size() * rate()));
|
||||
|
||||
_pop.swap(_breeders);
|
||||
|
||||
if (target < _pop.size())
|
||||
{
|
||||
partial_sort(_pop.begin(), _pop.begin() + target, _pop.end(),
|
||||
greater<Chrom>());
|
||||
_pop.erase(_pop.begin() + target, _pop.end());
|
||||
}
|
||||
else
|
||||
{
|
||||
target = min(target - _pop.size(), _breeders.size());
|
||||
partial_sort(_breeders.begin(), _breeders.begin() + target,
|
||||
_breeders.end(), greater<Chrom>());
|
||||
copy(_breeders.begin(), _breeders.begin() + target,
|
||||
back_insert_iterator<eoPop<Chrom> >(_pop));
|
||||
}
|
||||
};
|
||||
|
||||
/** @name Methods from eoObject */
|
||||
//@{
|
||||
/** readFrom and printOn inherited from eoMerge */
|
||||
|
||||
/** Inherited from eoObject. Returns the class name.
|
||||
@see eoObject
|
||||
*/
|
||||
virtual string className() const {return "eoInsertion";};
|
||||
//@}
|
||||
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#endif eoInsertion_h
|
||||
|
|
|
|||
|
|
@ -1,5 +1,26 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoLottery.h
|
||||
// Implements the lottery procedure for selection
|
||||
// (c) GeNeura Team, 1998
|
||||
/*
|
||||
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
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef eoLottery_h
|
||||
|
|
@ -16,14 +37,14 @@
|
|||
/// requires Chrom::Fitness to be float castable
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
template<class Chrom> class eoLottery: public eoSelect<Chrom>
|
||||
template<class Chrom> class eoLottery: public eoBinPopOp<Chrom>
|
||||
{
|
||||
public:
|
||||
/// (Default) Constructor.
|
||||
eoLottery(const float& _rate = 1.0): rate(_rate) {}
|
||||
|
||||
///
|
||||
void operator()(const eoPop<Chrom>& pop, eoPop<Chrom>& breeders) const
|
||||
void operator()( eoPop<Chrom>& pop, eoPop<Chrom>& breeders)
|
||||
{
|
||||
// scores of chromosomes
|
||||
vector<float> score(pop.size());
|
||||
|
|
|
|||
115
eo/src/eoMerge.h
Normal file
115
eo/src/eoMerge.h
Normal file
|
|
@ -0,0 +1,115 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoMerge.h
|
||||
// Base class for population-merging classes
|
||||
// (c) GeNeura Team, 1998
|
||||
/*
|
||||
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
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef eoMerge_h
|
||||
#define eoMerge_h
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <iostream>
|
||||
|
||||
// EO includes
|
||||
#include <eoPop.h> // eoPop
|
||||
#include <eoPopOps.h> // eoMerge
|
||||
|
||||
/**
|
||||
* eoMerge: Base class for replacement algorithms
|
||||
*/
|
||||
|
||||
template<class Chrom> class eoMerge: public eoBinPopOp<Chrom>
|
||||
{
|
||||
public:
|
||||
/// (Default) Constructor.
|
||||
eoMerge(const float& _rate = 1.0): eoBinPopOp<Chrom>(), repRate( _rate ) {}
|
||||
|
||||
/// Ctor from istream
|
||||
eoMerge( istream& _is): eoBinPopOp<Chrom>() { readFrom( _is ); };
|
||||
|
||||
/**
|
||||
* Creates a new population based on breeders and original populations.
|
||||
* @param breeders The population of breeders. Should be sorted to work correctly
|
||||
* @param pop The original population.
|
||||
*/
|
||||
void operator()( eoPop<Chrom>& _breeders, eoPop<Chrom>& _pop)
|
||||
{
|
||||
unsigned target = static_cast<unsigned>(rint(_pop.size() * rate()));
|
||||
|
||||
_pop.swap(_breeders);
|
||||
|
||||
if (target < _pop.size())
|
||||
{
|
||||
partial_sort(_pop.begin(), _pop.begin() + target, _pop.end(),
|
||||
greater<Chrom>());
|
||||
_pop.erase(_pop.begin() + target, _pop.end());
|
||||
}
|
||||
else
|
||||
{
|
||||
target = min(target - _pop.size(), _breeders.size());
|
||||
partial_sort(_breeders.begin(), _breeders.begin() + target,
|
||||
_breeders.end(), greater<Chrom>());
|
||||
copy(_breeders.begin(), _breeders.begin() + target,
|
||||
back_insert_iterator<eoPop<Chrom> >(_pop));
|
||||
}
|
||||
};
|
||||
|
||||
/** @name Methods from eoObject */
|
||||
//@{
|
||||
/**
|
||||
* Read object. The EOT class must have a ctor from a stream;
|
||||
in this case, a strstream is used.
|
||||
* @param _is A istream.
|
||||
|
||||
*/
|
||||
virtual void readFrom(istream& _is) {
|
||||
_is >> repRate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write object. Prints relevant parameters to standard output
|
||||
* @param _os A ostream. In this case, prints the population to
|
||||
standard output. The EOT class must hav standard output with cout,
|
||||
but since it should be an eoObject anyways, it's no big deal.
|
||||
*/
|
||||
virtual void printOn(ostream& _os) const {
|
||||
_os << repRate;
|
||||
};
|
||||
|
||||
/** Inherited from eoObject. Returns the class name.
|
||||
@see eoObject
|
||||
*/
|
||||
virtual string className() const {return "eoMerge";};
|
||||
//@}
|
||||
|
||||
protected:
|
||||
float rate() { return repRate;};
|
||||
|
||||
private:
|
||||
float repRate;
|
||||
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#endif eoInsertion_h
|
||||
99
eo/src/eoMultiBinOp.h
Normal file
99
eo/src/eoMultiBinOp.h
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoMultiBinOp.h
|
||||
// Class that combines several binary or unary operators
|
||||
// (c) GeNeura Team, 1998
|
||||
/*
|
||||
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
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef _EOMULTIBINOP_h
|
||||
#define _EOMULTIBINOP_h
|
||||
|
||||
#include <iterator>
|
||||
|
||||
#include <eoOp.h>
|
||||
|
||||
/** MultiMonOp combines several monary operators. By itself, it does nothing to the
|
||||
EO it´s handled*/
|
||||
template <class EOT>
|
||||
class eoMultiBinOp: public eoBinOp<EOT> {
|
||||
public:
|
||||
/// Ctor from an already existing op
|
||||
eoMultiBinOp( const eoBinOp<EOT>* _op )
|
||||
: eoBinOp< EOT >( ), vOp(){
|
||||
vOp.push_back( _op );
|
||||
};
|
||||
|
||||
///
|
||||
eoMultiBinOp( )
|
||||
: eoBinOp< EOT >( ), vOp(){};
|
||||
|
||||
/// Ads a new operator
|
||||
void adOp( const eoOp<EOT>* _op ){
|
||||
vOp.push_back( _op );
|
||||
};
|
||||
|
||||
/// needed virtual dtor
|
||||
virtual ~eoMultiBinOp() {};
|
||||
|
||||
///
|
||||
/// Applies all operators to the EO
|
||||
virtual void operator()( EOT& _eo1, EOT& _eo2 ) const {
|
||||
if ( vOp.begin() != vOp.end() ) { // which would mean it's empty
|
||||
for ( vector< const eoOp<EOT>* >::const_iterator i = vOp.begin();
|
||||
i != vOp.end(); i++ ) {
|
||||
// Admits only unary or binary operator
|
||||
switch ((*i)->readArity()) {
|
||||
case unary:
|
||||
{
|
||||
const eoMonOp<EOT>* monop = static_cast<const eoMonOp<EOT>* >(*i);
|
||||
(*monop)( _eo1 );
|
||||
(*monop)( _eo2 );
|
||||
break;
|
||||
}
|
||||
case binary:
|
||||
{
|
||||
const eoBinOp<EOT>* binop = static_cast<const eoBinOp<EOT>* >(*i);
|
||||
(*binop)( _eo1, _eo2 );
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/** @name Methods from eoObject
|
||||
readFrom and printOn are directly inherited from eoOp
|
||||
*/
|
||||
//@{
|
||||
/** Inherited from eoObject
|
||||
@see eoObject
|
||||
*/
|
||||
string className() const {return "eoMultiBinOp";};
|
||||
//@}
|
||||
|
||||
private:
|
||||
|
||||
/// uses pointers to base class since operators can be unary or binary
|
||||
vector< const eoOp<EOT>* > vOp;
|
||||
};
|
||||
|
||||
#endif
|
||||
408
eo/src/eoOp.h
408
eo/src/eoOp.h
|
|
@ -1,204 +1,204 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoOp.h
|
||||
// (c) GeNeura Team, 1998
|
||||
/*
|
||||
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
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef _eoOp_H
|
||||
#define _eoOp_H
|
||||
|
||||
#include <vector>
|
||||
#include <eoObject.h>
|
||||
#include <eoPrintable.h>
|
||||
|
||||
/** @name Genetic operators
|
||||
|
||||
What is a genetic algorithm without genetic operators? There is a genetic operator hierarchy, with
|
||||
eoOp as father and eoMonOp (monary or unary operator) and eoBinOp (binary operator) as siblings. Nobody
|
||||
should subclass eoOp, you should subclass eoBinOp or eoMonOp, those are the ones actually used here.\\
|
||||
#eoOp#s are only printable objects, so if you want to build them from a file, it has to
|
||||
be done in another class, namely factories. Each hierarchy of #eoOp#s should have its own
|
||||
factory, which know how to build them from a description in a file.
|
||||
@author GeNeura Team
|
||||
@version 0.1
|
||||
@see eoOpFactory
|
||||
*/
|
||||
//@{
|
||||
|
||||
///
|
||||
enum Arity { unary = 0, binary = 1, Nary = 2};
|
||||
|
||||
/** Abstract data types for EO operators.
|
||||
* Genetic operators act on chromosomes, changing them. The type to instantiate them should
|
||||
* be an eoObject, but in any case, they are type-specific; each kind of evolvable object
|
||||
* can have its own operators
|
||||
*/
|
||||
template<class EOType>
|
||||
class eoOp: public eoObject, public eoPrintable {
|
||||
public:
|
||||
|
||||
/// Ctor
|
||||
eoOp( Arity _arity = unary )
|
||||
:arity( _arity ) {};
|
||||
|
||||
/// Copy Ctor
|
||||
eoOp( const eoOp& _eop )
|
||||
:arity( _eop.arity ) {};
|
||||
|
||||
/// Needed virtual destructor
|
||||
virtual ~eoOp(){};
|
||||
|
||||
/// Arity: number of operands
|
||||
Arity readArity() const {return arity;};
|
||||
|
||||
/** @name Methods from eoObject */
|
||||
//@{
|
||||
|
||||
/**
|
||||
* Write object. It's called printOn since it prints the object _on_ a stream.
|
||||
* @param _os A ostream.
|
||||
*/
|
||||
virtual void printOn(ostream& _os) const {
|
||||
_os << className();
|
||||
// _os << arity;
|
||||
};
|
||||
|
||||
/** Inherited from eoObject
|
||||
@see eoObject
|
||||
*/
|
||||
virtual string className() const {return "eoOp";};
|
||||
//@}
|
||||
|
||||
|
||||
private:
|
||||
/// arity is the number of operands it takes
|
||||
Arity arity;
|
||||
|
||||
};
|
||||
|
||||
/** Binary genetic operator: subclasses eoOp, and defines
|
||||
basically the operator() with two operands
|
||||
*/
|
||||
template<class EOType>
|
||||
class eoBinOp: public eoOp<EOType> {
|
||||
public:
|
||||
|
||||
/// Ctor
|
||||
eoBinOp()
|
||||
:eoOp<EOType>( binary ) {};
|
||||
|
||||
/// Copy Ctor
|
||||
eoBinOp( const eoBinOp& _ebop )
|
||||
: eoOp<EOType>( _ebop ){};
|
||||
|
||||
/// Dtor
|
||||
~eoBinOp () {};
|
||||
|
||||
/** applies operator, to the object. Modifies both operands.
|
||||
*/
|
||||
virtual void operator()( EOType& _eo1, EOType& _eo2 ) const = 0;
|
||||
|
||||
/** @name Methods from eoObject
|
||||
readFrom and printOn are directly inherited from eoObject
|
||||
*/
|
||||
//@{
|
||||
/** Inherited from eoObject
|
||||
@see eoObject
|
||||
*/
|
||||
virtual string className() const {return "eoBinOp";};
|
||||
//@}
|
||||
|
||||
};
|
||||
|
||||
/** eoMonOp is the monary operator: genetic operator that takes
|
||||
only one EO
|
||||
*/
|
||||
template <class EOType>
|
||||
class eoMonOp: public eoOp<EOType> {
|
||||
public:
|
||||
|
||||
/// Ctor
|
||||
eoMonOp( )
|
||||
:eoOp<EOType>( unary ) {};
|
||||
|
||||
/// Copy Ctor
|
||||
eoMonOp( const eoMonOp& _emop )
|
||||
: eoOp<EOType>( _emop ){};
|
||||
|
||||
/// Dtor
|
||||
~eoMonOp() {};
|
||||
|
||||
/** applies randomly operator, to the object. If arity is more than 1,
|
||||
* keeps a copy of the operand in a cache.
|
||||
*/
|
||||
virtual void operator()( EOType& _eo1) const = 0;
|
||||
|
||||
/** @name Methods from eoObject
|
||||
readFrom and printOn are directly inherited from eoObject
|
||||
*/
|
||||
//@{
|
||||
/** Inherited from eoObject
|
||||
@see eoObject
|
||||
*/
|
||||
virtual string className() const {return "eoMonOp";};
|
||||
//@}
|
||||
|
||||
|
||||
};
|
||||
|
||||
#include <eoPop.h>
|
||||
/** eoNaryOp is the N-ary operator: genetic operator that takes
|
||||
several EOs. It could be called an {\em orgy} operator. It's a general operator
|
||||
that takes any number of inputs and spits out any number of outputs
|
||||
*/
|
||||
template <class EOType>
|
||||
class eoNaryOp: public eoOp<EOType> {
|
||||
public:
|
||||
|
||||
/// Ctor
|
||||
eoNaryOp( )
|
||||
:eoOp<EOType>( Nary ) {};
|
||||
|
||||
/// Copy Ctor
|
||||
eoNaryOp( const eoNaryOp& _emop )
|
||||
: eoOp<EOType>( _emop ){};
|
||||
|
||||
/// Dtor
|
||||
~eoNaryOp() {};
|
||||
|
||||
/** applies randomly operator, to the object.
|
||||
*/
|
||||
virtual void operator()( const eoPop<EOType> & _in, eoPop<EOType> _out ) const = 0;
|
||||
|
||||
/** @name Methods from eoObject
|
||||
readFrom and printOn are directly inherited from eoObject.
|
||||
*/
|
||||
//@{
|
||||
/** Inherited from eoObject
|
||||
@see eoObject
|
||||
*/
|
||||
string className() const {return "eoNaryOp";};
|
||||
//@}
|
||||
|
||||
};
|
||||
//@}
|
||||
|
||||
#endif
|
||||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoOp.h
|
||||
// (c) GeNeura Team, 1998
|
||||
/*
|
||||
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
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef _eoOp_H
|
||||
#define _eoOp_H
|
||||
|
||||
#include <vector>
|
||||
#include <eoObject.h>
|
||||
#include <eoPrintable.h>
|
||||
|
||||
/** @name Genetic operators
|
||||
|
||||
What is a genetic algorithm without genetic operators? There is a genetic operator hierarchy, with
|
||||
eoOp as father and eoMonOp (monary or unary operator) and eoBinOp (binary operator) as siblings. Nobody
|
||||
should subclass eoOp, you should subclass eoBinOp or eoMonOp, those are the ones actually used here.\\
|
||||
#eoOp#s are only printable objects, so if you want to build them from a file, it has to
|
||||
be done in another class, namely factories. Each hierarchy of #eoOp#s should have its own
|
||||
factory, which know how to build them from a description in a file.
|
||||
@author GeNeura Team
|
||||
@version 0.1
|
||||
@see eoOpFactory
|
||||
*/
|
||||
//@{
|
||||
|
||||
///
|
||||
enum Arity { unary = 0, binary = 1, Nary = 2};
|
||||
|
||||
/** Abstract data types for EO operators.
|
||||
* Genetic operators act on chromosomes, changing them. The type to instantiate them should
|
||||
* be an eoObject, but in any case, they are type-specific; each kind of evolvable object
|
||||
* can have its own operators
|
||||
*/
|
||||
template<class EOType>
|
||||
class eoOp: public eoObject, public eoPrintable {
|
||||
public:
|
||||
|
||||
/// Ctor
|
||||
eoOp( Arity _arity = unary )
|
||||
:arity( _arity ) {};
|
||||
|
||||
/// Copy Ctor
|
||||
eoOp( const eoOp& _eop )
|
||||
:arity( _eop.arity ) {};
|
||||
|
||||
/// Needed virtual destructor
|
||||
virtual ~eoOp(){};
|
||||
|
||||
/// Arity: number of operands
|
||||
Arity readArity() const {return arity;};
|
||||
|
||||
/** @name Methods from eoObject */
|
||||
//@{
|
||||
|
||||
/**
|
||||
* Write object. It's called printOn since it prints the object _on_ a stream.
|
||||
* @param _os A ostream.
|
||||
*/
|
||||
virtual void printOn(ostream& _os) const {
|
||||
_os << className();
|
||||
// _os << arity;
|
||||
};
|
||||
|
||||
/** Inherited from eoObject
|
||||
@see eoObject
|
||||
*/
|
||||
virtual string className() const {return "eoOp";};
|
||||
//@}
|
||||
|
||||
|
||||
private:
|
||||
/// arity is the number of operands it takes
|
||||
Arity arity;
|
||||
|
||||
};
|
||||
|
||||
/** Binary genetic operator: subclasses eoOp, and defines
|
||||
basically the operator() with two operands
|
||||
*/
|
||||
template<class EOType>
|
||||
class eoBinOp: public eoOp<EOType> {
|
||||
public:
|
||||
|
||||
/// Ctor
|
||||
eoBinOp()
|
||||
:eoOp<EOType>( binary ) {};
|
||||
|
||||
/// Copy Ctor
|
||||
eoBinOp( const eoBinOp& _ebop )
|
||||
: eoOp<EOType>( _ebop ){};
|
||||
|
||||
/// Dtor
|
||||
~eoBinOp () {};
|
||||
|
||||
/** applies operator, to the object. Modifies both operands.
|
||||
*/
|
||||
virtual void operator()( EOType& _eo1, EOType& _eo2 ) const = 0;
|
||||
|
||||
/** @name Methods from eoObject
|
||||
readFrom and printOn are directly inherited from eoObject
|
||||
*/
|
||||
//@{
|
||||
/** Inherited from eoObject
|
||||
@see eoObject
|
||||
*/
|
||||
virtual string className() const {return "eoBinOp";};
|
||||
//@}
|
||||
|
||||
};
|
||||
|
||||
/** eoMonOp is the monary operator: genetic operator that takes
|
||||
only one EO
|
||||
*/
|
||||
template <class EOType>
|
||||
class eoMonOp: public eoOp<EOType> {
|
||||
public:
|
||||
|
||||
/// Ctor
|
||||
eoMonOp( )
|
||||
:eoOp<EOType>( unary ) {};
|
||||
|
||||
/// Copy Ctor
|
||||
eoMonOp( const eoMonOp& _emop )
|
||||
: eoOp<EOType>( _emop ){};
|
||||
|
||||
/// Dtor
|
||||
~eoMonOp() {};
|
||||
|
||||
/** applies randomly operator, to the object. If arity is more than 1,
|
||||
* keeps a copy of the operand in a cache.
|
||||
*/
|
||||
virtual void operator()( EOType& _eo1) const = 0;
|
||||
|
||||
/** @name Methods from eoObject
|
||||
readFrom and printOn are directly inherited from eoObject
|
||||
*/
|
||||
//@{
|
||||
/** Inherited from eoObject
|
||||
@see eoObject
|
||||
*/
|
||||
virtual string className() const {return "eoMonOp";};
|
||||
//@}
|
||||
|
||||
|
||||
};
|
||||
|
||||
#include <eoPop.h>
|
||||
/** eoNaryOp is the N-ary operator: genetic operator that takes
|
||||
several EOs. It could be called an {\em orgy} operator. It's a general operator
|
||||
that takes any number of inputs and spits out any number of outputs
|
||||
*/
|
||||
template <class EOType>
|
||||
class eoNaryOp: public eoOp<EOType> {
|
||||
public:
|
||||
|
||||
/// Ctor
|
||||
eoNaryOp( )
|
||||
:eoOp<EOType>( Nary ) {};
|
||||
|
||||
/// Copy Ctor
|
||||
eoNaryOp( const eoNaryOp& _emop )
|
||||
: eoOp<EOType>( _emop ){};
|
||||
|
||||
/// Dtor
|
||||
~eoNaryOp() {};
|
||||
|
||||
/** applies randomly operator, to the object.
|
||||
*/
|
||||
virtual void operator()( const eoPop<EOType> & _in, eoPop<EOType> _out ) const = 0;
|
||||
|
||||
/** @name Methods from eoObject
|
||||
readFrom and printOn are directly inherited from eoObject.
|
||||
*/
|
||||
//@{
|
||||
/** Inherited from eoObject
|
||||
@see eoObject
|
||||
*/
|
||||
string className() const {return "eoNaryOp";};
|
||||
//@}
|
||||
|
||||
};
|
||||
//@}
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -833,7 +833,7 @@ void InitRandom( Parser & parser) {
|
|||
}
|
||||
|
||||
if (_seed == 0) { // use clock to get a "random" seed
|
||||
_seed = unsigned long( time( 0 ) );
|
||||
_seed = (unsigned long)( time( 0 ) );
|
||||
ostrstream s;
|
||||
s << _seed;
|
||||
parser.setParamValue("--seed", s.str()); // so it will be printed out in the status file, and canbe later re-used to re-run EXACTLY the same run
|
||||
|
|
|
|||
291
eo/src/eoPop.h
291
eo/src/eoPop.h
|
|
@ -1,147 +1,144 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoPop.h
|
||||
// (c) GeNeura Team, 1998
|
||||
/*
|
||||
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
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef _EOPOP_H
|
||||
#define _EOPOP_H
|
||||
|
||||
#include <vector>
|
||||
#include <strstream>
|
||||
|
||||
using namespace std;
|
||||
// from file
|
||||
|
||||
#include <eoRnd.h>
|
||||
#include <eoPersistent.h>
|
||||
|
||||
/** Subpopulation: it is used to move parts of population
|
||||
from one algorithm to another and one population to another. It is safer
|
||||
to declare it as a separate object. I have no idea if a population can be
|
||||
some other thing that a vector, but if somebody thinks of it, this concrete
|
||||
implementation will be moved to "generic" and an abstract Population
|
||||
interface will be provided.
|
||||
It can be instantiated with anything, provided that it accepts a "size" and a
|
||||
random generator in the ctor. This happens to all the eo1d chromosomes declared
|
||||
so far. EOT must also have a copy ctor, since temporaries are created and copied
|
||||
to the population.
|
||||
@author Geneura Team
|
||||
@version 0.0
|
||||
*/
|
||||
|
||||
template<class EOT>
|
||||
class eoPop: public vector<EOT>, public eoObject, public eoPersistent {
|
||||
|
||||
/// Type is the type of each gene in the chromosome
|
||||
#ifdef _MSC_VER
|
||||
typedef EOT::Type Type;
|
||||
#else
|
||||
typedef typename EOT::Type Type;
|
||||
#endif
|
||||
|
||||
public:
|
||||
/** Protected ctor. This is intended to avoid creation of void populations, except
|
||||
from sibling classes
|
||||
*/
|
||||
eoPop()
|
||||
:vector<EOT>() {};
|
||||
|
||||
|
||||
/** Ctor for fixed-size chromosomes, with variable content
|
||||
@param _popSize total population size
|
||||
@param _eoSize chromosome size. EOT should accept a fixed-size ctor
|
||||
@param _geneRdn random number generator for each of the genes
|
||||
*/
|
||||
eoPop( unsigned _popSize, unsigned _eoSize, eoRnd<Type> & _geneRnd )
|
||||
:vector<EOT>() {
|
||||
for ( unsigned i = 0; i < _popSize; i ++ ){
|
||||
EOT tmpEOT( _eoSize, _geneRnd);
|
||||
push_back( tmpEOT );
|
||||
}
|
||||
};
|
||||
|
||||
/** Ctor for variable-size chromosomes, with variable content
|
||||
@param _popSize total population size
|
||||
@param _sizeRnd RNG for the chromosome size. This will be added 1, just in case.
|
||||
@param _geneRdn random number generator for each of the genes
|
||||
*/
|
||||
eoPop( unsigned _popSize, eoRnd<unsigned> & _sizeRnd, eoRnd<Type> & _geneRnd )
|
||||
:vector<EOT>() {
|
||||
for ( unsigned i = 0; i < _popSize; i ++ ){
|
||||
unsigned size = 1 + _sizeRnd();
|
||||
EOT tmpEOT( size, _geneRnd);
|
||||
push_back( tmpEOT );
|
||||
}
|
||||
};
|
||||
|
||||
/** Ctor from an istream; reads the population from a stream,
|
||||
each element should be in different lines
|
||||
@param _is the stream
|
||||
*/
|
||||
eoPop( istream& _is ):vector<EOT>() {
|
||||
readFrom( _is );
|
||||
}
|
||||
|
||||
///
|
||||
~eoPop() {};
|
||||
|
||||
/** @name Methods from eoObject */
|
||||
//@{
|
||||
/**
|
||||
* Read object. The EOT class must have a ctor from a stream;
|
||||
in this case, a strstream is used.
|
||||
* @param _is A istream.
|
||||
|
||||
*/
|
||||
virtual void readFrom(istream& _is) {
|
||||
while( _is ) { // reads line by line, and creates an object per
|
||||
// line
|
||||
char line[MAXLINELENGTH];
|
||||
_is.getline( line, MAXLINELENGTH-1 );
|
||||
if (strlen( line ) ) {
|
||||
istrstream s( line );
|
||||
EOT thisEOT( s );
|
||||
push_back( thisEOT );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Write object. It's called printOn since it prints the object _on_ a stream.
|
||||
* @param _os A ostream. In this case, prints the population to
|
||||
standard output. The EOT class must hav standard output with cout,
|
||||
but since it should be an eoObject anyways, it's no big deal.
|
||||
*/
|
||||
virtual void printOn(ostream& _os) const {
|
||||
copy( begin(), end(), ostream_iterator<EOT>( _os, "\n") );
|
||||
};
|
||||
|
||||
/** Inherited from eoObject. Returns the class name.
|
||||
@see eoObject
|
||||
*/
|
||||
virtual string className() const {return "eoPop";};
|
||||
//@}
|
||||
|
||||
protected:
|
||||
|
||||
};
|
||||
#endif
|
||||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoPop.h
|
||||
// (c) GeNeura Team, 1998
|
||||
/*
|
||||
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
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef _EOPOP_H
|
||||
#define _EOPOP_H
|
||||
|
||||
#include <vector>
|
||||
#include <strstream>
|
||||
|
||||
// EO includes
|
||||
#include <eoRnd.h>
|
||||
#include <eoPersistent.h>
|
||||
|
||||
/** Subpopulation: it is used to move parts of population
|
||||
from one algorithm to another and one population to another. It is safer
|
||||
to declare it as a separate object. I have no idea if a population can be
|
||||
some other thing that a vector, but if somebody thinks of it, this concrete
|
||||
implementation will be moved to "generic" and an abstract Population
|
||||
interface will be provided.
|
||||
It can be instantiated with anything, provided that it accepts a "size" and a
|
||||
random generator in the ctor. This happens to all the eo1d chromosomes declared
|
||||
so far. EOT must also have a copy ctor, since temporaries are created and copied
|
||||
to the population.
|
||||
@author Geneura Team
|
||||
@version 0.0
|
||||
*/
|
||||
|
||||
template<class EOT>
|
||||
class eoPop: public vector<EOT>, public eoObject, public eoPersistent {
|
||||
|
||||
/// Type is the type of each gene in the chromosome
|
||||
#ifdef _MSC_VER
|
||||
typedef EOT::Type Type;
|
||||
#else
|
||||
typedef typename EOT::Type Type;
|
||||
#endif
|
||||
|
||||
public:
|
||||
/** Protected ctor. This is intended to avoid creation of void populations, except
|
||||
from sibling classes
|
||||
*/
|
||||
eoPop() :vector<EOT>() {};
|
||||
|
||||
|
||||
/** Ctor for fixed-size chromosomes, with variable content
|
||||
@param _popSize total population size
|
||||
@param _eoSize chromosome size. EOT should accept a fixed-size ctor
|
||||
@param _geneRdn random number generator for each of the genes
|
||||
*/
|
||||
eoPop( unsigned _popSize, unsigned _eoSize, eoRnd<Type> & _geneRnd )
|
||||
:vector<EOT>() {
|
||||
for ( unsigned i = 0; i < _popSize; i ++ ){
|
||||
EOT tmpEOT( _eoSize, _geneRnd);
|
||||
push_back( tmpEOT );
|
||||
}
|
||||
};
|
||||
|
||||
/** Ctor for variable-size chromosomes, with variable content
|
||||
@param _popSize total population size
|
||||
@param _sizeRnd RNG for the chromosome size. This will be added 1, just in case.
|
||||
@param _geneRdn random number generator for each of the genes
|
||||
*/
|
||||
eoPop( unsigned _popSize, eoRnd<unsigned> & _sizeRnd, eoRnd<Type> & _geneRnd )
|
||||
:vector<EOT>() {
|
||||
for ( unsigned i = 0; i < _popSize; i ++ ){
|
||||
unsigned size = 1 + _sizeRnd();
|
||||
EOT tmpEOT( size, _geneRnd);
|
||||
push_back( tmpEOT );
|
||||
}
|
||||
};
|
||||
|
||||
/** Ctor from an istream; reads the population from a stream,
|
||||
each element should be in different lines
|
||||
@param _is the stream
|
||||
*/
|
||||
eoPop( istream& _is ):vector<EOT>() {
|
||||
readFrom( _is );
|
||||
}
|
||||
|
||||
///
|
||||
~eoPop() {};
|
||||
|
||||
/** @name Methods from eoObject */
|
||||
//@{
|
||||
/**
|
||||
* Read object. The EOT class must have a ctor from a stream;
|
||||
in this case, a strstream is used.
|
||||
* @param _is A istream.
|
||||
|
||||
*/
|
||||
virtual void readFrom(istream& _is) {
|
||||
while( _is ) { // reads line by line, and creates an object per
|
||||
// line
|
||||
char line[MAXLINELENGTH];
|
||||
_is.getline( line, MAXLINELENGTH-1 );
|
||||
if (strlen( line ) ) {
|
||||
istrstream s( line );
|
||||
EOT thisEOT( s );
|
||||
push_back( thisEOT );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Write object. It's called printOn since it prints the object _on_ a stream.
|
||||
* @param _os A ostream. In this case, prints the population to
|
||||
standard output. The EOT class must hav standard output with cout,
|
||||
but since it should be an eoObject anyways, it's no big deal.
|
||||
*/
|
||||
virtual void printOn(ostream& _os) const {
|
||||
copy( begin(), end(), ostream_iterator<EOT>( _os, "\n") );
|
||||
};
|
||||
|
||||
/** Inherited from eoObject. Returns the class name.
|
||||
@see eoObject
|
||||
*/
|
||||
virtual string className() const {return "eoPop";};
|
||||
//@}
|
||||
|
||||
protected:
|
||||
|
||||
};
|
||||
#endif
|
||||
|
|
|
|||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue