Updated makefile and added t-eoExternalEO
This commit is contained in:
parent
230e1a03d1
commit
05c4bec808
5 changed files with 337 additions and 98 deletions
|
|
@ -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)
|
||||
|
||||
###############################################################################
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -75,28 +75,46 @@ int the_main(int argc, char **argv)
|
|||
|
||||
eoDummyPop pop;
|
||||
|
||||
eoGenTerm<EoType> genTerm(5); // 5 generations
|
||||
eoGenTerm<EoType> genTerm(5); // run for 5 generations
|
||||
|
||||
eoCheckPoint<EoType> checkpoint(genTerm);
|
||||
eoCheckPoint<EoType> checkpoint(genTerm);
|
||||
// The algorithm will now quit after five generations
|
||||
|
||||
// Create a counter parameter
|
||||
eoValueParam<unsigned> 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<unsigned> 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<EoType> 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;
|
||||
}
|
||||
|
||||
|
|
|
|||
127
eo/test/t-eoExternalEO.cpp
Normal file
127
eo/test/t-eoExternalEO.cpp
Normal file
|
|
@ -0,0 +1,127 @@
|
|||
// to avoid long name warnings
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(disable:4786)
|
||||
#endif
|
||||
|
||||
#include <iostream>
|
||||
#include <stdexcept> // runtime_error
|
||||
|
||||
#include <eoEvalFuncPtr.h>
|
||||
#include <other/external_eo>
|
||||
#include <utils/eoRNG.h>
|
||||
|
||||
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<int>(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<UserDefStruct::Enum>(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<float, External> EoType;
|
||||
|
||||
eoExternalInit<FitnessType, External> init(RandomStruct);
|
||||
eoExternalMonOp<FitnessType, External> mutate(UserDefMutate);
|
||||
eoExternalBinOp<FitnessType, External> cross1(UserDefBinCrossover);
|
||||
eoExternalQuadraticOp<FitnessType, External> cross2(UserDefQuadCrossover);
|
||||
|
||||
eoExternalEvalFunc<FitnessType, External> 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;
|
||||
};
|
||||
|
|
@ -1,87 +1,175 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// t-eofitness.cpp
|
||||
// (c) GeNeura Team 1998
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <time.h> // time
|
||||
#include <stdlib.h> // srand, rand
|
||||
#include <iostream> // cout
|
||||
#include <eo> // eoFitness
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
class eoFloat: public eoFitness
|
||||
{
|
||||
public:
|
||||
eoFloat(const float x) { fitness = x; }
|
||||
eoFloat(const int x) { fitness = static_cast<float>(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<float>(rand()) / RAND_MAX,
|
||||
b = static_cast<float>(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" <<endl;
|
||||
|
||||
cout << "testing > ";
|
||||
if (a > b)
|
||||
cout << a << " > " << b << " is true" << endl;
|
||||
else
|
||||
cout << a << " > " << b << " is false" <<endl;
|
||||
|
||||
cout << "testing == ";
|
||||
if (a == b)
|
||||
cout << a << " == " << b << " is true" << endl;
|
||||
else
|
||||
cout << a << " == " << b << " is false" <<endl;
|
||||
|
||||
cout << "testing != ";
|
||||
if (a != b)
|
||||
cout << a << " != " << b << " is true" << endl;
|
||||
else
|
||||
cout << a << " != " << b << " is false" <<endl;
|
||||
|
||||
a = b;
|
||||
}
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
// t-eofitness.cpp
|
||||
|
||||
// (c) GeNeura Team 1998
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
#include <time.h> // time
|
||||
|
||||
#include <stdlib.h> // srand, rand
|
||||
|
||||
#include <iostream> // cout
|
||||
|
||||
#include <eo> // eoFitness
|
||||
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
class eoFloat: public eoFitness
|
||||
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
eoFloat(const float x) { fitness = x; }
|
||||
|
||||
eoFloat(const int x) { fitness = static_cast<float>(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<float>(rand()) / RAND_MAX,
|
||||
|
||||
b = static_cast<float>(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" <<endl;
|
||||
|
||||
|
||||
|
||||
cout << "testing > ";
|
||||
|
||||
if (a > b)
|
||||
|
||||
cout << a << " > " << b << " is true" << endl;
|
||||
|
||||
else
|
||||
|
||||
cout << a << " > " << b << " is false" <<endl;
|
||||
|
||||
|
||||
|
||||
cout << "testing == ";
|
||||
|
||||
if (a == b)
|
||||
|
||||
cout << a << " == " << b << " is true" << endl;
|
||||
|
||||
else
|
||||
|
||||
cout << a << " == " << b << " is false" <<endl;
|
||||
|
||||
|
||||
|
||||
cout << "testing != ";
|
||||
|
||||
if (a != b)
|
||||
|
||||
cout << a << " != " << b << " is true" << endl;
|
||||
|
||||
else
|
||||
|
||||
cout << a << " != " << b << " is false" <<endl;
|
||||
|
||||
|
||||
|
||||
a = b;
|
||||
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ const unsigned Easy::size = 1;
|
|||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
main()
|
||||
int main()
|
||||
{
|
||||
Easy easy;
|
||||
Chrom chrom(Easy::size);
|
||||
|
|
@ -48,6 +48,8 @@ main()
|
|||
|
||||
cout << "chrom = " << chrom << endl
|
||||
<< "chrom.fitness() = " << chrom.fitness() << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
|
|
|||
Reference in a new issue