• 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 //#include <ecf/AlgGenHookeJeeves.h>
00004 
00005 
00006 //
00007 // pomocni operator za ispitivanje trenutnih rjesenja iz HoF-a na skupu za evaluaciju
00008 //
00009 class Zapis : public Operator
00010 {
00011 private:
00012     StateP state_;
00013     std::string logfilename;
00014     SchedulingEvalOpP testEvalOp;
00015 public:
00016     void registerParameters(StateP state)
00017     {
00018         state->getRegistry()->registerEntry("test_cases2", (voidP) (new std::string("fitness2.txt")), ECF::STRING);
00019     }
00020 
00021     bool initialize(StateP state)
00022     {
00023         state_ = state;
00024         voidP sptr = state->getRegistry()->getEntry("log.filename");
00025         logfilename = *((std::string*) sptr.get());
00026 
00027         // setup test cases evaluator
00028         testEvalOp = static_cast<SchedulingEvalOpP> (new SchedulingEvalOp);
00029         sptr = state->getRegistry()->getEntry("test_cases");
00030         voidP sptr2 = state->getRegistry()->getEntry("test_cases2");
00031         //state->getRegistry()->modifyEntry("test_cases", (voidP) (new std::string("fitness2.txt")));
00032         state->getRegistry()->modifyEntry("test_cases", sptr2);
00033         testEvalOp->initialize(state);
00034         state->getRegistry()->modifyEntry("test_cases", sptr);
00035 
00036         return true;
00037     }
00038 
00039     bool operate(StateP state)
00040     {
00041         ECF_LOG(state, 1, "Podaci\t" + uint2str(state->getGenerationNo()));
00042         IndividualP bestInd = state->getPopulation()->getHof()->getBest().at(0);
00043         ECF_LOG(state, 1, bestInd->toString());
00044 
00045         TreeP bestTree = boost::dynamic_pointer_cast<Tree::Tree> (bestInd->getGenotype(0));
00046         uint sizeOfBest = (uint) bestTree->size();
00047 
00048         DemeP deme = state->getPopulation()->getLocalDeme();
00049         uint popSize = (uint) deme->size();
00050         uint totalSize = 0;
00051         for(uint i = 0; i < popSize; i++) {
00052             TreeP tree = boost::dynamic_pointer_cast<Tree::Tree> (deme->at(i)->getGenotype(0));
00053             totalSize += (uint) tree->size();
00054         }
00055         uint averageSize = (uint) (1. * totalSize / popSize);
00056 
00057         // evaluate best individual on test data set
00058         double bestValue = testEvalOp->evaluate(bestInd)->getValue();
00059 
00060         ECF_LOG(state, 1, uint2str(sizeOfBest) + "\t" + uint2str(averageSize) + "\t" + dbl2str(bestValue));
00061 
00062         return true;
00063     }
00064 };
00065 
00066 
00067 
00068 //
00069 // pomocni operator za ispis trenutno najboljeg rjesenja
00070 //
00071 class WriteBest : public Operator
00072 {
00073 private:
00074     StateP state_;
00075     std::string logfilename;
00076     SchedulingEvalOpP testEvalOp;
00077 public:
00078 
00079     bool initialize(StateP state)
00080     {
00081         state_ = state;
00082         return true;
00083     }
00084 
00085     bool operate(StateP state)
00086     {
00087         ECF_LOG(state, 1, "Best in " + uint2str(state->getGenerationNo()));
00088         IndividualP bestInd = state->getPopulation()->getHof()->getBest().at(0);
00089         ECF_LOG(state, 1, bestInd->toString());
00090 
00091         return true;
00092     }
00093 };
00094 
00095 
00096 
00097 int main(int argc, char **argv)
00098 {
00099     StateP state (new State);
00100 
00101 //  GenHookeJeevesP alg (new GenHookeJeeves);
00102 //  state->addAlgorithm(alg);
00103 
00104     SchedulingEvalOpP evalOp (new SchedulingEvalOp);
00105     state->setEvalOp(evalOp);
00106 
00107 //  Zapis *z = new Zapis;
00108 //  state->addOperator((OperatorP) z);
00109 
00110     WriteBest *b = new WriteBest;
00111     state->addOperator((OperatorP) b);
00112 
00113     // citanje terminala iz 'fitness.txt'
00114     // -- ne radimo jer se cita iz 'terminalset' parametra!
00115     //TreeP tree = (TreeP) new Tree::Tree;
00116     //evalOp->in_file = "fitness.txt";
00117     //evalOp->ReadTerminals(tree);
00118     //state->addGenotype(tree);
00119 
00120     if(argc > 2) {
00121         uint primjer = atoi(argv[2]);
00122         state->getContext()->environment = new uint(primjer);
00123     }
00124 
00125     state->initialize(argc, argv);
00126     state->run();
00127 
00128     return 0;
00129 }
00130 
00131 
00132 
00133 //
00134 // fja main za citanje jedinke iz "ind.txt" i evaluaciju
00135 //
00136 /*
00137 int main(int argc, char **argv)
00138 {
00139     StateP state (new State);
00140 
00141     SchedulingEvalOpP evalOp (new SchedulingEvalOp);
00142     state->setEvalOp(evalOp);
00143     state->initialize(argc, argv);
00144 
00145     // citanje jedinke iz 'ind.txt' i provjera dobrote
00146     XMLNode xInd = XMLNode::parseFile("./ind.txt", "Individual");
00147     IndividualP ind = (IndividualP) new Individual(state);
00148     ind->read(xInd);
00149     state->getAlgorithm()->evaluate(ind);
00150     std::cout << ind->toString();
00151 
00152     return 0;
00153 }
00154 */

Generated on Thu Jul 10 2014 14:13:40 for ECF by  doxygen 1.7.1