diff --git a/eo/test/Makefile.am b/eo/test/Makefile.am index 33124b50..604922de 100644 --- a/eo/test/Makefile.am +++ b/eo/test/Makefile.am @@ -18,7 +18,7 @@ noinst_PROGRAMS = t-eobreeder t-eoinclusion t-eoinsertion t-eo t-eofitness \ t-eoproblem t-eobin t-eolottery t-eo2dVector t-eogeneration t-eoEasyEA\ t-eoNonUniform t-eoUniform t-eoRandom t-eoStateAndParser \ t-eoAtomOps t-selectOne t-eoGOpSel \ - t-eoVector t-eoCheckpointing + t-eoVector t-eoCheckpointing t-eoExternalEO ############################################################################### @@ -170,3 +170,12 @@ t_eoCheckpointing_LDFLAGS = -lm t_eoCheckpointing_LDADD = $(LDADDS) ############################################################################### + +t_eoExternalEO_SOURCES = t-eoCheckpointing.cpp +t_eoExternalEO_DEPENDENCIES = $(DEPS) +t_eoExternalEO_LDFLAGS = -lm +t_eoExternalEO_LDADD = $(LDADDS) + +############################################################################### + + diff --git a/eo/test/t-eoCheckpointing.cpp b/eo/test/t-eoCheckpointing.cpp index ee4ac0a9..742c357a 100644 --- a/eo/test/t-eoCheckpointing.cpp +++ b/eo/test/t-eoCheckpointing.cpp @@ -75,28 +75,46 @@ int the_main(int argc, char **argv) eoDummyPop pop; - eoGenTerm genTerm(5); // 5 generations + eoGenTerm genTerm(5); // run for 5 generations - eoCheckPoint checkpoint(genTerm); + eoCheckPoint checkpoint(genTerm); + // The algorithm will now quit after five generations + // Create a counter parameter eoValueParam generationCounter(0, "Generation"); + + // Create an incrementor (wich is an eoUpdater). Note that the + // Parameter's value is passed by reference, so every time the incrementer increments, + // the data in generationCounter will change. eoIncrementor increment(generationCounter.value()); + // Add it to the checkpoint, this will result in the counter being incremented every generation checkpoint.add(increment); + // The file monitor will print parameters to a comma seperated file eoFileMonitor monitor("monitor.csv"); + + // the checkpoint mechanism can handle multiple monitors checkpoint.add(monitor); + // the monitor can monitor parameters such as the generationCounter monitor.add(generationCounter); + // Second moment stats: average and stdev eoSecondMomentStats stats; + // Add it to the checkpoint to get it called at the appropriate time checkpoint.add(stats); + + // Add it to the monitor to get it written to the file monitor.add(stats); - eoCountedStateSaver stateSaver1(3, state, "generation"); // save every third generation - eoTimedStateSaver stateSaver2(2, state, "time"); // save every 2 seconds + // save state every third generation + eoCountedStateSaver stateSaver1(3, state, "generation"); + // save state every 2 seconds + eoTimedStateSaver stateSaver2(2, state, "time"); + // And add the two savers to the checkpoint checkpoint.add(stateSaver1); checkpoint.add(stateSaver2); @@ -150,11 +168,6 @@ int the_main(int argc, char **argv) state.save(file_name); } - for (int i = 0; i < 100; ++i) - rng.rand(); - - cout << "a random number is " << rng.random(1024) << endl;; - return 1; } diff --git a/eo/test/t-eoExternalEO.cpp b/eo/test/t-eoExternalEO.cpp new file mode 100644 index 00000000..d750280e --- /dev/null +++ b/eo/test/t-eoExternalEO.cpp @@ -0,0 +1,127 @@ +// to avoid long name warnings +#ifdef _MSC_VER +#pragma warning(disable:4786) +#endif + +#include +#include // runtime_error + +#include +#include +#include + +using namespace std; + +struct UserDefStruct +{ + int a; + float b; + double c; + enum Enum { just, another, test } d; +}; + +ostream& operator<<(ostream& os, const UserDefStruct& str) +{ + return os << str.a << ' ' << str.b << ' ' << str.c << ' ' << static_cast(str.d) << ' '; +} + +istream& operator>>(istream& is, UserDefStruct& str) +{ + is >> str.a; + is >> str.b; + is >> str.c; + int i; + is >> i; + str.d = static_cast(i); + + return is; +} + + +UserDefStruct RandomStruct() +{ + cout << "RandomStruct\n"; + + UserDefStruct result; + + result.a = rng.random(5); + result.b = rng.uniform(); + result.c = rng.uniform(); + result.d = UserDefStruct::another; + + return result; +} + +// reading and writing + + +void UserDefMutate(UserDefStruct& a) +{ + cout << "UserDefMutate\n"; + a = RandomStruct(); // just for testing + + if (rng.flip(0.1f)) + a.d = UserDefStruct::test; + else + a.d = UserDefStruct::another; +} + +void UserDefBinCrossover(UserDefStruct& a, const UserDefStruct& b) +{ + cout << "UserDefBinCrossover\n"; + + if (rng.flip(0.5)) + a.a = b.a; + if (rng.flip(0.5)) + a.b = b.b; + if (rng.flip(0.5)) + a.c = b.c; + if (rng.flip(0.5)) + a.d = b.d; +} + +void UserDefQuadCrossover(UserDefStruct& a, UserDefStruct& b) +{ + cout << "UserDefQuadCrossover\n"; + if (rng.flip(0.5)) + swap(a.a, b.a); + if (rng.flip(0.5)) + swap(a.b, b.b); + if (rng.flip(0.5)) + swap(a.c, b.c); + if (rng.flip(0.5)) + swap(a.d, b.d); +} + +float UserDefEvalFunc(const UserDefStruct& a) +{ + cout << "UserDefEvalFunc\n"; + return a.b; +} + +int main() +{ + typedef UserDefStruct External; + typedef float FitnessType; + typedef eoExternalEO EoType; + + eoExternalInit init(RandomStruct); + eoExternalMonOp mutate(UserDefMutate); + eoExternalBinOp cross1(UserDefBinCrossover); + eoExternalQuadraticOp cross2(UserDefQuadCrossover); + + eoExternalEvalFunc eval(UserDefEvalFunc); + + EoType eo1 = init(); + EoType eo2 = init(); + + cout << "before mutation " << eo1 << '\n'; + mutate(eo1); + cout << "after mutation " << eo1 << '\n'; + cross1(eo1, eo2); + cout << "after crossover " << eo1 << '\n'; + + cross2(eo1,eo2); + + return 1; +}; \ No newline at end of file diff --git a/eo/test/t-eofitness.cpp b/eo/test/t-eofitness.cpp index f3161620..9973444a 100644 --- a/eo/test/t-eofitness.cpp +++ b/eo/test/t-eofitness.cpp @@ -1,87 +1,175 @@ -//----------------------------------------------------------------------------- -// t-eofitness.cpp -// (c) GeNeura Team 1998 -//----------------------------------------------------------------------------- - -#include // time -#include // srand, rand -#include // cout -#include // eoFitness - -//----------------------------------------------------------------------------- - -class eoFloat: public eoFitness -{ -public: - eoFloat(const float x) { fitness = x; } - eoFloat(const int x) { fitness = static_cast(x); } - - bool operator<(const eoFitness& other) const - { - const eoFloat& x = (const eoFloat&) other; - return fitness < x.fitness; - } - - operator float() const - { - return fitness; - } - - void printOn(ostream& os) const - { - os << fitness; - } - - void readFrom(istream& is) - { - is >> fitness; - } - -private: - float fitness; -}; - -//----------------------------------------------------------------------------- - -main() -{ - srand(time(0)); - - eoFloat a = static_cast(rand()) / RAND_MAX, - b = static_cast(rand()) / RAND_MAX; - - cout.precision(2); - - unsigned repeat = 2; - while (repeat--) - { - cout << "------------------------------------------------------" << endl; - cout << "testing < "; - if (a < b) - cout << a << " < " << b << " is true" << endl; - else - cout << a << " < " << b << " is false" < "; - if (a > b) - cout << a << " > " << b << " is true" << endl; - else - cout << a << " > " << b << " is false" < // time + +#include // srand, rand + +#include // cout + +#include // eoFitness + + + +//----------------------------------------------------------------------------- + + + +class eoFloat: public eoFitness + +{ + +public: + + eoFloat(const float x) { fitness = x; } + + eoFloat(const int x) { fitness = static_cast(x); } + + + + bool operator<(const eoFitness& other) const + + { + + const eoFloat& x = (const eoFloat&) other; + + return fitness < x.fitness; + + } + + + + operator float() const + + { + + return fitness; + + } + + + + void printOn(ostream& os) const + + { + + os << fitness; + + } + + + + void readFrom(istream& is) + + { + + is >> fitness; + + } + + + +private: + + float fitness; + +}; + + + +//----------------------------------------------------------------------------- + + + +int main() + +{ + + srand(time(0)); + + + + eoFloat a = static_cast(rand()) / RAND_MAX, + + b = static_cast(rand()) / RAND_MAX; + + + + cout.precision(2); + + + + unsigned repeat = 2; + + while (repeat--) + + { + + cout << "------------------------------------------------------" << endl; + + cout << "testing < "; + + if (a < b) + + cout << a << " < " << b << " is true" << endl; + + else + + cout << a << " < " << b << " is false" < "; + + if (a > b) + + cout << a << " > " << b << " is true" << endl; + + else + + cout << a << " > " << b << " is false" <