ECF 1.5
main.cpp
1#include <ecf/ECF.h>
2//#include "GEPSymbRegEvalOp.h"
3#include "GPSymbRegEvalOp.h"
4#include "FileDataWriter.h"
5#include "WriteBest.h"
6
7#include "Sqrt.h"
8#include "Log.h"
9
10#include "IATree.h"
11#include "IAAdd.h"
12#include "IASub.h"
13#include "IAMul.h"
14#include "IADiv.h"
15#include "IASin.h"
16#include "IACos.h"
17#include "IASqrt.h"
18#include "IALog.h"
19#include "IATerminal.h"
20
21
22
23int main(int argc, char **argv)
24{
25 StateP state (new State);
26
27 // set the evaluation operator
29 DataWriter dataWriter;
30 op->dataWriter = &dataWriter;
31 state->setEvalOp(op);
32
33 TreeP tree = (TreeP) new Tree::Tree;
34 Tree::Primitives::PrimitiveP sqrtPrimitive = (Tree::Primitives::PrimitiveP) new Tree::Primitives::Sqrt;
35 tree->addFunction(sqrtPrimitive);
36 Tree::Primitives::PrimitiveP logPrimitive = (Tree::Primitives::PrimitiveP) new Tree::Primitives::Log;
37 tree->addFunction(logPrimitive);
38 state->addGenotype(tree);
39
40 Tree::IATreeP iaTree(new Tree::IATree);
41 // create new functions and add them to function set
42 Tree::PrimitiveP iaAdd(new IAAdd);
43 iaTree->addFunction(iaAdd);
44 Tree::PrimitiveP iaSub(new IASub);
45 iaTree->addFunction(iaSub);
46 Tree::PrimitiveP iaMul(new IAMul);
47 iaTree->addFunction(iaMul);
48 Tree::PrimitiveP iaDiv(new IADiv);
49 iaTree->addFunction(iaDiv);
50 Tree::PrimitiveP iaSin(new IASin);
51 iaTree->addFunction(iaSin);
52 Tree::PrimitiveP iaCos(new IACos);
53 iaTree->addFunction(iaCos);
54 Tree::PrimitiveP iaSqrt(new IASqrt);
55 iaTree->addFunction(iaSqrt);
56 Tree::PrimitiveP iaLog(new IALog);
57 iaTree->addFunction(iaLog);
58
59 // custom type terminals
60 for(uint i = 0; i < 20; i++) {
61 Tree::PrimitiveP myTerm = (Tree::PrimitiveP) new IATerminal;
62 std::string name = "x" + uint2str(i);
63 myTerm->setName(name);
64 iaTree->addTerminal(myTerm);
65 }
66
67 state->addGenotype(iaTree);
68
69 // add writeout of best individual each generation
70 state->addOperator((OperatorP) new WriteBest);
71
72 // initialize & run
73 state->initialize(argc, argv);
74
75 if (argc >= 4) {
76 // read best solution from the file (argv[2]), evaluate it on test data and save to out file (argv[3])
77 XMLNode xInd = XMLNode::parseFile(argv[2], "Individual");
78 IndividualP ind = (IndividualP) new Individual(state);
79 ind->read(xInd);
80 // std::cout << ind->toString();
81
82 // print linear scaling parameters
83 op->printParams = true;
84 std::string testOutFile(argv[3]);
85 FileDataWriter fileDataWriter(testOutFile);
86 op->dataWriter = &fileDataWriter;
87 state->getEvalOp()->evaluate(ind);
88 dataWriter.close();
89 } else {
90 // run experiment
91 state->run();
92
93 // after the evolution: show best evolved ant's behaviour on learning trails
94 std::vector<IndividualP> hof = state->getHoF()->getBest();
95 IndividualP ind = hof[0];
96 // std::cout << ind->toString();
97
98 // print linear scaling parameters
99 op->printParams = true;
100 state->getEvalOp()->evaluate(ind);
101 }
102
103 return 0;
104}
Symbolic regression evaluation operator.
Definition: IAAdd.h:8
Definition: IACos.h:8
Definition: IADiv.h:8
Definition: IALog.h:8
Definition: IAMul.h:8
Definition: IASin.h:8
Definition: IASqrt.h:8
Definition: IASub.h:8
Individual class - inherits a vector of Genotype objects.
Definition: Individual.h:12
void read(XMLNode &)
read individual from XML node
Definition: Individual.cpp:103
State class - backbone of the framework.
Definition: State.h:39
Log function primitive (Tree genotype)
Definition: Log.h:14
Sqrt function primitive (Tree genotype)
Definition: Sqrt.h:14
Tree class - implements genotype as a tree.
Definition: Tree_c.h:29