00001 /* 00002 * @file BinaryRealGene.h 00003 * @author Antonio Jesus Nebro Urbaneja 00004 * @version 1.0 00005 * @date 29 January 2004 00006 * @brief Class representing a binary-coded real gene 00007 */ 00008 00009 #include <BinaryRealGene.h> 00010 00017 BinaryRealGene::BinaryRealGene(int numberOfBits, 00018 double lowerBound , 00019 double upperBound, 00020 Random * random) : 00021 Gene(BINARY_REAL, random) { 00022 int i ; 00023 00024 upperBound_ = upperBound ; 00025 lowerBound_ = lowerBound ; 00026 numberOfBits_ = numberOfBits ; 00027 00028 binaryAllele_ = new char[numberOfBits] ; 00029 00030 if (binaryAllele_ == NULL) { 00031 cerr << "BinaryRealGene::BinaryRealGene-> Error when asking for " 00032 << "memory" << endl ; 00033 exit(-1) ; 00034 } // if 00035 00036 for (i = 0; i < numberOfBits; i++) 00037 if (random_->rnd(0,1) == 1) 00038 binaryAllele_[i] = '1' ; 00039 else 00040 binaryAllele_[i] = '0' ; 00041 00042 realAllele_ = decodeToReal() ; 00043 } // BinaryRealGene::BinaryRealGene 00044 00051 BinaryRealGene::BinaryRealGene(BinaryRealGene & gene) : 00052 Gene(gene) { 00053 int i ; 00054 00055 upperBound_ = gene.upperBound_ ; 00056 lowerBound_ = gene.lowerBound_ ; 00057 numberOfBits_ = gene.numberOfBits_ ; 00058 binaryAllele_ = new char[numberOfBits_] ; 00059 00060 if (binaryAllele_ == NULL) { 00061 cerr << "BinaryRealGene::BinaryRealGene-> Error when asking for " 00062 << "memory" << endl ; 00063 exit(-1) ; 00064 } // if 00065 00066 for (i = 0; i < numberOfBits_; i++) 00067 binaryAllele_[i] = gene.binaryAllele_[i] ; 00068 00069 realAllele_ = gene.realAllele_ ; 00070 } // BinaryRealGene::BinaryRealGene 00071 00072 00080 BinaryRealGene::BinaryRealGene(BinaryRealGene * gene) : Gene(gene) { 00081 int i ; 00082 00083 upperBound_ = gene->upperBound_ ; 00084 lowerBound_ = gene->lowerBound_ ; 00085 numberOfBits_ = gene->numberOfBits_ ; 00086 binaryAllele_ = new char[numberOfBits_] ; 00087 00088 if (binaryAllele_ == NULL) { 00089 cerr << "BinaryRealGene::BinaryRealGene-> Error when asking for " 00090 << "memory" << endl ; 00091 exit(-1) ; 00092 } // if 00093 00094 for (i = 0; i < numberOfBits_; i++) 00095 binaryAllele_[i] = gene->binaryAllele_[i] ; 00096 00097 realAllele_ = gene->realAllele_ ; 00098 } // BinaryRealGene::BinaryRealGene 00099 00105 BinaryRealGene::~BinaryRealGene() { 00106 delete [] binaryAllele_ ; 00107 } // BinaryRealGene::~BinaryRealGene 00108 00109 BinaryRealGene & BinaryRealGene::operator=(const BinaryRealGene& gene) { 00110 int i ; 00111 00112 upperBound_ = gene.upperBound_ ; 00113 lowerBound_ = gene.lowerBound_ ; 00114 numberOfBits_ = gene.numberOfBits_ ; 00115 for (i = 0; i < numberOfBits_; i++) 00116 binaryAllele_[i] = gene.binaryAllele_[i] ; 00117 realAllele_ = gene.realAllele_ ; 00118 00119 return *this ; 00120 } // BinaryRealGene::operator= 00121 00122 int BinaryRealGene::bitFlipMutation(double mutationProbability) { 00123 int mutations ; 00124 int i ; 00125 mutations = 0 ; 00126 00127 for (i = 0; i < numberOfBits_ ; i++) 00128 if (random_->flip(mutationProbability) == 1) { 00129 mutations ++ ; 00130 if (binaryAllele_[i] == '1') 00131 binaryAllele_[i] = '0' ; 00132 else 00133 binaryAllele_[i] = '1' ; 00134 } //if 00135 00136 realAllele_ = decodeToReal() ; 00137 return mutations ; 00138 } // BinaryRealGene::bitFlipMutation 00139 00140 double BinaryRealGene::getRealAllele() { 00141 return realAllele_ ; 00142 } // BinaryRealGene::getRealAllele 00143 00144 void BinaryRealGene::writeGenotype(ofstream& outputFile) { 00145 outputFile << realAllele_ ; 00146 } // BinaryRealGene::writeGenotype 00147 00148 00149 ostream& operator<< (ostream& outputStream, BinaryRealGene& gene) { 00150 int i ; 00151 00152 outputStream << (Gene&)gene << " Real allele: " << gene.realAllele_ ; 00153 00154 outputStream << " Bits: " << gene.numberOfBits_ << " Binary allele: " ; 00155 for (i = 0 ; i < gene.numberOfBits_; i++) 00156 if (gene.binaryAllele_[i] == '1') 00157 outputStream << "1" ; 00158 else 00159 outputStream << "0" ; 00160 outputStream << endl ; 00161 } // operator<< 00162 00163 double BinaryRealGene::decodeToReal() { 00164 int i ; 00165 double realValue ; 00166 00167 realValue = 0.0 ; 00168 for (i = 0; i < numberOfBits_ ;i++) { 00169 if (binaryAllele_[i] == '1') 00170 realValue += pow((double)2.0, i) ; 00171 } // for 00172 00173 return (lowerBound_ + realValue*(upperBound_ - lowerBound_) / 00174 (pow(2.0, numberOfBits_) - 1)) ; 00175 } // BinaryRealGene::decodeGene 00176 00177