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
00028 #ifndef eoStParseTreeOp_h
00029 #define eoStParseTreeOp_h
00030
00031 #include <EO.h>
00032 #include <eoOp.h>
00033 #include <map.h>
00034 #include <iostream>
00035
00036 #include <gp/eoParseTree.h>
00037
00038
00039 template <class EOT>
00040 void get_possible_nodes(const EOT &_eo, std::vector<int> &possible_nodes, const int type)
00041 {
00042 int n=0;
00043 possible_nodes.clear();
00044
00045 for(n=0; n < _eo.size(); n++)
00046 if (type == _eo[n]->type())
00047 possible_nodes.push_back(n);
00048 }
00049
00050
00055 template<class FType, class Node>
00056 class eoStSubtreeXOver: public eoQuadOp< eoParseTree<FType, Node> > {
00057 public:
00058
00059 typedef eoParseTree<FType,Node> EoType;
00064 eoStSubtreeXOver( unsigned _max_length)
00065 : eoQuadOp<EoType>(), max_length(_max_length) {};
00066
00068 virtual std::string className() const { return "eoStSubtreeXOver"; };
00069
00071 virtual ~eoStSubtreeXOver () {};
00072
00078 bool operator()(EoType & _eo1, EoType & _eo2 )
00079 {
00080 int i = 0;
00081 std::vector<int> nodes;
00082 int n = 0;
00083 int type = 0;
00084 int j = 0;
00085 set<int> test;
00086 do
00087 {
00088 do
00089 {
00090 i = rng.random(_eo1.size());
00091 }while(test.count(i) > 0);
00092
00093 test.insert(i);
00094
00095 type = _eo1[i]->type();
00096
00097 get_possible_nodes<EoType>(_eo2, nodes, type);
00098
00099 }while(nodes.empty() && (test.size() < _eo1.size()));
00100
00101 if (nodes.empty())
00102 return true;
00103
00104
00105
00106 n = rng.random(nodes.size());
00107 j = nodes[n];
00108
00109 parse_tree<Node>::subtree tmp = _eo1[i];
00110 _eo1[i] = _eo2[j];
00111 _eo2[j] = tmp;
00112
00113
00114
00115
00116
00117
00118
00119 return true;
00120 }
00121 private:
00122 unsigned max_length;
00123 };
00124
00129 template<class FType, class Node>
00130 class eoStBranchMutation: public eoMonOp< eoParseTree<FType, Node> >
00131 {
00132 public:
00133
00134 typedef eoParseTree<FType,Node> EoType;
00140 eoStBranchMutation(eoInit<EoType>& _init, unsigned _max_length)
00141 : eoMonOp<EoType>(), max_length(_max_length), initializer(_init)
00142 {};
00143
00145 virtual std::string className() const { return "eoStBranchMutation"; };
00146
00148 virtual ~eoStBranchMutation() {};
00149
00154 bool operator()(EoType& _eo1 )
00155 {
00156 int i = rng.random(_eo1.size());
00157 std::vector<int> nodes;
00158 int type = _eo1[i]->type();
00159 int j=0;
00160 int n=0;
00161
00162 EoType eo2;
00163
00164 do
00165 {
00166 initializer(eo2);
00167 get_possible_nodes(eo2, nodes, type);
00168 }while (nodes.empty());
00169
00170 n = rng.random(nodes.size());
00171 j = nodes[n];
00172
00173 _eo1[i] = eo2[j];
00174
00175
00176
00177
00178
00179
00180 return true;
00181 }
00182
00183 private :
00184
00185 unsigned max_length;
00186 eoInit<EoType>& initializer;
00187 };
00188
00189
00194 template<class FType, class Node>
00195 class eoStPointMutation: public eoMonOp< eoParseTree<FType, Node> >
00196 {
00197 public:
00198
00199 typedef eoParseTree<FType,Node> EoType;
00200
00205 eoStPointMutation( std::vector<Node>& _node)
00206 : eoMonOp<EoType>()
00207 {
00208 unsigned int i=0;
00209 int arity=0;
00210 int type=0;
00211 std::vector<Node> node_std::vector;
00212 for(i=0; i < _node.size(); i++)
00213 {
00214 arity = _node[i].arity();
00215 type = _node[i].type();
00216
00217 node_std::vector = node[type][arity];
00218 node_std::vector.push_back(_node[i]);
00219 node[type][arity]= node_std::vector;
00220
00221 };
00222 };
00223
00225 virtual std::string className() const { return "eoStPointMutation"; };
00226
00228 virtual ~eoStPointMutation() {};
00229
00234 bool operator()(EoType& _eo1 )
00235 {
00236
00237 int i = rng.random(_eo1.size());
00238 int arity = _eo1[i].arity();
00239 int type = _eo1[i]->type();
00240 int j = rng.random(node[type][arity].size());
00241
00242
00243 _eo1[i] = node[type][arity][j];
00244 return true;
00245 }
00246
00247 private :
00248
00249 map < int, map < int, std::vector<Node> > > node;
00250 };
00251
00252
00257 template<class FType, class Node>
00258 class eoStHoistMutation: public eoMonOp< eoParseTree<FType, Node> >
00259 {
00260 public:
00261
00262 typedef eoParseTree<FType,Node> EoType;
00268 eoStHoistMutation(eoInit<EoType>& _init, unsigned _max_length)
00269 : eoMonOp<EoType>(), max_length(_max_length), initializer(_init)
00270 {};
00271
00273 virtual std::string className() const { return "eoStHoistMutation"; };
00274
00276 virtual ~eoStHoistMutation() {};
00277
00282 bool operator()(EoType& _eo1 )
00283 {
00284
00285 std::vector<int> nodes;
00286
00287 int type = _eo1[ _eo1.size() - 1 ]->type();
00288
00289
00290
00291 do
00292 {
00293 initializer(eo2);
00294 get_possible_nodes(eo2, nodes, type);
00295 }while (nodes.empty());
00296
00297
00298 int n = rng.random(nodes.size());
00299 int i = nodes[n];
00300
00301 EoType eo2(_eo1[i]);
00302
00303 _eo1 = eo2;
00304
00305 return true;
00306 }
00307
00308 private :
00309
00310 unsigned max_length;
00311 eoInit<EoType>& initializer;
00312 };
00313
00314
00315 #endif