feat(eo): wrap ops on float vecs into ops on int vecs

Adds wrapper classes to make any MonOp or QuadOp that operates on eoReal embbedable in any operator needing an eoInt.
This commit is contained in:
Johann Dreo 2024-09-26 13:24:10 +02:00
commit 19ec4c4ff7
8 changed files with 241 additions and 1 deletions

View file

@ -224,6 +224,10 @@
#include "utils/eoLogger.h" #include "utils/eoLogger.h"
#include "utils/eoParallel.h" #include "utils/eoParallel.h"
#include "eoInt.h"
#include "eoRealToIntMonOp.h"
#include "eoRealToIntQuadOp.h"
#endif #endif
// Local Variables: // Local Variables:

View file

@ -36,9 +36,12 @@
* *
* @ingroup Representations * @ingroup Representations
*/ */
template <class FitT, class T = size_t> class eoInt: public eoVector<FitT, T> template <class FitT, class T = size_t>
class eoInt: public eoVector<FitT, T>
{ {
public: public:
using AtomType = size_t;
using FitnessType = FitT;
/** /**
* (Default) Constructor. * (Default) Constructor.

73
eo/src/eoRealToIntMonOp.h Normal file
View file

@ -0,0 +1,73 @@
#ifndef eoRealToIntMonOp_h_INCLUDED
#define eoRealToIntMonOp_h_INCLUDED
#include "es/eoReal.h"
#include "utils/eoIntBounds.h"
template<class EOTINT, class EOTREAL = eoReal<typename EOTINT::FitnessType>>
class eoRealToIntMonOp : public eoMonOp<EOTINT>
{
public:
using EOTreal = EOTREAL;
enum Repair {
folds,
truncate
};
eoRealToIntMonOp( eoMonOp<EOTreal>& monop ) :
_whenout(Repair::truncate),
_nobounds(),
_bounds(_nobounds),
_monop(monop)
{ }
eoRealToIntMonOp( eoMonOp<EOTreal>& monop, eoIntBounds& bounds, Repair whenout = Repair::truncate ) :
_whenout(whenout),
_nobounds(),
_bounds(bounds),
_monop(monop)
{ }
bool operator()(EOTINT& intsol)
{
#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) );
bool changed = _monop(floatsol);
if(changed) {
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);
}
}
return changed;
}
protected:
Repair _whenout;
eoIntNoBounds _nobounds;
eoIntBounds& _bounds;
eoMonOp<EOTreal>& _monop;
};
#endif // eoRealToIntMonOp_h_INCLUDED

View file

@ -0,0 +1,94 @@
#ifndef eoRealToIntQuadOp_h_INCLUDED
#define eoRealToIntQuadOp_h_INCLUDED
#include "es/eoReal.h"
#include "utils/eoIntBounds.h"
template<class EOTINT, class EOTREAL = eoReal<typename EOTINT::FitnessType>>
class eoRealToIntQuadOp : public eoQuadOp<EOTINT>
{
public:
using EOTreal = EOTREAL;
enum Repair {
folds,
truncate
};
eoRealToIntQuadOp( eoQuadOp<EOTreal>& quadop ) :
_whenout(Repair::truncate),
_nobounds(),
_bounds(_nobounds),
_quadop(quadop)
{ }
eoRealToIntQuadOp( eoQuadOp<EOTreal>& quadop, eoIntBounds& bounds, Repair whenout = Repair::truncate ) :
_whenout(whenout),
_nobounds(),
_bounds(bounds),
_quadop(quadop)
{ }
bool operator()(EOTINT& intsol1, EOTINT& intsol2)
{
#ifndef NDEBUG
for(size_t i=0; i < intsol1.size(); ++i) {
assert(_bounds.isInBounds(intsol1[i]));
}
for(size_t i=0; i < intsol2.size(); ++i) {
assert(_bounds.isInBounds(intsol2[i]));
}
#endif
EOTreal floatsol1;
std::copy( std::begin(intsol1), std::end(intsol1), std::back_inserter(floatsol1) );
EOTreal floatsol2;
std::copy( std::begin(intsol2), std::end(intsol2), std::back_inserter(floatsol2) );
bool changed = _quadop(floatsol1, floatsol2);
if(changed) {
for(size_t i=0; i < floatsol1.size(); ++i) {
typename EOTreal::AtomType rounded = std::round(floatsol1[i]);
if( not _bounds.isInBounds(rounded) ) {
switch(_whenout) {
case Repair::truncate:
_bounds.truncate(rounded);
break;
case Repair::folds:
_bounds.foldsInBounds(rounded);
break;
}
}
intsol1[i] = static_cast<typename EOTINT::AtomType>(rounded);
}
for(size_t i=0; i < floatsol2.size(); ++i) {
typename EOTreal::AtomType rounded = std::round(floatsol2[i]);
if( not _bounds.isInBounds(rounded) ) {
switch(_whenout) {
case Repair::truncate:
_bounds.truncate(rounded);
break;
case Repair::folds:
_bounds.foldsInBounds(rounded);
break;
}
}
intsol2[i] = static_cast<typename EOTINT::AtomType>(rounded);
}
}
return changed;
}
protected:
Repair _whenout;
eoIntNoBounds _nobounds;
eoIntBounds& _bounds;
eoQuadOp<EOTreal>& _quadop;
};
#endif // eoRealToIntQuadOp_h_INCLUDED

View file

@ -39,6 +39,7 @@
template <class FitT> class eoReal: public eoVector<FitT, double> template <class FitT> class eoReal: public eoVector<FitT, double>
{ {
public: public:
using AtomType = double;
using FitnessType = FitT; using FitnessType = FitT;
/** /**

View file

@ -80,6 +80,8 @@ set (TEST_LIST
t-forge-FastGA t-forge-FastGA
t-eoFoundryFastGA t-eoFoundryFastGA
t-eoAlgoFoundryFastGA t-eoAlgoFoundryFastGA
t-eoRealToIntMonOp
t-eoRealToIntQuadOp
) )

View file

@ -0,0 +1,28 @@
#include <iostream>
#include <eo>
#include <es.h>
using namespace std;
int main(int, char**)
{
eoIntInterval bounds(1,5);
using Chrom = eoInt<double>;
using MutWrapper = eoRealToIntMonOp<Chrom>;
eoDetUniformMutation< typename MutWrapper::EOTreal > mutreal(/*range*/6, /*nb*/5);
MutWrapper mutint(mutreal, bounds);
Chrom sol({1,2,3,4,5});
bool changed = mutint(sol);
assert(changed);
for(auto& x : sol) {
assert(bounds.isInBounds(x));
}
}

View file

@ -0,0 +1,35 @@
#include <iostream>
#include <eo>
#include <es.h>
using namespace std;
int main(int, char**)
{
eoIntInterval intbounds(1,5);
eoRealInterval rb(1,5);
eoRealVectorBounds realbounds(5, rb);
using Chrom = eoInt<double>;
using CrossWrapper = eoRealToIntQuadOp<Chrom>;
eoSegmentCrossover< typename CrossWrapper::EOTreal > crossreal(realbounds, /*alpha*/0);
CrossWrapper crossint(crossreal, intbounds);
Chrom sol1({1,2,3,4,5});
Chrom sol2({1,2,3,4,5});
bool changed = crossint(sol1, sol2);
assert(changed);
for(auto& x : sol1) {
assert(intbounds.isInBounds(x));
}
for(auto& x : sol2) {
assert(intbounds.isInBounds(x));
}
}