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

D:/Projekt/ECF_trunk/examples/GPCache/GPcache_main.cpp

00001 #include <ecf/ECF.h>
00002 #include "GPcacheEvalOp.h"
00003 #include "Greater.h"
00004 #include "Lesser.h"
00005 #include "GreaterOrEqual.h"
00006 #include "LesserOrEqual.h"
00007 #include "Equal.h"
00008 
00009 
00011 int fifo( int ulaz[MAXPOLJE] ) {
00012 
00013     int broj_promasaja=0 , trenutno[VEL_CASHA]={0}, i , sljed=0 , j , k , pom;
00014 
00015     for ( i=0 ; i<MAXPOLJE ; i++ ){
00016     
00017         for (j=0 ; j<VEL_CASHA ; j++ ){
00018             
00019             pom=0;
00020 
00021             for (k=0 ; k<VEL_CASHA ; k++){
00022                 if(ulaz[i]==trenutno[k]){
00023                     pom=1;
00024                     break ;
00025                 }
00026             }
00027 
00028             if (pom) break ; 
00029 
00030             if(trenutno[sljed]==0 ) {
00031                 broj_promasaja++;
00032                 trenutno[sljed]=ulaz[i];
00033                 sljed=((sljed+1)%VEL_CASHA);
00034                 break ;
00035             }
00036             
00037             broj_promasaja++ ;
00038             trenutno[sljed]=ulaz[i];
00039             sljed=((sljed+1)%VEL_CASHA);
00040             break ;
00041 
00042         }
00043     }
00044 
00045 
00046     return broj_promasaja ;
00047 }
00048 
00050 int lru( int *ulaz ) {
00051 
00052     int broj_promasaja=0 , i , prije_koliko[VEL_CASHA] , trenutno[VEL_CASHA]={0} 
00053         , j , max , k , g , index , flag ;
00054 
00055 
00056 
00057     for(k=0;k<VEL_CASHA;k++){         //povecaj indekse svih da budu veci od 0
00058     prije_koliko[k]=VEL_CASHA*2;
00059     }
00060 
00061     for ( i=0 ; i<MAXPOLJE ; i++ ){
00062     
00063 
00064         for (j=0 ; j<VEL_CASHA ; j++ ){
00065                         
00066             flag=0;
00067 
00068             for(k=0 ; k<VEL_CASHA ; k++){
00069             
00070                 if(*(ulaz+i)==trenutno[k]){
00071                     prije_koliko[k]=0;
00072                     flag=1 ;
00073 
00074                 for(g=0;g<VEL_CASHA;g++){         //povecaj indekse svih
00075                     prije_koliko[g]+=1;
00076                 }
00077                 break ;
00078                 }
00079             }
00080 
00081             if (flag) break ;
00082 
00083             max=prije_koliko[0] ;
00084             index=0;
00085             for (k=1 ; k<VEL_CASHA ; k++){
00086                 
00087                 if( prije_koliko[k] > max ){
00088                     max=prije_koliko[k];
00089                     index=k;
00090                 }
00091             }
00092             
00093             broj_promasaja++;
00094             prije_koliko[index]=0 ;
00095             trenutno[index]=*(ulaz+i) ;
00096             
00097             for(g=0;g<VEL_CASHA;g++){         //povecaj indekse svih
00098             prije_koliko[g]+=1;
00099             }
00100 
00101             break ;
00102         }   
00103 
00104     }   
00105 
00106     return broj_promasaja ;
00107 }
00108 
00110 void generiraj_polje(int sid, int *polje){
00111 
00112     int i;
00113     srand(unsigned(sid));
00114     for(i=0; i<MAXPOLJE ;i++){
00115         *(polje+i)=((rand())%BR_STR)+1; 
00116     }
00117 }
00118 
00119 
00120 
00121 int main (int argc , char **argv ) {
00122     
00123 
00124     int pom , i , j, br_fifo , br_lru , br_stablo;
00125     int polje[MAXPOLJE];
00126     int sidovi_za_random[10] = {436636,576542,73246,5762754,35636,6126788,975645642,4623656,34623,2365477} ;
00127     std::string trazi_fittnes;
00128     char pomocno[1000],a;
00129     float avg_fifo=0 , avg_lru=0 , avg_stablo=0;
00130 
00131     StateP state = static_cast<StateP> (new State) ;
00132 
00133     state->setEvalOp(static_cast<EvaluateOpP> (new GPcacheEvalOp)) ;
00134 
00135     TreeP tree (new Tree::Tree);
00136 
00137     Tree::PrimitiveP Greater (new Greater);    
00138     tree->addFunction(Greater);
00139     state->addGenotype(tree) ;
00140 
00141 
00142     Tree::PrimitiveP Lesser (new Lesser);    
00143     tree->addFunction(Lesser);
00144     state->addGenotype(tree) ;
00145 
00146     Tree::PrimitiveP GreaterOrEqual (new GreaterOrEqual);    
00147     tree->addFunction(GreaterOrEqual);
00148     state->addGenotype(tree) ;
00149 
00150     Tree::PrimitiveP LesserOrEqual (new LesserOrEqual);    
00151     tree->addFunction(LesserOrEqual);
00152     state->addGenotype(tree) ;
00153 
00154     Tree::PrimitiveP Equal (new Equal);    
00155     tree->addFunction(Equal);
00156     state->addGenotype(tree) ;
00157 
00158 
00159     state->initialize(argc , argv) ;
00160     
00161     printf("\nAko zelite evoluciju upisite 1, a za usporedbu upisite 0\n");
00162     scanf("%d", &pom);
00163 
00164     if(pom){ 
00165         state->run();
00166     }
00167     else{
00168     
00169         for(i=0;i<10;i++){
00170             
00171             generiraj_polje(sidovi_za_random[i],&polje[0]);
00172 
00173             br_fifo = fifo(&polje[0]);
00174             avg_fifo += br_fifo ;
00175             br_lru = lru(&polje[0]);
00176             avg_lru += br_lru ;
00177         
00178             XMLNode xInd = XMLNode::parseFile("./ind.txt", "Individual");
00179             IndividualP ind = (IndividualP) new Individual(state);
00180             ind->read(xInd);
00181             state->getAlgorithm()->evaluate(ind);
00182             trazi_fittnes=ind->toString();
00183 
00184             
00185             char *pomocno=(char*)trazi_fittnes.c_str();
00186             
00187             br_stablo=0;
00188 
00189             br_stablo += 100*(int)(pomocno[42]-'0');
00190             br_stablo += 10*(int)(pomocno[43]-'0');
00191             br_stablo += (int)(pomocno[44]-'0');
00192             avg_stablo += br_stablo;
00193 
00194 
00195             printf("\n");
00196             printf("\nBroj promasaj pagenator_20/FIFO je %d/%d = %f", br_stablo , br_fifo , (float)br_stablo/br_fifo);
00197             printf("\nBroj promasaj pagenator_20/LRU je %d/%d = %f", br_stablo , br_lru, (float)br_stablo/br_lru);
00198             printf("\n");
00199 
00200 
00201         }
00202     
00203     printf("\nProsjecan broj promasaja algoritma FIFO ..........%f", avg_fifo/i);
00204     printf("\nProsjecan broj promasaja algoritma LRU ...........%f", avg_lru/i);
00205     printf("\nProsjecan broj promasaja algoritma pagenator_20 ..%f", avg_stablo/i);
00206     
00207     
00208     
00209     
00210     } 
00211 
00212     printf("\n\n\n");
00213     system("PAUSE");
00214     return(0);
00215 
00216 }

Generated on Fri Jul 13 2012 10:53:29 for ECF by  doxygen 1.7.1