Skip to content

Commit

Permalink
feat(eo): adds eoRealToIntInit
Browse files Browse the repository at this point in the history
  • Loading branch information
jdreo committed Sep 27, 2024
1 parent 19ec4c4 commit 8ea6e2b
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
1 change: 1 addition & 0 deletions eo/src/eo
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@
#include "eoInt.h"
#include "eoRealToIntMonOp.h"
#include "eoRealToIntQuadOp.h"
#include "eoRealToIntInit.h"

#endif

Expand Down
74 changes: 74 additions & 0 deletions eo/src/eoRealToIntInit.h
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 8ea6e2b

Please sign in to comment.