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

D:/Projekt/ECF_trunk/examples/GPSymbReg_/SymbRegEvalOp.cpp

00001 #include <cmath>
00002 #include <ecf/ECF.h>
00003 #include "SymbRegEvalOp.h"
00004 
00005 using namespace std;
00006 StateP state_;
00007 
00008 // called only once, before the evolution – generates training data
00009 bool SymbRegEvalOp::initialize(StateP state)
00010 {
00011     state_ = state;
00012     nSamples = 10;
00013     double x = -10;
00014     for(uint i = 0; i < nSamples; i++) {
00015         domain.push_back(x);
00016         codomain.push_back(x + sin(x));
00017         x += 2;
00018     }
00019     return true;
00020 }
00021 
00022 
00023 //
00024 // dio za provjeru duplikata
00025 //
00026 vector<IndividualP> jedinke;
00027 // fja vraca true ako nadje istu, a prepise joj fitness od stare
00028 bool provjeriDuplikate(IndividualP individual, StateP state_)
00029 {
00030     static uint gen = 0;
00031     static uint jednakih = 0;
00032     static uint total = 0;
00033 
00034     bool jednaka = false;
00035     total++;
00036 
00037     // provjeri generaciju
00038     if(state_->getGenerationNo() != gen) {
00039         gen = state_->getGenerationNo();
00040 
00041         ECF_LOG(state_, 1, "jednakih: " + uint2str(jednakih) + ", " + uint2str(100*jednakih/total));
00042 
00043         ofstream dat("dupli.txt", ios_base::app);
00044         if(gen == 1)
00045             dat << endl;
00046         dat << uint2str(100*jednakih/total) << "\t";
00047 
00048         jedinke.clear();
00049         jednakih = 0;
00050         total = 0;
00051     }
00052 
00053     // provjeri sadrzaj
00054     uint broj = (uint) jedinke.size();
00055     Tree::Tree* nova = (Tree::Tree*) individual->getGenotype().get();
00056     for(uint i = 0; i < broj; i++) {
00057         Tree::Tree* stara = (Tree::Tree*) jedinke[i]->getGenotype().get();
00058         if(nova->size() != stara->size())
00059             continue;
00060         uint n, size = (uint) nova->size();
00061         Tree::NodeP cvor1, cvor2;
00062         for(n = 0; n < size; n++) {
00063             cvor1 = (*nova)[n];
00064             cvor2 = (*stara)[n];
00065             if(cvor1->primitive_ != cvor2->primitive_)
00066                 break;
00067         }
00068         // ako smo nasli jednaku, dodijeli joj Fitness od stare
00069         if(n == size) {
00070             jednakih++;
00071             individual->fitness = (FitnessP) jedinke[i]->fitness->copy();
00072             jednaka = true;
00073             break;
00074         }
00075     }
00076 
00077     return jednaka;
00078 }
00079 
00080 
00081 
00082 
00083 FitnessP SymbRegEvalOp::evaluate(IndividualP individual)
00084 {
00085     if(provjeriDuplikate(individual, state_)) {
00086         FitnessP fitness = individual->fitness;
00087         return fitness;
00088     }
00089 
00090     // we try to minimize the function value, so we use FitnessMin fitness (for minimization problems)
00091     FitnessP fitness (new FitnessMin);
00092 
00093     // get the genotype we defined in the configuration file
00094     Tree::Tree* tree = (Tree::Tree*) individual->getGenotype().get();
00095     // (you can also use boost smart pointers:)
00096     //TreeP tree = boost::static_pointer_cast<Tree::Tree> (individual->getGenotype());
00097 
00098     double value = 0;
00099     for(uint i = 0; i < nSamples; i++) {
00100         // for each test data instance, the x value (domain) must be set
00101         tree->setTerminalValue("X", &domain[i]);
00102         // get the y value of the current tree
00103         double result;
00104         tree->execute(&result);
00105         // add the difference
00106         value += fabs(codomain[i] - result);
00107     }
00108     fitness->setValue(value);
00109 
00110     // ova je bila nova, zapisi fitnes i zapamti ovu jedinku:
00111     individual->fitness = fitness;
00112     jedinke.push_back((IndividualP) individual->copy());
00113 
00114     return fitness;
00115 }

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