# eoParseTree.h

    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
    00053 template <class FType, class Node>
    00054 class eoParseTree : public EO<FType>, public parse_tree<Node>
    00055 {
    00056 public:
    00057
    00058     using parse_tree<Node>::back;
    00059     using parse_tree<Node>::ebegin;
    00060     using parse_tree<Node>::eend;
    00061     using parse_tree<Node>::size;
    00062
    00063
    00064     typedef typename parse_tree<Node>::subtree Subtree;
    00065
    00066     /* For Compatibility with the intel C++ compiler for Linux 5.x */
    00067     typedef Node reference;
    00068     typedef const reference const_reference;
    00069
    00073     eoParseTree(void)  {}
    00074
    00079     eoParseTree(const parse_tree<Node>& tree)  : parse_tree<Node>(tree) {}
    00080
    00081 //    eoParseTree(const eoParseTree<FType, Node>& tree) :  parse_tree<Node>(tree) {}
    00086     virtual void pruneTree(unsigned _size)
    00087     {
    00088         if (_size < 1)
    00089             return;
    00090
    00091         while (size() > _size)
    00092         {
    00093             back() = operator[](size()-2);
    00094         }
    00095     }
    00096
    00102     eoParseTree(std::istream& is) : EO<FType>(), parse_tree<Node>()
    00103     {
    00104         readFrom(is);
    00105     }
    00106
    00108     std::string className(void) const { return "eoParseTree"; }
    00109
    00114     void printOn(std::ostream& os) const
    00115     {
    00116         EO<FType>::printOn(os);
    00117         os << ' ';
    00118
    00119         os << size() << ' ';
    00120
    00121         std::copy(ebegin(), eend(), std::ostream_iterator<Node>(os, " "));
    00122     }
    00123
    00128     void readFrom(std::istream& is)
    00129     {
    00130
    00131
    00132         EO<FType>::readFrom(is);
    00133
    00134         unsigned sz;
    00135         is >> sz;
    00136
    00137
    00138         std::vector<Node> v(sz);
    00139
    00140         unsigned i;
    00141
    00142         for (i = 0; i < sz; ++i)
    00143         {
    00144             Node node;
    00145             is >> node;
    00146             v[i] = node;
    00147         }
    00148         parse_tree<Node> tmp(v.begin(), v.end());
    00149         swap(tmp);
    00150
    00151         /*
    00152          * old code which caused problems for paradisEO
    00153          *
    00154          * this can be removed once it has proved itself
    00155         EO<FType>::readFrom(is);
    00156
    00157         // even older code
    00158         FType fit;
    00159         is >> fit;
    00160
    00161         fitness(fit);
    00162
    00163
    00164         std::copy(std::istream_iterator<Node>(is), std::istream_iterator<Node>(), back_inserter(*this));
    00165         */
    00166     }
    00167 };
    00168
    00169 // friend function to print eoParseTree
    00170 template <class FType, class Node>
    00171 std::ostream& operator<<(std::ostream& os, const eoParseTree<FType, Node>& eot)
    00172 {
    00173     eot.printOn(os);
    00174     return os;
    00175 }
    00176
    00177 // friend function to read eoParseTree
    00178 template <class FType, class Node>
    00179 std::istream& operator>>(std::istream& is, eoParseTree<FType, Node>& eot)
    00180 {
    00181     eot.readFrom(is);
    00182     return is;
    00183 }
    00184
    00185 // for backward compatibility
    00186 #include <gp/eoParseTreeOp.h>
    00187 #include <gp/eoParseTreeDepthInit.h>
    00188
    00189 #endif