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

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

00001 #include <ecf/ECF.h>
00002 #include "fitnes.hpp"
00003 
00004 
00005 class Zapis : public Operator
00006 {
00007 private:
00008     StateP state_;
00009     std::string logfilename;
00010     SchedulingEvalOpP testEvalOp;
00011 public:
00012     void registerParameters(StateP state)
00013     {
00014         state->getRegistry()->registerEntry("test_cases2", (voidP) (new std::string("fitness2.txt")), ECF::STRING);
00015     }
00016 
00017     bool initialize(StateP state)
00018     {
00019         state_ = state;
00020         voidP sptr = state->getRegistry()->getEntry("log.filename");
00021         logfilename = *((std::string*) sptr.get());
00022 
00023         // setup test cases evaluator
00024         testEvalOp = static_cast<SchedulingEvalOpP> (new SchedulingEvalOp);
00025         sptr = state->getRegistry()->getEntry("test_cases");
00026         voidP sptr2 = state->getRegistry()->getEntry("test_cases2");
00027         //state->getRegistry()->modifyEntry("test_cases", (voidP) (new std::string("fitness2.txt")));
00028         state->getRegistry()->modifyEntry("test_cases", sptr2);
00029         testEvalOp->initialize(state);
00030         state->getRegistry()->modifyEntry("test_cases", sptr);
00031 
00032         return true;
00033     }
00034 
00035     bool operate(StateP state)
00036     {
00037         ECF_LOG(state, 1, "Podaci\t" + uint2str(state->getGenerationNo()));
00038         IndividualP bestInd = state->getPopulation()->getHof()->getBest().at(0);
00039         ECF_LOG(state, 1, bestInd->toString());
00040 
00041         TreeP bestTree = boost::dynamic_pointer_cast<Tree::Tree> (bestInd->getGenotype(0));
00042         uint sizeOfBest = (uint) bestTree->size();
00043 
00044         DemeP deme = state->getPopulation()->getLocalDeme();
00045         uint popSize = (uint) deme->size();
00046         uint totalSize = 0;
00047         for(uint i = 0; i < popSize; i++) {
00048             TreeP tree = boost::dynamic_pointer_cast<Tree::Tree> (deme->at(i)->getGenotype(0));
00049             totalSize += (uint) tree->size();
00050         }
00051         uint averageSize = (uint) (1. * totalSize / popSize);
00052 
00053         // evaluate best individual on test data set
00054         double bestValue = testEvalOp->evaluate(bestInd)->getValue();
00055 
00056         ECF_LOG(state, 1, uint2str(sizeOfBest) + "\t" + uint2str(averageSize) + "\t" + dbl2str(bestValue));
00057 
00058         return true;
00059     }
00060 };
00061 
00062 
00063 
00064 
00065 
00066 int main(int argc, char **argv)
00067 {
00068     StateP state = static_cast<StateP> (new State);
00069 
00070     SchedulingEvalOpP evalOp = static_cast<SchedulingEvalOpP> (new SchedulingEvalOp);
00071     state->setEvalOp(evalOp);
00072 
00073     Zapis *z = new Zapis;
00074     state->addOperator((OperatorP) z);
00075 
00076     // citanje terminala iz 'fitness.txt'
00077     // -- ne radimo jer se cita iz 'terminalset' parametra!
00078     //TreeP tree = (TreeP) new Tree::Tree;
00079     //evalOp->in_file = "fitness.txt";
00080     //evalOp->ReadTerminals(tree);
00081     //state->addGenotype(tree);
00082 
00083     state->initialize(argc, argv);
00084     state->run();
00085 
00086     // citanje jedinke iz 'ind.txt' i provjera dobrote
00087     //XMLNode xInd = XMLNode::parseFile("./ind.txt", "Individual");
00088     //IndividualP ind = (IndividualP) new Individual(state);
00089     //ind->read(xInd);
00090     //state->getAlgorithm()->evaluate(ind);
00091     //std::cout << ind->toString();
00092 
00093     return 0;
00094 }

Generated on Wed Sep 21 2011 13:46:52 for ECF by  doxygen 1.7.1