ECF 1.5
main.cpp
1#include <ecf/ECF.h>
2#include "AntEvalOp.h"
3#include "Prog2.h"
4#include "Prog3.h"
5#include "TurnLeft.h"
6#include "IfFoodAhead.h"
7#include "TurnRight.h"
8#include "MoveAhead.h"
9#include <iostream>
10#include <fstream>
11#include <string.h>
12
13
14int main(int argc, char **argv)
15{
16 StateP state (new State);
17
18 // crate evaluation operator
19 state->setEvalOp(new AntEvalOp);
20
21 // create tree genotype
22 TreeP tree (new Tree::Tree);
23
24 // create new functions and add them to function set
25 Tree::PrimitiveP ifa (new IfFoodAhead);
26 tree->addFunction(ifa);
27 Tree::PrimitiveP prog2 (new Prog2);
28 tree->addFunction(prog2);
29 Tree::PrimitiveP prog3 (new Prog3);
30 tree->addFunction(prog3);
31 Tree::PrimitiveP tl (new TurnLeft);
32 tree->addTerminal(tl);
33 Tree::PrimitiveP tr (new TurnRight);
34 tree->addTerminal(tr);
35 Tree::PrimitiveP mv (new MoveAhead);
36 tree->addTerminal(mv);
37
38 // register genotype with our primitives
39 state->addGenotype(tree);
40
41 // initialize and start evaluation
42 if(!state->initialize(argc, argv))
43 return 1;
44 state->run();
45
46
47 // after the evolution: show best evolved ant's behaviour on learning trails
48 std::vector<IndividualP> hof = state->getHoF()->getBest();
49 IndividualP ind = hof[0];
50 std::cout << ind->toString();
51 std::cout << "\nBest ant's performance on learning trail(s):" << std::endl;
52
53
54 // show ant movement on the trail(s)
56
57 // optional: show movements step by step (interactive)
58 //AntEvalOp::step = 1;
59
60 state->getEvalOp()->evaluate(ind);
61
62
63 // also, simulate best evolved ant on a (different) test trail!
64 std::cout << "\nBest ant's performance on test trail(s):" << std::endl;
65 AntEvalOp* evalOp = new AntEvalOp;
66
67 // substitute test trails for learning trails (defined in config file):
68 state->getRegistry()->modifyEntry("learning_trails", state->getRegistry()->getEntry("test_trails"));
69 evalOp->initialize(state);
70 evalOp->evaluate(ind);
71
72
73 // optional: write best individual to 'best.txt'
74// ofstream best("./best.txt");
75// best << ind->toString();
76// best.close();
77
78 // optional: read individual from 'best.txt' (for subsequent simulation)
79// XMLNode xInd = XMLNode::parseFile("./best.txt", "Individual");
80// IndividualP ind = (IndividualP) new Individual(state);
81// ind->read(xInd);
82
83 return 0;
84}
Artificial ant evaluation class (and environment simulator)
Definition: AntEvalOp.h:51
bool initialize(StateP)
Initialize the simulator, read environments from input file.
Definition: AntEvalOp.cpp:45
static bool trace
trace the ant's movement in the simulator (for visual output)
Definition: AntEvalOp.h:85
FitnessP evaluate(IndividualP individual)
Evaluation function, simulates ant movement and counts the eaten food.
Definition: AntEvalOp.cpp:98
GP function, checks if the food is ahead.
Definition: IfFoodAhead.h:11
GP terminal, moves the ant one square ahead.
Definition: MoveAhead.h:9
GP function, executes 2 subtrees in sequence.
Definition: Prog2.h:7
GP function, executes 3 subtrees in sequence.
Definition: Prog3.h:7
State class - backbone of the framework.
Definition: State.h:39
Tree class - implements genotype as a tree.
Definition: Tree_c.h:29
GP terminal, turns the ant left.
Definition: TurnLeft.h:9
GP terminal, turns the ant right.
Definition: TurnRight.h:9