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
BIN
eo/contrib/mathsym/test/test.o
Normal file
BIN
eo/contrib/mathsym/test/test.o
Normal file
Binary file not shown.
185
eo/contrib/mathsym/test/test_compile.cpp
Normal file
185
eo/contrib/mathsym/test/test_compile.cpp
Normal file
|
|
@ -0,0 +1,185 @@
|
|||
/*
|
||||
* 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("problem4.dat");
|
||||
|
||||
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());
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
BIN
eo/contrib/mathsym/test/test_compile.o
Normal file
BIN
eo/contrib/mathsym/test/test_compile.o
Normal file
Binary file not shown.
119
eo/contrib/mathsym/test/testeo.cpp
Normal file
119
eo/contrib/mathsym/test/testeo.cpp
Normal 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("problem4.dat");
|
||||
IntervalBoundsCheck check(dataset.input_minima(), dataset.input_maxima());
|
||||
ErrorMeasure measure(dataset, 0.90, ErrorMeasure::mean_squared_scaled);
|
||||
|
||||
eoSymPopEval<EoType> evaluator(check, measure);
|
||||
|
||||
eoPop<EoType> dummy;
|
||||
evaluator(pop, dummy);
|
||||
|
||||
for (unsigned i = 0; i < pop.size(); ++i) {
|
||||
cout << pop[i] << endl;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
BIN
eo/contrib/mathsym/test/testeo.o
Normal file
BIN
eo/contrib/mathsym/test/testeo.o
Normal file
Binary file not shown.
Reference in a new issue