feat: add an intermediate layer of init with dimension

This commit is contained in:
Johann Dreo 2020-04-28 15:55:28 +02:00
commit c95f5607d8

View file

@ -28,6 +28,7 @@
#define _eoInit_H #define _eoInit_H
#include <algorithm> #include <algorithm>
#include <cassert>
#include "eoOp.h" #include "eoOp.h"
#include "eoSTLFunctor.h" #include "eoSTLFunctor.h"
@ -83,32 +84,50 @@ private:
eoInit<EOT> & init; eoInit<EOT> & init;
}; };
template<class EOT>
class eoInitWithDim : public eoInit<EOT>
{
public:
//! Sets the default dimension.
eoInitWithDim(unsigned dimension = 0) :
_dimension(dimension)
{ }
//! Get the current dimension.
unsigned dimension() const {return _dimension;}
//! Set the current dimension.
void dimension(unsigned dimension) {assert(dimension > 0);_dimension = dimension;}
protected:
unsigned _dimension;
};
/** /**
Initializer for fixed length representations with a single type Initializer for fixed length representations with a single type
*/ */
template <class EOT> template <class EOT>
class eoInitFixedLength: public eoInit<EOT> class eoInitFixedLength: public eoInitWithDim<EOT>
{ {
public: public:
typedef typename EOT::AtomType AtomType; typedef typename EOT::AtomType AtomType;
eoInitFixedLength(unsigned _dimension, eoRndGenerator<AtomType>& _generator) eoInitFixedLength(unsigned dimension, eoRndGenerator<AtomType>& generator) :
: dim(_dimension), generator(_generator) {} eoInitWithDim<EOT>(dimension),
_generator(generator)
{ }
virtual void operator()(EOT& chrom) virtual void operator()(EOT& chrom)
{ {
chrom.resize(dim); chrom.resize(this->_dimension);
std::generate(chrom.begin(), chrom.end(), generator); std::generate(chrom.begin(), chrom.end(), _generator);
chrom.invalidate(); chrom.invalidate();
} }
unsigned dimension() const {return dim;}
private : private :
unsigned dim;
/// generic wrapper for eoFunctor (s), to make them have the function-pointer style copy semantics /// generic wrapper for eoFunctor (s), to make them have the function-pointer style copy semantics
eoSTLF<AtomType> generator; eoSTLF<AtomType> _generator;
}; };
/** /**
@ -162,7 +181,7 @@ private :
Initializer for permutation (integer-based) representations. Initializer for permutation (integer-based) representations.
*/ */
template <class EOT> template <class EOT>
class eoInitPermutation: public eoInit<EOT> class eoInitPermutation: public eoInit<EOT> // FIXME inherit from eoInitWithDim
{ {
public: public: