diff --git a/eo/contrib/mathsym/GNUmakefile b/eo/contrib/mathsym/GNUmakefile index f33c6efc..3bd5c779 100644 --- a/eo/contrib/mathsym/GNUmakefile +++ b/eo/contrib/mathsym/GNUmakefile @@ -19,7 +19,7 @@ CXXSOURCES=FunDef.cpp Sym.cpp SymImpl.cpp SymOps.cpp sym_compile.cpp TreeBuilder Dataset.cpp ErrorMeasure.cpp Scaling.cpp TargetInfo.cpp BoundsCheck.cpp util.cpp NodeSelector.cpp\ eoSymCrossover.cpp sym_operations.cpp eoSymMutate.cpp eoSymLambdaMutate.cpp MultiFunction.cpp -TESTPROGRAMS=test/test_compile test/testeo test/test_simplify test/test_diff test/test_lambda test/test_mf +TESTPROGRAMS=test/test_compile test/testeo test/test_simplify test/test_diff test/test_lambda test/test_mf test/test_interval OBJS= $(CXXSOURCES:.cpp=.o) c_compile.o @@ -40,7 +40,7 @@ libsym.a: $(OBJS) rm libsym.a; ar cq $(SYMLIB) $(OBJS) check: $(TESTPROGRAMS) - test/test_compile && test/testeo && test/test_simplify && test/test_diff && test/test_lambda && echo "all tests succeeded" + test/test_compile && test/test_interval && test/testeo && test/test_simplify && test/test_diff && test/test_lambda && echo "all tests succeeded" test/test_compile: test/test_compile.o ${SYMLIB} $(CXX) -o test/test_compile test/test_compile.o $(SYMLIB) ${LIBS} @@ -60,6 +60,10 @@ test/test_lambda: test/test_lambda.o $(SYMLIB) test/test_mf: test/test_mf.o $(SYMLIB) $(CXX) -o test/test_mf test/test_mf.o $(SYMLIB) ${LIBS} +test/test_interval: test/test_interval.o + $(CXX) -o test/test_interval test/test_interval.o $(SYMLIB) ${LIBS} + + # eo ../../src/libeo.a: make -C ../../src libeo.a diff --git a/eo/contrib/mathsym/eo_interface/eoSym.h b/eo/contrib/mathsym/eo_interface/eoSym.h index 747850e2..98579308 100644 --- a/eo/contrib/mathsym/eo_interface/eoSym.h +++ b/eo/contrib/mathsym/eo_interface/eoSym.h @@ -19,8 +19,8 @@ #define EOSYM_H_ #include -#include -#include +#include +#include template diff --git a/eo/contrib/mathsym/eo_interface/eoSymCrossover.cpp b/eo/contrib/mathsym/eo_interface/eoSymCrossover.cpp index 412ac78e..98398379 100644 --- a/eo/contrib/mathsym/eo_interface/eoSymCrossover.cpp +++ b/eo/contrib/mathsym/eo_interface/eoSymCrossover.cpp @@ -21,6 +21,9 @@ #include #include +#include + +using namespace std; bool subtree_quad(Sym& a, Sym& b, NodeSelector& select) { NodeSelector::NodeSelection sel_a = select.select_node(a); @@ -75,4 +78,46 @@ bool homologous_bin(Sym& a, const Sym& b) { return org != a; } +void set_size_levels(Sym sym, vector& l, vector& s, unsigned level = 1) { + l.push_back(level); + s.push_back(sym.size()); + + for (unsigned i = 0; i < sym.args().size(); ++i) { + set_size_levels(sym.args()[i], l, s, level+1); + } +} + + +bool size_level_xover(Sym& a, const Sym& b) { + + Sym org = a; + + vector levela; + vector sizesa; + vector levelb; + vector sizesb; + + set_size_levels(a, levela, sizesa); + set_size_levels(b, levelb, sizesb); + + unsigned p0; + unsigned p1; + + for (unsigned tries = 0;; ++tries) { + p0 = rng.random(a.size()); + p1 = rng.random(b.size()); + + if (tries < 5 && (sizesa[p0] != sizesb[p1] && levela[p0] != levelb[p1])) { + continue; + } + + break; + } + + a = insert_subtree(a, p0, get_subtree(b, p1)); + + return org != a; + +} + diff --git a/eo/contrib/mathsym/eo_interface/eoSymCrossover.h b/eo/contrib/mathsym/eo_interface/eoSymCrossover.h index faa9acfa..5e95ab3d 100644 --- a/eo/contrib/mathsym/eo_interface/eoSymCrossover.h +++ b/eo/contrib/mathsym/eo_interface/eoSymCrossover.h @@ -60,5 +60,14 @@ class eoBinHomologousCrossover : public eoBinOp { }; +extern bool size_level_xover(Sym& a, const Sym& b); +template +class eoSizeLevelCrossover : public eoBinOp { + public: + bool operator()(EoType& a, const EoType& b) { + return size_level_xover(a,b); + } +}; + #endif diff --git a/eo/contrib/mathsym/eo_interface/eoSymInit.h b/eo/contrib/mathsym/eo_interface/eoSymInit.h index 591b0050..1f6318ed 100644 --- a/eo/contrib/mathsym/eo_interface/eoSymInit.h +++ b/eo/contrib/mathsym/eo_interface/eoSymInit.h @@ -19,7 +19,7 @@ #define EOSYMINIT_H #include -#include +#include /** Default initializer, Koza style */ template diff --git a/eo/contrib/mathsym/eo_interface/eoSymMutate.cpp b/eo/contrib/mathsym/eo_interface/eoSymMutate.cpp index 0ab3f26b..55c78670 100644 --- a/eo/contrib/mathsym/eo_interface/eoSymMutate.cpp +++ b/eo/contrib/mathsym/eo_interface/eoSymMutate.cpp @@ -63,11 +63,10 @@ bool mutate_constants(Sym& sym, double stdev) { } for (unsigned i = 0; i < values.size(); ++i) { - values[i] += rng.normal() * stdev; + values[i] += rng.normal() * stdev / values.size(); } sym = set_constants(sym, values); - return true; } diff --git a/eo/contrib/mathsym/eo_interface/eoSymMutate.h b/eo/contrib/mathsym/eo_interface/eoSymMutate.h index aabe9141..08e23315 100644 --- a/eo/contrib/mathsym/eo_interface/eoSymMutate.h +++ b/eo/contrib/mathsym/eo_interface/eoSymMutate.h @@ -18,8 +18,8 @@ #ifndef SYMMUTATE_H #define SYMMUTATE_H -#include -#include +#include +#include #include #include diff --git a/eo/contrib/mathsym/eval/Interval.h b/eo/contrib/mathsym/eval/Interval.h index 1849af4c..3b60121e 100644 --- a/eo/contrib/mathsym/eval/Interval.h +++ b/eo/contrib/mathsym/eval/Interval.h @@ -77,39 +77,6 @@ std::ostream& operator<<(std::ostream& os, const Interval& val) { } #ifdef TEST_INTERVAL -using namespace std; -using namespace boost::numeric; - -int main() { - Interval a(0, 10); - Interval b(-1.5, 2); - cout << "a = " << a << endl; - cout << "b = " << b << endl; - cout << "a + b = " << a + b << endl; - cout << "a - b = " << a - b << endl; - cout << "b - a = " << b - a << endl; - cout << "-a = " << -a << endl; - cout << "a * b = " << a * b << endl; - cout << "b/(a+1) = " << b / (a + 1.0) << endl; - cout << "b * a = " << b * a << endl; - - cout << "b / a = " << b/a << endl; - - cout << "cos(a) = " << cos(a) << endl; - cout << "cos(b) = " << cos(b) << endl; - - cout << "log(b) = " << log(b) << endl; - - cout << "sqrt(b) = " << sqrt(b) << endl; - cout << "sqrt(a) = " << sqrt(a) << endl; - cout << "sqr(b) = " << sqr(b) << endl; - - cout << "exp(b*a)= " << exp(b*a) << endl; - - cout << "atan(a) = " << atan(a) << endl; - cout << "cosh(b) = " << cosh(b) << endl; - -} #endif #endif diff --git a/eo/contrib/mathsym/eval/sym_compile.h b/eo/contrib/mathsym/eval/sym_compile.h index a0980d8e..79a9d497 100644 --- a/eo/contrib/mathsym/eval/sym_compile.h +++ b/eo/contrib/mathsym/eval/sym_compile.h @@ -19,7 +19,7 @@ #define SYMCOMPILE_H_ #include - +#include typedef double (*single_function)(const double []); typedef double (*multi_function)(const double[], double[]); diff --git a/eo/contrib/mathsym/fun/FunDef.cpp b/eo/contrib/mathsym/fun/FunDef.cpp index 9a25afdf..3e2e7faa 100644 --- a/eo/contrib/mathsym/fun/FunDef.cpp +++ b/eo/contrib/mathsym/fun/FunDef.cpp @@ -247,8 +247,6 @@ class Const : public FunDef { } Interval eval(const vector& _, const vector& inputs) const { - // Profil/Bias seems to have a problem with 0 * inf when the Interval is exact zero (fpe) - //if (value == 0.0) return Interval(-BiasEpsilon,BiasEpsilon); return Interval(value); } diff --git a/eo/contrib/mathsym/fun/FunDef.h b/eo/contrib/mathsym/fun/FunDef.h index 2c88af05..d4ed95a1 100644 --- a/eo/contrib/mathsym/fun/FunDef.h +++ b/eo/contrib/mathsym/fun/FunDef.h @@ -23,8 +23,8 @@ #include #include -#include "Sym.h" -#include "Interval.h" +#include "sym/Sym.h" +#include "eval/Interval.h" class FunDef { public: diff --git a/eo/contrib/mathsym/fun/SymOps.h b/eo/contrib/mathsym/fun/SymOps.h index 16eb35fe..a11994ff 100644 --- a/eo/contrib/mathsym/fun/SymOps.h +++ b/eo/contrib/mathsym/fun/SymOps.h @@ -18,7 +18,7 @@ #ifndef SYMOPS_H #define SYMOPS_H -#include "Sym.h" +#include "sym/Sym.h" extern Sym operator+(Sym a, Sym b); extern Sym operator*(Sym a, Sym b); diff --git a/eo/contrib/mathsym/gen/LanguageTable.h b/eo/contrib/mathsym/gen/LanguageTable.h index dcbb0292..978ef3f5 100644 --- a/eo/contrib/mathsym/gen/LanguageTable.h +++ b/eo/contrib/mathsym/gen/LanguageTable.h @@ -18,7 +18,7 @@ #ifndef LANGUAGE_TABLE_H #define LANGUAGE_TABLE_H -#include "token.h" +#include class LanguageImpl; class Sym; diff --git a/eo/contrib/mathsym/gen/NodeSelector.h b/eo/contrib/mathsym/gen/NodeSelector.h index e30abd9f..c8b2ccf0 100644 --- a/eo/contrib/mathsym/gen/NodeSelector.h +++ b/eo/contrib/mathsym/gen/NodeSelector.h @@ -18,7 +18,7 @@ #ifndef NODESELECTOR_H #define NODESELECTOR_H -#include +#include /** Base class for selecting nodes */ class NodeSelector { diff --git a/eo/contrib/mathsym/gen/TreeBuilder.h b/eo/contrib/mathsym/gen/TreeBuilder.h index 1969a52b..57cb5458 100644 --- a/eo/contrib/mathsym/gen/TreeBuilder.h +++ b/eo/contrib/mathsym/gen/TreeBuilder.h @@ -18,7 +18,7 @@ #ifndef TREEBUILDER_H_ #define TREEBUILDER_H_ -#include "Sym.h" +#include "sym/Sym.h" #include "LanguageTable.h" class TreeBuilder { diff --git a/eo/contrib/mathsym/sym/Sym.h b/eo/contrib/mathsym/sym/Sym.h index e90882fb..2eecc598 100644 --- a/eo/contrib/mathsym/sym/Sym.h +++ b/eo/contrib/mathsym/sym/Sym.h @@ -38,6 +38,9 @@ struct UniqueNodeStats { virtual ~UniqueNodeStats(){} }; #else #define USE_TR1 0 #endif +// TR1 is buggy at times +#undef USE_TR1 +#define USE_TR1 0 #if USE_TR1 #include diff --git a/eo/contrib/mathsym/sym/SymImpl.cpp b/eo/contrib/mathsym/sym/SymImpl.cpp index b2d568a6..1e15e70a 100644 --- a/eo/contrib/mathsym/sym/SymImpl.cpp +++ b/eo/contrib/mathsym/sym/SymImpl.cpp @@ -47,7 +47,7 @@ SymArgs::SymArgs(const SymArgs& args) : pimpl(0), args_ptr(args.args_ptr) { } } -const SymArgs& SymArgs::operator=(const SymArgs& args) { +SymArgs& SymArgs::operator=(const SymArgs& args) { if (args.pimpl && args.args_ptr == &args.pimpl->owned_args) { pimpl = new SymArgsImpl(*args.pimpl); args_ptr = &pimpl->owned_args; diff --git a/eo/contrib/mathsym/sym/SymImpl.h b/eo/contrib/mathsym/sym/SymImpl.h index 64251f57..b5bf9637 100644 --- a/eo/contrib/mathsym/sym/SymImpl.h +++ b/eo/contrib/mathsym/sym/SymImpl.h @@ -47,7 +47,7 @@ class SymArgs { ~SymArgs(); SymArgs(const SymArgs& args); - const SymArgs& SymArgs::operator=(const SymArgs& other); + SymArgs& operator=(const SymArgs& other); size_t len() const; const std::vector& vec() const { return *args_ptr; } diff --git a/eo/contrib/mathsym/symreg.cpp b/eo/contrib/mathsym/symreg.cpp index cc90e4e1..5e55196b 100644 --- a/eo/contrib/mathsym/symreg.cpp +++ b/eo/contrib/mathsym/symreg.cpp @@ -115,7 +115,7 @@ int main(int argc, char* argv[]) { /* Population */ - unsigned pop_size = parser.createParam(1500u, "population-size", "Population Size", 'p', string("Population")).value(); + unsigned pop_size = parser.createParam(500u, "population-size", "Population Size", 'p', string("Population")).value(); uint32_t seed = parser.createParam( uint32_t(time(0)), "random-seed", "Seed for rng", 'D').value(); @@ -274,7 +274,7 @@ int main(int argc, char* argv[]) { genetic_operator.add( submutate, subtree_mut_prob); // todo, make this parameter, etc - double std = 0.01; + double std = 1.0; eoSymConstantMutate constmutate(std); genetic_operator.add(constmutate, constant_mut_prob); @@ -285,7 +285,8 @@ int main(int argc, char* argv[]) { // genetic_operator.add(lambda_mutate, lambda_mut_prob); // TODO: prob should be settable //eoQuadSubtreeCrossover quad(node_selector); - eoBinSubtreeCrossover bin(node_selector); + eoSizeLevelCrossover bin;//(node_selector); + //eoBinSubtreeCrossover bin(node_selector); genetic_operator.add(bin, subtree_xover_prob); eoBinHomologousCrossover hom; @@ -298,7 +299,7 @@ int main(int argc, char* argv[]) { eoSymPopEval evaluator(check, measure, maximumSize); eoDetTournamentSelect selectOne(tournamentsize); - eoGeneralBreeder breeder(selectOne, genetic_operator); + eoGeneralBreeder breeder(selectOne, genetic_operator,1); eoPlusReplacement replace; // Terminators diff --git a/eo/contrib/mathsym/test/test_compile.cpp b/eo/contrib/mathsym/test/test_compile.cpp index 2c2f02ae..adac41a9 100644 --- a/eo/contrib/mathsym/test/test_compile.cpp +++ b/eo/contrib/mathsym/test/test_compile.cpp @@ -109,7 +109,9 @@ int main() { multi_function f = compile(pop); compile_time = time(0); vector out(pop.size()); - + + cout << "Compiled" << endl; + for (unsigned j = 0; j < dataset.n_records(); ++j) { f(&dataset.get_inputs(j)[0], &out[0]); } diff --git a/eo/contrib/mathsym/test_data.txt b/eo/contrib/mathsym/test_data.txt index a8c792b1..754b1f0d 100644 --- a/eo/contrib/mathsym/test_data.txt +++ b/eo/contrib/mathsym/test_data.txt @@ -1,5 +1,5 @@ # 101 2 nan nan - 0 -0 + 0.0 -0 0.1 -8.89903723981037e-05 0.2 -0.00122598240763888 0.3 -0.00517587564272387