00001 #include <BinaryGrayRealGene.h> 00002 00003 BinaryGrayRealGene::BinaryGrayRealGene(int numberOfBits, 00004 double lowerBound , 00005 double upperBound, 00006 Random * random) : 00007 Gene(BINARY_GRAY_REAL, random) { 00008 int i ; 00009 00010 upperBound_ = upperBound ; 00011 lowerBound_ = lowerBound ; 00012 numberOfBits_ = numberOfBits ; 00013 00014 binaryAllele_ = new char[numberOfBits] ; 00015 00016 if (binaryAllele_ == NULL) { 00017 cerr << "BinaryGrayRealGene::BinaryGrayRealGene-> Error when asking for " 00018 << "memory" << endl ; 00019 exit(-1) ; 00020 } // if 00021 00022 for (i = 0; i < numberOfBits; i++) 00023 if (random_->rnd(0,1) == 1) 00024 binaryAllele_[i] = '1' ; 00025 else 00026 binaryAllele_[i] = '0' ; 00027 00028 realAllele_ = random_->rndreal(lowerBound, upperBound) ; 00029 } // RealGene::RealGene 00030 00037 BinaryGrayRealGene::BinaryGrayRealGene(BinaryGrayRealGene & gene) : 00038 Gene(gene) { 00039 int i ; 00040 00041 upperBound_ = gene.upperBound_ ; 00042 lowerBound_ = gene.lowerBound_ ; 00043 numberOfBits_ = gene.numberOfBits_ ; 00044 binaryAllele_ = new char[numberOfBits_] ; 00045 00046 if (binaryAllele_ == NULL) { 00047 cerr << "BinaryGrayRealGene::BinaryGrayRealGene-> Error when asking for " 00048 << "memory" << endl ; 00049 exit(-1) ; 00050 } // if 00051 00052 for (i = 0; i < numberOfBits_; i++) 00053 binaryAllele_[i] = gene.binaryAllele_[i] ; 00054 00055 realAllele_ = gene.realAllele_ ; 00056 } // BinaryGrayRealGene::BinaryGrayRealGene 00057 00058 00066 BinaryGrayRealGene::BinaryGrayRealGene(BinaryGrayRealGene * gene) : 00067 Gene(gene) { 00068 int i ; 00069 00070 upperBound_ = gene->upperBound_ ; 00071 lowerBound_ = gene->lowerBound_ ; 00072 numberOfBits_ = gene->numberOfBits_ ; 00073 binaryAllele_ = new char[numberOfBits_] ; 00074 00075 if (binaryAllele_ == NULL) { 00076 cerr << "BinaryGrayRealGene::BinaryGrayRealGene-> Error when asking for " 00077 << "memory" << endl ; 00078 exit(-1) ; 00079 } // if 00080 00081 for (i = 0; i < numberOfBits_; i++) 00082 binaryAllele_[i] = gene->binaryAllele_[i] ; 00083 00084 realAllele_ = gene->realAllele_ ; 00085 } // BinaryGrayRealGene::BinaryGrayRealGene 00086 00087 00088 BinaryGrayRealGene & BinaryGrayRealGene::operator=(const BinaryGrayRealGene& 00089 gene) { 00090 int i ; 00091 00092 upperBound_ = gene.upperBound_ ; 00093 lowerBound_ = gene.lowerBound_ ; 00094 numberOfBits_ = gene.numberOfBits_ ; 00095 for (i = 0; i < numberOfBits_; i++) 00096 binaryAllele_[i] = gene.binaryAllele_[i] ; 00097 realAllele_ = gene.realAllele_ ; 00098 00099 return *this ; 00100 } // BinaryGrayRealGene::operator= 00101 00102 int BinaryGrayRealGene::bitFlipMutation(double mutationProbability) { 00103 int mutations ; 00104 int i ; 00105 cout << "To be implemented" << endl ; 00106 return 0 ; 00107 mutations = 0 ; 00108 00109 for (int i = 0; i < numberOfBits_ ; i++) 00110 if (random_->flip(mutationProbability) == 1) { 00111 mutations ++ ; 00112 if (binaryAllele_[i] == '1') 00113 binaryAllele_[i] == '0' ; 00114 else 00115 binaryAllele_[i] == '1' ; 00116 } //if 00117 00118 realAllele_ = random_->rndreal(lowerBound_, upperBound_) ; 00119 cout << "To be implemented" << endl ; 00120 return mutations ; 00121 } // BinaryGrayRealGene::bitFlipMutation 00122 00123 void BinaryGrayRealGene::writeGenotype(ofstream &outputFile) { 00124 cout << "To be implemented" << endl ; 00125 } // BinaryGrayRealGene::writeGenotype 00126 00127 ostream& operator<< (ostream& outputStream, BinaryGrayRealGene& gene) { 00128 int i ; 00129 00130 outputStream << (Gene&)gene << " Real allele: " << gene.realAllele_ ; 00131 00132 outputStream << " Bits: " << gene.numberOfBits_ << " Binary allele: " ; 00133 for (i = 0 ; i < gene.numberOfBits_; i++) 00134 if (gene.binaryAllele_[i] == '1') 00135 outputStream << "1" ; 00136 else 00137 outputStream << "0" ; 00138 outputStream << endl ; 00139 } // operator<< 00140 00141 double BinaryGrayRealGene::getRealAllele() { 00142 return realAllele_ ; 00143 } // Gene::getRealAllele 00144 00145 00146 void BinaryGrayRealGene::decodeGene(double lowerBound, double upperBound) { 00147 } // BinaryGrayRealGene::decodeGene