Added mathsym+tcc and boost against all advice
This commit is contained in:
parent
58ae49dd99
commit
90702a435d
136 changed files with 14409 additions and 0 deletions
97
eo/contrib/mathsym/gen/LanguageTable.cpp
Normal file
97
eo/contrib/mathsym/gen/LanguageTable.cpp
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
/*
|
||||
* 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;
|
||||
|
||||
eoRng rng(time(0));
|
||||
|
||||
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(unsigned arity) const
|
||||
{
|
||||
return rng.choice(pimpl->functions_per_arity[arity]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
56
eo/contrib/mathsym/gen/LanguageTable.h
Normal file
56
eo/contrib/mathsym/gen/LanguageTable.h
Normal 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 "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(unsigned arity) const;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
40
eo/contrib/mathsym/gen/NodeSelector.cpp
Normal file
40
eo/contrib/mathsym/gen/NodeSelector.cpp
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
/*
|
||||
* 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>
|
||||
|
||||
unsigned RandomNodeSelector::select_node(Sym sym) const {
|
||||
return rng.random(sym.size());
|
||||
}
|
||||
|
||||
unsigned BiasedNodeSelector::select_node(Sym sym) const {
|
||||
|
||||
unsigned p = rng.random(sym.size());
|
||||
for (unsigned i = 0; i < nRounds; ++i) {
|
||||
Sym res = get_subtree(sym, p);
|
||||
|
||||
if (res.args().size() > 0) break;
|
||||
|
||||
p = rng.random(sym.size());
|
||||
}
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
47
eo/contrib/mathsym/gen/NodeSelector.h
Normal file
47
eo/contrib/mathsym/gen/NodeSelector.h
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
/*
|
||||
* 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
|
||||
|
||||
class Sym;
|
||||
|
||||
/** Base class for selecting nodes */
|
||||
class NodeSelector {
|
||||
public:
|
||||
virtual unsigned select_node(Sym sym) const = 0;
|
||||
};
|
||||
|
||||
|
||||
/** Select nodes uniformly */
|
||||
class RandomNodeSelector : public NodeSelector {
|
||||
public:
|
||||
unsigned 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) {}
|
||||
|
||||
unsigned select_node(Sym sym) const;
|
||||
};
|
||||
|
||||
#endif
|
||||
46
eo/contrib/mathsym/gen/TreeBuilder.cpp
Normal file
46
eo/contrib/mathsym/gen/TreeBuilder.cpp
Normal 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);
|
||||
}
|
||||
|
||||
45
eo/contrib/mathsym/gen/TreeBuilder.h
Normal file
45
eo/contrib/mathsym/gen/TreeBuilder.h
Normal 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.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
|
||||
|
||||
Reference in a new issue