# TreeBuilder.cpp

    00001 /*
    00002  *             Copyright (C) 2005 Maarten Keijzer
    00003  *
    00004  *          This program is free software; you can redistribute it and/or modify
    00005  *          it under the terms of version 2 of the GNU General Public License as
    00006  *          published by the Free Software Foundation.
    00007  *
    00008  *          This program is distributed in the hope that it will be useful,
    00009  *          but WITHOUT ANY WARRANTY; without even the implied warranty of
    00010  *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00011  *          GNU General Public License for more details.
    00012  *
    00013  *          You should have received a copy of the GNU General Public License
    00014  *          along with this program; if not, write to the Free Software
    00015  *          Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    00016  */
    00017
    00018 #include <utils/eoRNG.h>
    00019 #include "TreeBuilder.h"
    00020
    00021 Sym TreeBuilder::make_terminal() const {
    00022     if (rng.flip(vcprob)) {
    00023         return table.get_random_var();
    00024     }
    00025
    00026     return table.get_random_const();
    00027 }
    00028
    00029 Sym TreeBuilder::build_tree(unsigned max_depth,  bool grow) const {
    00030     if (max_depth == 0 || grow && rng.random(2) == 0) {
    00031         return make_terminal();
    00032     }
    00033
    00034     // pick a random function, no matter what arity
    00035
    00036     functor_t func = table.get_random_function();
    00037
    00038     SymVec args(func.arity);
    00039
    00040     for (unsigned i = 0; i < args.size(); ++i) {
    00041         args[i] = build_tree(max_depth-1, grow);
    00042     }
    00043
    00044     return Sym(func.token, args);
    00045 }
    00046