Changed the directory structure for gp
now: eoParseTree <-- the eoParseTree class
eoParseTreeDepthInit <-- the eoParseTree depth initializer (eoGpDepthInitializer)
eoParseTreeOp <-- the operators (xover and mutation)
base documentation written for:
* eoParseTree
* eoGpDepthInitializer
* eoSubtreeXOver
* eoBranchMutation
* eoPointMutation
* eoExpansionMutation
* eoCollapseSubtreeMutation
* eoHoistMutation
I also created a group ParseTree which contains all classes related to eoParseTree
eoGpMutate.h has been removed (merged with eoParseTree operators into eoParseTreeOp
This commit is contained in:
parent
402e34c238
commit
a611939e68
7 changed files with 340 additions and 198 deletions
|
|
@ -43,7 +43,7 @@ using namespace std;
|
||||||
class MinimizingFitnessTraits : public eoParetoFitnessTraits
|
class MinimizingFitnessTraits : public eoParetoFitnessTraits
|
||||||
{
|
{
|
||||||
public :
|
public :
|
||||||
static bool maximizing(int which) { return false;} // we want to minimize both fitnesses}
|
static bool maximizing(int which) { return false;} // we want to minimize both fitnesses
|
||||||
static unsigned nObjectives() { return 2;} // the number of fitnesses }
|
static unsigned nObjectives() { return 2;} // the number of fitnesses }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -148,7 +148,7 @@ void init(vector<TreeNode> &initSequence)
|
||||||
}
|
}
|
||||||
|
|
||||||
// next we add the unary functions
|
// next we add the unary functions
|
||||||
/*
|
|
||||||
initSequence.push_back( NEGATE );
|
initSequence.push_back( NEGATE );
|
||||||
initSequence.push_back( SIN );
|
initSequence.push_back( SIN );
|
||||||
initSequence.push_back( COS );
|
initSequence.push_back( COS );
|
||||||
|
|
@ -162,14 +162,14 @@ void init(vector<TreeNode> &initSequence)
|
||||||
initSequence.push_back( MULTIPLIES );
|
initSequence.push_back( MULTIPLIES );
|
||||||
initSequence.push_back( DIVIDE );
|
initSequence.push_back( DIVIDE );
|
||||||
initSequence.push_back( POW );
|
initSequence.push_back( POW );
|
||||||
*/
|
|
||||||
// and the binary operators
|
// and the binary operators
|
||||||
initSequence.push_back( OpPLUS);
|
initSequence.push_back( OpPLUS);
|
||||||
initSequence.push_back( OpMINUS );
|
initSequence.push_back( OpMINUS );
|
||||||
/*
|
|
||||||
initSequence.push_back( OpMULTIPLIES );
|
initSequence.push_back( OpMULTIPLIES );
|
||||||
initSequence.push_back( OpDIVIDE );
|
initSequence.push_back( OpDIVIDE );
|
||||||
*/
|
|
||||||
initSequence.push_back( OpPOW );
|
initSequence.push_back( OpPOW );
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,6 @@
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <gp/eoParseTree.h>
|
#include <gp/eoParseTree.h>
|
||||||
#include <gp/eoGpMutate.h>
|
|
||||||
#include <eo>
|
#include <eo>
|
||||||
|
|
||||||
using namespace gp_parse_tree;
|
using namespace gp_parse_tree;
|
||||||
|
|
@ -84,7 +83,9 @@ int main(int argc, char *argv[])
|
||||||
|
|
||||||
|
|
||||||
// define X-OVER
|
// define X-OVER
|
||||||
eoSubtreeXOver<FitnessType, Node> xover(parameter.MaxSize);
|
|
||||||
|
eoSubtreeXOver<FitnessType, Node> xover(parameter.MaxSize);
|
||||||
|
|
||||||
// define MUTATION
|
// define MUTATION
|
||||||
eoBranchMutation<FitnessType, Node> mutation(initializer, parameter.MaxSize);
|
eoBranchMutation<FitnessType, Node> mutation(initializer, parameter.MaxSize);
|
||||||
// eoExpansionMutation<FitnessType, Node> mutation(initializer, parameter.MaxSize);
|
// eoExpansionMutation<FitnessType, Node> mutation(initializer, parameter.MaxSize);
|
||||||
|
|
|
||||||
|
|
@ -214,7 +214,7 @@ RECURSIVE = YES
|
||||||
# excluded from the INPUT source files. This way you can easily exclude a
|
# excluded from the INPUT source files. This way you can easily exclude a
|
||||||
# subdirectory from a directory tree whose root is specified with the INPUT tag.
|
# subdirectory from a directory tree whose root is specified with the INPUT tag.
|
||||||
|
|
||||||
EXCLUDE = ../src/obsolete ../src/gp
|
EXCLUDE = ../src/obsolete
|
||||||
|
|
||||||
# If the value of the INPUT tag contains directories, you can use the
|
# If the value of the INPUT tag contains directories, you can use the
|
||||||
# EXCLUDE_PATTERNS tag to specify one or more wildcard patterns to exclude
|
# EXCLUDE_PATTERNS tag to specify one or more wildcard patterns to exclude
|
||||||
|
|
|
||||||
|
|
@ -5,4 +5,4 @@
|
||||||
###############################################################################
|
###############################################################################
|
||||||
|
|
||||||
libeoincdir = $(includedir)/eo/gp
|
libeoincdir = $(includedir)/eo/gp
|
||||||
libeoinc_HEADERS = eoParseTree.h node_pool.h parse_tree.h eoGpMutate.h
|
libeoinc_HEADERS = eoParseTree.h node_pool.h parse_tree.h eoParseTreeDepthInit.h eoParseTreeOp.h
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,31 @@
|
||||||
#ifndef EO_PARSE_TREE_H
|
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||||
#define EO_PARSE_TREE_H
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// eoParseTree.h : eoParseTree class (for Tree-based Genetic Programming)
|
||||||
|
// (c) Maarten Keijzer 2000
|
||||||
|
/*
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
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
|
||||||
|
|
||||||
|
*/
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#ifndef eoParseTree_h
|
||||||
|
#define eoParseTree_h
|
||||||
|
|
||||||
#include <list>
|
#include <list>
|
||||||
|
|
||||||
|
|
@ -11,6 +37,18 @@
|
||||||
using namespace gp_parse_tree;
|
using namespace gp_parse_tree;
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
/**
|
||||||
|
\defgroup ParseTree
|
||||||
|
|
||||||
|
Various functions for a tree-based Genetic Programming
|
||||||
|
*/
|
||||||
|
|
||||||
|
/** eoParseTree : implementation of parse-tree for genetic programming
|
||||||
|
\class eoParseTree eoParseTree.h gp/eoParseTree.h
|
||||||
|
\ingroup ParseTree
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
template <class FType, class Node>
|
template <class FType, class Node>
|
||||||
class eoParseTree : public EO<FType>, public parse_tree<Node>
|
class eoParseTree : public EO<FType>, public parse_tree<Node>
|
||||||
{
|
{
|
||||||
|
|
@ -18,9 +56,20 @@ public :
|
||||||
|
|
||||||
typedef parse_tree<Node>::subtree Subtree;
|
typedef parse_tree<Node>::subtree Subtree;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default Constructor
|
||||||
|
*/
|
||||||
eoParseTree(void) {}
|
eoParseTree(void) {}
|
||||||
|
/**
|
||||||
|
* Copy Constructor
|
||||||
|
* @param tree The tree to copy
|
||||||
|
*/
|
||||||
eoParseTree(const parse_tree<Node>& tree) : parse_tree<Node>(tree) {}
|
eoParseTree(const parse_tree<Node>& tree) : parse_tree<Node>(tree) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* To prune me to a certain size
|
||||||
|
* @param _size My maximum size
|
||||||
|
*/
|
||||||
virtual void pruneTree(unsigned _size)
|
virtual void pruneTree(unsigned _size)
|
||||||
{
|
{
|
||||||
if (_size < 1)
|
if (_size < 1)
|
||||||
|
|
@ -32,20 +81,34 @@ public :
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* To read me from a stream
|
||||||
|
* @param is The istream
|
||||||
|
*/
|
||||||
|
|
||||||
eoParseTree(std::istream& is) : EO<FType>(), parse_tree<Node>()
|
eoParseTree(std::istream& is) : EO<FType>(), parse_tree<Node>()
|
||||||
{
|
{
|
||||||
readFrom(is);
|
readFrom(is);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// My class name
|
||||||
string className(void) const { return "eoParseTree"; }
|
string className(void) const { return "eoParseTree"; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* To print me on a stream
|
||||||
|
* @param os The ostream
|
||||||
|
*/
|
||||||
void printOn(std::ostream& os) const
|
void printOn(std::ostream& os) const
|
||||||
{
|
{
|
||||||
os << fitness() << ' ';
|
os << fitness() << ' ';
|
||||||
|
|
||||||
std::copy(ebegin(), eend(), ostream_iterator<Node>(os));
|
std::copy(ebegin(), eend(), ostream_iterator<Node>(os));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* To read me from a stream
|
||||||
|
* @param is The istream
|
||||||
|
*/
|
||||||
void readFrom(std::istream& is)
|
void readFrom(std::istream& is)
|
||||||
{
|
{
|
||||||
FType fit;
|
FType fit;
|
||||||
|
|
@ -58,6 +121,7 @@ public :
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// friend function to print eoParseTree
|
||||||
template <class FType, class Node>
|
template <class FType, class Node>
|
||||||
std::ostream& operator<<(std::ostream& os, const eoParseTree<FType, Node>& eot)
|
std::ostream& operator<<(std::ostream& os, const eoParseTree<FType, Node>& eot)
|
||||||
{
|
{
|
||||||
|
|
@ -65,6 +129,7 @@ std::ostream& operator<<(std::ostream& os, const eoParseTree<FType, Node>& eot)
|
||||||
return os;
|
return os;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// friend function to read eoParseTree
|
||||||
template <class FType, class Node>
|
template <class FType, class Node>
|
||||||
std::istream& operator>>(std::istream& is, eoParseTree<FType, Node>& eot)
|
std::istream& operator>>(std::istream& is, eoParseTree<FType, Node>& eot)
|
||||||
{
|
{
|
||||||
|
|
@ -72,167 +137,8 @@ std::istream& operator>>(std::istream& is, eoParseTree<FType, Node>& eot)
|
||||||
return is;
|
return is;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class Node>
|
// for backward compatibility
|
||||||
bool lt_arity(const Node &node1, const Node &node2)
|
#include <gp/eoParseTreeOp.h>
|
||||||
{
|
#include <gp/eoParseTreeDepthInit.h>
|
||||||
return (node1.arity() < node2.arity());
|
|
||||||
}
|
|
||||||
|
|
||||||
template <class FType, class Node>
|
|
||||||
class eoGpDepthInitializer : public eoInit< eoParseTree<FType, Node> >
|
|
||||||
{
|
|
||||||
public :
|
|
||||||
|
|
||||||
typedef eoParseTree<FType, Node> EoType;
|
|
||||||
|
|
||||||
eoGpDepthInitializer(
|
|
||||||
unsigned _max_depth,
|
|
||||||
const vector<Node>& _initializor,
|
|
||||||
bool _grow = true)
|
|
||||||
:
|
|
||||||
eoInit<EoType>(),
|
|
||||||
max_depth(_max_depth),
|
|
||||||
initializor(_initializor),
|
|
||||||
grow(_grow)
|
|
||||||
{
|
|
||||||
if(initializor.empty())
|
|
||||||
{
|
|
||||||
throw logic_error("eoGpDepthInitializer: uhm, wouldn't you rather give a non-empty set of Nodes?");
|
|
||||||
}
|
|
||||||
// lets sort the initializor vector according to arity (so we can be sure the terminals are in front)
|
|
||||||
// we use stable_sort so that if element i was in front of element j and they have the same arity i remains in front of j
|
|
||||||
stable_sort(initializor.begin(), initializor.end(), lt_arity<Node>);
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual string className() const { return "eoDepthInitializer"; };
|
|
||||||
|
|
||||||
void operator()(EoType& _tree)
|
|
||||||
{
|
|
||||||
list<Node> sequence;
|
|
||||||
|
|
||||||
generate(sequence, max_depth);
|
|
||||||
|
|
||||||
parse_tree<Node> tmp(sequence.begin(), sequence.end());
|
|
||||||
_tree.swap(tmp);
|
|
||||||
}
|
|
||||||
|
|
||||||
void generate(list<Node>& sequence, int the_max, int last_terminal = -1)
|
|
||||||
{
|
|
||||||
if (last_terminal == -1)
|
|
||||||
{ // check where the last terminal in the sequence resides
|
|
||||||
vector<Node>::iterator it;
|
|
||||||
for (it = initializor.begin(); it != initializor.end(); ++it)
|
|
||||||
{
|
|
||||||
if (it->arity() > 0)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
last_terminal = it - initializor.begin();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (the_max == 1)
|
|
||||||
{ // generate terminals only
|
|
||||||
vector<Node>::iterator it = initializor.begin() + rng.random(last_terminal);
|
|
||||||
sequence.push_front(*it);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
for (int i = 0; i < what_it->arity(); ++i)
|
|
||||||
generate(sequence, the_max - 1, last_terminal);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private :
|
|
||||||
|
|
||||||
unsigned max_depth;
|
|
||||||
std::vector<Node> initializor;
|
|
||||||
bool grow;
|
|
||||||
};
|
|
||||||
|
|
||||||
template<class FType, class Node>
|
|
||||||
class eoSubtreeXOver: public eoQuadOp< eoParseTree<FType, Node> > {
|
|
||||||
public:
|
|
||||||
|
|
||||||
typedef eoParseTree<FType, Node> EoType;
|
|
||||||
|
|
||||||
eoSubtreeXOver( unsigned _max_length)
|
|
||||||
: eoQuadOp<EoType>(), max_length(_max_length) {};
|
|
||||||
|
|
||||||
virtual string className() const { return "eoSubtreeXOver"; };
|
|
||||||
|
|
||||||
/// Dtor
|
|
||||||
virtual ~eoSubtreeXOver () {};
|
|
||||||
|
|
||||||
bool operator()(EoType & _eo1, EoType & _eo2 )
|
|
||||||
{
|
|
||||||
int i = rng.random(_eo1.size());
|
|
||||||
int j = rng.random(_eo2.size());
|
|
||||||
|
|
||||||
parse_tree<Node>::subtree tmp = _eo1[i];
|
|
||||||
_eo1[i] = _eo2[j]; // insert subtree
|
|
||||||
_eo2[j] = tmp;
|
|
||||||
|
|
||||||
_eo1.pruneTree(max_length);
|
|
||||||
_eo2.pruneTree(max_length);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned max_length;
|
|
||||||
};
|
|
||||||
|
|
||||||
template<class FType, class Node>
|
|
||||||
class eoBranchMutation: public eoMonOp< eoParseTree<FType, Node> >
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
|
|
||||||
typedef eoParseTree<FType, Node> EoType;
|
|
||||||
|
|
||||||
eoBranchMutation(eoInit<EoType>& _init, unsigned _max_length)
|
|
||||||
: eoMonOp<EoType>(), max_length(_max_length), initializer(_init)
|
|
||||||
{};
|
|
||||||
|
|
||||||
virtual string className() const { return "eoBranchMutation"; };
|
|
||||||
|
|
||||||
/// Dtor
|
|
||||||
virtual ~eoBranchMutation() {};
|
|
||||||
|
|
||||||
bool operator()(EoType& _eo1 )
|
|
||||||
{
|
|
||||||
int i = rng.random(_eo1.size());
|
|
||||||
|
|
||||||
EoType eo2;
|
|
||||||
initializer(eo2);
|
|
||||||
|
|
||||||
int j = rng.random(eo2.size());
|
|
||||||
|
|
||||||
_eo1[i] = eo2[j]; // insert subtree
|
|
||||||
|
|
||||||
_eo1.pruneTree(max_length);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
private :
|
|
||||||
|
|
||||||
unsigned max_length;
|
|
||||||
eoInit<EoType>& initializer;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
145
eo/src/gp/eoParseTreeDepthInit.h
Normal file
145
eo/src/gp/eoParseTreeDepthInit.h
Normal file
|
|
@ -0,0 +1,145 @@
|
||||||
|
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// eoParseTreeDepthInit.h : initializor for eoParseTree class
|
||||||
|
// (c) Maarten Keijzer 2000
|
||||||
|
/*
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
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
|
||||||
|
|
||||||
|
*/
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#ifndef eoParseTreeDepthInit_h
|
||||||
|
#define eoParseTreeDepthInit_h
|
||||||
|
|
||||||
|
#include <EO.h>
|
||||||
|
#include <gp/eoParseTree.h>
|
||||||
|
#include <eoInit.h>
|
||||||
|
#include <eoOp.h>
|
||||||
|
|
||||||
|
using namespace gp_parse_tree;
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
// we need this for sorting the initializor vector
|
||||||
|
template <class Node>
|
||||||
|
bool lt_arity(const Node &node1, const Node &node2)
|
||||||
|
{
|
||||||
|
return (node1.arity() < node2.arity());
|
||||||
|
}
|
||||||
|
|
||||||
|
/** eoGpDepthInitializer : the initializer class for eoParseTree
|
||||||
|
\class eoGpDepthInitializer eoParseTreeDepthInit.h gp/eoParseTreeDepthInit.h
|
||||||
|
\ingroup ParseTree
|
||||||
|
*/
|
||||||
|
|
||||||
|
template <class FType, class Node>
|
||||||
|
class eoGpDepthInitializer : public eoInit< eoParseTree<FType, Node> >
|
||||||
|
{
|
||||||
|
public :
|
||||||
|
|
||||||
|
typedef eoParseTree<FType, Node> EoType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
|
* @parm _max_depth The maximum depth of a tree
|
||||||
|
* @param _initializor A vector containing the possible nodes
|
||||||
|
* @param _grow False results in a full tree, True result is a randomly grown tree
|
||||||
|
*/
|
||||||
|
eoGpDepthInitializer(
|
||||||
|
unsigned _max_depth,
|
||||||
|
const vector<Node>& _initializor,
|
||||||
|
bool _grow = true)
|
||||||
|
:
|
||||||
|
eoInit<EoType>(),
|
||||||
|
max_depth(_max_depth),
|
||||||
|
initializor(_initializor),
|
||||||
|
grow(_grow)
|
||||||
|
{
|
||||||
|
if(initializor.empty())
|
||||||
|
{
|
||||||
|
throw logic_error("eoGpDepthInitializer: uhm, wouldn't you rather give a non-empty set of Nodes?");
|
||||||
|
}
|
||||||
|
// lets sort the initializor vector according to arity (so we can be sure the terminals are in front)
|
||||||
|
// we use stable_sort so that if element i was in front of element j and they have the same arity i remains in front of j
|
||||||
|
stable_sort(initializor.begin(), initializor.end(), lt_arity<Node>);
|
||||||
|
}
|
||||||
|
/// My class name
|
||||||
|
virtual string className() const { return "eoDepthInitializer"; };
|
||||||
|
|
||||||
|
/**initialize a tree
|
||||||
|
* @param _tree : the tree to be initialized
|
||||||
|
*/
|
||||||
|
void operator()(EoType& _tree)
|
||||||
|
{
|
||||||
|
list<Node> sequence;
|
||||||
|
|
||||||
|
generate(sequence, max_depth);
|
||||||
|
|
||||||
|
parse_tree<Node> tmp(sequence.begin(), sequence.end());
|
||||||
|
_tree.swap(tmp);
|
||||||
|
}
|
||||||
|
private :
|
||||||
|
void generate(list<Node>& sequence, int the_max, int last_terminal = -1)
|
||||||
|
{
|
||||||
|
if (last_terminal == -1)
|
||||||
|
{ // check where the last terminal in the sequence resides
|
||||||
|
vector<Node>::iterator it;
|
||||||
|
for (it = initializor.begin(); it != initializor.end(); ++it)
|
||||||
|
{
|
||||||
|
if (it->arity() > 0)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
last_terminal = it - initializor.begin();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (the_max == 1)
|
||||||
|
{ // generate terminals only
|
||||||
|
vector<Node>::iterator it = initializor.begin() + rng.random(last_terminal);
|
||||||
|
sequence.push_front(*it);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
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;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
@ -1,8 +1,9 @@
|
||||||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
// eoGpMutate.h : GP mutation
|
// eoParseTreeOp.h : crossover and mutation operator for the eoParseTree class
|
||||||
// (c) Jeroen Eggermont 2001 for mutation operators
|
// (c) Maarten Keijzer 2000 for eoSubtreeXOver, eoBranchMutation
|
||||||
|
// (c) Jeroen Eggermont 2001 for other mutation operators
|
||||||
|
|
||||||
/*
|
/*
|
||||||
This library is free software; you can redistribute it and/or
|
This library is free software; you can redistribute it and/or
|
||||||
|
|
@ -20,27 +21,114 @@
|
||||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||||
|
|
||||||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||||
|
mak@dhi.dk
|
||||||
jeggermo@liacs.nl
|
jeggermo@liacs.nl
|
||||||
*/
|
*/
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
#ifndef eoGpMutate_h
|
#ifndef eoParseTreeOp_h
|
||||||
#define eoGpMutate_h
|
#define eoParseTreeOp_h
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
#include <gp/eoParseTree.h>
|
|
||||||
#include <list>
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
|
|
||||||
#include <EO.h>
|
#include <EO.h>
|
||||||
#include <eoOp.h>
|
#include <eoOp.h>
|
||||||
#include <gp/eoParseTree.h>
|
|
||||||
#include <eoInit.h>
|
|
||||||
|
|
||||||
using namespace gp_parse_tree;
|
#include <gp/eoParseTree.h>
|
||||||
using namespace std;
|
|
||||||
|
/** eoSubtreeXOver --> subtree xover
|
||||||
|
\class eoSubtreeXOver eoParseTreeOp.h gp/eoParseTreeOp.h
|
||||||
|
\ingroup ParseTree
|
||||||
|
*/
|
||||||
|
template<class FType, class Node>
|
||||||
|
class eoSubtreeXOver: public eoQuadOp< eoParseTree<FType, Node> > {
|
||||||
|
public:
|
||||||
|
|
||||||
|
typedef eoParseTree<FType,Node> EoType;
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
|
* @param _max_length the maximum size of an individual
|
||||||
|
*/
|
||||||
|
eoSubtreeXOver( unsigned _max_length)
|
||||||
|
: eoQuadOp<EoType>(), max_length(_max_length) {};
|
||||||
|
|
||||||
|
/// the ckassname
|
||||||
|
virtual string className() const { return "eoSubtreeXOver"; };
|
||||||
|
|
||||||
|
/// Dtor
|
||||||
|
virtual ~eoSubtreeXOver () {};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Perform crossover on two individuals
|
||||||
|
* param _eo1 The first parent individual
|
||||||
|
* param _eo2 The second parent individual
|
||||||
|
*/
|
||||||
|
bool operator()(EoType & _eo1, EoType & _eo2 )
|
||||||
|
{
|
||||||
|
int i = rng.random(_eo1.size());
|
||||||
|
int j = rng.random(_eo2.size());
|
||||||
|
|
||||||
|
parse_tree<Node>::subtree tmp = _eo1[i];
|
||||||
|
_eo1[i] = _eo2[j]; // insert subtree
|
||||||
|
_eo2[j] = tmp;
|
||||||
|
|
||||||
|
_eo1.pruneTree(max_length);
|
||||||
|
_eo2.pruneTree(max_length);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
private:
|
||||||
|
unsigned max_length;
|
||||||
|
};
|
||||||
|
|
||||||
|
/** eoBranchMutation --> replace a subtree with a randomly created subtree
|
||||||
|
\class eoBranchMutation eoParseTreeOp.h gp/eoParseTreeOp.h
|
||||||
|
\ingroup ParseTree
|
||||||
|
*/
|
||||||
|
template<class FType, class Node>
|
||||||
|
class eoBranchMutation: public eoMonOp< eoParseTree<FType, Node> >
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
typedef eoParseTree<FType,Node> EoType;
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
|
* @param _init An instantiation of eoGpDepthInitializer
|
||||||
|
* @param _max_length the maximum size of an individual
|
||||||
|
*/
|
||||||
|
eoBranchMutation(eoInit<EoType>& _init, unsigned _max_length)
|
||||||
|
: eoMonOp<EoType>(), max_length(_max_length), initializer(_init)
|
||||||
|
{};
|
||||||
|
|
||||||
|
/// the class name
|
||||||
|
virtual 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());
|
||||||
|
|
||||||
|
EoType eo2;
|
||||||
|
initializer(eo2);
|
||||||
|
|
||||||
|
int j = rng.random(eo2.size());
|
||||||
|
|
||||||
|
_eo1[i] = eo2[j]; // insert subtree
|
||||||
|
|
||||||
|
_eo1.pruneTree(max_length);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private :
|
||||||
|
|
||||||
|
unsigned max_length;
|
||||||
|
eoInit<EoType>& initializer;
|
||||||
|
};
|
||||||
|
|
||||||
// Additional Mutation operators from
|
// Additional Mutation operators from
|
||||||
// TITLE:"Genetic Programming~An Introduction"
|
// TITLE:"Genetic Programming~An Introduction"
|
||||||
|
|
@ -51,7 +139,8 @@ using namespace std;
|
||||||
// For the eoParseTree class
|
// For the eoParseTree class
|
||||||
|
|
||||||
/** eoPointMutation --> replace a Node with a Node of the same arity
|
/** eoPointMutation --> replace a Node with a Node of the same arity
|
||||||
\class eoPointMutation eoGpMutate.h gp/eoGpMutate.h
|
\class eoPointMutation eoParseTreeOp.h gp/eoParseTreeOp.h
|
||||||
|
\ingroup ParseTree
|
||||||
*/
|
*/
|
||||||
|
|
||||||
template<class FType, class Node>
|
template<class FType, class Node>
|
||||||
|
|
@ -59,7 +148,7 @@ class eoPointMutation: public eoMonOp< eoParseTree<FType, Node> >
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
typedef eoParseTree<FType, Node> EoType;
|
typedef eoParseTree<FType,Node> EoType;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor
|
||||||
|
|
@ -107,7 +196,8 @@ private :
|
||||||
};
|
};
|
||||||
|
|
||||||
/** eoExpansionMutation --> replace a terminal with a randomly created subtree
|
/** eoExpansionMutation --> replace a terminal with a randomly created subtree
|
||||||
\class eoExpansionMutation eoGpMutate.h gp/eoGpMutate.h
|
\class eoExpansionMutation eoParseTreeOp.h gp/eoParseTreeOp.h
|
||||||
|
\ingroup ParseTree
|
||||||
*/
|
*/
|
||||||
|
|
||||||
template<class FType, class Node>
|
template<class FType, class Node>
|
||||||
|
|
@ -174,7 +264,8 @@ private :
|
||||||
};
|
};
|
||||||
|
|
||||||
/** eoCollapseSubtree --> replace a subtree with a randomly chosen terminal
|
/** eoCollapseSubtree --> replace a subtree with a randomly chosen terminal
|
||||||
\class eoCollapseSubtreeMutation eoGpMutate.h gp/eoGpMutate.h
|
\class eoCollapseSubtreeMutation eoParseTreeOp.h gp/eoParseTreeOp.h
|
||||||
|
\ingroup ParseTree
|
||||||
*/
|
*/
|
||||||
|
|
||||||
template<class FType, class Node>
|
template<class FType, class Node>
|
||||||
|
|
@ -182,7 +273,7 @@ class eoCollapseSubtreeMutation: public eoMonOp< eoParseTree<FType, Node> >
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
typedef eoParseTree<FType, Node> EoType;
|
typedef eoParseTree<FType,Node> EoType;
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor
|
||||||
* @param _init An instantiation of eoGpDepthInitializer
|
* @param _init An instantiation of eoGpDepthInitializer
|
||||||
|
|
@ -236,11 +327,9 @@ private :
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/** eoHoistMutation --> replace the individual with one of its subtree's
|
||||||
|
\class eoHoistMutation eoParseTreeOp.h gp/eoParseTreeOp.h
|
||||||
|
\ingroup ParseTree
|
||||||
/** eoHoist --> replace the individual with one of its subtree's
|
|
||||||
\class eoHoist eoGpMutate.h gp/eoGpMutate.h
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
template<class FType, class Node>
|
template<class FType, class Node>
|
||||||
|
|
@ -248,9 +337,10 @@ class eoHoistMutation: public eoMonOp< eoParseTree<FType, Node> >
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
typedef eoParseTree<FType, Node> EoType;
|
typedef eoParseTree<FType,Node> EoType;
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor
|
||||||
|
* @param none
|
||||||
*/
|
*/
|
||||||
eoHoistMutation()
|
eoHoistMutation()
|
||||||
: eoMonOp<EoType>()
|
: eoMonOp<EoType>()
|
||||||
Reference in a new issue