move paradiseo/eo to deprecated/ before merge with eodev

This commit is contained in:
Johann Dreo 2012-10-05 15:12:12 +02:00
commit 0c5120f675
717 changed files with 0 additions and 0 deletions

View file

@ -0,0 +1,98 @@
/*
* Copyright (C) 2005 Maarten Keijzer
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "LanguageTable.h"
#include "Sym.h"
#include <utils/eoRNG.h>
using namespace std;
extern Sym default_const();
class LanguageImpl {
public :
std::vector<Sym> vars;
LanguageTable::erc_func erc;
std::vector<functor_t> functions;
std::vector< std::vector<token_t> > functions_per_arity;
LanguageImpl() : erc(default_const) {}
};
LanguageTable::LanguageTable() {
pimpl = new LanguageImpl;
}
LanguageTable::~LanguageTable() {
delete pimpl;
}
LanguageTable::LanguageTable(const LanguageTable& that) {
pimpl = new LanguageImpl(*that.pimpl);
}
LanguageTable& LanguageTable::operator=(const LanguageTable& that) {
*pimpl = *that.pimpl;
return *this;
}
void LanguageTable::add_function(token_t token, unsigned arity) {
functor_t f = {token, arity};
add_function( f );
}
void LanguageTable::add_function(functor_t f) {
if (f.arity > 0) {
pimpl->functions.push_back(f);
} else {
pimpl->vars.push_back(Sym(f.token));
}
if (pimpl->functions_per_arity.size() <= f.arity) pimpl->functions_per_arity.resize(f.arity+1);
pimpl->functions_per_arity[f.arity].push_back(f.token);
}
void LanguageTable::set_erc( erc_func func) { pimpl->erc = func; }
/* Getting info out */
extern Sym SymConst(double val);
Sym LanguageTable::get_random_var() const { return rng.choice(pimpl->vars); }
Sym LanguageTable::get_random_const() const { return pimpl->erc(); }
functor_t LanguageTable::get_random_function() const
{
return rng.choice(pimpl->functions);
}
token_t LanguageTable::get_random_function(token_t token, unsigned arity) const
{
if (pimpl->functions_per_arity.size() <= arity || pimpl->functions_per_arity[arity].empty()) {
return token; // return original token if no functions of this arity are found
}
return rng.choice(pimpl->functions_per_arity[arity]);
}

View file

@ -0,0 +1,56 @@
/*
* Copyright (C) 2005 Maarten Keijzer
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef LANGUAGE_TABLE_H
#define LANGUAGE_TABLE_H
#include <sym/token.h>
class LanguageImpl;
class Sym;
class LanguageTable {
LanguageImpl* pimpl;
public:
LanguageTable();
~LanguageTable();
LanguageTable(const LanguageTable& org);
LanguageTable& operator=(const LanguageTable& org);
/* setting it up */
typedef Sym (*erc_func)();
void add_function(token_t token, unsigned arity);
void add_function(functor_t functor);
void set_erc(erc_func func);
/* Getting info out */
Sym get_random_var() const;
Sym get_random_const() const;
functor_t get_random_function() const;
token_t get_random_function(token_t org, unsigned arity) const;
};
#endif

View file

@ -0,0 +1,50 @@
/*
* Copyright (C) 2005 Maarten Keijzer
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "NodeSelector.h"
#include "Sym.h"
#include <utils/eoRNG.h>
// If subtree is not set (by randomnodeselector for instance, find it now
Sym NodeSelector::NodeSelection::subtree() {
if (subtree_.empty()) {
subtree_ = get_subtree(root_, subtree_index_);
}
return subtree_;
}
NodeSelector::NodeSelection RandomNodeSelector::select_node(Sym sym) const {
unsigned idx = rng.random(sym.size());
return NodeSelection(sym, idx, Sym() ); // empty subtree, find it when needed
}
NodeSelector::NodeSelection BiasedNodeSelector::select_node(Sym sym) const {
unsigned p = rng.random(sym.size());
Sym res;
for (unsigned i = 0; i < nRounds; ++i) {
res = get_subtree(sym, p);
if (res.args().size() > 0) break;
p = rng.random(sym.size());
}
return NodeSelection(sym, p, res);
}

View file

@ -0,0 +1,65 @@
/*
* Copyright (C) 2005 Maarten Keijzer
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef NODESELECTOR_H
#define NODESELECTOR_H
#include <sym/Sym.h>
/** Base class for selecting nodes */
class NodeSelector {
public:
class NodeSelection {
Sym root_;
unsigned subtree_index_;
Sym subtree_;
public :
NodeSelection(Sym r, unsigned idx, Sym s)
: root_(r), subtree_index_(idx), subtree_(s) {}
Sym root() const { return root_; }
unsigned idx() const { return subtree_index_; }
Sym subtree();
};
virtual ~NodeSelector() {}
virtual NodeSelection select_node(Sym sym) const = 0;
};
/** Select nodes uniformly */
class RandomNodeSelector : public NodeSelector {
public:
NodeSelection select_node(Sym sym) const;
};
/** A node selector that does a specified number of rounds ignoring terminals */
class BiasedNodeSelector : public NodeSelector {
public:
unsigned nRounds;
BiasedNodeSelector() : nRounds(3) {} // 3: for binary trees 87.5% chance of selecting an internal node
BiasedNodeSelector(unsigned n) : nRounds(n) {}
NodeSelection select_node(Sym sym) const;
};
#endif

View file

@ -0,0 +1,46 @@
/*
* Copyright (C) 2005 Maarten Keijzer
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <utils/eoRNG.h>
#include "TreeBuilder.h"
Sym TreeBuilder::make_terminal() const {
if (rng.flip(vcprob)) {
return table.get_random_var();
}
return table.get_random_const();
}
Sym TreeBuilder::build_tree(unsigned max_depth, bool grow) const {
if (max_depth == 0 || grow && rng.random(2) == 0) {
return make_terminal();
}
// pick a random function, no matter what arity
functor_t func = table.get_random_function();
SymVec args(func.arity);
for (unsigned i = 0; i < args.size(); ++i) {
args[i] = build_tree(max_depth-1, grow);
}
return Sym(func.token, args);
}

View file

@ -0,0 +1,45 @@
/*
* Copyright (C) 2005 Maarten Keijzer
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef TREEBUILDER_H_
#define TREEBUILDER_H_
#include "sym/Sym.h"
#include "LanguageTable.h"
class TreeBuilder {
const LanguageTable& table;
// probability of selecting a var versus a const when the choice boils down to selecting a terminal
double vcprob;
Sym make_terminal() const;
public:
TreeBuilder(const LanguageTable& t) : table(t), vcprob(0.9) {};
TreeBuilder(const LanguageTable& t, double vc) : table(t), vcprob(vc) {};
void set_var_vs_const_probability(double p) { vcprob = p; }
Sym build_tree(unsigned max_depth, bool grow) const;
void build_tree(Sym& tree, unsigned max_depth, bool grow) const { tree = build_tree(max_depth, grow); }
};
#endif