feat(eo): adds eoRealToIntInit
This commit is contained in:
parent
19ec4c4ff7
commit
8ea6e2b680
2 changed files with 75 additions and 0 deletions
|
|
@ -227,6 +227,7 @@
|
||||||
#include "eoInt.h"
|
#include "eoInt.h"
|
||||||
#include "eoRealToIntMonOp.h"
|
#include "eoRealToIntMonOp.h"
|
||||||
#include "eoRealToIntQuadOp.h"
|
#include "eoRealToIntQuadOp.h"
|
||||||
|
#include "eoRealToIntInit.h"
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
||||||
74
eo/src/eoRealToIntInit.h
Normal file
74
eo/src/eoRealToIntInit.h
Normal file
|
|
@ -0,0 +1,74 @@
|
||||||
|
#ifndef eoRealToIntInit_h_INCLUDED
|
||||||
|
#define eoRealToIntInit_h_INCLUDED
|
||||||
|
|
||||||
|
#include "es/eoReal.h"
|
||||||
|
#include "utils/eoIntBounds.h"
|
||||||
|
|
||||||
|
template<class EOTINT, class EOTREAL = eoReal<typename EOTINT::FitnessType>>
|
||||||
|
class eoRealToIntInit : public eoInit<EOTINT>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
using EOTreal = EOTREAL;
|
||||||
|
|
||||||
|
enum Repair {
|
||||||
|
folds,
|
||||||
|
truncate
|
||||||
|
};
|
||||||
|
|
||||||
|
eoRealToIntInit( eoInit<EOTreal>& init ) :
|
||||||
|
_whenout(Repair::truncate),
|
||||||
|
_nobounds(),
|
||||||
|
_bounds(_nobounds),
|
||||||
|
_init(init)
|
||||||
|
{ }
|
||||||
|
|
||||||
|
eoRealToIntInit( eoInit<EOTreal>& init, eoIntBounds& bounds, Repair whenout = Repair::truncate ) :
|
||||||
|
_whenout(whenout),
|
||||||
|
_nobounds(),
|
||||||
|
_bounds(bounds),
|
||||||
|
_init(init)
|
||||||
|
{ }
|
||||||
|
|
||||||
|
virtual void operator()(EOTINT& intsol) override
|
||||||
|
{
|
||||||
|
#ifndef NDEBUG
|
||||||
|
for(size_t i=0; i < intsol.size(); ++i) {
|
||||||
|
assert(_bounds.isInBounds(intsol[i]));
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
EOTreal floatsol;
|
||||||
|
std::copy( std::begin(intsol), std::end(intsol), std::back_inserter(floatsol) );
|
||||||
|
|
||||||
|
_init(floatsol);
|
||||||
|
|
||||||
|
intsol.resize(floatsol.size());
|
||||||
|
|
||||||
|
for(size_t i=0; i < floatsol.size(); ++i) {
|
||||||
|
typename EOTreal::AtomType rounded = std::round(floatsol[i]);
|
||||||
|
if( not _bounds.isInBounds(rounded) ) {
|
||||||
|
switch(_whenout) {
|
||||||
|
case Repair::truncate:
|
||||||
|
_bounds.truncate(rounded);
|
||||||
|
break;
|
||||||
|
case Repair::folds:
|
||||||
|
_bounds.foldsInBounds(rounded);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
intsol[i] = static_cast<typename EOTINT::AtomType>(rounded);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
Repair _whenout;
|
||||||
|
eoIntNoBounds _nobounds;
|
||||||
|
|
||||||
|
eoIntBounds& _bounds;
|
||||||
|
eoInit<EOTreal>& _init;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#endif // eoRealToIntInit_h_INCLUDED
|
||||||
Loading…
Add table
Add a link
Reference in a new issue