fix: refactor foundry to be more dynamic than static
- Remove size template in favor of dynamic container: allow using the generic interface. - Using `operator=` was inducing slicing.
This commit is contained in:
parent
78be884270
commit
11ff72bdd9
5 changed files with 56 additions and 55 deletions
|
|
@ -23,8 +23,7 @@
|
|||
#ifndef _eoAlgoFoundry_H_
|
||||
#define _eoAlgoFoundry_H_
|
||||
|
||||
#include <array>
|
||||
#include <tuple>
|
||||
#include <vector>
|
||||
|
||||
/**
|
||||
*
|
||||
|
|
@ -32,18 +31,23 @@
|
|||
* @ingroup Foundry
|
||||
* @ingroup Algorithms
|
||||
*/
|
||||
template<class EOT, unsigned NBOP>
|
||||
template<class EOT>
|
||||
class eoAlgoFoundry : public eoAlgo<EOT>
|
||||
{
|
||||
public:
|
||||
static const size_t dim = NBOP;
|
||||
|
||||
/** The constructon only take an eval, because all other operators
|
||||
* are stored in the public containers.
|
||||
/**
|
||||
*/
|
||||
eoAlgoFoundry()
|
||||
eoAlgoFoundry( size_t nb_operators ) :
|
||||
_size(nb_operators),
|
||||
_encoding(_size,0)
|
||||
{ }
|
||||
|
||||
/** Select indices of all the operators.
|
||||
*/
|
||||
void select( std::vector<size_t> encoding )
|
||||
{
|
||||
_encoding = { 0 }; // dim * 0
|
||||
assert(encoding.size() == _encoding.size());
|
||||
_encoding = encoding;
|
||||
}
|
||||
|
||||
/** Access to the index of the currently selected operator.
|
||||
|
|
@ -53,15 +57,14 @@ class eoAlgoFoundry : public eoAlgo<EOT>
|
|||
return _encoding.at(i);
|
||||
}
|
||||
|
||||
/** Select indices of all the operators.
|
||||
*/
|
||||
void operator=( std::array<size_t,dim> a)
|
||||
size_t size() const
|
||||
{
|
||||
_encoding = a;
|
||||
return _size;
|
||||
}
|
||||
|
||||
protected:
|
||||
std::array<size_t, dim> _encoding;
|
||||
const size_t _size;
|
||||
std::vector<size_t> _encoding;
|
||||
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -76,10 +76,20 @@
|
|||
* @ingroup Algorithms
|
||||
*/
|
||||
template<class EOT>
|
||||
class eoAlgoFoundryEA : public eoAlgoFoundry<EOT,5>
|
||||
class eoAlgoFoundryEA : public eoAlgoFoundry<EOT>
|
||||
{
|
||||
public:
|
||||
using eoAlgoFoundry<EOT,5>::dim;
|
||||
/** The constructon only take an eval, because all other operators
|
||||
* are stored in the public containers.
|
||||
*/
|
||||
eoAlgoFoundryEA( eoPopEvalFunc<EOT>& eval, size_t max_gen ) :
|
||||
eoAlgoFoundry<EOT>(5),
|
||||
index_of(),
|
||||
_eval(eval),
|
||||
_max_gen(max_gen)
|
||||
{ }
|
||||
|
||||
public:
|
||||
|
||||
struct Indices
|
||||
{
|
||||
|
|
@ -93,17 +103,6 @@ class eoAlgoFoundryEA : public eoAlgoFoundry<EOT,5>
|
|||
/** Helper for keeping track of the indices of the underlying encoding. */
|
||||
const Indices index_of;
|
||||
|
||||
/** The constructon only take an eval, because all other operators
|
||||
* are stored in the public containers.
|
||||
*/
|
||||
eoAlgoFoundryEA( eoPopEvalFunc<EOT>& eval, size_t max_gen ) :
|
||||
index_of(),
|
||||
_eval(eval),
|
||||
_max_gen(max_gen)
|
||||
{
|
||||
_encoding = { 0 }; // dim * 0
|
||||
}
|
||||
|
||||
/* Operators containers @{ */
|
||||
eoForgeVector< eoContinue<EOT> > continuators;
|
||||
eoForgeVector< eoQuadOp<EOT> > crossovers;
|
||||
|
|
@ -116,11 +115,11 @@ class eoAlgoFoundryEA : public eoAlgoFoundry<EOT,5>
|
|||
*/
|
||||
void operator()(eoPop<EOT>& pop)
|
||||
{
|
||||
assert(continuators.size() > 0); assert(_encoding.at(index_of.continuators) < continuators.size());
|
||||
assert( crossovers.size() > 0); assert(_encoding.at(index_of.crossovers) < crossovers.size());
|
||||
assert( mutations.size() > 0); assert(_encoding.at(index_of.mutations) < mutations.size());
|
||||
assert( selectors.size() > 0); assert(_encoding.at(index_of.selectors) < selectors.size());
|
||||
assert(replacements.size() > 0); assert(_encoding.at(index_of.replacements) < replacements.size());
|
||||
assert(continuators.size() > 0); assert(this->at(index_of.continuators) < continuators.size());
|
||||
assert( crossovers.size() > 0); assert(this->at(index_of.crossovers) < crossovers.size());
|
||||
assert( mutations.size() > 0); assert(this->at(index_of.mutations) < mutations.size());
|
||||
assert( selectors.size() > 0); assert(this->at(index_of.selectors) < selectors.size());
|
||||
assert(replacements.size() > 0); assert(this->at(index_of.replacements) < replacements.size());
|
||||
|
||||
eoSequentialOp<EOT> variator;
|
||||
variator.add(this->crossover(), 1.0);
|
||||
|
|
@ -145,48 +144,47 @@ class eoAlgoFoundryEA : public eoAlgoFoundry<EOT,5>
|
|||
std::string name()
|
||||
{
|
||||
std::ostringstream name;
|
||||
name << _encoding.at(index_of.continuators) << " (" << this->continuator().className() << ") + ";
|
||||
name << _encoding.at(index_of.crossovers) << " (" << this->crossover().className() << ") + ";
|
||||
name << _encoding.at(index_of.mutations) << " (" << this->mutation().className() << ") + ";
|
||||
name << _encoding.at(index_of.selectors) << " (" << this->selector().className() << ") + ";
|
||||
name << _encoding.at(index_of.replacements) << " (" << this->replacement().className() << ")";
|
||||
name << this->at(index_of.continuators) << " (" << this->continuator().className() << ") + ";
|
||||
name << this->at(index_of.crossovers) << " (" << this->crossover().className() << ") + ";
|
||||
name << this->at(index_of.mutations) << " (" << this->mutation().className() << ") + ";
|
||||
name << this->at(index_of.selectors) << " (" << this->selector().className() << ") + ";
|
||||
name << this->at(index_of.replacements) << " (" << this->replacement().className() << ")";
|
||||
return name.str();
|
||||
}
|
||||
|
||||
protected:
|
||||
eoPopEvalFunc<EOT>& _eval;
|
||||
const size_t _max_gen;
|
||||
std::array<size_t, dim> _encoding;
|
||||
|
||||
public:
|
||||
eoContinue<EOT>& continuator()
|
||||
{
|
||||
assert(_encoding.at(index_of.continuators) < continuators.size());
|
||||
return continuators.instanciate(_encoding.at(index_of.continuators));
|
||||
assert(this->at(index_of.continuators) < continuators.size());
|
||||
return continuators.instanciate(this->at(index_of.continuators));
|
||||
}
|
||||
|
||||
eoQuadOp<EOT>& crossover()
|
||||
{
|
||||
assert(_encoding.at(index_of.crossovers) < crossovers.size());
|
||||
return crossovers.instanciate(_encoding.at(index_of.crossovers));
|
||||
assert(this->at(index_of.crossovers) < crossovers.size());
|
||||
return crossovers.instanciate(this->at(index_of.crossovers));
|
||||
}
|
||||
|
||||
eoMonOp<EOT>& mutation()
|
||||
{
|
||||
assert(_encoding.at(index_of.mutations) < mutations.size());
|
||||
return mutations.instanciate(_encoding.at(index_of.mutations));
|
||||
assert(this->at(index_of.mutations) < mutations.size());
|
||||
return mutations.instanciate(this->at(index_of.mutations));
|
||||
}
|
||||
|
||||
eoSelectOne<EOT>& selector()
|
||||
{
|
||||
assert(_encoding.at(index_of.selectors) < selectors.size());
|
||||
return selectors.instanciate(_encoding.at(index_of.selectors));
|
||||
assert(this->at(index_of.selectors) < selectors.size());
|
||||
return selectors.instanciate(this->at(index_of.selectors));
|
||||
}
|
||||
|
||||
eoReplacement<EOT>& replacement()
|
||||
{
|
||||
assert(_encoding.at(index_of.replacements) < replacements.size());
|
||||
return replacements.instanciate(_encoding.at(index_of.replacements));
|
||||
assert(this->at(index_of.replacements) < replacements.size());
|
||||
return replacements.instanciate(this->at(index_of.replacements));
|
||||
}
|
||||
|
||||
};
|
||||
|
|
|
|||
|
|
@ -72,6 +72,8 @@ public:
|
|||
static const size_t i_repl = eoAlgoFoundryEA<SUB>::Indices::replacements;
|
||||
|
||||
/** Decode the high-level problem encoding as an array of indices.
|
||||
*
|
||||
* @note: If the EOT is an eoInt, this will be optimized out.
|
||||
*
|
||||
* May be useful for getting a solution back into an eoAlgoFoundryEA.
|
||||
* @code
|
||||
|
|
@ -80,7 +82,7 @@ public:
|
|||
* auto& cont = foundry.continuator(); // Get the configured operator
|
||||
* @encode
|
||||
*/
|
||||
std::array<size_t,eoAlgoFoundryEA<SUB>::dim> decode( const EOT& sol )
|
||||
std::vector<size_t> decode( const EOT& sol ) const
|
||||
{
|
||||
// Denormalize
|
||||
size_t cont = static_cast<size_t>(std::ceil( sol[i_cont] * _foundry.continuators.size() ));
|
||||
|
|
@ -121,7 +123,7 @@ public:
|
|||
and 0 <= sele and sele < _foundry.selectors .size()
|
||||
and 0 <= repl and repl < _foundry.replacements.size()
|
||||
) {
|
||||
_foundry = config;
|
||||
_foundry.select(config);
|
||||
|
||||
// Actually perform a search
|
||||
_foundry(pop);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue