* indentations + whitespace cleanup

This commit is contained in:
Caner Candan 2011-05-05 16:54:00 +02:00
commit 56c6edab04
285 changed files with 6068 additions and 6223 deletions

View file

@ -19,7 +19,7 @@
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
mak@dhi.dk
mak@dhi.dk
*/
//-----------------------------------------------------------------------------
@ -117,7 +117,7 @@ public:
*/
void printOn(std::ostream& os) const
{
EO<FType>::printOn(os);
EO<FType>::printOn(os);
os << ' ';
os << size() << ' ';
@ -133,13 +133,13 @@ public:
{
EO<FType>::readFrom(is);
EO<FType>::readFrom(is);
unsigned sz;
is >> sz;
std::vector<Node> v(sz);
std::vector<Node> v(sz);
unsigned i;
@ -149,24 +149,24 @@ public:
is >> node;
v[i] = node;
}
parse_tree<Node> tmp(v.begin(), v.end());
swap(tmp);
parse_tree<Node> tmp(v.begin(), v.end());
swap(tmp);
/*
* old code which caused problems for paradisEO
*
* this can be removed once it has proved itself
EO<FType>::readFrom(is);
/*
* old code which caused problems for paradisEO
*
* this can be removed once it has proved itself
EO<FType>::readFrom(is);
// even older code
FType fit;
// even older code
FType fit;
is >> fit;
fitness(fit);
std::copy(std::istream_iterator<Node>(is), std::istream_iterator<Node>(), back_inserter(*this));
*/
*/
}
};
/** @example t-eoSymreg.cpp

View file

@ -1,5 +1,5 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
//-----------------------------------------------------------------------------
// eoParseTreeDepthInit.h : initializor for eoParseTree class
// (c) Maarten Keijzer 2000 Jeroen Eggermont 2002
@ -8,20 +8,20 @@
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
mak@dhi.dk
jeggermo@liacs.nl
mak@dhi.dk
jeggermo@liacs.nl
*/
//-----------------------------------------------------------------------------
@ -48,17 +48,17 @@ template <class FType, class Node>
class eoParseTreeDepthInit : public eoInit< eoParseTree<FType, Node> >
{
protected:
// a binary predicate for sorting
// hopefully this will work with M$VC++ 6.0
struct lt_arity:public std::binary_function<Node,Node,bool>
{
bool operator()(const Node &_node1, const Node &_node2) { return (_node1.arity() < _node2.arity());};
};
// a binary predicate for sorting
// hopefully this will work with M$VC++ 6.0
struct lt_arity:public std::binary_function<Node,Node,bool>
{
bool operator()(const Node &_node1, const Node &_node2) { return (_node1.arity() < _node2.arity());};
};
public :
typedef eoParseTree<FType, Node> EoType;
/**
* Constructor
* @param _max_depth The maximum depth of a tree
@ -66,18 +66,18 @@ class eoParseTreeDepthInit : public eoInit< eoParseTree<FType, Node> >
* @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,
const std::vector<Node>& _initializor,
const std::vector<Node>& _initializor,
bool _grow = true,
bool _ramped_half_and_half = false)
bool _ramped_half_and_half = false)
:
eoInit<EoType>(),
max_depth(_max_depth),
initializor(_initializor),
grow(_grow),
ramped_half_and_half(_ramped_half_and_half),
current_depth(_max_depth)
max_depth(_max_depth),
initializor(_initializor),
grow(_grow),
ramped_half_and_half(_ramped_half_and_half),
current_depth(_max_depth)
{
if(initializor.empty())
{
@ -88,82 +88,82 @@ class eoParseTreeDepthInit : public eoInit< eoParseTree<FType, Node> >
stable_sort(initializor.begin(), initializor.end(), lt_arity());
}
/// My class name
virtual std::string className() const { return "eoParseTreeDepthInit"; };
virtual std::string className() const { return "eoParseTreeDepthInit"; };
/**initialize a tree
* @param _tree : the tree to be initialized
*/
void operator()(EoType& _tree)
{
{
std::list<Node> sequence;
generate(sequence, current_depth);
parse_tree<Node> tmp(sequence.begin(), sequence.end());
_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;
};
}
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 :
void generate(std::list<Node>& sequence, int the_max, int last_terminal = -1)
{
if (last_terminal == -1)
{ // check where the last terminal in the sequence resides
if (last_terminal == -1)
{ // check where the last terminal in the sequence resides
typename std::vector<Node>::iterator it;
for (it = initializor.begin(); it != initializor.end(); ++it)
{
if (it->arity() > 0)
break;
}
last_terminal = it - initializor.begin();
}
for (it = initializor.begin(); it != initializor.end(); ++it)
{
if (it->arity() > 0)
break;
}
if (the_max == 1)
{ // generate terminals only
typename std::vector<Node>::iterator it = initializor.begin() + rng.random(last_terminal);
it->randomize();
sequence.push_front(*it);
return;
}
typename std::vector<Node>::iterator what_it;
last_terminal = it - initializor.begin();
}
if (grow)
{
what_it = initializor.begin() + rng.random(initializor.size());
}
else // full
{
what_it = initializor.begin() + last_terminal + rng.random(initializor.size() - last_terminal);
}
if (the_max == 1)
{ // generate terminals only
typename std::vector<Node>::iterator it = initializor.begin() + rng.random(last_terminal);
it->randomize();
sequence.push_front(*it);
return;
}
typename std::vector<Node>::iterator what_it;
if (grow)
{
what_it = initializor.begin() + rng.random(initializor.size());
}
else // full
{
what_it = initializor.begin() + last_terminal + rng.random(initializor.size() - last_terminal);
}
what_it->randomize();
sequence.push_front(*what_it);
sequence.push_front(*what_it);
for (int i = 0; i < what_it->arity(); ++i)
generate(sequence, the_max - 1, last_terminal);
for (int i = 0; i < what_it->arity(); ++i)
generate(sequence, the_max - 1, last_terminal);
}
unsigned max_depth;
std::vector<Node> initializor;
bool grow;
bool ramped_half_and_half;
unsigned current_depth;
unsigned max_depth;
std::vector<Node> initializor;
bool grow;
bool ramped_half_and_half;
unsigned current_depth;
};
/**
@ -172,46 +172,46 @@ class eoParseTreeDepthInit : public eoInit< eoParseTree<FType, Node> >
* @param population_size the size of the population to be created
* @param init_max_depth the initial maximum tree depth
* @param initializor A std::vector containing the possible nodes
\ingroup ParseTree
*/
template <class FType, class Node>
void eoInitRampedHalfAndHalf(eoPop< eoParseTree<FType,Node> > &pop, unsigned int population_size, unsigned int init_max_depth, std::vector<Node> &initializor)
{
typedef eoParseTree<FType,Node> 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;
typedef eoParseTree<FType,Node> EoType;
typedef eoPop< EoType > Pop;
std::cerr << "EO WARNING: Ramped Half and Half Initialization is now supported by eoParseTreeDepthInit." << std::endl;
std::cerr << " This function is now obsolete and might be removed in the future so you should"<< std::endl;
std::cerr << " update your code to use: " << std::endl << std::endl;
std::cerr << " eoParseTreeDepthInit( _max_depth, _initializer, bool _grow, bool _ramped_half_and_half)" << std::endl << std::endl;
pop.clear();
// initialize with Depth's (D) -> 2
for(m=init_max_depth; m >= 2; m--)
{
eoParseTreeDepthInit<FType, Node> grow_initializer(m, initializor, true);
Pop grow(part_pop_size, grow_initializer);
pop.insert(pop.begin(), grow.begin(), grow.end());
eoParseTreeDepthInit<FType, Node> 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)
{
eoParseTreeDepthInit<FType, Node> initializer(init_max_depth, initializor, g);
Pop p(1, initializer);
pop.insert(pop.begin(), p.begin(), p.end());
g= !g;
}
unsigned int M = init_max_depth - 1;
unsigned int part_pop_size = population_size / (2*M);
unsigned int m=0;
std::cerr << "EO WARNING: Ramped Half and Half Initialization is now supported by eoParseTreeDepthInit." << std::endl;
std::cerr << " This function is now obsolete and might be removed in the future so you should"<< std::endl;
std::cerr << " update your code to use: " << std::endl << std::endl;
std::cerr << " eoParseTreeDepthInit( _max_depth, _initializer, bool _grow, bool _ramped_half_and_half)" << std::endl << std::endl;
pop.clear();
// initialize with Depth's (D) -> 2
for(m=init_max_depth; m >= 2; m--)
{
eoParseTreeDepthInit<FType, Node> grow_initializer(m, initializor, true);
Pop grow(part_pop_size, grow_initializer);
pop.insert(pop.begin(), grow.begin(), grow.end());
eoParseTreeDepthInit<FType, Node> 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)
{
eoParseTreeDepthInit<FType, Node> initializer(init_max_depth, initializor, g);
Pop p(1, initializer);
pop.insert(pop.begin(), p.begin(), p.end());
g= !g;
}
}

