00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
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
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
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 cout << "Compiled" << endl;
00114
00115 for (unsigned j = 0; j < dataset.n_records(); ++j) {
00116 f(&dataset.get_inputs(j)[0], &out[0]);
00117 }
00118 }
00119
00120 time_t end_time = time(0);
00121
00122 cout << "Evaluated " << n-k << " syms in " << end_time - start_time << " seconds, compile took " << compile_time - start_time << " seconds" << endl;
00123
00124 start_time = time(0);
00125 vector<single_function> funcs;
00126 compile(pop, funcs);
00127 compile_time = time(0);
00128 for (unsigned i = 0; i < pop.size(); ++i) {
00129
00130 single_function f = funcs[i];
00131 for (unsigned j = 0; j < dataset.n_records(); ++j) {
00132 f(&dataset.get_inputs(j)[0]);
00133 }
00134
00135 }
00136
00137 end_time = time(0);
00138
00139 cout << "Evaluated " << n-k << " syms in " << end_time - start_time << " seconds, compile took " << compile_time - start_time << " seconds" << endl;
00140 return 0;
00141 start_time = time(0);
00142 for (unsigned i = 0; i < pop.size(); ++i) {
00143
00144 single_function f = compile(pop[i]);
00145 for (unsigned j = 0; j < dataset.n_records(); ++j) {
00146 f(&dataset.get_inputs(j)[0]);
00147 }
00148
00149 }
00150
00151 end_time = time(0);
00152
00153 cout << "Evaluated " << n-k << " syms in " << end_time - start_time << " seconds" << endl;
00154
00155 }
00156
00157 void test_xover() {
00158 Sym c = SymVar(0);
00159 Sym x = c + c * c + c;
00160
00161 cout << c << endl;
00162 cout << x << endl;
00163
00164 vector<Sym> pop;
00165 for (unsigned i = 0; i < x.size(); ++i) {
00166 for (unsigned j = 0; j < x.size(); ++j) {
00167
00168 Sym s = insert_subtree(x, i, get_subtree(x, j));
00169 pop.push_back(s);
00170 cout << i << ' ' << j << ' ' << s << endl;
00171 }
00172 }
00173
00174 x = Sym();
00175 c = Sym();
00176
00177 SymMap& dag = Sym::get_dag();
00178
00179 for (SymMap::iterator it = dag.begin(); it != dag.end(); ++it) {
00180 Sym s(it);
00181 cout << s << ' ' << s.refcount() << endl;
00182 }
00183
00184
00185
00186 }
00187