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

RealGene.cpp

Go to the documentation of this file.
00001 /*
00002  * @file    RealGene.h
00003  * @author  Antonio Jesus Nebro Urbaneja
00004  * @version 1.0
00005  * @date    29 January 2004
00006  * @brief   Class representing a real gene
00007  */
00008  
00009 
00010 #include <RealGene.h>
00011 
00018 RealGene::RealGene(Random * random) : Gene(REAL, random) {
00019   lowerBound_ = MIN_REAL ;
00020   upperBound_ = MAX_REAL ;
00021   allele_ = random_->rndreal(lowerBound_, upperBound_) ;
00022 
00023 #ifdef __MPI__
00024 this->calculateSize() ;
00025 #endif  
00026 } // RealGene::RealGene
00027 
00034 RealGene::RealGene(double lowerBound, double upperBound, Random * random) : 
00035           Gene(REAL, random) {
00036   lowerBound_ = lowerBound ;
00037   upperBound_ = upperBound ;
00038   allele_ = random_->rndreal(lowerBound, upperBound) ;
00039 
00040 #ifdef __MPI__
00041 this->calculateSize() ;
00042 #endif    
00043 } // RealGene::RealGene
00044 
00051 RealGene::RealGene(RealGene & realGene) : Gene(realGene) {
00052   allele_     = realGene.allele_     ;
00053   lowerBound_ = realGene.lowerBound_ ;
00054   upperBound_ = realGene.upperBound_ ;
00055   
00056 #ifdef __MPI__
00057 this->calculateSize() ;
00058 #endif  
00059 } // RealGene::RealGene
00060 
00067 RealGene::RealGene(RealGene * realGene) : Gene(realGene) {
00068   allele_     = realGene->allele_     ;
00069   lowerBound_ = realGene->lowerBound_ ;
00070   upperBound_ = realGene->upperBound_ ;
00071   
00072 #ifdef __MPI__
00073 this->calculateSize() ;
00074 #endif  
00075 } // RealGene::RealGene
00076 
00082 RealGene::~RealGene() {
00083 } // RealGene::~RealGene
00084 
00085 int RealGene::randomMutation(double mutationProbability) {
00086   int    i         ;
00087   int    mutations ;
00088   double rnd       ;
00089   
00090   mutations = 0 ;
00091   
00092   rnd = random_->rndreal(0.0, 1.0) ;
00093   if (rnd <= mutationProbability) {
00094     allele_ = lowerBound_ + rnd *(upperBound_ - lowerBound_) ;
00095 
00096     mutations ++ ;
00097   } // if    
00098   
00099   return mutations ;
00100 } // RealGene::uniformMutation
00101 
00102 int RealGene::polynomialMutation(double mutationProbability, 
00103                                  double distributionIndex) {                                   
00104   int    i          ;
00105   double temp       ;
00106   int    mutations  ;
00107   double rnd        ;
00108 
00109   double delta      ;
00110   double deltaq     ;
00111   double mu         ;
00112   
00113   mutations = 0 ;
00114 
00115   rnd = random_->rndreal(0.0, 1.0) ;
00116   if (rnd <= mutationProbability) {
00117     if (allele_ > lowerBound_) { // calculate delta
00118       if ((allele_ - lowerBound_) < (upperBound_ - allele_))
00119         delta = (allele_ - lowerBound_) / (upperBound_ - allele_) ;
00120       else
00121         delta = (upperBound_ - allele_) / (upperBound_ - lowerBound_) ;
00122       rnd = random_->rndreal(0.0, 1.0) ;
00123       mu  = 1.0/(distributionIndex + 1.0);
00124       if(rnd <= 0.5) {
00125         double xy = 1.0-delta;
00126         temp = 2*rnd+(1-2*rnd)*(pow(xy,(distributionIndex + 1)));
00127         deltaq =  pow(temp, mu) - 1.0;
00128       } // if
00129       else {
00130         double xy = 1.0-delta;
00131         temp = 2.0*(1.0-rnd)+2.0*(rnd-0.5)*(pow(xy,(distributionIndex + 1)));
00132         deltaq = 1.0 - (pow(temp,mu));
00133       } // else
00134 
00135       /*Change the value of the allele */
00136       allele_ += deltaq * (upperBound_ - lowerBound_);
00137       if (allele_ < lowerBound_)
00138         allele_ = lowerBound_ ;
00139       if (allele_ > upperBound_)
00140         allele_ = upperBound_ ;
00141     } // if
00142     else {
00143       rnd = random_->rndreal(0.0, 1.0) ;
00144       allele_ = rnd * (upperBound_ - lowerBound_) + lowerBound_ ;
00145     } // else
00146     mutations ++ ;
00147   } // if
00148 
00149   return mutations ;
00150 } // Individual::PolynomialMutationn
00151 
00152 int RealGene::uniformMutation(double mutationProbability, double perturbation) {
00153   int    i         ;
00154   int    mutations ;
00155   double rnd       ;
00156   
00157   mutations = 0 ;
00158   
00159   rnd = random_->rndreal(0.0, 1.0) ;
00160   if (rnd <= mutationProbability) {
00161 //cout << "allele_ " << allele_ ;
00162     double tmp = (rnd + 0.5)*perturbation ;
00163 //cout << "\tVar: " << tmp ;
00164 //    allele_ = allele_ + (rnd - 0.5)*perturbation ;
00165     allele_ = allele_ + tmp ;
00166     if (allele_ < lowerBound_)
00167       allele_ = lowerBound_ ;
00168     if (allele_ > upperBound_)
00169       allele_ = upperBound_ ;
00170 //cout << "\tMallele_ " << allele_ << endl ;    
00171 
00172     mutations ++ ;
00173   } // if    
00174   
00175   return mutations ;
00176 } // RealGene::uniformMutation
00177 
00178 
00179 double RealGene::getRealAllele() {
00180   return allele_ ;
00181 } // Gene::getRealAllele
00182 
00183 void RealGene::writeGenotype(ofstream& outputFile) {
00184   outputFile << allele_ ;
00185 } // RealGene::writeGenotype
00186 
00187 RealGene & RealGene::operator=(const RealGene& realGene) {
00188   allele_ = realGene.allele_ ;
00189 
00190   return *this ;  
00191 } // RealGene::operator=
00192 
00193 ostream& operator<< (ostream& outputStream, RealGene& gene) {
00194   outputStream << (Gene&)gene << " allele: " << gene.allele_ << " ";
00195 } // operator<< 
00196 
00197 #ifdef __MPI__
00198 void RealGene::send(int address) {
00199   MPI_Send(&allele_, 1, MPI_DOUBLE, address, 1, MPI_COMM_WORLD) ;
00200   MPI_Send(&lowerBound_, 1, MPI_DOUBLE, address, 1, MPI_COMM_WORLD) ;
00201   MPI_Send(&upperBound_, 1, MPI_DOUBLE, address, 1, MPI_COMM_WORLD) ;
00202 } // RealGene::send
00203   
00204 void RealGene::receive(int address) {
00205   MPI_Status status ;
00206     
00207   MPI_Recv(&allele_, 1, MPI_DOUBLE, address, 1, MPI_COMM_WORLD, &status) ;    
00208   MPI_Recv(&lowerBound_, 1, MPI_DOUBLE, address, 1, MPI_COMM_WORLD, &status) ;    
00209   MPI_Recv(&upperBound_, 1, MPI_DOUBLE, address, 1, MPI_COMM_WORLD, &status) ;    
00210 } // RealGene::receive
00211   
00212 void RealGene::calculateSize() {
00213   int size ;
00214     
00215   MPI_Pack_size(1, MPI_DOUBLE, MPI_COMM_WORLD, &size) ;
00216   dataSize_ = 3 * size ;
00217 }
00218 
00219 int RealGene::getSize() {
00220   return dataSize_ ;
00221 }
00222 
00223 void RealGene::packData(char * buffer, int * bufferOffset, int bufferSize) {
00224   MPI_Pack(&allele_,     1, MPI_DOUBLE, buffer, bufferSize, bufferOffset, MPI_COMM_WORLD) ;
00225   MPI_Pack(&lowerBound_, 1, MPI_DOUBLE, buffer, bufferSize, bufferOffset, MPI_COMM_WORLD) ;
00226   MPI_Pack(&upperBound_, 1, MPI_DOUBLE, buffer, bufferSize, bufferOffset, MPI_COMM_WORLD) ;
00227 } 
00228   
00229 void RealGene::unpackData(char * buffer, int * bufferOffset, int bufferSize) {
00230   MPI_Unpack(buffer, bufferSize, bufferOffset, &allele_, 1, MPI_DOUBLE, MPI_COMM_WORLD) ;
00231   MPI_Unpack(buffer, bufferSize, bufferOffset, &lowerBound_, 1, MPI_DOUBLE, MPI_COMM_WORLD) ;
00232   MPI_Unpack(buffer, bufferSize, bufferOffset, &upperBound_, 1, MPI_DOUBLE, MPI_COMM_WORLD) ;
00233 } 
00234 
00235 #endif    

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