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

Population.cpp

Go to the documentation of this file.
00001 
00009 #include <Population.h>
00010 
00019 Population::Population(int populationSize       , 
00020                        int maximumPopulationSize,
00021                        Random * random          ,   
00022                        MultiobjectiveProblem * problem) {   
00023   problem_               = problem               ;
00024   populationSize_        = populationSize        ;
00025   maximumPopulationSize_ = maximumPopulationSize ;
00026   random_                = random                ;
00027   
00028   population_ = new Individual * [maximumPopulationSize] ;
00029   if (!population_) {
00030     cerr << "Population::Population-> error when asking for memory " << endl ;
00031     exit(-1) ;
00032   } // if
00033   
00034   int i ;
00035   for (i = 0; i < populationSize_; i++) 
00036     population_[i] = new Individual(problem_, random) ;
00037 } // Population::Population
00038 
00044 Population::~Population(void) {
00045 } // Population::~Population
00046 
00051 int Population::getPopulationSize() const {
00052   return populationSize_;
00053 } // getPopulationSize
00054   
00055  
00060 int Population::getMaximumPopulationSize() const {
00061   return maximumPopulationSize_ ;
00062 } // getMaximumPopulationSize
00063   
00069 Individual * Population::getIth(int index) const {
00070   if ((index < populationSize_) && (index >= 0))
00071     return population_[index];
00072   else {
00073     cerr << "Population::getIth - Index out of range when getting a copy of "
00074          << "an individual" ;
00075     cerr << endl ;
00076     exit(-1) ;
00077   } // else
00078 } // getIth
00079 
00085 void Population::setIth(int index, Individual * individual) {
00086   if ((index < populationSize_) && (index >= 0))
00087     population_[index] = individual ;
00088   else {
00089     cerr << "Population::setIth - Index out of range when inserting an "
00090          << "individual" ;
00091     cerr << endl ;
00092     exit(-1) ;
00093   } // else  
00094 } // setIth
00095 
00101 void Population::setFitness(int index, double * fitness) {
00102   population_[index]->setFitness(fitness);
00103 } // setFitness
00104 
00105 
00111 void Population::addIndividual(Individual * individual) {
00112   if (populationSize_ < maximumPopulationSize_) {
00113     population_[populationSize_] = individual ;
00114     populationSize_ += 1 ;
00115   } // if
00116   else {
00117     cerr << "Population::addIndividual->The population size has reach to its "
00118          << "maximum size" << endl ;
00119     exit(-1) ;
00120   } // else
00121 } // Population::addIndividual
00122 
00123   
00129 void Population::deleteIth(int index) {
00130   if ((index < populationSize_) && (index >= 0)) {
00131     delete population_[index] ;
00132     int i ;
00133     for (i = index; i < (populationSize_ - 1); i++)
00134       population_[i] = population_[i + 1] ;
00135     populationSize_ -- ;
00136   } // if
00137   else {
00138     cerr << "Population::deleteIth - Index " << index << " out of range " ;
00139     cerr << endl ;
00140     exit(-1) ;
00141   } // else
00142 } // Population::deleteIth
00143 
00148 void Population::printFitness(char *fileName) {
00149   ofstream outputFile ;
00150   int      i          ;
00151   int      j          ;
00152 
00153   outputFile.open(fileName, ios::out) ;
00154   outputFile.precision(10) ;
00155   
00156   for (j = 0 ; j < getPopulationSize(); j++) {
00157     for (i = 0; i < problem_->numberOfFunctions_ ; i++)
00158       outputFile << getIth(j)->getFitness()[i] << "\t" ;
00159     outputFile << endl ;
00160   } // for
00161 
00162   outputFile.close() ;      
00163 } // Population::printFitness
00164 
00169 void Population::printGenotype(char *fileName) {
00170   ofstream outputFile ;
00171   int      i          ;
00172   int      j          ;
00173 
00174   outputFile.open(fileName, ios::out) ;
00175   outputFile.precision(15) ;
00176   
00177   for (j = 0 ; j < getPopulationSize(); j++) {
00178     for (i = 0; i < problem_->numberOfVariables_ ; i++) {
00179       getIth(j)->chromosome_->gene_[i]->writeGenotype(outputFile) ;
00180       outputFile << "\t" ;
00181     } // for
00182     outputFile << endl ;
00183   } // for 
00184   
00185   outputFile.close() ;      
00186 } // Population::printGenotype

Generated on Wed Feb 11 10:38:01 2004 for Paes by doxygen 1.3.3