Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | Related Pages | Search for

# t-eoExternalEO.cpp

    00001 // to avoid long name warnings
    00002 #ifdef _MSC_VER
    00003 #pragma warning(disable:4786)
    00004 #endif
    00005
    00006 #include <iostream>
    00007 #include <stdexcept>  // runtime_error
    00008
    00009 #include <eoEvalFuncPtr.h>
    00010 #include <other/external_eo>
    00011 #include <utils/eoRNG.h>
    00012
    00013 using namespace std;
    00014
    00015 struct UserDefStruct
    00016 {
    00017     int a;
    00018     float b;
    00019     double c;
    00020     enum Enum { just, another, test } d;
    00021 };
    00022
    00023 std::ostream& operator<<(std::ostream& os, const UserDefStruct& str)
    00024 {
    00025     return os << str.a << ' ' << str.b << ' ' << str.c << ' ' << static_cast<int>(str.d) << ' ';
    00026 }
    00027
    00028 istream& operator>>(istream& is, UserDefStruct& str)
    00029 {
    00030     is >> str.a;
    00031     is >> str.b;
    00032     is >> str.c;
    00033     int i;
    00034     is >> i;
    00035     str.d = static_cast<UserDefStruct::Enum>(i);
    00036
    00037     return is;
    00038 }
    00039
    00040
    00041 UserDefStruct RandomStruct()
    00042 {
    00043     std::cout << "RandomStruct\n";
    00044
    00045     UserDefStruct result;
    00046
    00047     result.a = rng.random(5);
    00048     result.b = rng.uniform();
    00049     result.c = rng.uniform();
    00050     result.d = UserDefStruct::another;
    00051
    00052     return result;
    00053 }
    00054
    00055 // reading and writing
    00056
    00057
    00058 bool UserDefMutate(UserDefStruct& a)
    00059 {
    00060     std::cout << "UserDefMutate\n";
    00061     a = RandomStruct(); // just for testing
    00062
    00063     if (rng.flip(0.1f))
    00064         a.d = UserDefStruct::test;
    00065     else
    00066         a.d = UserDefStruct::another;
    00067     return true;
    00068 }
    00069
    00070 bool UserDefBinCrossover(UserDefStruct& a, const UserDefStruct& b)
    00071 {
    00072     std::cout << "UserDefBinCrossover\n";
    00073
    00074     if (rng.flip(0.5))
    00075         a.a = b.a;
    00076     if (rng.flip(0.5))
    00077         a.b = b.b;
    00078     if (rng.flip(0.5))
    00079         a.c = b.c;
    00080     if (rng.flip(0.5))
    00081         a.d = b.d;
    00082     return true;
    00083 }
    00084
    00085 bool UserDefQuadCrossover(UserDefStruct& a, UserDefStruct& b)
    00086 {
    00087     std::cout << "UserDefQuadCrossover\n";
    00088     if (rng.flip(0.5))
    00089         swap(a.a, b.a);
    00090     if (rng.flip(0.5))
    00091         swap(a.b, b.b);
    00092     if (rng.flip(0.5))
    00093         swap(a.c, b.c);
    00094     if (rng.flip(0.5))
    00095         swap(a.d, b.d);
    00096
    00097     return true;
    00098 }
    00099
    00100 float UserDefEvalFunc(const UserDefStruct& a)
    00101 {
    00102     std::cout << "UserDefEvalFunc\n";
    00103     return a.b;
    00104 }
    00105
    00106 int main()
    00107 {
    00108     typedef UserDefStruct External;
    00109     typedef float FitnessType;
    00110     typedef eoExternalEO<float, External> EoType;
    00111
    00112     eoExternalInit<FitnessType, External> init(RandomStruct);
    00113     eoExternalMonOp<FitnessType, External> mutate(UserDefMutate);
    00114     eoExternalBinOp<FitnessType, External> cross1(UserDefBinCrossover);
    00115     eoExternalQuadOp<FitnessType, External> cross2(UserDefQuadCrossover);
    00116
    00117     // eoExternalEvalFunc<FitnessType, External>   eval(UserDefEvalFunc);
    00118
    00119     EoType eo1;
    00120     init(eo1);
    00121     EoType eo2;
    00122     init(eo2);
    00123
    00124     std::cout << "before mutation " << eo1 << '\n';
    00125     mutate(eo1);
    00126     std::cout << "after mutation " << eo1 << '\n';
    00127     cross1(eo1, eo2);
    00128     std::cout << "after crossover " << eo1 << '\n';
    00129
    00130     cross2(eo1,eo2);
    00131
    00132 };