00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifndef eoStParseTreeDepthInit_h
00028 #define eoStParseTreeDepthInit_h
00029
00030 #include <EO.h>
00031 #include <gp/eoParseTree.h>
00032 #include <eoInit.h>
00033 #include <eoOp.h>
00034
00035 #include <map>
00036
00037 using namespace gp_parse_tree;
00038
00039 #define TERMINAL 0
00040
00041 #define NONTERMINAL 4
00042 #define ALL 5
00043
00061 template <class FType, class Node>
00062 class eoStParseTreeDepthInit : public eoInit< eoParseTree<FType, Node> >
00063 {
00064 public :
00065
00066 typedef eoParseTree<FType, Node> EoType;
00067
00074 eoStParseTreeDepthInit(
00075 unsigned _max_depth,
00076 const std::vector<Node>& _node,
00077 const int& _return_type,
00078 bool _grow = true)
00079 :
00080 eoInit<EoType>(),
00081 max_depth(_max_depth),
00082 return_type(_return_type),
00083 grow(_grow)
00084 {
00085 if(_node.empty())
00086 {
00087 throw std::logic_error("eoStParseTreeDepthInit: uhm, wouldn't you rather give a non-empty set of Nodes?");
00088 }
00089
00090
00091 unsigned int i=0;
00092 int arity=0;
00093 int type=0;
00094 std::vector<Node> node_vector;
00095 for(i=0; i < _node.size(); i++)
00096 {
00097 arity = _node[i].arity();
00098 type = _node[i].type();
00099 if(arity==0)
00100 {
00101 node_vector = node[type][TERMINAL];
00102 node_vector.push_back(_node[i]);
00103 node[type][TERMINAL]= node_vector;
00104 }
00105 else
00106
00107 {
00108 node_vector = node[type][NONTERMINAL];
00109 node_vector.push_back(_node[i]);
00110 node[type][NONTERMINAL] = node_vector;
00111 }
00112 node_vector = node[type][ALL];
00113 node_vector.push_back(_node[i]);
00114 node[type][ALL] = node_vector;
00115
00116 }
00117
00118
00119 }
00121 virtual std::string className() const { return "eoStParseTreeDepthInit"; };
00122
00126 void operator()(EoType& _tree)
00127 {
00128 std::list<Node> sequence;
00129 bool good_tree = false;
00130 do
00131 {
00132 sequence.clear();
00133 good_tree = generate(sequence, max_depth, return_type);
00134 }while (!good_tree);
00135
00136 parse_tree<Node> tmp(sequence.begin(), sequence.end());
00137 _tree.swap(tmp);
00138 }
00139 private :
00140 bool generate(std::list<Node>& sequence, int the_max, int request_type)
00141 {
00142
00143 int selected=0;
00144 bool ok = true;
00145
00146 if (the_max == 1)
00147 {
00148 if( node[request_type][TERMINAL].empty() )
00149 return false;
00150 else
00151 {
00152 selected = rng.random((node[request_type][TERMINAL]).size());
00153 sequence.push_front(node[request_type][TERMINAL][selected]);
00154 return true;
00155 }
00156
00157 }
00158
00159 int arity=0;
00160 if (grow)
00161 {
00162 selected = rng.random((node[request_type][ALL]).size());
00163 arity = node[request_type][ALL][selected].arity();
00164 sequence.push_front(node[request_type][ALL][selected]);
00165 for (int i = 0; i < arity; ++i)
00166 ok &= generate(sequence, the_max - 1, node[request_type][ALL][selected].type(i));
00167 }
00168 else
00169 {
00170 selected = rng.random((node[request_type][NONTERMINAL]).size());
00171 arity = node[request_type][NONTERMINAL][selected].arity();
00172 sequence.push_front(node[request_type][NONTERMINAL][selected]);
00173 for (int i = 0; i < arity; ++i)
00174 ok &=generate(sequence, the_max - 1, node[request_type][NONTERMINAL][selected].type(i));
00175 }
00176
00177 return ok;
00178
00179 }
00180
00181
00182
00183
00184 unsigned max_depth;
00185 std::map < int, std::map < int, std::vector<Node> > > node;
00186
00187 int return_type;
00188 bool grow;
00189 };
00190
00191 #endif