New bitOp factories and things like that

This commit is contained in:
jmerelo 1999-02-09 17:31:16 +00:00
commit 9860e201f2
8 changed files with 435 additions and 19 deletions

View file

@ -10,22 +10,15 @@
#include <iostream> // ostream, istream
#include <functional> // bind2nd
#if defined( _MSC_VER ) || defined( __BCPLUSPLUS__ ) //
#include <vector> //
typedef vector<bool> bit_vector; // bit_vector
#else //
#include <bvector.h> //
#endif //
#include <string> // string
#include <EO.h> // EO
#include <eoVector.h> // EO
//-----------------------------------------------------------------------------
/// eoBin: implementation of binary chromosome.
/// based on STL's bit_vector (vector<bool>)
/** eoBin: implementation of binary chromosome.
based on STL's bit_vector (vector<bool>)*/
//-----------------------------------------------------------------------------
template <class F> class eoBin: public EO<F>, public bit_vector
template <class F> class eoBin: public eoVector<bool,F>
{
public:
typedef bool Type;
@ -33,11 +26,11 @@ template <class F> class eoBin: public EO<F>, public bit_vector
/// (Default) Constructor.
/// @param size Size of the binary string.
eoBin(const unsigned& size = 0, const bool& value = false):
bit_vector(size, value) {}
eoVector<bool,F>(size, value) {}
/// (Default) Constructor.
/// @param size Size of the binary string.
eoBin(const unsigned& size, const eoRnd<Type>& rnd): bit_vector(size)
eoBin(const unsigned& size, const eoRnd<Type>& rnd): eoVector<bool,F>(size)
{
generate(begin(), end(), rnd);
}

View file

@ -1,14 +1,21 @@
//-----------------------------------------------------------------------------
// eoBinOp.h
// eoBitOp.h
//-----------------------------------------------------------------------------
#ifndef eoBinOp_h
#define eoBinOp_h
#ifndef eoBitOp_h
#define eoBitOp_h
//-----------------------------------------------------------------------------
#include <eoBin.h> // eoBin
#include <eoOp.h> // eoMonOp
#ifndef min
#define min _MIN
#endif
#ifndef max
#define max _MAX
#endif
//-----------------------------------------------------------------------------
// eoBinRandom --> mofify a chromosome in a random way
@ -340,4 +347,4 @@ template<class Chrom> class eoBinUxOver: public eoBinOp<Chrom>
//-----------------------------------------------------------------------------
#endif eoBinOp_h
#endif eoBitOp_h

101
eo/src/eoBitOpFactory.h Normal file
View file

@ -0,0 +1,101 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
//-----------------------------------------------------------------------------
// eoOpFactory.h
// (c) GeNeura Team, 1998
//-----------------------------------------------------------------------------
#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 ( 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

View file

@ -41,7 +41,7 @@ public:
@throw runtime_exception if the object type is not known
*/
virtual eoOp<EOT>* make(istream& _is) {
eoOp<EOT> * opPtr;
eoOp<EOT> * opPtr = NULL;
string objectTypeStr;
_is >> objectTypeStr;
if ( objectTypeStr == "eoDup") {
@ -57,7 +57,7 @@ public:
opPtr = new eoXOver2<EOT>( );
}
if ( !opPtr ) {
throw runtime_error( "Incorrect selector type" );
throw objectTypeStr;
}
return opPtr;
}