Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | Related Pages | Search for

# eoSymMutate.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 <eoSymMutate.h>
    00019 #include <FunDef.h>
    00020 #include <utils/eoRNG.h>
    00021
    00022 using namespace std;
    00023
    00024 std::pair<Sym, bool> do_mutate(Sym sym, double p, const LanguageTable& table) {
    00025
    00026     bool changed = false;
    00027     SymVec args = sym.args();
    00028     if (rng.flip(p)) {
    00029         token_t new_token = table.get_random_function(sym.token(), args.size());
    00030         if (new_token != sym.token()) {
    00031             changed = true;
    00032             sym = Sym(new_token, args);
    00033         }
    00034     }
    00035
    00036     for (unsigned i = 0; i < args.size(); ++i) {
    00037         std::pair<Sym,bool> r = do_mutate(args[i], p, table);
    00038         changed |= r.second;
    00039         if (r.second)
    00040             args[i] = r.first;
    00041     }
    00042
    00043     if (changed)
    00044         return std::make_pair(Sym(sym.token(), args), true);
    00045     // else
    00046     return std::make_pair(sym, false);
    00047 }
    00048
    00049
    00050 // these two can (should?) move to an impl file
    00051 bool mutate(Sym& sym, double p, const LanguageTable& table) {
    00052     std::pair<Sym, bool> r = do_mutate(sym, p, table);
    00053     sym = r.first;
    00054     return r.second;
    00055 }
    00056
    00057
    00058 bool mutate_constants(Sym& sym, double stdev) {
    00059     vector<double> values = get_constants(sym);
    00060
    00061     if (values.empty()) {
    00062         return false;
    00063     }
    00064
    00065     for (unsigned i = 0; i < values.size(); ++i) {
    00066         values[i] += rng.normal() * stdev;
    00067     }
    00068
    00069     sym = set_constants(sym, values);
    00070
    00071     return true;
    00072 }
    00073