Changes
This commit is contained in:
parent
de33c67f0e
commit
d3642e4fde
12 changed files with 397 additions and 207 deletions
154
eo/src/eoBin.h
154
eo/src/eoBin.h
|
|
@ -1,78 +1,76 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// eoBin.h
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#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
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoBin.h
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#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
|
||||
|
|
|
|||
|
|
@ -31,10 +31,12 @@
|
|||
#include <eoEvalFunc.h>
|
||||
#include <eoPopOps.h> // eoSelect, eoTranform, eoMerge
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoGeneration
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
/** eoGeneration
|
||||
* Single step of a evolutionary algorithm. Applies selection, then genetic
|
||||
* operators, replaces using a replacement policy, and finally evaluates the
|
||||
* new ones */
|
||||
template<class Chrom> class eoGeneration: public eoAlgo<Chrom>
|
||||
{
|
||||
public:
|
||||
|
|
|
|||
Reference in a new issue