moving...
This commit is contained in:
parent
5ecddc312c
commit
2799347236
7 changed files with 872 additions and 0 deletions
93
eo/src/ga/eoBin.h
Normal file
93
eo/src/ga/eoBin.h
Normal file
|
|
@ -0,0 +1,93 @@
|
||||||
|
/*
|
||||||
|
eoBin.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 eoBin_h
|
||||||
|
#define eoBin_h
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#include <iostream> // ostream, istream
|
||||||
|
#include <functional> // bind2nd
|
||||||
|
#include <string> // string
|
||||||
|
#include <eoVector.h> // EO
|
||||||
|
|
||||||
|
/** eoBin: implementation of binary chromosome.
|
||||||
|
* based on STL's bit_vector (vector<bool>).
|
||||||
|
*/
|
||||||
|
template <class F> class eoBin: public eoVector<bool, F>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
/**
|
||||||
|
* (Default) Constructor.
|
||||||
|
* @param size Size of the binary string.
|
||||||
|
*/
|
||||||
|
eoBin(unsigned size = 0, bool value = false):
|
||||||
|
eoVector<bool,F>(size, value) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor.
|
||||||
|
* @param size Size of the binary string.
|
||||||
|
*/
|
||||||
|
eoBin(unsigned size, const eoRnd<bool>& rnd): eoVector<bool,F>(size)
|
||||||
|
{
|
||||||
|
generate(begin(), end(), rnd);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Constructor from istream.
|
||||||
|
@param is The istream to read from.*/
|
||||||
|
eoBin(istream& _is):eoVector<bool,F>(_is){};
|
||||||
|
|
||||||
|
/// My class name.
|
||||||
|
string className() const
|
||||||
|
{
|
||||||
|
return "eoBin";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* To print me on a stream.
|
||||||
|
* @param os The ostream.
|
||||||
|
*/
|
||||||
|
void printOn(ostream& os) const
|
||||||
|
{
|
||||||
|
copy(begin(), end(), ostream_iterator<bool>(os));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* To read me from a stream.
|
||||||
|
* @param is The istream.
|
||||||
|
*/
|
||||||
|
void readFrom(istream& is)
|
||||||
|
{
|
||||||
|
string bits;
|
||||||
|
is >> bits;
|
||||||
|
if (is)
|
||||||
|
{
|
||||||
|
resize(bits.size());
|
||||||
|
transform(bits.begin(), bits.end(), begin(),
|
||||||
|
bind2nd(equal_to<char>(), '1'));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#endif eoBin_h
|
||||||
325
eo/src/ga/eoBitOp.h
Normal file
325
eo/src/ga/eoBitOp.h
Normal file
|
|
@ -0,0 +1,325 @@
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// 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
|
||||||
|
|
||||||
|
Even as these operators might seem general, they are particular versions of genetic
|
||||||
|
operators useful only for binary operators. As any set of genetic operators, it must
|
||||||
|
have a factory that knows how to build them from a description
|
||||||
|
@author GeNeura Team
|
||||||
|
@version 0.1
|
||||||
|
@see eoBin
|
||||||
|
@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 eoQuadraticOp<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 eoQuadraticOp<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 eoQuadraticOp<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;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
//@}
|
||||||
|
#endif eoBitOp_h
|
||||||
|
|
||||||
120
eo/src/ga/eoBitOpFactory.h
Normal file
120
eo/src/ga/eoBitOpFactory.h
Normal file
|
|
@ -0,0 +1,120 @@
|
||||||
|
// eoBitOpFactory.h
|
||||||
|
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// eoOpFactory.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 _EOBITOPFACTORY_H
|
||||||
|
#define _EOBITOPFACTORY_H
|
||||||
|
|
||||||
|
#include <eoOpFactory.h>
|
||||||
|
#include <eoBitOp.h>
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
/** EO Factory. An instance of the factory class to create operators that act
|
||||||
|
on bitstring chromosomes. Only those chromosomes can instantiate the operators
|
||||||
|
that are created here
|
||||||
|
@see eoSelect*/
|
||||||
|
template< class EOT>
|
||||||
|
class eoBitOpFactory: public eoOpFactory<EOT> {
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
/// @name ctors and dtors
|
||||||
|
//{@
|
||||||
|
/// constructor
|
||||||
|
eoBitOpFactory( ) {};
|
||||||
|
|
||||||
|
/// destructor
|
||||||
|
virtual ~eoBitOpFactory() {};
|
||||||
|
//@}
|
||||||
|
|
||||||
|
/** Another factory method: creates an object from an istream, reading from
|
||||||
|
it whatever is needed to create the object. Usually, the format for the istream will be\\
|
||||||
|
objectType parameter1 parameter2 ... parametern\\
|
||||||
|
If there are problems, an exception is raised; it should be caught at the
|
||||||
|
upper level, because it might be something for that level\\
|
||||||
|
At the same time, it catches exceptions thrown at a lower level, which will
|
||||||
|
indicate that whatever is in the stream is for this method to process
|
||||||
|
@param _is an stream from where a single line will be read
|
||||||
|
@throw runtime_exception if the object type is not known
|
||||||
|
*/
|
||||||
|
virtual eoOp<EOT>* make(istream& _is) {
|
||||||
|
eoOp<EOT> * opPtr = NULL;
|
||||||
|
try {
|
||||||
|
opPtr = eoOpFactory<EOT>::make( _is );
|
||||||
|
} catch ( const string& objectTypeStr ) {
|
||||||
|
if ( objectTypeStr == "eoBinRandom") {
|
||||||
|
opPtr = new eoBinRandom<EOT>();
|
||||||
|
}
|
||||||
|
if ( objectTypeStr == "eoBinBitFlip" ) {
|
||||||
|
opPtr = new eoBinBitFlip<EOT>( );
|
||||||
|
}
|
||||||
|
if ( objectTypeStr == "eoBinMutation" ) {
|
||||||
|
float rate;
|
||||||
|
_is >> rate;
|
||||||
|
opPtr = new eoBinMutation<EOT>( rate );
|
||||||
|
}
|
||||||
|
if ( objectTypeStr == "eoBinInversion" ) {
|
||||||
|
opPtr = new eoBinInversion<EOT>( );
|
||||||
|
}
|
||||||
|
if ( objectTypeStr == "eoBinNext" ) {
|
||||||
|
opPtr = new eoBinNext<EOT>( );
|
||||||
|
}
|
||||||
|
if ( objectTypeStr == "eoBinPrev" ) {
|
||||||
|
opPtr = new eoBinPrev<EOT>( );
|
||||||
|
}
|
||||||
|
if ( objectTypeStr == "eoBinNext" ) {
|
||||||
|
opPtr = new eoBinNext<EOT>( );
|
||||||
|
}
|
||||||
|
if ( objectTypeStr == "eoBinCrossover" ) {
|
||||||
|
opPtr = new eoBinCrossover<EOT>( );
|
||||||
|
}
|
||||||
|
if ( objectTypeStr == "eoBinNxOver" ) {
|
||||||
|
unsigned nPoints;
|
||||||
|
_is >> nPoints;
|
||||||
|
opPtr = new eoBinNxOver<EOT>( nPoints );
|
||||||
|
}
|
||||||
|
if ( objectTypeStr == "eoBinGxOver" ) {
|
||||||
|
unsigned geneSize, nPoints;
|
||||||
|
_is >> geneSize >> nPoints;
|
||||||
|
opPtr = new eoBinGxOver<EOT>( geneSize, nPoints );
|
||||||
|
}
|
||||||
|
if ( objectTypeStr == "eoBinUxOver" ) {
|
||||||
|
float rate;
|
||||||
|
_is >> rate;
|
||||||
|
opPtr = new eoBinUxOver<EOT>( rate );
|
||||||
|
}
|
||||||
|
if ( !opPtr ) { // to be caught by the upper level
|
||||||
|
throw objectTypeStr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return opPtr;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif _EOBITOPFACTORY_H
|
||||||
|
|
||||||
67
eo/src/other/eoExternalEO.h
Normal file
67
eo/src/other/eoExternalEO.h
Normal file
|
|
@ -0,0 +1,67 @@
|
||||||
|
/* -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||||
|
|
||||||
|
-----------------------------------------------------------------------------
|
||||||
|
eoExternalEO.h
|
||||||
|
* Definition of an object that allows an external struct
|
||||||
|
* to be inserted in EO
|
||||||
|
(c) Maarten Keijzer (mkeijzer@mad.scientist.com) and GeNeura Team, 1999, 2000
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either
|
||||||
|
version 2 of the License, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public
|
||||||
|
License along with this library; if not, write to the Free Software
|
||||||
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||||
|
|
||||||
|
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef eoExternalEO_h
|
||||||
|
#define eoExternalEO_h
|
||||||
|
|
||||||
|
#include "EO.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Definition of an object that allows an external struct
|
||||||
|
* to be inserted in EO
|
||||||
|
*/
|
||||||
|
template <class Fit, class External>
|
||||||
|
class eoExternalEO : public EO, virtual public External
|
||||||
|
{
|
||||||
|
public :
|
||||||
|
|
||||||
|
typedef External Type;
|
||||||
|
|
||||||
|
eoExternalEO(void) : EO(), Base() {}
|
||||||
|
eoExternalEO(istream& is) : EO(), Base() { readFrom(is); }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read object.\\
|
||||||
|
* @param _is a istream.
|
||||||
|
* @throw runtime_exception If a valid object can't be read.
|
||||||
|
*/
|
||||||
|
virtual void readFrom(istream& _is)
|
||||||
|
{
|
||||||
|
EO::readFrom(is);
|
||||||
|
throw runtime_excpetion("Reading not defined yet");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write object. Called printOn since it prints the object _on_ a stream.
|
||||||
|
* @param _os A ostream.
|
||||||
|
*/
|
||||||
|
virtual void printOn(ostream& _os) const
|
||||||
|
{
|
||||||
|
EO::printOn(is);
|
||||||
|
throw runtime_excpetion("Writing not defined yet");
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
41
eo/src/other/eoExternalOpFunctions.cpp
Normal file
41
eo/src/other/eoExternalOpFunctions.cpp
Normal file
|
|
@ -0,0 +1,41 @@
|
||||||
|
/* -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||||
|
|
||||||
|
-----------------------------------------------------------------------------
|
||||||
|
eoExternalOpFunc.h
|
||||||
|
Defines eoExternalInitOpFunc, eoExternalMonOpFunc, eoExternalBinOpFunc, eoExternalQuadOpFunc
|
||||||
|
that are used to wrap a function pointer to externally defined initialization
|
||||||
|
and 'genetic' operators
|
||||||
|
|
||||||
|
(c) Maarten Keijzer (mkeijzer@mad.scientist.com) and GeNeura Team, 1999, 2000
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either
|
||||||
|
version 2 of the License, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public
|
||||||
|
License along with this library; if not, write to the Free Software
|
||||||
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||||
|
|
||||||
|
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef eoExternalOpFunc_h
|
||||||
|
#define eoExternalOpFunc_h
|
||||||
|
|
||||||
|
#include "eoExternalEO.h"
|
||||||
|
#include "eoOps.h"
|
||||||
|
#include "eoRnd.h"
|
||||||
|
|
||||||
|
template <class F, class External>
|
||||||
|
class eoExternalInitFunc
|
||||||
|
{
|
||||||
|
|
||||||
|
public :
|
||||||
|
|
||||||
|
};
|
||||||
148
eo/src/other/eoString.h
Normal file
148
eo/src/other/eoString.h
Normal file
|
|
@ -0,0 +1,148 @@
|
||||||
|
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// eoString.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 _eoString_H
|
||||||
|
#define _eoString_H
|
||||||
|
|
||||||
|
// STL libraries
|
||||||
|
#include <string>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
// EO headers
|
||||||
|
#include <eo1d.h>
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// eoString
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
/** Adaptor that turns an STL string into an EO */
|
||||||
|
template <class fitnessT >
|
||||||
|
class eoString: public eo1d<char, fitnessT>, public string {
|
||||||
|
public:
|
||||||
|
|
||||||
|
/// Canonical part of the objects: several ctors, copy ctor, dtor and assignment operator
|
||||||
|
//@{
|
||||||
|
/// ctor
|
||||||
|
eoString( const string& _str ="" )
|
||||||
|
: eo1d<char, fitnessT>(), string( _str ) {};
|
||||||
|
|
||||||
|
|
||||||
|
/** Ctor using a random number generator
|
||||||
|
@param _size Lineal length of the object
|
||||||
|
@param _rnd a random number generator, which returns a random value each time it´s called
|
||||||
|
*/
|
||||||
|
eoString( unsigned _size, eoRnd<char>& _rnd )
|
||||||
|
: eo1d<char, fitnessT>(), string(){
|
||||||
|
for ( unsigned i = 0; i < _size; i ++ ) {
|
||||||
|
*this += _rnd();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/** Ctor from a stream
|
||||||
|
@param _s input stream
|
||||||
|
*/
|
||||||
|
eoString( istream & _s )
|
||||||
|
: eo1d<char, fitnessT>(){
|
||||||
|
_s >> *this;
|
||||||
|
};
|
||||||
|
|
||||||
|
/// copy ctor
|
||||||
|
eoString( const eoString<fitnessT>& _eoStr )
|
||||||
|
:eo1d<char, fitnessT>( static_cast<const eo1d<char, fitnessT> & > ( _eoStr ) ),
|
||||||
|
string( _eoStr ){};
|
||||||
|
|
||||||
|
/// Assignment operator
|
||||||
|
const eoString& operator =( const eoString& _eoStr ) {
|
||||||
|
if ( this != & _eoStr ) {
|
||||||
|
eo1d<char, fitnessT>::operator = ( _eoStr );
|
||||||
|
string::operator = ( _eoStr );
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// dtor
|
||||||
|
virtual ~eoString() {};
|
||||||
|
//@}
|
||||||
|
|
||||||
|
|
||||||
|
/** methods that implement the eo1d <em>protocol</em>
|
||||||
|
@exception out_of_range if _i is larger than EO´s size
|
||||||
|
*/
|
||||||
|
virtual char getGene( unsigned _i ) const {
|
||||||
|
if ( _i >= length() )
|
||||||
|
throw out_of_range( "out_of_range when reading gene");
|
||||||
|
return (*this)[_i];
|
||||||
|
};
|
||||||
|
|
||||||
|
/** methods that implement the eo1d <em>protocol</em>
|
||||||
|
@exception out_of_range if _i is larger than EO´s size
|
||||||
|
*/
|
||||||
|
virtual void setGene( unsigned _i, const char& _value ) {
|
||||||
|
if ( _i >= size() )
|
||||||
|
throw out_of_range( "out_of_range when writing a gene");
|
||||||
|
(*this)[_i] = _value;
|
||||||
|
};
|
||||||
|
|
||||||
|
/** Inserts a value after _i, displacing anything to the right
|
||||||
|
@exception out_of_range if _i is larger than EO´s size
|
||||||
|
*/
|
||||||
|
virtual void insertGene( unsigned _i, char _val ) {
|
||||||
|
if (_i <= this->size() ) {
|
||||||
|
string::iterator i = this->begin()+_i;
|
||||||
|
this->insert( i, _val );
|
||||||
|
} else
|
||||||
|
throw out_of_range( "out_of_range when inserting gene");
|
||||||
|
};
|
||||||
|
|
||||||
|
/** Eliminates the gene at position _i
|
||||||
|
@exception out_of_range if _i is larger than EO´s size
|
||||||
|
*/
|
||||||
|
virtual void deleteGene( unsigned _i ) {
|
||||||
|
if (_i < this->size() ) {
|
||||||
|
string::iterator i = this->begin()+_i;
|
||||||
|
this->erase( i );
|
||||||
|
} else
|
||||||
|
throw out_of_range( "out_of_range when deleting gene");
|
||||||
|
};
|
||||||
|
|
||||||
|
/// methods that implement the EO <em>protocol</em>
|
||||||
|
virtual unsigned length() const { return this->size(); };
|
||||||
|
|
||||||
|
/** @name Methods from eoObject
|
||||||
|
readFrom and printOn are directly inherited from eo1d
|
||||||
|
*/
|
||||||
|
//@{
|
||||||
|
/** Inherited from eoObject
|
||||||
|
@see eoObject
|
||||||
|
*/
|
||||||
|
virtual string className() const {return "eoString";};
|
||||||
|
//@}
|
||||||
|
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
78
eo/src/other/eoStringMutation.h
Normal file
78
eo/src/other/eoStringMutation.h
Normal file
|
|
@ -0,0 +1,78 @@
|
||||||
|
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// eoStringMutation.h
|
||||||
|
// (c) GeNeura Team, 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
|
||||||
|
*/
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#ifndef _EOSTRINGMUTATION_H
|
||||||
|
#define _EOSRTINGMUTATION_H
|
||||||
|
|
||||||
|
#include <math.h>
|
||||||
|
// EO includes
|
||||||
|
#include <eoOp.h>
|
||||||
|
#include <eoUniform.h>
|
||||||
|
#include <eoMutation.h>
|
||||||
|
|
||||||
|
/** Mutation of an eoString.
|
||||||
|
The eoString's genes are changed by adding or substracting 1 to
|
||||||
|
*/
|
||||||
|
|
||||||
|
template <class EOT>
|
||||||
|
class eoStringMutation: public eoMutation<EOT> {
|
||||||
|
public:
|
||||||
|
|
||||||
|
///
|
||||||
|
eoStringMutation(const double _rate=0.0) : eoMutation< EOT >(_rate) {};
|
||||||
|
|
||||||
|
///
|
||||||
|
virtual ~eoStringMutation() {};
|
||||||
|
|
||||||
|
/** @name Methods from eoObject
|
||||||
|
*/
|
||||||
|
//@{
|
||||||
|
/** Inherited from eoObject
|
||||||
|
@see eoObject
|
||||||
|
*/
|
||||||
|
string className() const {return "eoStringMutation";};
|
||||||
|
//@}
|
||||||
|
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
typedef EOT::Type Type;
|
||||||
|
#else
|
||||||
|
typedef typename EOT::Type Type;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/// applies operator to one gene in the EO. It increments or decrements the value of that gene by one.
|
||||||
|
virtual void applyAt( EOT& _eo, unsigned _i ) const {
|
||||||
|
eoUniform<double> uniform( 0, 1 );
|
||||||
|
if( rate < uniform() ) {
|
||||||
|
_eo.gene(_i) += ( uniform()>=0.5 )? (1) : (-1) ;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#endif
|
||||||
Reference in a new issue