Added some files, compiled some stuff in VC++, and finished eoOpSelMason
This commit is contained in:
parent
044acb151b
commit
11be20aefa
14 changed files with 353 additions and 74 deletions
|
|
@ -17,6 +17,7 @@
|
|||
* based on STL's bit_vector (vector<bool>). *
|
||||
*****************************************************************************/
|
||||
|
||||
|
||||
template <class F> class eoBin: public eoVector<bool, F>
|
||||
{
|
||||
public:
|
||||
|
|
@ -37,14 +38,9 @@ template <class F> class eoBin: public eoVector<bool, F>
|
|||
generate(begin(), end(), rnd);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor from istream.
|
||||
* @param is The istream to read from.
|
||||
*/
|
||||
eoBin(istrstream& is)
|
||||
{
|
||||
readFrom(is);
|
||||
}
|
||||
/// Constructor from istream.
|
||||
/// @param is The istream to read from.
|
||||
eoBin(istream& _is):eoVector<bool,F>(_is){};
|
||||
|
||||
/// My class name.
|
||||
string className() const
|
||||
|
|
|
|||
|
|
@ -10,16 +10,23 @@
|
|||
#include <eoBin.h> // eoBin
|
||||
#include <eoOp.h> // eoMonOp
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#define min _MIN
|
||||
#define max _MAX
|
||||
#endif
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoBinRandom --> mofify a chromosome in a random way
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
|
||||
/** @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:
|
||||
|
|
@ -345,5 +352,5 @@ template<class Chrom> class eoBinUxOver: public eoBinOp<Chrom>
|
|||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
//@}
|
||||
#endif eoBitOp_h
|
||||
|
|
|
|||
41
eo/src/eoData.h
Normal file
41
eo/src/eoData.h
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// eoData.h
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef EODATA_H
|
||||
#define EODATA_H
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <vector> // vector
|
||||
#include <set> // set
|
||||
#include <string> // string
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#include <limits> // MAXDOUBLE
|
||||
#define MAXFLOAT numeric_limits<float>::max()
|
||||
#define MINFLOAT numeric_limits<float>::min()
|
||||
#define MAXDOUBLE numeric_limits<double>::max()
|
||||
#define MAXINT numeric_limits<int>::max()
|
||||
#define min _MIN
|
||||
#define max _MAX
|
||||
#else
|
||||
#include <limits.h>
|
||||
#ifndef MAXFLOAT
|
||||
#define MAXFLOAT (float)1e127
|
||||
#define MAXDOUBLE (double)1.79769313486231570e+308
|
||||
#define MAXINT 2147483647
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef _MSC_VER
|
||||
#include <math.h>
|
||||
#define _isnan isnan
|
||||
#endif
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#endif npi_DATATYPES_H
|
||||
|
|
@ -11,44 +11,39 @@
|
|||
|
||||
/******************************************************************************
|
||||
* eoInsertion: A replacement algorithm.
|
||||
* Creates a new population with all the breeders and the best individuals
|
||||
* from the original population.
|
||||
* Takes two populations: breeders and original populations. At the en of the
|
||||
* process, the original population has chenge in the followin way:
|
||||
* (1) the worst individuals haa been erased
|
||||
* (2) the best individuals from the breeders has been added
|
||||
*****************************************************************************/
|
||||
|
||||
template<class Chrom> class eoInsertion: public eoMerge<Chrom>
|
||||
{
|
||||
public:
|
||||
/// (Default) Constructor.
|
||||
eoInsertion(const float& _rate = 1.0): eoMerge(_rate) {}
|
||||
eoInsertion(const float& _rate = 1.0): eoMerge<Chrom>(_rate) {}
|
||||
|
||||
/**
|
||||
* Creates a new population based on breeders and original populations.
|
||||
* Creates a new population based on breeders and original population
|
||||
* @param breeders The population of breeders.
|
||||
* @param pop The original population.
|
||||
*/
|
||||
void operator()(const eoPop<Chrom>& breeders, eoPop<Chrom>& pop)
|
||||
{
|
||||
int new_size = static_cast<int>(pop.size() * rate());
|
||||
cout << "new_size = " << new_size << endl;
|
||||
sort(pop.begin(), pop.end());
|
||||
|
||||
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());
|
||||
}
|
||||
if (rated() > 1)
|
||||
pop.erase(pop.end() +
|
||||
(int)(pop.size() * (rate() - 1) - breeders.size()),
|
||||
pop.end());
|
||||
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));
|
||||
cout << "eoInsertion no funciona con rate < 1"
|
||||
exit(1);
|
||||
}
|
||||
|
||||
copy(breeders.begin(), breeders.end(),
|
||||
back_insert_iterator<eoPop<Chrom> >(pop));
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -9,7 +9,6 @@
|
|||
|
||||
#include <functional>
|
||||
|
||||
#include <values.h> // MINFLOAT
|
||||
#include <numeric> // accumulate
|
||||
#include <eo> // eoPop eoSelect
|
||||
|
||||
|
|
|
|||
|
|
@ -9,9 +9,10 @@
|
|||
#define EOOBJECT_H
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <iostream> // istream, ostream
|
||||
#include <string> // para string
|
||||
|
||||
#include <eoData.h> // For limits definition
|
||||
#include <iostream> // istream, ostream
|
||||
#include <string> // para string
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
|
|
|||
|
|
@ -16,9 +16,13 @@
|
|||
|
||||
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.
|
||||
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.0
|
||||
@version 0.1
|
||||
@see eoOpFactory
|
||||
*/
|
||||
//@{
|
||||
|
||||
|
|
@ -57,7 +61,7 @@ public:
|
|||
*/
|
||||
virtual void printOn(ostream& _os) const {
|
||||
_os << className();
|
||||
_os << arity;
|
||||
// _os << arity;
|
||||
};
|
||||
|
||||
/** Inherited from eoObject
|
||||
|
|
|
|||
|
|
@ -60,7 +60,7 @@ public:
|
|||
throw objectTypeStr;
|
||||
}
|
||||
return opPtr;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
};
|
||||
|
|
|
|||
|
|
@ -9,7 +9,10 @@
|
|||
#define _EOOPSELMASON_H
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
#include <eoOpSelector.h>
|
||||
#include <eoProportionalOpSel.h>
|
||||
#include <eoOpFactory.h> // for eoFactory and eoOpFactory
|
||||
|
||||
#include <map>
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
|
|
@ -19,14 +22,16 @@ template<class eoClass>
|
|||
class eoOpSelMason: public eoFactory<eoOpSelector<eoClass> > {
|
||||
|
||||
public:
|
||||
|
||||
typedef vector<eoOp<eoClass>* > vOpP;
|
||||
typedef map<eoOpSelector<eoClass>*, vOpP > MEV;
|
||||
|
||||
/// @name ctors and dtors
|
||||
//{@
|
||||
/// constructor
|
||||
eoOpSelMason( ) {}
|
||||
eoOpSelMason( eoOpFactory<eoClass>& _opFact): operatorFactory( _opFact ) {};
|
||||
|
||||
/// destructor
|
||||
virtual ~eoOpSelMason() {}
|
||||
virtual ~eoOpSelMason() {};
|
||||
//@}
|
||||
|
||||
/** Factory methods: creates an object from an istream, reading from
|
||||
|
|
@ -41,33 +46,47 @@ public:
|
|||
from outside, using the #destroy# method
|
||||
*/
|
||||
virtual eoOpSelector<eoClass>* make(istream& _is) {
|
||||
|
||||
string opSelName;
|
||||
_is >> opSelName;
|
||||
eoMonOpFactory<eoClass> selMaker;
|
||||
eoOpSelector<eoClass>* opSelectorP;
|
||||
// Build the operator selector
|
||||
if ( opSelName == "eoProportionalOpSel" ) {
|
||||
opSelectorP = new eoProportionalOpSel<eoClass>();
|
||||
}
|
||||
|
||||
// Temp vector for storing pointers
|
||||
vOpP tmpPVec;
|
||||
// read operator rate and name
|
||||
while ( _is ) {
|
||||
float rate;
|
||||
_is >> rate;
|
||||
|
||||
if ( _is ) {
|
||||
eoOp<eoClass>* op = operatorFactory.make( _is ); // This reads the rest of the line
|
||||
// Add the operators to the selector, don´t pay attention to the IDs
|
||||
opSelectorP->addOp( *op, rate );
|
||||
// Keep it in the store, to destroy later
|
||||
tmpPVec.push_back( op );
|
||||
} // if
|
||||
} // while
|
||||
|
||||
// Create an stream
|
||||
strstream s0;
|
||||
eoMonOp<IEO>* op0 = selMaker.make( s0 );
|
||||
}
|
||||
|
||||
}
|
||||
// Put it in the map
|
||||
allocMap.insert( MEV::value_type( opSelectorP, tmpPVec ) );
|
||||
|
||||
return opSelectorP;
|
||||
};
|
||||
|
||||
///@name eoObject methods
|
||||
//@{
|
||||
/** Return the class id */
|
||||
virtual string className() const { return "eoFactory"; }
|
||||
virtual string className() const { return "eoOpSelMason"; }
|
||||
|
||||
/** Read and print are left without implementation */
|
||||
//@}
|
||||
|
||||
private:
|
||||
map<eoOpSelector<eoClass>*,vector<eoOp<eoClass>* > > allocMap;
|
||||
eoOpFactory<eoClass>& operatorFactory;
|
||||
};
|
||||
|
||||
|
||||
#endif _EOFACTORY_H
|
||||
#endif _EOOPSELMASON_H
|
||||
|
|
|
|||
Reference in a new issue