Removed double line feeds

This commit is contained in:
mac 2000-02-19 17:37:18 +00:00
commit 6569496f6e

View file

@ -1,276 +1,277 @@
#pragma warning(disable:4786) #pragma warning(disable:4786)
#include "eoParseTree.h" #include "eoParseTree.h"
#include "eoEvalFunc.h" #include "eoEvalFunc.h"
using namespace gp_parse_tree; using namespace gp_parse_tree;
using namespace std; using namespace std;
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
class SymregNode class SymregNode
{ {
public : public :
enum Operator {X = 'x', Plus = '+', Min = '-', Mult = '*', PDiv = '/'}; enum Operator {X = 'x', Plus = '+', Min = '-', Mult = '*', PDiv = '/'};
SymregNode(void) { init(); } SymregNode(void) { init(); }
SymregNode(Operator _op) { op = _op; } SymregNode(Operator _op) { op = _op; }
virtual ~SymregNode(void) {} virtual ~SymregNode(void) {}
// arity function // arity function
int arity(void) const { return op == X? 0 : 2; } int arity(void) const { return op == X? 0 : 2; }
// evaluation function, single case, using first argument to give value of variable // evaluation function, single case, using first argument to give value of variable
template <class Children> template <class Children>
double operator()(double var, Children args) const double operator()(double var, Children args) const
{ {
switch (op) switch (op)
{ {
case Plus : return args[0].apply(var) + args[1].apply(var); case Plus : return args[0].apply(var) + args[1].apply(var);
case Min : return args[0].apply(var) - args[1].apply(var); case Min : return args[0].apply(var) - args[1].apply(var);
case Mult : return args[0].apply(var) * args[1].apply(var); case Mult : return args[0].apply(var) * args[1].apply(var);
case PDiv : case PDiv :
{ {
double arg1 = args[1].apply(var); double arg1 = args[1].apply(var);
if (arg1 == 0.0) if (arg1 == 0.0)
return 1.0; // protection a la Koza, realistic implementations should maybe throw an exception return 1.0; // protection a la Koza, realistic implementations should maybe throw an exception
return args[0].apply(var) / arg1; return args[0].apply(var) / arg1;
} }
case X : return var; case X : return var;
} }
return var; // to avoid compiler error return var; // to avoid compiler error
} }
/// 'Pretty' print to ostream function /// 'Pretty' print to ostream function
template <class Children> template <class Children>
string operator()(string dummy, Children args) string operator()(string dummy, Children args)
{ {
static const string lb = "("; static const string lb = "(";
static const string rb = ")"; static const string rb = ")";
char opStr[4] = " "; char opStr[4] = " ";
opStr[1] = op; opStr[1] = op;
if (arity() == 0) if (arity() == 0)
{ {
return string("x"); return string("x");
} }
// else // else
string result = lb + args[0].apply(dummy); string result = lb + args[0].apply(dummy);
result += opStr; result += opStr;
result += args[1].apply(dummy) + rb; result += args[1].apply(dummy) + rb;
return result; return result;
} }
Operator getOp(void) const { return op; } Operator getOp(void) const { return op; }
protected : protected :
void init(void) { op = X; } void init(void) { op = X; }
private : private :
Operator op; // the type of node Operator op; // the type of node
}; };
/// initializor /// initializor
static SymregNode init_sequence[5] = {SymregNode::X, SymregNode::Plus, SymregNode::Min, SymregNode::Mult, SymregNode::PDiv}; // needed for intialization static SymregNode init_sequence[5] = {SymregNode::X, SymregNode::Plus, SymregNode::Min, SymregNode::Mult, SymregNode::PDiv}; // needed for intialization
//----------------------------------------------------------- //-----------------------------------------------------------
// saving, loading // saving, loading
std::ostream& operator<<(std::ostream& os, const SymregNode& eot) std::ostream& operator<<(std::ostream& os, const SymregNode& eot)
{ {
os << static_cast<char>(eot.getOp()); os << static_cast<char>(eot.getOp());
return os; return os;
} }
std::istream& operator>>(std::istream& is, SymregNode& eot) std::istream& operator>>(std::istream& is, SymregNode& eot)
{ {
char type; char type;
type = (char) is.get(); type = (char) is.get();
eot = SymregNode(static_cast<SymregNode::Operator>(type)); eot = SymregNode(static_cast<SymregNode::Operator>(type));
return is; return is;
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
/** Implementation of a function evaluation object. */ /** Implementation of a function evaluation object. */
float targetFunction(float x) float targetFunction(float x)
{ {
return x * x * x * x - x * x * x + x * x * x - x * x + x - 1; return x * x * x * x - x * x * x + x * x * x - x * x + x - 1;
} }
// parameters controlling the sampling of points // parameters controlling the sampling of points
const float xbegin = -10.0f; const float xbegin = -10.0f;
const float xend = 10.0f; const float xend = 10.0f;
const float xstep = 1.3f; const float 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 : public :
typedef eoParseTree<FType, Node> EoType; typedef eoParseTree<FType, Node> EoType;
typedef eoParseTree<FType, Node> argument_type; typedef eoParseTree<FType, Node> argument_type;
typedef double fitness_type; typedef double fitness_type;
RMS(void) : eoEvalFunc<EoType>() RMS(void) : eoEvalFunc<EoType>()
{ {
int n = int( (xend - xbegin) / xstep); int n = int( (xend - xbegin) / xstep);
inputs.resize(n); inputs.resize(n);
target.resize(n); target.resize(n);
int i = 0; int i = 0;
for (double x = xbegin; x < xend && i < n; ++i) for (double x = xbegin; x < xend && i < n; ++i)
{ {
target[i] = targetFunction(x); target[i] = targetFunction(x);
inputs[i] = x; inputs[i] = x;
} }
} }
~RMS() {} ~RMS() {}
void operator()( EoType & _eo ) const void operator()( EoType & _eo ) const
{ {
vector<double> outputs; vector<double> outputs;
outputs.resize(inputs.size()); outputs.resize(inputs.size());
double fitness = 0.0; double fitness = 0.0;
for (int i = 0; i < inputs.size(); ++i) for (int i = 0; i < inputs.size(); ++i)
{ {
outputs[i] = _eo.apply(inputs[i]); outputs[i] = _eo.apply(inputs[i]);
fitness += (outputs[i] - target[i]) * (outputs[i] - target[i]); fitness += (outputs[i] - target[i]) * (outputs[i] - target[i]);
} }
fitness /= (double) target.size(); fitness /= (double) target.size();
fitness = sqrt(fitness); fitness = sqrt(fitness);
if (fitness > 1e+20) if (fitness > 1e+20)
fitness = 1e+20; fitness = 1e+20;
_eo.fitness(fitness); _eo.fitness(fitness);
} }
private : private :
vector<double> inputs; vector<double> inputs;
vector<double> target; vector<double> target;
}; };
#include "eoTerm.h" #include "eoTerm.h"
template <class EOT> template <class EOT>
class eoGenerationTerm : public eoTerm<EOT> class eoGenerationTerm : public eoTerm<EOT>
{ {
public : public :
eoGenerationTerm(size_t _ngen) : eoTerm<EOT>(), ngen(_ngen) {} eoGenerationTerm(size_t _ngen) : eoTerm<EOT>(), ngen(_ngen) {}
bool operator()(const eoPop<EOT>&) bool operator()(const eoPop<EOT>&)
{ {
cout << '.'; // pacifier cout << '.'; // pacifier
cout.flush(); cout.flush();
return --ngen > 0; return --ngen > 0;
} }
private : private :
unsigned ngen; unsigned ngen;
}; };
template <class EOT, class FitnessType> template <class EOT, class FitnessType>
void print_best(eoPop<EOT>& pop) void print_best(eoPop<EOT>& pop)
{ {
cout << endl; cout << endl;
FitnessType best = pop[0].fitness(); FitnessType best = pop[0].fitness();
int index = 0; int index = 0;
for (int i = 1; i < pop.size(); ++i) for (int i = 1; i < pop.size(); ++i)
{ {
if (best < pop[i].fitness()) if (best < pop[i].fitness())
{ {
best = pop[i].fitness(); best = pop[i].fitness();
index = i; index = i;
} }
} }
cout << "\t"; cout << "\t";
string str = pop[index].apply(string()); string str = pop[index].apply(string());
cout << str.c_str(); cout << str.c_str();
cout << endl << "Error = " << pop[index].fitness() << endl; cout << endl << "Error = " << pop[index].fitness() << endl;
} }
#include <eo> #include <eo>
#include "eoGOpBreeder.h" #include "eoGOpBreeder.h"
#include "eoSequentialGOpSelector.h" #include "eoSequentialGOpSelector.h"
#include "eoProportionalGOpSelector.h" #include "eoProportionalGOpSelector.h"
#include "eoDetTournamentIndiSelector.h" #include "eoDetTournamentIndiSelector.h"
#include "eoDetTournamentInserter.h" #include "eoDetTournamentInserter.h"
#include "eoSteadyStateEA.h" #include "eoSteadyStateEA.h"
#include "eoScalarFitness.h" #include "eoScalarFitness.h"
void main() void main()
{ {
typedef eoScalarFitness<double, greater<double> > FitnessType; typedef eoScalarFitness<double, greater<double> > FitnessType;
typedef SymregNode GpNode; typedef SymregNode GpNode;
typedef eoParseTree<FitnessType, GpNode> EoType; typedef eoParseTree<FitnessType, GpNode> EoType;
typedef eoPop<EoType> Pop; typedef eoPop<EoType> Pop;
const int MaxSize = 75; const int MaxSize = 75;
const int nGenerations = 50; const int nGenerations = 50;
// Initializor sequence, contains the allowable nodes // Initializor sequence, contains the allowable nodes
vector<GpNode> init(init_sequence, init_sequence + 5); vector<GpNode> init(init_sequence, init_sequence + 5);
// Depth Initializor, defaults to grow method. // Depth Initializor, defaults to grow method.
eoGpDepthInitializer<FitnessType, GpNode> initializer(10, init); eoGpDepthInitializer<FitnessType, GpNode> initializer(10, init);
// Root Mean Squared Error Measure // Root Mean Squared Error Measure
RMS<FitnessType, GpNode> eval; RMS<FitnessType, GpNode> eval;
Pop pop(500, MaxSize, initializer, eval); Pop pop(500, MaxSize, initializer, eval);
eoSubtreeXOver<FitnessType, GpNode> xover(MaxSize); eoSubtreeXOver<FitnessType, GpNode> xover(MaxSize);
eoBranchMutation<FitnessType, GpNode> mutation(initializer, MaxSize); eoBranchMutation<FitnessType, GpNode> mutation(initializer, MaxSize);
eoSequentialGOpSel<EoType> seqSel; eoSequentialGOpSel<EoType> seqSel;
seqSel.addOp(mutation, 0.25); seqSel.addOp(mutation, 0.25);
seqSel.addOp(xover, 0.75); seqSel.addOp(xover, 0.75);
eoDetTournamentIndiSelector<EoType> selector(5); eoDetTournamentIndiSelector<EoType> selector(5);
eoDetTournamentInserter<EoType> inserter(eval, 5); eoDetTournamentInserter<EoType> inserter(eval, 5);
// Terminators // Terminators
eoGenerationTerm<EoType> term(nGenerations); eoGenerationTerm<EoType> term(nGenerations);
// GP generation // GP generation
eoSteadyStateEA<EoType> gp(seqSel, selector, inserter, term); eoSteadyStateEA<EoType> gp(seqSel, selector, inserter, term);
cout << "Initialization done" << endl; cout << "Initialization done" << endl;
print_best<EoType, FitnessType>(pop); print_best<EoType, FitnessType>(pop);
try try
{ {
gp(pop); gp(pop);
} }
catch (exception& e) catch (exception& e)
{ {
cout << "exception: " << e.what() << endl;; cout << "exception: " << e.what() << endl;;
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
print_best<EoType, FitnessType>(pop); print_best<EoType, FitnessType>(pop);
} }