* eoTimedMonitor.h (eoTimedMonitor::seconds): Make unsigned.
* eoRNG.cpp, eoRNG.h (K, M, N): Declare static and initialize in cpp. * t-eoGenOp.cpp (init): Do not add std::ends to end of string, as this results in escape-codes (^@) to be printed at runtime and is not necessary anyway. * test/t-eoSymreg.cpp (SymregNode::operator()): Initialize r1 and r2 to avoid compiler warnings.
This commit is contained in:
parent
da50bf0986
commit
7baf7cb799
6 changed files with 114 additions and 84 deletions
|
|
@ -1,5 +1,7 @@
|
|||
2006-12-02 Jochen Küpper <jochen@fhi-berlin.mpg.de>
|
||||
|
||||
* eoTimedMonitor.h (eoTimedMonitor::seconds): Make unsigned.
|
||||
|
||||
* eoRNG.h: Cleanup docs and document /all/ members.
|
||||
|
||||
* eoRNG.cpp, eoRNG.h (K, M, N): Declare static and initialize in cpp.
|
||||
|
|
|
|||
|
|
@ -6,13 +6,13 @@
|
|||
#include <ctime>
|
||||
#include "eoRNG.h"
|
||||
|
||||
// initialize static constants
|
||||
const uint32_t eoRng::K(0x9908B0DFU);
|
||||
const int eoRng::M(397);
|
||||
const int eoRng::N(624);
|
||||
|
||||
namespace eo
|
||||
{
|
||||
// initialize static constants
|
||||
const uint32_t eoRng::K(0x9908B0DFU);
|
||||
const int eoRng::M(397);
|
||||
const int eoRng::N(624);
|
||||
|
||||
// global random number generator object
|
||||
eoRng rng(static_cast<uint32_t>(time(0)));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,11 +27,11 @@
|
|||
#ifndef _eoTimedMonitor_h
|
||||
#define _eoTimedMonitor_h
|
||||
|
||||
#include <ctime>
|
||||
#include <string>
|
||||
|
||||
#include <utils/eoMonitor.h>
|
||||
#include <eoObject.h>
|
||||
#include <time.h>
|
||||
|
||||
/**
|
||||
Holds a collection of monitors and only fires them when a time limit
|
||||
|
|
@ -39,8 +39,14 @@
|
|||
*/
|
||||
class eoTimedMonitor : public eoMonitor
|
||||
{
|
||||
public :
|
||||
eoTimedMonitor(int seconds_) : last_tick(0), seconds(seconds_) {}
|
||||
public:
|
||||
|
||||
/** Constructor
|
||||
|
||||
No negative time can be specified, use 0 if you want it to fire "always".
|
||||
@param seconds_ Specify time limit (s).
|
||||
*/
|
||||
eoTimedMonitor(unsigned seconds_) : last_tick(0), seconds(seconds_) {}
|
||||
|
||||
eoMonitor& operator()(void) {
|
||||
bool monitor = false;
|
||||
|
|
@ -67,10 +73,14 @@ public :
|
|||
void add(eoMonitor& mon) { monitors.push_back(&mon); }
|
||||
|
||||
virtual std::string className(void) const { return "eoTimedMonitor"; }
|
||||
private :
|
||||
clock_t last_tick;
|
||||
int seconds;
|
||||
std::vector<eoMonitor*> monitors;
|
||||
|
||||
private:
|
||||
|
||||
clock_t last_tick;
|
||||
|
||||
unsigned seconds;
|
||||
|
||||
std::vector<eoMonitor*> monitors;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
|||
15
eo/test/ChangeLog
Normal file
15
eo/test/ChangeLog
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
2006-12-02 Jochen Küpper <jochen@fhi-berlin.mpg.de>
|
||||
|
||||
* t-eoGenOp.cpp (init): Do not add std::ends to end of string, as this
|
||||
results in escape-codes (^@) to be printed at runtime and is not
|
||||
necessary anyway.
|
||||
|
||||
* test/t-eoSymreg.cpp (SymregNode::operator()): Initialize r1 and r2 to
|
||||
avoid compiler warnings.
|
||||
|
||||
|
||||
* Local Variables:
|
||||
* coding: iso-8859-1
|
||||
* mode: flyspell
|
||||
* fill-column: 80
|
||||
* End:
|
||||
|
|
@ -35,16 +35,15 @@
|
|||
|
||||
struct Dummy : public EO<double>
|
||||
{
|
||||
typedef double Type;
|
||||
Dummy(std::string _s="") : s(_s) {}
|
||||
Dummy(std::string _s="") : s(_s) {}
|
||||
|
||||
void printOn(std::ostream & _os) const
|
||||
{
|
||||
EO<double>::printOn(_os);
|
||||
_os << " - " << s ;
|
||||
}
|
||||
void printOn(std::ostream & _os) const
|
||||
{
|
||||
EO<double>::printOn(_os);
|
||||
_os << " - " << s ;
|
||||
}
|
||||
|
||||
std::string s;
|
||||
std::string s;
|
||||
};
|
||||
|
||||
typedef Dummy EOT;
|
||||
|
|
@ -180,18 +179,18 @@ void init(eoPop<Dummy> & _pop, unsigned _pSize)
|
|||
for (unsigned i=0; i<_pSize; i++)
|
||||
{
|
||||
std::ostringstream os;
|
||||
os << i << std::ends;
|
||||
_pop[i] = Dummy(os.str());
|
||||
_pop[i].fitness(i);
|
||||
os << i;
|
||||
_pop[i] = Dummy(os.str());
|
||||
_pop[i].fitness(i);
|
||||
}
|
||||
}
|
||||
|
||||
// ok, now for the real work
|
||||
int the_main(int argc, char **argv)
|
||||
{
|
||||
|
||||
eoParser parser(argc, argv);
|
||||
eoValueParam<unsigned int> parentSizeParam = parser.createParam(unsigned(10), "parentSize", "Parent size",'P');
|
||||
eoParser parser(argc, argv);
|
||||
eoValueParam<unsigned int> parentSizeParam(
|
||||
parser.createParam(unsigned(10), "parentSize", "Parent size",'P'));
|
||||
pSize = parentSizeParam.value(); // global variable
|
||||
|
||||
eoValueParam<uint32_t> seedParam(time(0), "seed", "Random number seed", 'S');
|
||||
|
|
@ -269,7 +268,7 @@ int the_main(int argc, char **argv)
|
|||
init(pop, pSize);
|
||||
// sort pop so seqPopulator is identical to SelectPopulator(SequentialSelect)
|
||||
pop.sort();
|
||||
std::cout << "Population initiale\n" << pop << std::endl;
|
||||
std::cout << "Population initiale" << std::endl << pop << std::endl;
|
||||
|
||||
// To simulate SGA: first a prop between quadOp and quadClone
|
||||
eoProportionalOp<EOT> pSGAOp;
|
||||
|
|
|
|||
|
|
@ -16,44 +16,41 @@ public :
|
|||
|
||||
enum Operator {X = 'x', Plus = '+', Min = '-', Mult = '*', PDiv = '/'};
|
||||
|
||||
SymregNode(void) { init(); }
|
||||
SymregNode(Operator _op) { op = _op; }
|
||||
virtual ~SymregNode(void) {}
|
||||
SymregNode() { init(); }
|
||||
SymregNode(Operator _op) { op = _op; }
|
||||
virtual ~SymregNode() {}
|
||||
|
||||
// arity function, need this function!
|
||||
int arity(void) const { return op == X? 0 : 2; }
|
||||
int arity() const { return op == X? 0 : 2; }
|
||||
|
||||
void randomize() {}
|
||||
|
||||
void randomize(void) {}
|
||||
|
||||
// evaluation function, single case, using first argument to give value of variable
|
||||
template <class Children>
|
||||
void operator()(double& result, Children args, double var) const
|
||||
{
|
||||
double r1, r2;
|
||||
|
||||
if (arity() == 2)
|
||||
{
|
||||
args[0].apply(r1, var);
|
||||
args[1].apply(r2, var);
|
||||
}
|
||||
|
||||
switch (op)
|
||||
{
|
||||
case Plus : result = r1 + r2; break;
|
||||
case Min : result = r1 - r2; break;
|
||||
case Mult : result = r1 * r2; break;
|
||||
case PDiv :
|
||||
{
|
||||
if (r2 == 0.0)
|
||||
result = 1.0; // protection a la Koza, realistic implementations should maybe throw an exception
|
||||
double r1(0.), r2(0.);
|
||||
if (arity() == 2)
|
||||
{
|
||||
args[0].apply(r1, var);
|
||||
args[1].apply(r2, var);
|
||||
}
|
||||
switch (op)
|
||||
{
|
||||
case Plus : result = r1 + r2; break;
|
||||
case Min : result = r1 - r2; break;
|
||||
case Mult : result = r1 * r2; break;
|
||||
case PDiv : {
|
||||
if (r2 == 0.0)
|
||||
// protection a la Koza, realistic implementations
|
||||
// should maybe throw an exception
|
||||
result = 1.0;
|
||||
else
|
||||
result = r1 / r2;
|
||||
result = r1 / r2;
|
||||
break;
|
||||
}
|
||||
|
||||
case X : result = var; break;
|
||||
}
|
||||
|
||||
}
|
||||
case X : result = var; break;
|
||||
}
|
||||
}
|
||||
|
||||
/// 'Pretty' print to ostream function
|
||||
|
|
@ -64,7 +61,7 @@ public :
|
|||
static const string rb = ")";
|
||||
char opStr[4] = " ";
|
||||
opStr[1] = op;
|
||||
|
||||
|
||||
if (arity() == 0)
|
||||
{
|
||||
result = "x";
|
||||
|
|
@ -74,16 +71,16 @@ public :
|
|||
string r1;
|
||||
args[0].apply(r1);
|
||||
result = lb + r1;
|
||||
result += opStr;
|
||||
result += opStr;
|
||||
args[1].apply(r1);
|
||||
result += r1 + rb;
|
||||
}
|
||||
|
||||
Operator getOp(void) const { return op; }
|
||||
Operator getOp() const { return op; }
|
||||
|
||||
protected :
|
||||
|
||||
void init(void) { op = X; }
|
||||
void init() { op = X; }
|
||||
|
||||
private :
|
||||
|
||||
|
|
@ -98,9 +95,9 @@ static SymregNode init_sequence[5] = {SymregNode::X, SymregNode::Plus, SymregNod
|
|||
// 2 months later, it seems it does not accept this definition ...
|
||||
// but dies accept the lt_arity<Node> in eoParseTreeDepthInit
|
||||
// !!!
|
||||
// #ifdef _MSC_VER
|
||||
// #ifdef _MSC_VER
|
||||
// template <>
|
||||
// bool lt_arity(const SymregNode &node1, const SymregNode &node2)
|
||||
// bool lt_arity(const SymregNode &node1, const SymregNode &node2)
|
||||
// {
|
||||
// return (node1.arity() < node2.arity());
|
||||
// }
|
||||
|
|
@ -128,16 +125,16 @@ std::istream& operator>>(std::istream& is, SymregNode& eot)
|
|||
/** Implementation of a function evaluation object. */
|
||||
|
||||
double targetFunction(double x)
|
||||
{
|
||||
return x * x * x * x - x * x * x + x * x * x - x * x + x - 10;
|
||||
{
|
||||
return x * x * x * x - x * x * x + x * x * x - x * x + x - 10;
|
||||
}
|
||||
|
||||
// parameters controlling the sampling of points
|
||||
const double xbegin = -10.0f;
|
||||
const double xend = 10.0f;
|
||||
const double xstep = 1.3f;
|
||||
const double xstep = 1.3f;
|
||||
|
||||
template <class FType, class Node> struct RMS: public eoEvalFunc< eoParseTree<FType, Node> >
|
||||
template <class FType, class Node> struct RMS: public eoEvalFunc< eoParseTree<FType, Node> >
|
||||
{
|
||||
public :
|
||||
|
||||
|
|
@ -146,10 +143,10 @@ public :
|
|||
typedef eoParseTree<FType, Node> argument_type;
|
||||
typedef double fitness_type;
|
||||
|
||||
RMS(void) : eoEvalFunc<EoType>()
|
||||
RMS() : eoEvalFunc<EoType>()
|
||||
{
|
||||
int n = int( (xend - xbegin) / xstep);
|
||||
|
||||
|
||||
inputs.resize(n);
|
||||
target.resize(n);
|
||||
|
||||
|
|
@ -163,23 +160,23 @@ public :
|
|||
}
|
||||
|
||||
~RMS() {}
|
||||
|
||||
void operator()( EoType & _eo )
|
||||
|
||||
void operator()( EoType & _eo )
|
||||
{
|
||||
vector<double> outputs;
|
||||
outputs.resize(inputs.size());
|
||||
|
||||
|
||||
double fitness = 0.0;
|
||||
|
||||
|
||||
for (unsigned i = 0; i < inputs.size(); ++i)
|
||||
{
|
||||
_eo.apply(outputs[i], inputs[i]);
|
||||
fitness += (outputs[i] - target[i]) * (outputs[i] - target[i]);
|
||||
}
|
||||
|
||||
|
||||
fitness /= (double) target.size();
|
||||
fitness = sqrt(fitness);
|
||||
|
||||
|
||||
if (fitness > 1e+20)
|
||||
fitness = 1e+20;
|
||||
|
||||
|
|
@ -206,12 +203,12 @@ void print_best(eoPop<EOT>& pop)
|
|||
index = i;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
std::cout << "\t";
|
||||
|
||||
|
||||
string str;
|
||||
pop[index].apply(str);
|
||||
|
||||
|
||||
std::cout << str.c_str();
|
||||
std::cout << std::endl << "RMS Error = " << pop[index].fitness() << std::endl;
|
||||
}
|
||||
|
|
@ -222,7 +219,7 @@ int main()
|
|||
typedef SymregNode GpNode;
|
||||
|
||||
typedef eoParseTree<FitnessType, GpNode> EoType;
|
||||
typedef eoPop<EoType> Pop;
|
||||
typedef eoPop<EoType> Pop;
|
||||
|
||||
const int MaxSize = 100;
|
||||
const int nGenerations = 10; // only a test, so few generations
|
||||
|
|
@ -232,7 +229,7 @@ int main()
|
|||
|
||||
// Depth Initializor, defaults to grow method.
|
||||
eoGpDepthInitializer<FitnessType, GpNode> initializer(10, init);
|
||||
|
||||
|
||||
// Root Mean Squared Error Measure
|
||||
RMS<FitnessType, GpNode> eval;
|
||||
|
||||
|
|
@ -243,18 +240,18 @@ int main()
|
|||
eoSubtreeXOver<FitnessType, GpNode> xover(MaxSize);
|
||||
eoBranchMutation<FitnessType, GpNode> mutation(initializer, MaxSize);
|
||||
|
||||
// The operators are encapsulated into an eoTRansform object,
|
||||
// The operators are encapsulated into an eoTRansform object,
|
||||
// that performs sequentially crossover and mutation
|
||||
eoSGATransform<EoType> transform(xover, 0.75, mutation, 0.25);
|
||||
|
||||
// The robust tournament selection
|
||||
eoDetTournamentSelect<EoType> selectOne(2); // tSize in [2,POPSIZE]
|
||||
// is now encapsulated in a eoSelectMany: 2 at a time -> SteadyState
|
||||
eoSelectMany<EoType> select(selectOne,2, eo_is_an_integer);
|
||||
|
||||
eoSelectMany<EoType> select(selectOne,2, eo_is_an_integer);
|
||||
|
||||
// and the Steady-State replacement
|
||||
eoSSGAWorseReplacement<EoType> replace;
|
||||
|
||||
|
||||
// Terminators
|
||||
eoGenContinue<EoType> term(nGenerations);
|
||||
|
||||
|
|
@ -289,7 +286,14 @@ int main()
|
|||
}
|
||||
|
||||
print_best<EoType, FitnessType>(pop);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Local Variables:
|
||||
// coding: iso-8859-1
|
||||
// mode: C++
|
||||
// c-file-offsets: ((c . 0))
|
||||
// c-file-style: "Stroustrup"
|
||||
// fill-column: 80
|
||||
// End:
|
||||
|
|
|
|||
Reference in a new issue