EvolvingObjects
|
00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- 00002 00003 //----------------------------------------------------------------------------- 00004 // eoParseTree.h : eoParseTree class (for Tree-based Genetic Programming) 00005 // (c) Maarten Keijzer 2000 00006 /* 00007 This library is free software; you can redistribute it and/or 00008 modify it under the terms of the GNU Lesser General Public 00009 License as published by the Free Software Foundation; either 00010 version 2 of the License, or (at your option) any later version. 00011 00012 This library is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 Lesser General Public License for more details. 00016 00017 You should have received a copy of the GNU Lesser General Public 00018 License along with this library; if not, write to the Free Software 00019 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00020 00021 Contact: todos@geneura.ugr.es, http://geneura.ugr.es 00022 mak@dhi.dk 00023 00024 */ 00025 //----------------------------------------------------------------------------- 00026 00027 #ifndef eoParseTree_h 00028 #define eoParseTree_h 00029 00030 #include <iterator> 00031 #include <list> 00032 00033 #include <EO.h> 00034 #include <eoInit.h> 00035 #include <eoOp.h> 00036 #include <gp/parse_tree.h> 00037 00038 using namespace gp_parse_tree; 00039 00057 template <class FType, class Node> 00058 class eoParseTree : public EO<FType>, public parse_tree<Node> 00059 { 00060 public: 00061 00062 using parse_tree<Node>::back; 00063 using parse_tree<Node>::ebegin; 00064 using parse_tree<Node>::eend; 00065 using parse_tree<Node>::size; 00066 00067 00068 typedef typename parse_tree<Node>::subtree Subtree; 00069 00070 /* For Compatibility with the intel C++ compiler for Linux 5.x */ 00071 typedef Node reference; 00072 typedef const reference const_reference; 00073 00077 eoParseTree(void) {} 00078 00083 eoParseTree(const parse_tree<Node>& tree) : parse_tree<Node>(tree) {} 00084 00085 // eoParseTree(const eoParseTree<FType, Node>& tree) : parse_tree<Node>(tree) {} 00090 virtual void pruneTree(unsigned _size) 00091 { 00092 if (_size < 1) 00093 return; 00094 00095 while (size() > _size) 00096 { 00097 back() = this->operator[](size()-2); 00098 } 00099 } 00100 00106 eoParseTree(std::istream& is) : EO<FType>(), parse_tree<Node>() 00107 { 00108 readFrom(is); 00109 } 00110 00112 std::string className(void) const { return "eoParseTree"; } 00113 00118 void printOn(std::ostream& os) const 00119 { 00120 EO<FType>::printOn(os); 00121 os << ' '; 00122 00123 os << size() << ' '; 00124 00125 std::copy(ebegin(), eend(), std::ostream_iterator<Node>(os, " ")); 00126 } 00127 00132 void readFrom(std::istream& is) 00133 { 00134 00135 00136 EO<FType>::readFrom(is); 00137 00138 unsigned sz; 00139 is >> sz; 00140 00141 00142 std::vector<Node> v(sz); 00143 00144 unsigned i; 00145 00146 for (i = 0; i < sz; ++i) 00147 { 00148 Node node; 00149 is >> node; 00150 v[i] = node; 00151 } 00152 parse_tree<Node> tmp(v.begin(), v.end()); 00153 this->swap(tmp); 00154 00155 /* 00156 * old code which caused problems for paradisEO 00157 * 00158 * this can be removed once it has proved itself 00159 EO<FType>::readFrom(is); 00160 00161 // even older code 00162 FType fit; 00163 is >> fit; 00164 00165 fitness(fit); 00166 00167 00168 std::copy(std::istream_iterator<Node>(is), std::istream_iterator<Node>(), back_inserter(*this)); 00169 */ 00170 } 00171 }; 00175 // friend function to print eoParseTree 00176 template <class FType, class Node> 00177 std::ostream& operator<<(std::ostream& os, const eoParseTree<FType, Node>& eot) 00178 { 00179 eot.printOn(os); 00180 return os; 00181 } 00182 00183 // friend function to read eoParseTree 00184 template <class FType, class Node> 00185 std::istream& operator>>(std::istream& is, eoParseTree<FType, Node>& eot) 00186 { 00187 eot.readFrom(is); 00188 return is; 00189 } 00190 00191 // for backward compatibility 00192 #include <gp/eoParseTreeOp.h> 00193 #include <gp/eoParseTreeDepthInit.h> 00194 00195 #endif