Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members  

SIG_GPPopulation.cpp

00001 #include "SIGEL_GP/SIG_GPPopulation.h"
00002 
00003 #include<iostream>
00004 
00005 #include "SIGEL_Tools/SIG_IO.h"
00006 
00007 SIGEL_GP::SIG_GPPopulation::SIG_GPPopulation()
00008   : poolGeneration(0),
00009     randomizer( new SIGEL_Tools::SIG_Randomizer() ), 
00010     nextIdentifier( QString::number( 0 ) ),
00011     pool()
00012 {  
00013   pool.setAutoDelete(true);
00014 };
00015 
00016 SIGEL_GP::SIG_GPPopulation::SIG_GPPopulation(int size)
00017 {
00018    if( getRandomizerPointer()==0 )
00019      {
00020        SIGEL_Tools::SIG_IO::cerr << "\n\nA randomizer is needed to initialize a population ! Program terminated.\n";
00021        exit(1); 
00022        // ToDo: Exception
00023      }
00024 
00025    pool.setAutoDelete( true );
00026    pool.resize( size );
00027 
00028    setNextIdentifier( QString::number(0) );
00029    setPoolGeneration( 0 );
00030    
00031    for( int x=0; x<getSize(); x++ ) 
00032      { 
00033        pool.insert( x, new SIGEL_GP::SIG_GPIndividual( *getRandomizerPointer() ) );
00034        getIndividualPointer( x )->setPoolPos( x );
00035      }  
00036 };
00037 
00038     
00039 SIGEL_GP::SIG_GPPopulation::SIG_GPPopulation(QString data)
00040 {
00041    QTextStream                 inputFile(&data, IO_ReadOnly);
00042 
00043 #ifdef SIG_DEBUG
00044 
00045      SIGEL_Tools::SIG_IO::cerr << "\n\nREADING POPULATION DATA FROM FILE:\n";    
00046      
00047 #endif
00048 
00049    readFromFile(inputFile);
00050 
00051 #ifdef SIG_DEBUG
00052 
00053      SIGEL_Tools::SIG_IO::cerr << "\n\nREADING FINISHED.\n\n";
00054      
00055 #endif
00056 
00057 };
00058 
00059 
00060 SIGEL_GP::SIG_GPPopulation::SIG_GPPopulation(int size, 
00061                                              SIGEL_Tools::SIG_Randomizer &r, 
00062                                              SIGEL_GP::SIG_GPParameter& param,
00063                                              SIGEL_Robot::SIG_LanguageParameters& languageP)
00064 {
00065    setRandomizer( &r );
00066 
00067    pool.setAutoDelete( true );
00068    pool.resize( size );
00069 
00070    setNextIdentifier( QString::number(0) );
00071    setPoolGeneration( 0 );
00072 
00073    for( int x=0; x<getSize(); x++ )
00074      { 
00075        pool.insert( x, new SIGEL_GP::SIG_GPIndividual( r, param, languageP ) );
00076        getIndividualPointer( x )->setPoolPos( x );
00077      }
00078 }  
00079 
00080 SIGEL_GP::SIG_GPPopulation::SIG_GPPopulation(int size, 
00081                                              SIGEL_Tools::SIG_Randomizer &r)
00082 {
00083    setRandomizer( &r );
00084 
00085    pool.setAutoDelete( true );
00086    pool.resize( size );
00087    
00088    for( int x=0; x<getSize(); x++ )
00089      { 
00090        pool.insert( x, new SIGEL_GP::SIG_GPIndividual( r ) );
00091        getIndividualPointer( x )->setPoolPos( x );
00092      }
00093 }  
00094     
00095 SIGEL_GP::SIG_GPPopulation::~SIG_GPPopulation()
00096 {
00097 
00098 #ifdef SIG_DEBUG
00099 
00100   //   SIGEL_Tools::SIG_IO::cerr << "\nDestructor of POPULATION called!\n";
00101 
00102 #endif
00103 
00104   pool.clear();
00105   delete randomizer;
00106 };
00107 
00108     
00109 void SIGEL_GP::SIG_GPPopulation::sort()
00110 {
00111 
00112 
00113 };
00114 
00115     
00116 SIGEL_GP::SIG_GPIndividual& SIGEL_GP::SIG_GPPopulation::getIndividual(int poolpos)
00117 {
00118   if( poolpos<getSize() )
00119     {
00120       return  *pool[poolpos];
00121     }
00122   else
00123     {
00124       // Todo: Exception!
00125       SIGEL_Tools::SIG_IO::cerr << "Wrong Position requested from Population!\n";
00126       exit( 1 );
00127     } 
00128 };
00129 
00130     
00131 void SIGEL_GP::SIG_GPPopulation::setIndividual(SIG_GPIndividual& indi, 
00132                                                int poolpos)
00133 {
00134   // pool.resize(getSize()+1);
00135   pool.insert( poolpos, &indi );
00136 };
00137 
00138    
00139 
00140 void SIGEL_GP::SIG_GPPopulation::addRandomIndividuals(int quantity, 
00141                                                       SIGEL_GP::SIG_GPParameter& param, 
00142                                                       SIGEL_Robot::SIG_LanguageParameters& languageP)
00143 {
00144    int maxPos=getSize();
00145 
00146    pool.resize( maxPos + quantity );
00147 
00148    QProgressDialog *progress;
00149    
00150    if( qApp ) progress = new QProgressDialog ( "Progress:", "Cancel", quantity,
00151                                                0, "Progress", TRUE );
00152    if( qApp ) progress->setCaption( "Generating" );
00153    
00154    for( int x = maxPos; x<maxPos + quantity; x++ )
00155      { 
00156        SIG_GPIndividual *newInd = new SIG_GPIndividual( *getRandomizerPointer(), param, languageP );
00157        newInd->setName( getNextIdentifier() );
00158        pool.insert( x, newInd );
00159        getIndividualPointer( x )->setPoolPos( x );
00160 
00161        if( qApp )
00162          {
00163            progress->setProgress( x - maxPos );
00164            qApp->processEvents(); 
00165 
00166            if( progress->wasCancelled() )
00167              {
00168                // The process has been canceled. Because of process preparations the system may crash if
00169                // these preparation are not made undone:
00170 
00171                pool.resize( x + 1 );
00172   
00173                break;
00174              }
00175          }
00176      }
00177 
00178     if( qApp ) delete progress;
00179 };
00180     
00181 int SIGEL_GP::SIG_GPPopulation::getSize()
00182 {
00183     return pool.size();
00184 };
00185 
00186     
00187 QString SIGEL_GP::SIG_GPPopulation::getNextIdentifier()
00188 {
00189   QString releasedIdentifier = nextIdentifier;
00190 
00191   int nextIdentifierNumber = nextIdentifier.toInt() + 1;
00192 
00193   nextIdentifier = QString::number( nextIdentifierNumber );
00194 
00195   return releasedIdentifier;
00196 };
00197 
00198 void SIGEL_GP::SIG_GPPopulation::setNextIdentifier(QString identifier)
00199 {
00200     nextIdentifier = identifier;
00201 }; 
00202 
00203 int SIGEL_GP::SIG_GPPopulation::getPoolGeneration()
00204 {
00205     return poolGeneration;
00206 };
00207 
00208 void SIGEL_GP::SIG_GPPopulation::setPoolGeneration(int pGen)
00209 {
00210     poolGeneration = pGen;
00211 }
00212 
00213 void SIGEL_GP::SIG_GPPopulation::loadPool(QTextStream & pool)
00214 {
00215     readFromFile( pool );
00216 };
00217 
00218 
00219 void SIGEL_GP::SIG_GPPopulation::savePool(QTextStream & pool)
00220 {
00221     writeToFile( pool );
00222 };
00223 
00224 
00225 SIGEL_GP::SIG_GPIndividual *SIGEL_GP::SIG_GPPopulation::getIndividualPointer(int poolpos)
00226 {
00227     return pool[poolpos];
00228 }
00229 
00230 void SIGEL_GP::SIG_GPPopulation::deleteIndividual(int poolpos)
00231 {
00232   // pool.remove(poolpos);
00233 
00234    SIGEL_GP::SIG_GPIndividual *tmpInd;
00235 
00236    for( int x=poolpos; x<getSize()-1; x++ )
00237      { 
00238        //pool.remove(poolpos);
00239        tmpInd = getIndividualPointer( x + 1 );
00240        tmpInd->setPoolPos( x );
00241        pool.insert( x, pool.take( x+1 ) );
00242      }
00243 
00244    pool.resize( getSize() - 1 );
00245 }
00246 
00247 
00248   
00249 void SIGEL_GP::SIG_GPPopulation::setRandomizer(SIGEL_Tools::SIG_Randomizer *r)
00250 {
00251   randomizer=r;
00252 }
00253 
00254   
00255 SIGEL_Tools::SIG_Randomizer SIGEL_GP::SIG_GPPopulation::getRandomizer()
00256 {
00257   return *randomizer;
00258 }
00259 
00260 SIGEL_Tools::SIG_Randomizer *SIGEL_GP::SIG_GPPopulation::getRandomizerPointer()
00261 {
00262   return randomizer;
00263 }
00264 
00265 void SIGEL_GP::SIG_GPPopulation::importNewIndividual( QString& filename )
00266 {
00267    int lastPos=getSize();
00268 
00269    pool.resize( lastPos + 1 );
00270 
00271    SIG_GPIndividual *newInd = new SIG_GPIndividual();
00272 
00273    newInd->importIndividual( filename );
00274    newInd->setName( getNextIdentifier() );
00275    newInd->setPoolPos( lastPos );
00276 
00277    pool.insert( lastPos, newInd );
00278 }
00279 
00280 void SIGEL_GP::SIG_GPPopulation::readFromFile(QTextStream &file)
00281 {
00282 
00283   QString          populationStr=file.read();
00284   QString          tmpStr1, indStr;
00285   long             pos, pos2;
00286   QProgressDialog *progress;
00287 
00288 #ifdef SIG_DEBUG
00289 
00290      SIGEL_Tools::SIG_IO::cerr << "\n\nREADING POPULATION DATA FROM FILE:\n";    
00291      
00292 #endif
00293   
00294   //cout<<"TEST Population : \n"<<populationStr<<"\n\n";  
00295              
00296   if( (pos=populationStr.find("POPULATIONSIZE=",0,false) )!=-1 ) 
00297     { 
00298         pos2=populationStr.find( ";", pos + 16, false ); 
00299         pool.resize((populationStr.mid(pos+15,pos2-pos-15)).toLong());
00300 
00301 #ifdef SIG_DEBUG
00302 
00303         SIGEL_Tools::SIG_IO::cerr << "\n<Poolsize loaded:"
00304                                   << populationStr.mid(pos+15,pos2-pos-15).toLong()
00305                                   << ">";
00306 
00307 #endif
00308 
00309         if( qApp ) 
00310            progress = new QProgressDialog( "Progress:", "Cancel", getSize(), 
00311                                            0, "Progress", TRUE );
00312         if( qApp ) progress->setCaption( "Loading" );
00313 
00314         pos2=populationStr.find( ";", pos2+1, false );
00315         if( (pos=populationStr.find("NEXTIDENTIFIER=",0,false))!=-1 )
00316           {
00317 
00318 #ifdef SIG_DEBUG
00319 
00320              SIGEL_Tools::SIG_IO::cerr << "\n<Identifier loaded:"
00321                                        << populationStr.mid(pos+15,pos2-pos-15).toLong()
00322                                        << ">";
00323 
00324 #endif
00325              setNextIdentifier(populationStr.mid(pos+15,pos2-pos-15));
00326           }
00327         else 
00328              setNextIdentifier(QString::number(0));
00329         
00330 
00331         pos2=populationStr.find( ";", pos2+1, false );
00332         if( (pos=populationStr.find("POOLGENERATION=",0,false))!=-1 ) 
00333           {
00334 #ifdef SIG_DEBUG
00335 
00336              SIGEL_Tools::SIG_IO::cerr << "\n<Poolgeneration loaded:"
00337                                        << populationStr.mid(pos+15,pos2-pos-15)
00338                                        << ">\n\n";
00339 #endif
00340              setPoolGeneration((populationStr.mid(pos+15,pos2-pos-15)).toLong());
00341           }
00342         else
00343              setPoolGeneration(0); 
00344 
00345         for(long x=0;x<getSize();x++) 
00346           {  
00347              tmpStr1.setNum(x); 
00348              
00349              pos  = populationStr.find("INDIVIDUAL("+tmpStr1+") BEGIN{",pos2,false); 
00350              pos2 = populationStr.find("}INDIVIDUAL("+tmpStr1+") END",pos2,false); 
00351              
00352              
00353              pool.insert(x,new SIGEL_GP::SIG_GPIndividual());  
00354 
00355              indStr = populationStr.mid(pos+19+tmpStr1.length(),pos2-pos-20-tmpStr1.length()); 
00356              
00357              getIndividualPointer(x)->readFromFile(indStr); 
00358 
00359              if( qApp )
00360               {
00361                 progress->setProgress( x );
00362                 qApp->processEvents(); 
00363 
00364                 if ( progress->wasCancelled() )
00365                   {
00366 
00367                     // The process has been canceled. Because of process preparations the system may crash if
00368                     // these preparation are not made undone:
00369 
00370                     pool.resize( x + 1 );
00371                     
00372                     break;
00373                   }
00374               }
00375 
00376           } 
00377 #ifdef SIG_DEBUG
00378 
00379         SIGEL_Tools::SIG_IO::cerr << "\n\nREADING POPULATION FINISHED.\n";    
00380      
00381 #endif
00382 
00383         if( qApp ) delete progress;  
00384 
00385     } 
00386   else 
00387     { 
00388       
00389     }
00390  
00391 }
00392 
00393 void SIGEL_GP::SIG_GPPopulation::writeToFile(QTextStream &file)
00394 { 
00395   file<<"\n// ---------------------------------------";
00396   file<<"\n// (C)2001, PG 368, UNIVERSITY OF DORTMUND";
00397   file<<"\n// ---------------------------------------";
00398 
00399   file<<"\n\nPOPULATION BEGIN{ \n"<<"\n  POPULATIONSIZE="<<getSize()<<";";
00400   file<<"\n  NEXTIDENTIFIER="<<nextIdentifier<<";";
00401   file<<"\n  POOLGENERATION="<<getPoolGeneration()<<";";
00402 
00403   for( long x=0; x<getSize(); x++ )
00404     { 
00405         file<<"\n\n  INDIVIDUAL("<<x<<") BEGIN{";
00406   
00407         getIndividualPointer(x)->writeToFile(file);
00408 
00409         file<<"\n  }INDIVIDUAL("<<x<<") END;";
00410 
00411         if( qApp )
00412           {
00413             qApp->wakeUpGuiThread();
00414             qApp->processEvents();
00415           }  
00416     }
00417 
00418   file<<"\n\n}POPULATION END";
00419 }
00420 
00421 double SIGEL_GP::SIG_GPPopulation::getBestFitness(bool high)
00422 {
00423   if (getSize()>0)
00424     {
00425       double bestFitness = getIndividualPointer( 0 )->getFitness();
00426 
00427       for (int i=1; i<getSize(); i++)
00428         {
00429           double actFitness = getIndividualPointer( i )->getFitness();
00430 
00431           if (high)
00432             bestFitness = ( actFitness > bestFitness ) ? actFitness : bestFitness;
00433           else
00434             bestFitness = ( actFitness < bestFitness ) ? actFitness : bestFitness;
00435         };
00436 
00437       return bestFitness;
00438     }
00439   else
00440     return 0;
00441 }
00442 
00443 void SIGEL_GP::SIG_GPPopulation::resetPool()
00444 {
00445   for( int counter = 0; counter < pool.size(); counter++ )
00446     pool[ counter ]->setFitness( -1 );
00447 };
00448 
00449 double SIGEL_GP::SIG_GPPopulation::getWorstFitness(bool high)
00450 {
00451   if (getSize()>0)
00452     {
00453       double worstFitness = getIndividualPointer( 0 )->getFitness();
00454 
00455       for (int i=1; i<getSize(); i++)
00456         {
00457           double actFitness = getIndividualPointer( i )->getFitness();
00458 
00459           if (high)
00460             worstFitness = ( actFitness < worstFitness ) ? actFitness : worstFitness;
00461           else
00462             worstFitness = ( actFitness > worstFitness ) ? actFitness : worstFitness;
00463         };
00464 
00465       return worstFitness;
00466     }
00467   else
00468     return 0;
00469 };
00470 
00471 double SIGEL_GP::SIG_GPPopulation::getAverageFitness()
00472 {
00473   double fitnessSum = 0;
00474 
00475   for (int i=0; i<getSize(); i++)
00476     fitnessSum += getIndividualPointer( i )->getFitness();
00477 
00478   double averageFitness = fitnessSum / getSize();
00479 
00480   return averageFitness;
00481 };

Generated at Mon Sep 3 01:32:25 2001 for PG 368 - SIGEL by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000