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,187 @@
/*
* 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 <FunDef.h>
#include <sym_compile.h>
#include <Dataset.h>
#include <ErrorMeasure.h>
#include <LanguageTable.h>
#include <BoundsCheck.h>
#include <TreeBuilder.h>
#include <iostream>
using namespace std;
void test_xover();
int main() {
Dataset dataset;
dataset.load_data("test_data.txt");
cout << "Records/Fields " << dataset.n_records() << ' ' << dataset.n_fields() << endl;
LanguageTable table;
table.add_function(sum_token, 2);
table.add_function(prod_token, 2);
table.add_function(sum_token, 0);
table.add_function(prod_token, 0);
table.add_function(inv_token, 1);
table.add_function(min_token, 1);
for (unsigned i = 0; i < dataset.n_fields(); ++i) {
table.add_function( SymVar(i).token(), 0);
}
TreeBuilder builder(table);
IntervalBoundsCheck bounds(dataset.input_minima(), dataset.input_maxima() );
ErrorMeasure measure(dataset, 1.0);
unsigned n = 1000;
unsigned k = 0;
vector<Sym> pop;
double sumsize = 0;
for (unsigned i = 0; i < n; ++i) {
Sym sym = builder.build_tree(6, i%2);
pop.push_back(sym);
sumsize += sym.size();
}
cout << "Size " << sumsize/pop.size() << endl;
// shuffle
for (unsigned gen = 0; gen < 10; ++gen) {
random_shuffle(pop.begin(), pop.end());
for (unsigned i = 0; i < pop.size(); i+=2) {
unsigned p1 = rng.random(pop[i].size());
unsigned p2 = rng.random(pop[i+1].size());
Sym a = insert_subtree(pop[i], p1, get_subtree(pop[i+1], p2));
Sym b = insert_subtree(pop[i+1], p2, get_subtree(pop[i], p1));
pop[i] = a;
pop[i+1] = b;
}
cout << gen << ' ' << Sym::get_dag().size() << endl;
}
vector<Sym> oldpop;
swap(pop,oldpop);
for (unsigned i = 0; i < oldpop.size(); ++i) {
Sym sym = oldpop[i];
if (!bounds.in_bounds(sym)) {
k++;
continue;
}
pop.push_back(sym);
}
cout << "Done" << endl;
// full compilation
time_t start_time = time(0);
time_t compile_time;
{
multi_function f = compile(pop);
compile_time = time(0);
vector<double> out(pop.size());
cout << "Compiled" << endl;
for (unsigned j = 0; j < dataset.n_records(); ++j) {
f(&dataset.get_inputs(j)[0], &out[0]);
}
}
time_t end_time = time(0);
cout << "Evaluated " << n-k << " syms in " << end_time - start_time << " seconds, compile took " << compile_time - start_time << " seconds" << endl;
start_time = time(0);
vector<single_function> funcs;
compile(pop, funcs);
compile_time = time(0);
for (unsigned i = 0; i < pop.size(); ++i) {
single_function f = funcs[i];
for (unsigned j = 0; j < dataset.n_records(); ++j) {
f(&dataset.get_inputs(j)[0]);
}
}
end_time = time(0);
cout << "Evaluated " << n-k << " syms in " << end_time - start_time << " seconds, compile took " << compile_time - start_time << " seconds" << endl;
return 0; // skip the 'slow' one-by-one method
start_time = time(0);
for (unsigned i = 0; i < pop.size(); ++i) {
single_function f = compile(pop[i]);
for (unsigned j = 0; j < dataset.n_records(); ++j) {
f(&dataset.get_inputs(j)[0]);
}
}
end_time = time(0);
cout << "Evaluated " << n-k << " syms in " << end_time - start_time << " seconds" << endl;
}
void test_xover() {
Sym c = SymVar(0);
Sym x = c + c * c + c;
cout << c << endl;
cout << x << endl;
vector<Sym> pop;
for (unsigned i = 0; i < x.size(); ++i) {
for (unsigned j = 0; j < x.size(); ++j) {
Sym s = insert_subtree(x, i, get_subtree(x, j));
pop.push_back(s);
cout << i << ' ' << j << ' ' << s << endl;
}
}
x = Sym();
c = Sym();
SymMap& dag = Sym::get_dag();
for (SymMap::iterator it = dag.begin(); it != dag.end(); ++it) {
Sym s(it);
cout << s << ' ' << s.refcount() << endl;
}
}

View file

@ -0,0 +1,17 @@
#include <Sym.h>
#include <FunDef.h>
#include <iostream>
using namespace std;
int main() {
Sym v = SymConst(1.2);
Sym g = exp(-sqr(v));
cout << g << endl;
cout << differentiate(g, v.token()) << endl;
}

View file

@ -0,0 +1,34 @@
#include <FunDef.h>
using namespace std;
int main() {
Sym x = SymVar(0);
Sym y = SymVar(1);
Sym f = y + x*x;
Sym l = SymLambda(f);
SymVec args = l.args();
args[0] = x;
args[1] = y;
l = Sym(l.token(), args);
vector<double> v(3);
v[0] = 2.0;
v[1] = 3.0;
v[2] = 4.0;
double v1 = eval(f,v);
double v2 = eval(l,v);
cout << v1 << ' ' << v2 << endl;
cout << f << endl;
cout << l << endl;
if (v1 != 7.0) return 1;
if (v2 != 11.0) return 1;
}

View file

@ -0,0 +1,45 @@
#include "Sym.h"
#include "MultiFunction.h"
#include "FunDef.h"
using namespace std;
int main() {
Sym v = SymVar(0);
Sym c = SymConst(0.1);
Sym sym = inv(v) + c;
Sym a = sym;
sym = sym * sym;
Sym b = sym;
sym = sym + sym;
c = sym;
vector<Sym> pop;
pop.push_back(sym);
MultiFunction m(pop);
vector<double> vec(1);
vec[0] = 10.0;
cout << sym << endl;
cout << "Eval " << eval(sym, vec);
vector<double> y(1);
m(vec,y);
cout << " " << y[0] << endl;
cout << "3 " << eval(a,vec) << endl;
cout << "4 " << eval(b, vec) << endl;
cout << "5 " << eval(c, vec) << endl;
}

View file

@ -0,0 +1,21 @@
#include <FunDef.h>
using namespace std;
int main() {
Sym c1 = SymConst(0.4);
Sym c2 = SymConst(0.3);
Sym v1 = SymVar(0);
Sym expr = (c1 + c2) * ( (c1 + c2) * v1);
cout << expr << endl;
cout << simplify(expr) << endl;
Sym dv = differentiate( exp(expr) , v1.token());
cout << dv << endl;
cout << simplify(dv) << endl;
}

View file

@ -0,0 +1,119 @@
/*
* 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 <TreeBuilder.h>
#include <FunDef.h>
#include <Dataset.h>
#include <eoSymInit.h>
#include <eoSym.h>
#include <eoPop.h>
#include <eoSymMutate.h>
#include <eoSymCrossover.h>
#include <eoSymEval.h>
typedef EoSym<double> EoType;
int main() {
LanguageTable table;
table.add_function(sum_token, 2);
table.add_function(prod_token, 2);
table.add_function(inv_token, 1);
table.add_function(min_token, 1);
table.add_function( SymVar(0).token(), 0);
table.add_function(tan_token, 1);
table.add_function(sum_token, 0);
table.add_function(prod_token, 0);
TreeBuilder builder(table);
eoSymInit<EoType> init(builder);
eoPop<EoType> pop(10, init);
for (unsigned i = 0; i < pop.size(); ++i) {
// write out pretty printed
cout << (Sym) pop[i] << endl;
}
BiasedNodeSelector node_selector;
eoSymSubtreeMutate<EoType> mutate1(builder, node_selector);
eoSymNodeMutate<EoType> mutate2(table);
cout << "****** MUTATION ************" << endl;
for (unsigned i = 0; i < pop.size(); ++i) {
cout << "Before " << (Sym) pop[i] << endl;
mutate1(pop[i]);
cout << "After 1 " << (Sym) pop[i] << endl;
mutate2(pop[i]);
cout << "After 2 " << (Sym) pop[i] << endl;
}
cout << "****** CROSSOVER ***********" << endl;
eoQuadSubtreeCrossover<EoType> quad(node_selector);
eoBinSubtreeCrossover<EoType> bin(node_selector);
eoBinHomologousCrossover<EoType> hom;
for (unsigned i = 0; i < pop.size()-1; ++i) {
cout << "Before " << (Sym) pop[i] << endl;
cout << "Before " << (Sym) pop[i+1] << endl;
hom(pop[i], pop[i+1]);
cout << "After hom " << (Sym) pop[i] << endl;
cout << "After hom " << (Sym) pop[i+1] << endl;
quad(pop[i], pop[i+1]);
cout << "After quad " << (Sym) pop[i] << endl;
cout << "After quad " << (Sym) pop[i+1] << endl;
bin(pop[i], pop[i+1]);
cout << "After bin " << (Sym) pop[i] << endl;
cout << "After bin " << (Sym) pop[i+1] << endl;
cout << endl;
}
cout << "****** Evaluation **********" << endl;
Dataset dataset;
dataset.load_data("test_data.txt");
IntervalBoundsCheck check(dataset.input_minima(), dataset.input_maxima());
ErrorMeasure measure(dataset, 0.90, ErrorMeasure::mean_squared_scaled);
eoSymPopEval<EoType> evaluator(check, measure, 20000);
eoPop<EoType> dummy;
evaluator(pop, dummy);
for (unsigned i = 0; i < pop.size(); ++i) {
cout << pop[i] << endl;
}
}