• Main Page
  • Modules
  • Classes
  • Files
  • File List

D:/Projekt/ECF_trunk/examples/GPArtificialAnt/main.cpp

00001 #include <ecf/ECF.h>
00002 #include "AntEvalOp.h"
00003 #include "Prog2.h"
00004 #include "Prog3.h"
00005 #include "TurnLeft.h"
00006 #include "IfFoodAhead.h"
00007 #include "TurnRight.h"
00008 #include "MoveAhead.h"
00009 #include <iostream>
00010 #include <fstream>
00011 #include <string.h>
00012 
00013 
00014 int main(int argc, char **argv)
00015 {
00016     StateP state (new State);
00017 
00018     // crate evaluation operator
00019     state->setEvalOp(new AntEvalOp);
00020     
00021     // create tree genotype
00022     TreeP tree (new Tree::Tree);
00023     
00024     // create new functions and add them to function set 
00025     Tree::PrimitiveP ifa (new IfFoodAhead);
00026     tree->addFunction(ifa);
00027     Tree::PrimitiveP prog2 (new Prog2);
00028     tree->addFunction(prog2);
00029     Tree::PrimitiveP prog3 (new Prog3);
00030     tree->addFunction(prog3);
00031     Tree::PrimitiveP tl (new TurnLeft);
00032     tree->addTerminal(tl);
00033     Tree::PrimitiveP tr (new TurnRight);
00034     tree->addTerminal(tr);
00035     Tree::PrimitiveP mv (new MoveAhead);
00036     tree->addTerminal(mv);
00037 
00038     // register genotype with our primitives
00039     state->addGenotype(tree);
00040 
00041     // initialize and start evaluation
00042     if(!state->initialize(argc, argv))
00043         return 1;
00044     state->run();
00045 
00046 
00047     // after the evolution: show best evolved ant's behaviour on learning trails
00048     std::vector<IndividualP> hof = state->getHoF()->getBest();
00049     IndividualP ind = hof[0];
00050     std::cout << ind->toString();
00051     std::cout << "\nBest ant's performance on learning trail(s):" << std::endl;
00052 
00053 
00054     // show ant movement on the trail(s)
00055     AntEvalOp::trace = 1;
00056 
00057     // optional: show movements step by step (interactive)
00058     //AntEvalOp::step = 1;
00059 
00060     state->getEvalOp()->evaluate(ind);
00061 
00062 
00063     // also, simulate best evolved ant on a (different) test trail!
00064     std::cout << "\nBest ant's performance on test trail(s):" << std::endl;
00065     AntEvalOp* evalOp = new AntEvalOp;
00066 
00067     // substitute test trails for learning trails (defined in config file):
00068     state->getRegistry()->modifyEntry("learning_trails", state->getRegistry()->getEntry("test_trails"));
00069     evalOp->initialize(state);
00070     evalOp->evaluate(ind);
00071 
00072 
00073     // optional: write best individual to 'best.txt'
00074 //  ofstream best("./best.txt");
00075 //  best << ind->toString();
00076 //  best.close();
00077 
00078     // optional: read individual from 'best.txt' (for subsequent simulation)
00079 //  XMLNode xInd = XMLNode::parseFile("./best.txt", "Individual");
00080 //  IndividualP ind = (IndividualP) new Individual(state);
00081 //  ind->read(xInd); 
00082 
00083     return 0;
00084 }

Generated on Tue Nov 4 2014 13:04:31 for ECF by  doxygen 1.7.1