ramped half and half initialization has been added to eoParseTreeDepthInit

This commit is contained in:
jeggermo 2002-05-09 15:43:01 +00:00
commit 07f2de55dc
2 changed files with 34 additions and 11 deletions

View file

@ -70,8 +70,8 @@ int main(int argc, char *argv[])
// the parameters are passed on as well // the parameters are passed on as well
RegFitness eval(generationCounter, initSequence, parameter); RegFitness eval(generationCounter, initSequence, parameter);
// Depth Initializor, defaults to grow method. // Depth Initializor, set for Ramped Half and Half Initialization
eoParseTreeDepthInit<FitnessType, Node> initializer(parameter.InitMaxDepth, initSequence); eoParseTreeDepthInit<FitnessType, Node> initializer(parameter.InitMaxDepth, initSequence, true, true);
// create the initial population // create the initial population
Pop pop(parameter.population_size, initializer); Pop pop(parameter.population_size, initializer);

View file

@ -2,7 +2,7 @@
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// eoParseTreeDepthInit.h : initializor for eoParseTree class // eoParseTreeDepthInit.h : initializor for eoParseTree class
// (c) Maarten Keijzer 2000 // (c) Maarten Keijzer 2000 Jeroen Eggermont 2002
/* /*
This library is free software; you can redistribute it and/or This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public modify it under the terms of the GNU Lesser General Public
@ -20,6 +20,7 @@
Contact: todos@geneura.ugr.es, http://geneura.ugr.es Contact: todos@geneura.ugr.es, http://geneura.ugr.es
mak@dhi.dk mak@dhi.dk
jeggermo@liacs.nl
*/ */
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -64,16 +65,20 @@ class eoParseTreeDepthInit : public eoInit< eoParseTree<FType, Node> >
* @parm _max_depth The maximum depth of a tree * @parm _max_depth The maximum depth of a tree
* @param _initializor A vector containing the possible nodes * @param _initializor A vector containing the possible nodes
* @param _grow False results in a full tree, True result is a randomly grown tree * @param _grow False results in a full tree, True result is a randomly grown tree
* @param _ramped_half_and_half True results in Ramped Half and Half Initialization
*/ */
eoParseTreeDepthInit( eoParseTreeDepthInit(
unsigned _max_depth, unsigned _max_depth,
const vector<Node>& _initializor, const vector<Node>& _initializor,
bool _grow = true) bool _grow = true,
bool _ramped_half_and_half = false)
: :
eoInit<EoType>(), eoInit<EoType>(),
max_depth(_max_depth), max_depth(_max_depth),
initializor(_initializor), initializor(_initializor),
grow(_grow) grow(_grow),
ramped_half_and_half(_ramped_half_and_half),
current_depth(_max_depth)
{ {
if(initializor.empty()) if(initializor.empty())
{ {
@ -92,11 +97,24 @@ class eoParseTreeDepthInit : public eoInit< eoParseTree<FType, Node> >
void operator()(EoType& _tree) void operator()(EoType& _tree)
{ {
list<Node> sequence; list<Node> sequence;
generate(sequence, current_depth);
generate(sequence, max_depth);
parse_tree<Node> tmp(sequence.begin(), sequence.end()); parse_tree<Node> tmp(sequence.begin(), sequence.end());
_tree.swap(tmp); _tree.swap(tmp);
if(ramped_half_and_half)
{
if(grow)
{
if (current_depth > 2)
current_depth--;
else
current_depth = max_depth;
}
// change the grow method from 'grow' to 'full' or from 'full' to 'grow'
grow = !grow;
};
} }
private : private :
void generate(list<Node>& sequence, int the_max, int last_terminal = -1) void generate(list<Node>& sequence, int the_max, int last_terminal = -1)
@ -141,10 +159,11 @@ class eoParseTreeDepthInit : public eoInit< eoParseTree<FType, Node> >
unsigned max_depth; unsigned max_depth;
std::vector<Node> initializor; std::vector<Node> initializor;
bool grow; bool grow;
bool ramped_half_and_half;
unsigned current_depth;
}; };
/** /**
@ -165,7 +184,11 @@ void eoInitRampedHalfAndHalf(eoPop< eoParseTree<FType,Node> > &pop, unsigned in
unsigned int M = init_max_depth - 1; unsigned int M = init_max_depth - 1;
unsigned int part_pop_size = population_size / (2*M); unsigned int part_pop_size = population_size / (2*M);
unsigned int m=0; unsigned int m=0;
cerr << "EO WARNING: Ramped Half and Half Initialization is now supported by eoParseTreeDepthInit." << endl;
cerr << " This function is now obsolete and might be removed in the future so you should"<< endl;
cerr << " update your code to use: " << endl << endl;
cerr << " eoParseTreeDepth(_max_depth,_initializer,_grow, bool _ramped_half_and_half)" << endl << endl;
pop.clear(); pop.clear();