View file

@ -1,5 +1,5 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
//-----------------------------------------------------------------------------
// eoParseTreeOp.h : crossover and mutation operator for the eoParseTree class
// (c) Maarten Keijzer 2000 for eoSubtreeXOver, eoBranchMutation
@ -10,18 +10,18 @@
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
mak@dhi.dk
mak@dhi.dk
jeggermo@liacs.nl
*/
//-----------------------------------------------------------------------------
@ -34,8 +34,8 @@
#include <gp/eoParseTree.h>
/** eoSubtreeXOver --> subtree xover
\class eoSubtreeXOver eoParseTreeOp.h gp/eoParseTreeOp.h
/** eoSubtreeXOver --> subtree xover
\class eoSubtreeXOver eoParseTreeOp.h gp/eoParseTreeOp.h
\ingroup ParseTree
*/
template<class FType, class Node>
@ -63,23 +63,23 @@ public:
*/
bool operator()(EoType & _eo1, EoType & _eo2 )
{
int i = rng.random(_eo1.size());
int j = rng.random(_eo2.size());
int i = rng.random(_eo1.size());
int j = rng.random(_eo2.size());
typename parse_tree<Node>::subtree tmp = _eo1[i];
_eo1[i] = _eo2[j]; // insert subtree
_eo2[j] = tmp;
typename parse_tree<Node>::subtree tmp = _eo1[i];
_eo1[i] = _eo2[j]; // insert subtree
_eo2[j] = tmp;
_eo1.pruneTree(max_length);
_eo2.pruneTree(max_length);
_eo1.pruneTree(max_length);
_eo2.pruneTree(max_length);
return true;
}
private:
unsigned max_length;
};
/** eoBranchMutation --> replace a subtree with a randomly created subtree
/** eoBranchMutation --> replace a subtree with a randomly created subtree
\class eoBranchMutation eoParseTreeOp.h gp/eoParseTreeOp.h
\ingroup ParseTree
*/
@ -97,29 +97,29 @@ public:
eoBranchMutation(eoInit<EoType>& _init, unsigned _max_length)
: eoMonOp<EoType>(), max_length(_max_length), initializer(_init)
{};
/// the class name
virtual std::string className() const { return "eoBranchMutation"; };
/// Dtor
virtual ~eoBranchMutation() {};
/**
* Mutate an individual
* @param _eo1 The individual that is to be changed
*/
bool operator()(EoType& _eo1 )
{
int i = rng.random(_eo1.size());
int i = rng.random(_eo1.size());
EoType eo2;
initializer(eo2);
int j = rng.random(eo2.size());
int j = rng.random(eo2.size());
_eo1[i] = eo2[j]; // insert subtree
_eo1[i] = eo2[j]; // insert subtree
_eo1.pruneTree(max_length);
_eo1.pruneTree(max_length);
return true;
}
@ -130,7 +130,7 @@ private :
eoInit<EoType>& initializer;
};
// Additional Mutation operators from
// Additional Mutation operators from
// TITLE:"Genetic Programming~An Introduction"
// AUTHORS: Banzhaf, Nordin, Keller, Francone
// ISBN: 3-920993-58-6
@ -138,8 +138,8 @@ private :
//
// For the eoParseTree class
/** eoPointMutation --> replace a Node with a Node of the same arity
\class eoPointMutation eoParseTreeOp.h gp/eoParseTreeOp.h
/** eoPointMutation --> replace a Node with a Node of the same arity
\class eoPointMutation eoParseTreeOp.h gp/eoParseTreeOp.h
\ingroup ParseTree
*/
@ -157,7 +157,7 @@ public:
eoPointMutation( std::vector<Node>& _initializor)
: eoMonOp<EoType>(), initializor(_initializor)
{};
/// the class name
virtual std::string className() const { return "eoPointMutation"; };
@ -170,32 +170,32 @@ public:
*/
bool operator()(EoType& _eo1 )
{
// select a random node i that is to be mutated
int i = rng.random(_eo1.size());
// request the arity of the node that is to be replaced
int arity = _eo1[i].arity();
int j=0;
do
{
j = rng.random(initializor.size());
}while ((initializor[j].arity() != arity));
_eo1[i] = initializor[j];
return true;
// select a random node i that is to be mutated
int i = rng.random(_eo1.size());
// request the arity of the node that is to be replaced
int arity = _eo1[i].arity();
int j=0;
do
{
j = rng.random(initializor.size());
}while ((initializor[j].arity() != arity));
_eo1[i] = initializor[j];
return true;
}
private :
std::vector<Node>& initializor;
std::vector<Node>& initializor;
};
/** eoExpansionMutation --> replace a terminal with a randomly created subtree
/** eoExpansionMutation --> replace a terminal with a randomly created subtree
\class eoExpansionMutation eoParseTreeOp.h gp/eoParseTreeOp.h
\ingroup ParseTree
*/
@ -206,7 +206,7 @@ class eoExpansionMutation: public eoMonOp< eoParseTree<FType, Node> >
public:
typedef eoParseTree<FType, Node> EoType;
/**
* Constructor
* @param _init An instantiation of eoGpDepthInitializer
@ -215,7 +215,7 @@ public:
eoExpansionMutation(eoInit<EoType>& _init, unsigned _max_length)
: eoMonOp<EoType>(), max_length(_max_length), initializer(_init)
{};
/// The class name
virtual std::string className() const { return "eoExpansionMutation"; };
@ -227,33 +227,33 @@ public:
*/
bool operator()(EoType& _eo1 )
{
int i = rng.random(_eo1.size());
// look for a terminal
int i = rng.random(_eo1.size());
// look for a terminal
while (_eo1[i].arity() != 0)
{
i= rng.random(_eo1.size());
};
// create a new tree to
EoType eo2;
// make sure we get a tree with more than just a terminal
do
{
initializer(eo2);
}while(eo2.size() == 1);
int j = rng.random(eo2.size());
// make sure we select a subtree (and not a terminal)
while((eo2[j].arity() == 0))
{
j = rng.random(eo2.size());
};
{
i= rng.random(_eo1.size());
};
_eo1[i] = eo2[j]; // insert subtree
// create a new tree to
EoType eo2;
// make sure we get a tree with more than just a terminal
do
{
initializer(eo2);
}while(eo2.size() == 1);
int j = rng.random(eo2.size());
// make sure we select a subtree (and not a terminal)
while((eo2[j].arity() == 0))
{
j = rng.random(eo2.size());
};
_eo1[i] = eo2[j]; // insert subtree
_eo1.pruneTree(max_length);
_eo1.pruneTree(max_length);
return true;
}
@ -294,29 +294,29 @@ public:
*/
bool operator()(EoType& _eo1 )
{
int i = rng.random(_eo1.size());
// look for a subtree
int i = rng.random(_eo1.size());
// look for a subtree
while ((_eo1[i].arity() == 0) && (_eo1.size() > 1))
{
i= rng.random(_eo1.size());
};
// create a new tree to
EoType eo2;
initializer(eo2);
int j = rng.random(eo2.size());
// make sure we select a subtree (and not a terminal)
while(eo2[j].arity() != 0)
{
j = rng.random(eo2.size());
};
{
i= rng.random(_eo1.size());
};
// create a new tree to
EoType eo2;
initializer(eo2);
int j = rng.random(eo2.size());
// make sure we select a subtree (and not a terminal)
while(eo2[j].arity() != 0)
{
j = rng.random(eo2.size());
};
_eo1[i] = eo2[j]; // insert subtree
// we don't have to prune because the subtree is always smaller
_eo1.pruneTree(max_length);
_eo1[i] = eo2[j]; // insert subtree
// we don't have to prune because the subtree is always smaller
_eo1.pruneTree(max_length);
return true;
}
@ -327,11 +327,11 @@ private :
};
/** eoHoistMutation --> replace the individual with one of its subtree's
/** eoHoistMutation --> replace the individual with one of its subtree's
\class eoHoistMutation eoParseTreeOp.h gp/eoParseTreeOp.h
\ingroup ParseTree
*/
template<class FType, class Node>
class eoHoistMutation: public eoMonOp< eoParseTree<FType, Node> >
{
@ -345,7 +345,7 @@ public:
eoHoistMutation()
: eoMonOp<EoType>()
{};
/// The class name
virtual std::string className() const { return "eoHoistMutation"; };
@ -357,17 +357,17 @@ public:
*/
bool operator()(EoType& _eo1 )
{
// select a hoist point
int i = rng.random(_eo1.size());
// and create a new tree
EoType eo2(_eo1[i]);
// we don't have to prune because the new tree is always smaller
//_eo1.pruneTree(max_length);
_eo1 = eo2;
// select a hoist point
int i = rng.random(_eo1.size());
// and create a new tree
EoType eo2(_eo1[i]);
// we don't have to prune because the new tree is always smaller
//_eo1.pruneTree(max_length);
_eo1 = eo2;
return true;
}

View file

@ -1,5 +1,5 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
//-----------------------------------------------------------------------------
// eoStParseTreeDepthInit.h : initializor strongly type GP
// (c) Jeroen Eggermont 2001
@ -8,19 +8,19 @@
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
jeggermo@liacs.nl
jeggermo@liacs.nl
*/
//-----------------------------------------------------------------------------
@ -49,7 +49,7 @@ using namespace gp_parse_tree;
individual but now each node class must have two additional functions.
\li int type(void) which returns the return type of the node
\li int type(int child) which returns the required type for child 0, 1 or 2
Pruning strongly typed trees is not possible at the moment.
\ingroup Representations
@ -66,7 +66,7 @@ class eoStParseTreeDepthInit : public eoInit< eoParseTree<FType, Node> >
public :
typedef eoParseTree<FType, Node> EoType;
/**
* Constructor
* @param _max_depth The maximum depth of a tree
@ -74,121 +74,121 @@ class eoStParseTreeDepthInit : public eoInit< eoParseTree<FType, Node> >
* @param _return_type (JD_2010-11-09: don't know the use of this parameter, maybe to force implicit template instanciation?)
* @param _grow False results in a full tree, True result is a randomly grown tree
*/
eoStParseTreeDepthInit(
eoStParseTreeDepthInit(
unsigned _max_depth,
const std::vector<Node>& _node,
const int& _return_type,
const std::vector<Node>& _node,
const int& _return_type,
bool _grow = true)
:
eoInit<EoType>(),
max_depth(_max_depth),
return_type(_return_type),
grow(_grow)
max_depth(_max_depth),
return_type(_return_type),
grow(_grow)
{
if(_node.empty())
{
throw std::logic_error("eoStParseTreeDepthInit: uhm, wouldn't you rather give a non-empty set of Nodes?");
}
unsigned int i=0;
int arity=0;
int type=0;
std::vector<Node> node_vector;
for(i=0; i < _node.size(); i++)
{
arity = _node[i].arity();
type = _node[i].type();
if(arity==0)
{
node_vector = node[type][TERMINAL];
node_vector.push_back(_node[i]);
node[type][TERMINAL]= node_vector;
}
else
//if (arity != 0) // non-terminal
{
node_vector = node[type][NONTERMINAL];
node_vector.push_back(_node[i]);
node[type][NONTERMINAL] = node_vector;
}
node_vector = node[type][ALL];
node_vector.push_back(_node[i]);
node[type][ALL] = node_vector;
arity = _node[i].arity();
type = _node[i].type();
if(arity==0)
{
node_vector = node[type][TERMINAL];
node_vector.push_back(_node[i]);
node[type][TERMINAL]= node_vector;
}
else
//if (arity != 0) // non-terminal
{
node_vector = node[type][NONTERMINAL];
node_vector.push_back(_node[i]);
node[type][NONTERMINAL] = node_vector;
}
node_vector = node[type][ALL];
node_vector.push_back(_node[i]);
node[type][ALL] = node_vector;
}
}
/// My class name
virtual std::string className() const { return "eoStParseTreeDepthInit"; };
virtual std::string className() const { return "eoStParseTreeDepthInit"; };
/**initialize a tree
* @param _tree : the tree to be initialized
*/
void operator()(EoType& _tree)
{
std::list<Node> sequence;
bool good_tree = false;
do
{
sequence.clear();
good_tree = generate(sequence, max_depth, return_type);
}while (!good_tree);
{
std::list<Node> sequence;
bool good_tree = false;
do
{
sequence.clear();
good_tree = generate(sequence, max_depth, return_type);
}while (!good_tree);
parse_tree<Node> tmp(sequence.begin(), sequence.end());
_tree.swap(tmp);
}
parse_tree<Node> tmp(sequence.begin(), sequence.end());
_tree.swap(tmp);
}
private :
bool generate(std::list<Node>& sequence, int the_max, int request_type)
{
int selected=0;
bool ok = true;
if (the_max == 1)
{ // generate terminals only
if( node[request_type][TERMINAL].empty() ) // no possible terminal node of this type
return false; // we have an invalid tree
else
{
selected = rng.random((node[request_type][TERMINAL]).size());
sequence.push_front(node[request_type][TERMINAL][selected]);
return true;
}
}
int arity=0;
if (grow)
{
selected = rng.random((node[request_type][ALL]).size());
arity = node[request_type][ALL][selected].arity();
sequence.push_front(node[request_type][ALL][selected]);
for (int i = 0; i < arity; ++i)
ok &= generate(sequence, the_max - 1, node[request_type][ALL][selected].type(i));
}
else // full
{
selected = rng.random((node[request_type][NONTERMINAL]).size());
arity = node[request_type][NONTERMINAL][selected].arity();
sequence.push_front(node[request_type][NONTERMINAL][selected]);
for (int i = 0; i < arity; ++i)
ok &=generate(sequence, the_max - 1, node[request_type][NONTERMINAL][selected].type(i));
}
return ok;
int selected=0;
bool ok = true;
if (the_max == 1)
{ // generate terminals only
if( node[request_type][TERMINAL].empty() ) // no possible terminal node of this type
return false; // we have an invalid tree
else
{
selected = rng.random((node[request_type][TERMINAL]).size());
sequence.push_front(node[request_type][TERMINAL][selected]);
return true;
}
}
int arity=0;
if (grow)
{
selected = rng.random((node[request_type][ALL]).size());
arity = node[request_type][ALL][selected].arity();
sequence.push_front(node[request_type][ALL][selected]);
for (int i = 0; i < arity; ++i)
ok &= generate(sequence, the_max - 1, node[request_type][ALL][selected].type(i));
}
else // full
{
selected = rng.random((node[request_type][NONTERMINAL]).size());
arity = node[request_type][NONTERMINAL][selected].arity();
sequence.push_front(node[request_type][NONTERMINAL][selected]);
for (int i = 0; i < arity; ++i)
ok &=generate(sequence, the_max - 1, node[request_type][NONTERMINAL][selected].type(i));
}
return ok;
}
unsigned max_depth;
std::map < int, std::map < int, std::vector<Node> > > node;
unsigned max_depth;
std::map < int, std::map < int, std::vector<Node> > > node;
int return_type;
bool grow;
bool grow;
};
#endif

View file

@ -1,5 +1,5 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
//-----------------------------------------------------------------------------
// eoStParseTreeOp.h : crossover and mutation operators for the strongly typed GP
// (c) Jeroen Eggermont 2001 for other mutation operators
@ -9,18 +9,18 @@
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
mak@dhi.dk
mak@dhi.dk
jeggermo@liacs.nl
*/
//-----------------------------------------------------------------------------
@ -40,17 +40,17 @@
template <class EOT>
void get_possible_nodes(const EOT &_eo, std::vector<int> &possible_nodes, const int type)
{
int n=0;
possible_nodes.clear();
// collect the possible crossover points in _eo (nodes with the same type)
for(n=0; n < _eo.size(); n++)
if (type == _eo[n]->type())
possible_nodes.push_back(n);
}
int n=0;
possible_nodes.clear();
// collect the possible crossover points in _eo (nodes with the same type)
for(n=0; n < _eo.size(); n++)
if (type == _eo[n]->type())
possible_nodes.push_back(n);
}
/** eoStSubtreeXOver --> subtree xover for strongly typed tree-based genetic programming
\class eoStSubtreeXOver eoStParseTreeOp.h gp/eoStParseTreeOp.h
\class eoStSubtreeXOver eoStParseTreeOp.h gp/eoStParseTreeOp.h
\ingroup StParseTree
*/
template<class FType, class Node>
@ -78,54 +78,54 @@ public:
*/
bool operator()(EoType & _eo1, EoType & _eo2 )
{
int i = 0;
std::vector<int> nodes;
int n = 0;
int type = 0;
int j = 0;
std::set<int> test;
do
{
do // select a random node in _eo1 as crossover point, and check if we didn't try it already
{
i = rng.random(_eo1.size());
}while(test.count(i) > 0);
test.insert(i);
type = _eo1[i]->type();
get_possible_nodes<EoType>(_eo2, nodes, type);
}while(nodes.empty() && (test.size() < _eo1.size()));
if (nodes.empty()) // we failed to select a crossover point but tried all points (test.size() == _eo1.size()).
return true; // should this be false ??
int i = 0;
std::vector<int> nodes;
int n = 0;
int type = 0;
int j = 0;
std::set<int> test;
do
{
do // select a random node in _eo1 as crossover point, and check if we didn't try it already
{
i = rng.random(_eo1.size());
}while(test.count(i) > 0);
// we did find at least one possible crossover point in _eo2
n = rng.random(nodes.size());
j = nodes[n];
test.insert(i);
typename eoParseTree<FType, Node>::subtree tmp = _eo1[i];
_eo1[i] = _eo2[j]; // insert subtree
_eo2[j] = tmp;
type = _eo1[i]->type();
// we can't prune anymore
/*
get_possible_nodes<EoType>(_eo2, nodes, type);
}while(nodes.empty() && (test.size() < _eo1.size()));
if (nodes.empty()) // we failed to select a crossover point but tried all points (test.size() == _eo1.size()).
return true; // should this be false ??
// we did find at least one possible crossover point in _eo2
n = rng.random(nodes.size());
j = nodes[n];
typename eoParseTree<FType, Node>::subtree tmp = _eo1[i];
_eo1[i] = _eo2[j]; // insert subtree
_eo2[j] = tmp;
// we can't prune anymore
/*
_eo1.pruneTree(max_length);
_eo2.pruneTree(max_length);
*/
return true;
*/
return true;
}
private:
unsigned max_length;
};
/** eoStBranchMutation --> replace a strongly typed subtree with a randomly created strongly typed subtree
/** eoStBranchMutation --> replace a strongly typed subtree with a randomly created strongly typed subtree
\class eoStBranchMutation eoStParseTreeOp.h gp/eoStParseTreeOp.h
\ingroup StParseTree
*/
@ -143,42 +143,42 @@ public:
eoStBranchMutation(eoInit<EoType>& _init, unsigned _max_length)
: eoMonOp<EoType>(), max_length(_max_length), initializer(_init)
{};
/// the class name
virtual std::string className() const { return "eoStBranchMutation"; };
/// Dtor
virtual ~eoStBranchMutation() {};
/**
* Mutate an individual
* @param _eo1 The individual that is to be changed
*/
bool operator()(EoType& _eo1 )
{
int i = rng.random(_eo1.size());
std::vector<int> nodes;
int type = _eo1[i]->type();
int j=0;
int n=0;
int i = rng.random(_eo1.size());
std::vector<int> nodes;
int type = _eo1[i]->type();
int j=0;
int n=0;
EoType eo2;
do
{
initializer(eo2);
get_possible_nodes(eo2, nodes, type);
}while (nodes.empty());
EoType eo2;
n = rng.random(nodes.size());
j = nodes[n];
_eo1[i] = eo2[j]; // insert subtree
// no more pruning
/*
_eo1.pruneTree(max_length);
*/
do
{
initializer(eo2);
get_possible_nodes(eo2, nodes, type);
}while (nodes.empty());
n = rng.random(nodes.size());
j = nodes[n];
_eo1[i] = eo2[j]; // insert subtree
// no more pruning
/*
_eo1.pruneTree(max_length);
*/
return true;
}
@ -208,22 +208,22 @@ public:
eoStPointMutation( std::vector<Node>& _node)
: eoMonOp<EoType>()
{
unsigned int i=0;
unsigned int i=0;
int arity=0;
int type=0;
std::vector<Node> node_vector;
for(i=0; i < _node.size(); i++)
{
arity = _node[i].arity();
type = _node[i].type();
node_vector = node[type][arity];
node_vector.push_back(_node[i]);
node[type][arity]= node_vector;
arity = _node[i].arity();
type = _node[i].type();
node_vector = node[type][arity];
node_vector.push_back(_node[i]);
node[type][arity]= node_vector;
};
};
/// the class name
virtual std::string className() const { return "eoStPointMutation"; };
@ -236,24 +236,24 @@ public:
*/
bool operator()(EoType& _eo1 )
{
// select a random node i that is to be mutated
int i = rng.random(_eo1.size());
int arity = _eo1[i].arity();
int type = _eo1[i]->type();
int j = rng.random(node[type][arity].size());
// select a random node i that is to be mutated
int i = rng.random(_eo1.size());
int arity = _eo1[i].arity();
int type = _eo1[i]->type();
int j = rng.random(node[type][arity].size());
_eo1[i] = node[type][arity][j];
return true;
_eo1[i] = node[type][arity][j];
return true;
}
private :
std::map < int, std::map < int, std::vector<Node> > > node;
std::map < int, std::map < int, std::vector<Node> > > node;
};
/** eoStHoistMutation --> replace the individual with one of its strongly typed subtree's
/** eoStHoistMutation --> replace the individual with one of its strongly typed subtree's
\class eoStHoistMutation eoStParseTreeOp.h gp/eoStParseTreeOp.h
\ingroup StParseTree
*/
@ -271,13 +271,13 @@ public:
eoStHoistMutation(eoInit<EoType>& _init, unsigned _max_length)
: eoMonOp<EoType>(), max_length(_max_length), initializer(_init)
{};
/// the class name
virtual std::string className() const { return "eoStHoistMutation"; };
/// Dtor
virtual ~eoStHoistMutation() {};
/**
* Mutate an individual
* @param _eo1 The individual that is to be changed
@ -285,19 +285,19 @@ public:
bool operator()(EoType& _eo1 )
{
std::vector<int> nodes;
// get the type of the current tree
int type = _eo1[ _eo1.size() - 1 ]->type();
std::vector<int> nodes;
// get the type of the current tree
int type = _eo1[ _eo1.size() - 1 ]->type();
get_possible_nodes(_eo1, nodes, type);
// select a subtree-node to replace the current tree
int n = rng.random(nodes.size());
int i = nodes[n];
EoType eo2(_eo1[i]);
_eo1 = eo2;
get_possible_nodes(_eo1, nodes, type);
// select a subtree-node to replace the current tree
int n = rng.random(nodes.size());
int i = nodes[n];
EoType eo2(_eo1[i]);
_eo1 = eo2;
return true;
}

View file

@ -1,13 +1,13 @@
/**
* Pool allocator for the subtree and parse tree classes (homebrew and not compliant to ANSI allocator requirements)
* (c) copyright Maarten Keijzer 1999, 2000
* (c) copyright Maarten Keijzer 1999, 2000
* Permission to copy, use, modify, sell and distribute this software is granted provided
* Permission to copy, use, modify, sell and distribute this software is granted provided
* this copyright notice appears in all copies. This software is provided "as is" without
* express or implied warranty, and with no claim as to its suitability for
* any purpose.
* Permission to modify the code and to distribute modified code is granted,
* provided the above notices are retained, and a notice that the code was
* modified is included with the above copyright notice.
@ -21,7 +21,7 @@
class MemPool
{
public :
MemPool(unsigned int sz) : esize(sz<sizeof(Link)? sizeof(Link) : sz) {}
~MemPool()
{
@ -33,7 +33,7 @@ public :
delete p;
}
}
void* allocate()
{
if (head == 0) grow();
@ -62,10 +62,10 @@ private :
char* last = &start[(nelem-1)*esize];
for (char* p = start; p < last; p += esize)
{
reinterpret_cast<Link*>(p)->next =
reinterpret_cast<Link*>(p)->next =
reinterpret_cast<Link*>(p + esize);
}
reinterpret_cast<Link*>(last)->next = 0;
head = reinterpret_cast<Link*>(start);
}
@ -91,15 +91,15 @@ template<class T>
class Node_alloc
{
public :
T* allocate(void)
T* allocate(void)
{
T* t = static_cast<T*>(mem.allocate());
t = new (t) T;
return t;
}
T* construct(const T& org)
T* construct(const T& org)
{
T* t = static_cast<T*>(mem.allocate());
t = new (t) T(org);
@ -117,7 +117,7 @@ private :
};
template <class T>
template <class T>
class Standard_alloc
{
public :
@ -130,7 +130,7 @@ public :
return new T [arity];
}
T* construct(size_t arity, T* org)
{
if (arity == 0)
@ -154,7 +154,7 @@ public :
};
template <class T>
template <class T>
class Standard_Node_alloc
{
public :
@ -270,19 +270,19 @@ public :
switch(arity)
{
case 0: return;
case 3 :
case 3 :
{
t[2].~T(); t[1].~T(); t[0].~T();
mem3.deallocate(static_cast<void*>(t));
return;
}
case 2 :
case 2 :
{
t[1].~T(); t[0].~T();
mem2.deallocate(static_cast<void*>(t));
return;
}
case 1 :
case 1 :
{
t[0].~T();
mem1.deallocate(static_cast<void*>(t));
@ -295,7 +295,7 @@ public :
}
}
}
private :
static MemPool mem1;

View file

@ -23,22 +23,22 @@
****** Arity ******
\code
int arity(void) const
int arity(void) const
\endcode
Note: the default constructor of a Node should provide a
Node with arity 0!
Note: the default constructor of a Node should provide a
Node with arity 0!
****** Evaluation ******
A parse_tree is evaluated through one of it's apply() members:
1) parse_tree::apply(RetVal)
1) parse_tree::apply(RetVal)
is the simplest evaluation, it will call
is the simplest evaluation, it will call
\code
RetVal Node::operator()(RetVal, subtree<Node, RetVal>::const_iterator)
RetVal Node::operator()(RetVal, subtree<Node, RetVal>::const_iterator)
\endcode
(Unfortunately the first RetVal argument is mandatory (although you
@ -49,7 +49,7 @@
error. That is why you have to call tree.apply(double()) instead.)
2) parse_tree::apply(RetVal v, It values)
2) parse_tree::apply(RetVal v, It values)
will call:
@ -58,10 +58,10 @@
\endcode
where It is whatever type you desire (most of the time
this will be a std::vector containing the values of your
variables);
this will be a std::vector containing the values of your
variables);
3) parse_tree::apply(RetVal, It values, It2 moreValues)
3) parse_tree::apply(RetVal, It values, It2 moreValues)
will call:
@ -69,26 +69,26 @@
RetVal Node::operator()(RetVal, subtree<... , It values, It2 moreValues)
\endcode
although I do not see the immediate use of this, however...
although I do not see the immediate use of this, however...
4) parse_tree::apply(RetVal, It values, It2 args, It3 adfs)
4) parse_tree::apply(RetVal, It values, It2 args, It3 adfs)
that calls:
that calls:
\code
RetVal Node::operator()(subtree<... , It values, It2 args, It3 adfs)
\endcode
can be useful for implementing adfs.
can be useful for implementing adfs.
In general it is a good idea to leave the specifics of the
arguments open so that different ways of evaluation remain
possible. Implement the simplest eval as:
In general it is a good idea to leave the specifics of the
arguments open so that different ways of evaluation remain
possible. Implement the simplest eval as:
\code
template <class It>
RetVal operator()(RetVal dummy, It begin) const
template <class It>
RetVal operator()(RetVal dummy, It begin) const
\endcode
****** Internal Structure ******
@ -99,10 +99,10 @@
The nodes are stored in a tree like :
node4
/ \
node3 node2
/ \
node4
/ \
node3 node2
/ \
node1 node0
where nodes 2 and 4 have arity 2 and nodes 0,1 and 3 arity 0 (terminals)
@ -129,9 +129,9 @@
will not crash and result in a tree structured as:
node4
/ \
node3 node0
node4
/ \
node3 node0
Note that the rank numbers no longer specify their place in the tree:
@ -212,10 +212,10 @@ public :
typedef subtree* iterator;
typedef const subtree* const_iterator;
/* Constructors, assignments */
/* Constructors, assignments */
subtree(void) : content(node_allocator.allocate()), args(0), parent(0), _cumulative_size(0), _depth(0), _size(1)
{}
{}
subtree(const subtree& s)
: content(node_allocator.allocate()),
args(0),
@ -228,7 +228,7 @@ public :
}
subtree(const T& t) : content(node_allocator.allocate()), args(0), parent(0), _cumulative_size(0), _depth(0), _size(1)
{ copy(t); }
{ copy(t); }
template <class It>
subtree(It b, It e) : content(node_allocator.allocate()), args(0), parent(0), _cumulative_size(0), _depth(0), _size(1)
@ -239,57 +239,57 @@ public :
virtual ~subtree(void) { tree_allocator.deallocate(args, arity()); node_allocator.deallocate(content); }
subtree& operator=(const subtree& s)
{
if (s.get_root() == get_root())
{
if (s.get_root() == get_root())
{ // from the same tree, maybe a child. Don't take any chances
subtree anotherS = s;
return copy(anotherS);
}
copy(s);
copy(s);
updateAfterInsert();
return *this;
}
}
subtree& operator=(const T& t) { copy(t); updateAfterInsert(); return *this; }
/* Access to the nodes */
/* Access to the nodes */
T& operator*(void) { return *content; }
const T& operator*(void) const { return *content; }
T* operator->(void) { return content; }
const T* operator->(void) const { return content; }
/* Equality, inequality check, Node needs to implement operator== */
/* Equality, inequality check, Node needs to implement operator== */
bool operator==(const subtree& other) const
{
if (! (*content == *other.content))
return false;
bool operator==(const subtree& other) const
{
if (! (*content == *other.content))
return false;
for (int i = 0; i < arity(); i++)
{
if (!(args[i] == other.args[i]))
return false;
}
for (int i = 0; i < arity(); i++)
{
if (!(args[i] == other.args[i]))
return false;
}
return true;
}
return true;
}
bool operator !=(const subtree& other) const
{
return !operator==(other);
}
bool operator !=(const subtree& other) const
{
return !operator==(other);
}
/* Arity */
/* Arity */
int arity(void) const { return content->arity(); }
/* Evaluation with an increasing amount of user defined arguments */
template <class RetVal>
void apply(RetVal& v) const { (*content)(v, begin()); }
/* Evaluation with an increasing amount of user defined arguments */
template <class RetVal>
void apply(RetVal& v) const { (*content)(v, begin()); }
template <class RetVal, class It>
void apply(RetVal& v, It values) const
template <class RetVal, class It>
void apply(RetVal& v, It values) const
{
(*content)(v, begin(), values);
}
@ -302,12 +302,12 @@ public :
/* template <class RetVal, class It, class It2>
void apply(RetVal& v, It values, It2 moreValues) const
{ (*content)(v, begin(), values, moreValues); }
void apply(RetVal& v, It values, It2 moreValues) const
{ (*content)(v, begin(), values, moreValues); }
template <class RetVal, class It, class It2, class It3>
void apply(RetVal& v, It values, It2 moreValues, It3 evenMoreValues) const
{ (*content)(v, begin(), values, moreValues, evenMoreValues); }
template <class RetVal, class It, class It2, class It3>
void apply(RetVal& v, It values, It2 moreValues, It3 evenMoreValues) const
{ (*content)(v, begin(), values, moreValues, evenMoreValues); }
*/
template <class Pred>
@ -338,7 +338,7 @@ public :
}
}
/* Iterators */
/* Iterators */
iterator begin(void) { return args; }
const_iterator begin(void) const { return args; }
@ -346,10 +346,10 @@ public :
iterator end(void) { return args + arity(); }
const_iterator end(void) const { return args + arity(); }
subtree& operator[](int i) { return *(begin() + i); }
const subtree& operator[](int i) const { return *(begin() + i); }
subtree& operator[](int i) { return *(begin() + i); }
const subtree& operator[](int i) const { return *(begin() + i); }
/* Some statistics */
/* Some statistics */
size_t size(void) const { return _size; }
@ -370,11 +370,11 @@ public :
subtree* get_parent(void) { return parent; }
const subtree* get_parent(void) const { return parent; }
void clear(void)
{ tree_allocator.deallocate(args, arity()); args = 0; *content = T(); parent = 0; _cumulative_size = 0; _depth = 0; _size = 0; }
void clear(void)
{ tree_allocator.deallocate(args, arity()); args = 0; *content = T(); parent = 0; _cumulative_size = 0; _depth = 0; _size = 0; }
void swap(subtree& y)
{
void swap(subtree& y)
{
do_the_swap(content, y.content);
do_the_swap(args, y.args);
@ -386,12 +386,12 @@ public :
do_the_swap(_cumulative_size, y._cumulative_size);
do_the_swap(_depth, y._depth);
do_the_swap(_size, y._size);
updateAfterInsert();
}
updateAfterInsert();
}
protected :
virtual void updateAfterInsert(void)
virtual void updateAfterInsert(void)
{
_depth = 0;
_size = 1;
@ -419,7 +419,7 @@ private :
// else
for (int i = arity() - 1; i >= 0; --i)
{
{
if (which < args[i]._cumulative_size)
return args[i].imp_select_cumulative(which);
which -= args[i]._cumulative_size;
@ -434,7 +434,7 @@ private :
return *this;
for (int i = arity() - 1; i >= 0; --i)
{
{
unsigned c_size = args[i].size();
if (which < c_size)
return args[i].imp_get_node(which);
@ -454,34 +454,34 @@ private :
}
subtree& copy(const subtree& s)
{
int old_arity = arity();
int old_arity = arity();
int new_arity = s.arity();
int new_arity = s.arity();
if (new_arity != old_arity)
{
tree_allocator.deallocate(args, old_arity);
if (new_arity != old_arity)
{
tree_allocator.deallocate(args, old_arity);
args = tree_allocator.allocate(new_arity);
}
}
switch(new_arity)
{
case 3 : args[2].copy(s.args[2]); args[2].parent = this; // no break!
case 2 : args[1].copy(s.args[1]); args[1].parent = this;
case 1 : args[0].copy(s.args[0]); args[0].parent = this;
case 0 : break;
default :
{
for (int i = 0; i < new_arity; ++i)
{
args[i].copy(s.args[i]);
switch(new_arity)
{
case 3 : args[2].copy(s.args[2]); args[2].parent = this; // no break!
case 2 : args[1].copy(s.args[1]); args[1].parent = this;
case 1 : args[0].copy(s.args[0]); args[0].parent = this;
case 0 : break;
default :
{
for (int i = 0; i < new_arity; ++i)
{
args[i].copy(s.args[i]);
args[i].parent = this;
}
}
}
}
}
}
*content = *s.content;
*content = *s.content;
_size = s._size;
_depth = s._depth;
_cumulative_size = s._cumulative_size;
@ -493,66 +493,66 @@ private :
{
int oldArity = arity();
if (content != &t)
if (content != &t)
*content = t;
else
oldArity = -1;
else
oldArity = -1;
int ar = arity();
int ar = arity();
if (ar != oldArity)
{
if (ar != oldArity)
{
if (oldArity != -1)
tree_allocator.deallocate(args, oldArity);
tree_allocator.deallocate(args, oldArity);
args = tree_allocator.allocate(ar);
//if (ar > 0)
// args = new subtree [ar];
//else
// args = 0;
}
// args = new subtree [ar];
//else
// args = 0;
}
adopt();
updateAfterInsert();
return *this;
return *this;
}
void disown(void)
{
switch(arity())
{
case 3 : args[2].parent = 0; // no break!
case 2 : args[1].parent = 0;
case 1 : args[0].parent = 0; break;
case 0 : break;
default :
{
for (iterator it = begin(); it != end(); ++it)
{
it->parent = 0;
}
}
}
void disown(void)
{
switch(arity())
{
case 3 : args[2].parent = 0; // no break!
case 2 : args[1].parent = 0;
case 1 : args[0].parent = 0; break;
case 0 : break;
default :
{
for (iterator it = begin(); it != end(); ++it)
{
it->parent = 0;
}
}
}
}
}
void adopt(void)
{
switch(arity())
{
case 3 : args[2].parent = this; // no break!
case 2 : args[1].parent = this;
case 1 : args[0].parent = this; break;
case 0 : break;
default :
{
for (iterator it = begin(); it != end(); ++it)
{
it->parent = this;
}
}
}
switch(arity())
{
case 3 : args[2].parent = this; // no break!
case 2 : args[1].parent = this;
case 1 : args[0].parent = this; break;
case 0 : break;
default :
{
for (iterator it = begin(); it != end(); ++it)
{
it->parent = this;
}
}
}
}
template <class It>
@ -581,7 +581,7 @@ private :
subtree* args;
subtree* parent;
size_t _cumulative_size;
size_t _cumulative_size;
size_t _depth;
size_t _size;
};
@ -590,7 +590,7 @@ private :
typedef T value_type;
/* Constructors and Assignments */
/* Constructors and Assignments */
parse_tree(void) : _root(), pushed() {}
parse_tree(const parse_tree& org) : _root(org._root), pushed(org.pushed) { }
@ -602,33 +602,33 @@ private :
virtual ~parse_tree(void) {}
parse_tree& operator=(const parse_tree& org) { return copy(org); }
parse_tree& operator=(const subtree& sub)
{ return copy(sub); }
parse_tree& operator=(const subtree& sub)
{ return copy(sub); }
/* Equality and inequality */
/* Equality and inequality */
bool operator==(const parse_tree& other) const
{ return _root == other._root; }
bool operator==(const parse_tree& other) const
{ return _root == other._root; }
bool operator !=(const parse_tree& other) const
{ return !operator==(other); }
bool operator !=(const parse_tree& other) const
{ return !operator==(other); }
/* Simple tree statistics */
/* Simple tree statistics */
size_t size(void) const { return _root.size(); }
size_t depth(void) const { return _root.depth(); }
size_t depth(void) const { return _root.depth(); }
void clear(void) { _root.clear(); pushed.resize(0); }
/* Evaluation (application), with an increasing number of user defined arguments */
/* Evaluation (application), with an increasing number of user defined arguments */
template <class RetVal>
void apply(RetVal& v) const
{ _root.apply(v); }
template <class RetVal>
void apply(RetVal& v) const
{ _root.apply(v); }
template <class RetVal, class It>
void apply(RetVal& v, It varValues) const
{ _root.apply(v, varValues); }
template <class RetVal, class It>
void apply(RetVal& v, It varValues) const
{ _root.apply(v, varValues); }
template <class RetVal, class It>
void apply_mem_func(RetVal& v, It misc, void (T::* f)(RetVal&, typename subtree::iterator, It))
@ -636,13 +636,13 @@ private :
_root.apply_mem_func(v, misc, f);
}
//template <class RetVal, class It, class It2>
// void apply(RetVal& v, It varValues, It2 moreValues) const
// { _root.apply(v, varValues, moreValues); }
//template <class RetVal, class It, class It2>
// void apply(RetVal& v, It varValues, It2 moreValues) const
// { _root.apply(v, varValues, moreValues); }
//template <class RetVal, class It, class It2, class It3>
// void apply(RetVal& v, It varValues, It2 moreValues, It3 evenMoreValues) const
// { _root.apply(v, varValues, moreValues, evenMoreValues); }
//template <class RetVal, class It, class It2, class It3>
// void apply(RetVal& v, It varValues, It2 moreValues, It3 evenMoreValues) const
// { _root.apply(v, varValues, moreValues, evenMoreValues); }
template <class Pred>
void find_nodes(std::vector<subtree*>& result, Pred& p)
@ -656,14 +656,14 @@ private :
_root.find_nodes(p);
}
/* Customized Swap */
void swap(parse_tree<T>& other)
{
do_the_swap(pushed, other.pushed);
_root.swap(other._root);
}
/* Customized Swap */
void swap(parse_tree<T>& other)
{
do_the_swap(pushed, other.pushed);
_root.swap(other._root);
}
/* Definitions of the iterators */
/* Definitions of the iterators */
class base_iterator
{
@ -680,19 +680,19 @@ private :
bool operator!=(const base_iterator& org) const
{ return !operator==(org); }
base_iterator operator+(size_t n) const
{
base_iterator tmp = *this;
base_iterator operator+(size_t n) const
{
base_iterator tmp = *this;
for(;n != 0; --n)
{
++tmp;
}
for(;n != 0; --n)
{
++tmp;
}
return tmp;
}
return tmp;
}
base_iterator& operator++(void)
base_iterator& operator++(void)
{
subtree* parent = node->get_parent();
@ -702,7 +702,7 @@ private :
return *this;
}
// else
typename subtree::iterator it;
typename subtree::iterator it;
for (it = parent->begin(); it != parent->end(); ++it)
{
if (node == &(*it))
@ -801,7 +801,7 @@ private :
return *this;
}
// else
typename subtree::const_iterator it;
typename subtree::const_iterator it;
for (it = parent->begin(); it != parent->end(); ++it)
{
@ -856,35 +856,35 @@ private :
using base_const_iterator::node;
typedef std::forward_iterator_tag iterator_category;
typedef const T value_type;
typedef size_t distance_type;
typedef size_t difference_type;
typedef const T* pointer;
typedef const T& reference;
typedef std::forward_iterator_tag iterator_category;
typedef const T value_type;
typedef size_t distance_type;
typedef size_t difference_type;
typedef const T* pointer;
typedef const T& reference;
embedded_const_iterator() : base_const_iterator() {}
embedded_const_iterator(const subtree* n): base_const_iterator(n) {}
embedded_const_iterator& operator=(const embedded_const_iterator& org)
{ base_const_iterator::operator=(org); return *this; }
embedded_const_iterator operator+(size_t n) const
{
embedded_const_iterator tmp = *this;
embedded_const_iterator operator+(size_t n) const
{
embedded_const_iterator tmp = *this;
for(;n != 0; --n)
{
++tmp;
}
for(;n != 0; --n)
{
++tmp;
}
return tmp;
}
return tmp;
}
const T& operator*(void) const { return **node; }
const T* operator->(void) const { return node->operator->(); }
};
/* Iterator access */
/* Iterator access */
iterator begin(void) { return iterator(&operator[](0)); }
const_iterator begin(void) const { return const_iterator(&operator[](0)); }
@ -899,32 +899,32 @@ private :
bool empty(void) const { return size() == 0; }
bool valid(void) const { return pushed.empty(); }
/* push_back */
/* push_back */
void push_back(const parse_tree<T>& tree)
{
if (!empty())
pushed.push_back(_root);
void push_back(const parse_tree<T>& tree)
{
if (!empty())
pushed.push_back(_root);
_root = tree.back();
}
_root = tree.back();
}
void push_back(const T& t)
{
if (!empty())
pushed.push_back(_root);
_root = t;
_root = t;
for (typename subtree::iterator it = _root.begin(); it != _root.end(); it++)
{
*it = pushed.back();
*it = pushed.back();
pushed.pop_back();
}
}
/* Access to subtrees */
/* Access to subtrees */
subtree& back(void) { return _root; }
const subtree& back(void) const { return _root; }
@ -954,7 +954,7 @@ private :
return *this;
}
parse_tree& copy(const subtree& sub)
parse_tree& copy(const subtree& sub)
{ _root = sub; pushed.resize(0); return *this; }
subtree _root;
@ -970,25 +970,25 @@ namespace std
template <class T> inline
std::forward_iterator_tag iterator_category(typename gp_parse_tree::parse_tree<T>::embedded_iterator)
{
return std::forward_iterator_tag();
return std::forward_iterator_tag();
}
template <class T> inline
ptrdiff_t* distance_type(typename gp_parse_tree::parse_tree<T>::embedded_iterator)
{
return 0;
return 0;
}
template <class T> inline
std::forward_iterator_tag iterator_category(typename gp_parse_tree::parse_tree<T>::iterator)
{
return std::forward_iterator_tag();
return std::forward_iterator_tag();
}
template <class T> inline
ptrdiff_t* distance_type(typename gp_parse_tree::parse_tree<T>::iterator)
{
return 0;
return 0;
}
/* Put customized swaps also in std...