# test_compile.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 <utils/eoRNG.h>
    00019
    00020 #include <FunDef.h>
    00021 #include <sym_compile.h>
    00022
    00023 #include <Dataset.h>
    00024 #include <ErrorMeasure.h>
    00025 #include <LanguageTable.h>
    00026 #include <BoundsCheck.h>
    00027 #include <TreeBuilder.h>
    00028
    00029 #include <iostream>
    00030
    00031 using namespace std;
    00032
    00033 void test_xover();
    00034
    00035 int main() {
    00036     Dataset dataset;
    00037     dataset.load_data("test_data.txt");
    00038
    00039     cout << "Records/Fields " << dataset.n_records() << ' ' << dataset.n_fields() << endl;
    00040
    00041     LanguageTable table;
    00042     table.add_function(sum_token, 2);
    00043     table.add_function(prod_token, 2);
    00044     table.add_function(sum_token, 0);
    00045     table.add_function(prod_token, 0);
    00046     table.add_function(inv_token, 1);
    00047     table.add_function(min_token, 1);
    00048
    00049     for (unsigned i = 0; i < dataset.n_fields(); ++i) {
    00050         table.add_function( SymVar(i).token(), 0);
    00051     }
    00052
    00053     TreeBuilder builder(table);
    00054
    00055     IntervalBoundsCheck bounds(dataset.input_minima(), dataset.input_maxima() );
    00056     ErrorMeasure measure(dataset, 1.0);
    00057
    00058
    00059     unsigned n = 1000;
    00060     unsigned k = 0;
    00061
    00062     vector<Sym> pop;
    00063     double sumsize = 0;
    00064     for (unsigned i = 0; i < n; ++i) {
    00065
    00066         Sym sym = builder.build_tree(6, i%2);
    00067         pop.push_back(sym);
    00068         sumsize += sym.size();
    00069     }
    00070
    00071     cout << "Size " << sumsize/pop.size() << endl;
    00072
    00073     // shuffle
    00074     for (unsigned gen = 0; gen < 10; ++gen) {
    00075         random_shuffle(pop.begin(), pop.end());
    00076         for (unsigned i = 0; i < pop.size(); i+=2) {
    00077
    00078             unsigned p1 = rng.random(pop[i].size());
    00079             unsigned p2 = rng.random(pop[i+1].size());
    00080
    00081             Sym a = insert_subtree(pop[i], p1, get_subtree(pop[i+1], p2));
    00082             Sym b = insert_subtree(pop[i+1], p2, get_subtree(pop[i], p1));
    00083
    00084             pop[i] = a;
    00085             pop[i+1] = b;
    00086
    00087         }
    00088         cout << gen << ' ' << Sym::get_dag().size() << endl;
    00089     }
    00090
    00091     vector<Sym> oldpop;
    00092     swap(pop,oldpop);
    00093     for (unsigned i = 0; i < oldpop.size(); ++i) {
    00094         Sym sym = oldpop[i];
    00095         if (!bounds.in_bounds(sym)) {
    00096             k++;
    00097             continue;
    00098         }
    00099         pop.push_back(sym);
    00100     }
    00101
    00102     cout << "Done" << endl;
    00103
    00104     // full compilation
    00105
    00106     time_t start_time = time(0);
    00107     time_t compile_time;
    00108     {
    00109         multi_function f = compile(pop);
    00110         compile_time = time(0);
    00111         vector<double> out(pop.size());
    00112
    00113         for (unsigned j = 0; j < dataset.n_records(); ++j) {
    00114             f(&dataset.get_inputs(j)[0], &out[0]);
    00115         }
    00116     }
    00117
    00118     time_t end_time = time(0);
    00119
    00120     cout << "Evaluated " << n-k << " syms in " << end_time - start_time << " seconds, compile took " << compile_time - start_time << " seconds" << endl;
    00121
    00122     start_time = time(0);
    00123     vector<single_function> funcs;
    00124     compile(pop, funcs);
    00125     compile_time = time(0);
    00126     for (unsigned i = 0; i < pop.size(); ++i) {
    00127
    00128         single_function f = funcs[i];
    00129         for (unsigned j = 0; j < dataset.n_records(); ++j) {
    00130             f(&dataset.get_inputs(j)[0]);
    00131         }
    00132
    00133     }
    00134
    00135     end_time = time(0);
    00136
    00137     cout << "Evaluated " << n-k << " syms in " << end_time - start_time << " seconds, compile took " << compile_time - start_time << " seconds" << endl;
    00138     return 0; // skip the 'slow' one-by-one method
    00139     start_time = time(0);
    00140     for (unsigned i = 0; i < pop.size(); ++i) {
    00141
    00142         single_function f = compile(pop[i]);
    00143         for (unsigned j = 0; j < dataset.n_records(); ++j) {
    00144             f(&dataset.get_inputs(j)[0]);
    00145         }
    00146
    00147     }
    00148
    00149     end_time = time(0);
    00150
    00151     cout << "Evaluated " << n-k << " syms in " << end_time - start_time << " seconds" << endl;
    00152
    00153 }
    00154
    00155 void test_xover() {
    00156     Sym c = SymVar(0);
    00157     Sym x = c + c * c + c;
    00158
    00159     cout << c << endl;
    00160     cout << x << endl;
    00161
    00162     vector<Sym> pop;
    00163     for (unsigned i = 0; i < x.size(); ++i) {
    00164         for (unsigned j = 0; j < x.size(); ++j) {
    00165
    00166             Sym s = insert_subtree(x, i, get_subtree(x, j));
    00167             pop.push_back(s);
    00168             cout << i << ' ' << j << ' ' << s << endl;
    00169         }
    00170     }
    00171
    00172     x = Sym();
    00173     c = Sym();
    00174
    00175     SymMap& dag = Sym::get_dag();
    00176
    00177     for (SymMap::iterator it = dag.begin(); it != dag.end(); ++it) {
    00178         Sym s(it);
    00179         cout << s << ' ' << s.refcount() << endl;
    00180     }
    00181
    00182
    00183
    00184 }
    00185