# eoSymMutate.h

    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 #ifndef SYMMUTATE_H
    00019 #define SYMMUTATE_H
    00020
    00021 #include <TreeBuilder.h>
    00022 #include <NodeSelector.h>
    00023
    00024 #include <eoSym.h>
    00025 #include <eoOp.h>
    00026
    00027 template <class EoType>
    00028 class eoSymSubtreeMutate : public eoMonOp<EoType> {
    00029
    00030         TreeBuilder& subtree_builder;
    00031         NodeSelector& node_selector;
    00032     public :
    00033
    00034         eoSymSubtreeMutate(TreeBuilder& _subtree_builder, NodeSelector& _node_selector)
    00035         : subtree_builder(_subtree_builder), node_selector(_node_selector) {}
    00036
    00037
    00038         bool operator()(EoType& tomutate) {
    00039             unsigned xover_point = node_selector.select_node(tomutate).idx();
    00040             // create subtree
    00041             Sym newtree = subtree_builder.build_tree(6, true); // TODO, parameterize
    00042             static_cast<Sym&>(tomutate) = insert_subtree(tomutate, xover_point, newtree);
    00043             return true;
    00044         }
    00045
    00046 };
    00047
    00055 extern bool mutate(Sym& sym, double p, const LanguageTable& table);
    00056
    00057 template <class EoType>
    00058 class eoSymNodeMutate : public eoMonOp<EoType> {
    00059
    00060         LanguageTable& table;
    00061         double own_mutation_rate;
    00062         bool   own_is_rate_absolute;
    00063
    00064
    00065     public:
    00066
    00067         double& mutation_rate;
    00068         bool& is_rate_absolute;
    00069
    00070         eoSymNodeMutate(LanguageTable& _table)
    00071         :   table(_table),
    00072             own_mutation_rate(1.0),
    00073             own_is_rate_absolute(false), // this means a probability of node mutation of 1/sym.size()
    00074             mutation_rate(own_mutation_rate),
    00075             is_rate_absolute(own_is_rate_absolute)
    00076         {}
    00077
    00078         eoSymNodeMutate(LanguageTable& _table, double& _mutation_rate, bool& _is_rate_absolute)
    00079         :   table(_table),
    00080             mutation_rate(_mutation_rate),
    00081             is_rate_absolute(_is_rate_absolute)
    00082         {}
    00083
    00084
    00085         bool operator()(EoType& _eo) {
    00086             double p = mutation_rate;
    00087             if (!is_rate_absolute) p /= _eo.size();
    00088
    00089             return mutate(_eo, p, table);
    00090         }
    00091
    00092 };
    00093
    00097 extern bool mutate_constants(Sym& sym, double stdev);
    00098 template <class EoType>
    00099 class eoSymConstantMutate : public eoMonOp<EoType> {
    00100
    00101     double& stdev;
    00102
    00103
    00104     public :
    00105     eoSymConstantMutate(double& _stdev) : stdev(_stdev) {}
    00106
    00107     bool operator()(EoType& _eo) {
    00108         return mutate_constants(_eo, stdev);
    00109     }
    00110
    00111
    00112 };
    00113
    00114 #endif