diff --git a/eo/src/gp/eoParseTreeDepthInit.h b/eo/src/gp/eoParseTreeDepthInit.h index ca9be86b..85cc8efe 100644 --- a/eo/src/gp/eoParseTreeDepthInit.h +++ b/eo/src/gp/eoParseTreeDepthInit.h @@ -31,6 +31,7 @@ #include #include #include +#include using namespace gp_parse_tree; using namespace std; @@ -142,4 +143,49 @@ class eoGpDepthInitializer : public eoInit< eoParseTree > bool grow; }; +/** + * A template function for ramped half and half initialization of an eoParseTree population + * @param pop the population to be created + * @param population_size the size of the population to be created + * @param init_max_depth the initial maximum tree depth + * @param initializor A vector containing the possible nodes + + \ingroup ParseTree + */ +template +void eoInitRampedHalfAndHalf(eoPop< eoParseTree > &pop, unsigned int population_size, unsigned int init_max_depth, vector &initializor) +{ + typedef eoParseTree EoType; + typedef eoPop< EoType > Pop; + + unsigned int M = init_max_depth - 1; + unsigned int part_pop_size = population_size / (2*M); + unsigned int m=0; + + + pop.clear(); + + // initialize with Depth's (D) -> 2 + for(m=init_max_depth; m >= 2; m--) + { + eoGpDepthInitializer grow_initializer(m, initializor, true); + Pop grow(part_pop_size, grow_initializer); + pop.insert(pop.begin(), grow.begin(), grow.end()); + + eoGpDepthInitializer full_initializer(m, initializor, false); + Pop full(part_pop_size, full_initializer); + pop.insert(pop.begin(), full.begin(), full.end()); + } + + bool g = true; + while (pop.size() < population_size) + { + eoGpDepthInitializer initializer(init_max_depth, initializor, g); + Pop p(1, initializer); + pop.insert(pop.begin(), p.begin(), p.end()); + g= !g; + } +} + + #endif