00001 #include <ecf/ECF.h>
00002 #include "fitnes.hpp"
00003
00004
00005
00006
00007
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
00028 testEvalOp = static_cast<SchedulingEvalOpP> (new SchedulingEvalOp);
00029 sptr = state->getRegistry()->getEntry("test_cases");
00030 voidP sptr2 = state->getRegistry()->getEntry("test_cases2");
00031
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
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
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
00102
00103
00104 SchedulingEvalOpP evalOp (new SchedulingEvalOp);
00105 state->setEvalOp(evalOp);
00106
00107
00108
00109
00110 WriteBest *b = new WriteBest;
00111 state->addOperator((OperatorP) b);
00112
00113
00114
00115
00116
00117
00118
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
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154