• Main Page
  • Related Pages
  • Classes
  • Files
  • Examples
  • File List
  • File Members

ChromosomeT_double.cpp

Go to the documentation of this file.
00001 
00043 #include <LinAlg/LinAlg.h>
00044 #include <EALib/ChromosomeT.h>
00045 
00046 //===========================================================================
00047 
00048 ChromosomeT< double >::~ChromosomeT()
00049 {}
00050 
00051 //===========================================================================
00052 
00053 inline unsigned long pow2(unsigned n)
00054 {
00055     return 1UL << n;
00056 }
00057 
00058 void ChromosomeT< double >::decodeBinary(const Chromosome& chrom,
00059         const Interval&   range,
00060         unsigned          nbits,
00061         bool              useGray)
00062 {
00063     SIZE_CHECK(chrom.size() % nbits == 0)
00064 
00065     const std::vector< bool >& src = dynamic_cast< const std::vector< bool >& >(chrom);
00066 
00067     double stepSize = (pow2(nbits) - 1) / range.width();
00068     unsigned i, j, k;
00069     unsigned long l, m;
00070 
00071     resize(src.size() / nbits);
00072 
00073 
00074     for (j = size(), k = src.size(); j--;) {
00075         if (useGray)
00076             for (l = m = 0, i = nbits; i--;)
00077                 l = (l << 1) | (m ^= (src[ --k ] ? 1 : 0));
00078         else
00079             for (l = 0, i = nbits; i--;)
00080                 l = (l << 1) | (src[ --k ] ? 1 : 0);
00081 
00082         (*this)[ j ] = l / stepSize + range.lowerBound();
00083     }
00084 }
00085 
00086 //===========================================================================
00087 
00088 void ChromosomeT< double >::accumulate(const std::vector< double >& acc, double c)
00089 {
00090     SIZE_CHECK(size() == acc.size())
00091 
00092     for (unsigned i = size(); i--;)
00093         (*this)[ i ] = (1 - c) * (*this)[ i ] + c * acc[ i ];
00094 }
00095 
00096 //===========================================================================
00097 
00098 void ChromosomeT< double >::accumulate(const Chromosome& acc, double c)
00099 {
00100     accumulate(dynamic_cast< const std::vector< double >& >(acc), c);
00101 }
00102 
00103 //===========================================================================
00104 
00105 void ChromosomeT< double >::mutateNormal(double variance)
00106 {
00107     for (unsigned i = size(); i--;)
00108         (*this)[ i ] += Rng::gauss(0, variance);
00109 }
00110 
00111 //===========================================================================
00112 
00113 void ChromosomeT< double >::mutateNormal(const std::vector< double >& variances, bool cycle)
00114 {
00115     RANGE_CHECK(variances.size() <= size())
00116 
00117     for (unsigned i = cycle ? size() : variances.size(); i--;)
00118         (*this)[ i ] += Rng::gauss(0, variances[ i % variances.size()]);
00119 }
00120 
00121 //===========================================================================
00122 
00123 void ChromosomeT< double >::mutateNormal(const ChromosomeT< double >& variances,
00124         bool cycle)
00125 {
00126     mutateNormal(static_cast< const std::vector< double >& >(variances), cycle);
00127 }
00128 
00129 //===========================================================================
00130 
00131 void ChromosomeT< double >::mutateNormal(const Chromosome& variances,
00132         bool cycle)
00133 {
00134     mutateNormal(dynamic_cast< const std::vector< double >& >(variances), cycle);
00135 }
00136 
00137 //===========================================================================
00138 
00139 void ChromosomeT< double >::mutateCauchy(double scale)
00140 {
00141     for (unsigned i = size(); i--;)
00142         (*this)[ i ] += Rng::cauchy() * scale;
00143 }
00144 
00145 //===========================================================================
00146 
00147 void ChromosomeT< double >::mutateCauchy(const std::vector< double >& scale,
00148         bool cycle)
00149 {
00150     RANGE_CHECK(scale.size() <= size())
00151 
00152     for (unsigned i = cycle ? size() : scale.size(); i--;)
00153         (*this)[ i ] += Rng::cauchy() * scale[ i % scale.size()];
00154 }
00155 
00156 //===========================================================================
00157 
00158 void ChromosomeT< double >::mutateCauchy(const ChromosomeT< double >& scale,
00159         bool cycle)
00160 {
00161     mutateNormal(static_cast< const std::vector< double >& >(scale), cycle);
00162 }
00163 
00164 //===========================================================================
00165 
00166 void ChromosomeT< double >::mutateCauchy(const Chromosome& scale,
00167         bool cycle)
00168 {
00169     mutateNormal(dynamic_cast< const std::vector< double >& >(scale), cycle);
00170 }
00171 
00172 //===========================================================================
00173 
00174 void ChromosomeT< double >::mutateNormalRotAngles(const Chromosome& sigma,
00175         const Chromosome& alpha)
00176 {
00177     mutateNormalRotAngles(dynamic_cast< const std::vector< double >& >(sigma),
00178                           dynamic_cast< const std::vector< double >& >(alpha));
00179 }
00180 
00181 //===========================================================================
00182 
00183 void
00184 ChromosomeT< double >::mutateNormalRotAngles(const std::vector< double >& sigma_sqr,
00185         const std::vector< double >& alpha)
00186 {
00187     RANGE_CHECK(sigma_sqr.size() > 0 || size() == 0)
00188 
00189     register unsigned m, i, j, k;
00190 
00191     std::vector< double >& v = *this;
00192     std::vector< double > z(v.size());
00193 
00194     //
00195     // create random vector z
00196     //
00197     for (m = 0; m < z.size() && m < sigma_sqr.size(); m++)
00198         z[ m ] = Rng::gauss(0, sigma_sqr[ m ]);
00199 
00200     for (; m < z.size(); m++)
00201         z[ m ] = Rng::gauss(0, sigma_sqr[ sigma_sqr.size() - 1 ]);
00202 
00203     //
00204     // rotate vector z
00205     //
00206     for (k = 0, j = 1; j < m && k < alpha.size(); j++)
00207         for (i = 0; i < m - j && k < alpha.size(); i++, k++) {
00208             double sina = sin(alpha[ k ]);
00209             double cosa = cos(alpha[ k ]);
00210             double t    = cosa * z[ i ] - sina * z[ i + j ];
00211             z[ i + j ]  = sina * z[ i ] + cosa * z[ i + j ];
00212             z[ i     ]  = t;
00213         }
00214 
00215     //
00216     // mutate *this with rotated random vector z
00217     //
00218     for (m = v.size(); m--;)
00219         v[ m ] += z[ m ];
00220 }
00221 
00222 //===========================================================================
00223 
00224 void ChromosomeT< double >::mutateLogNormal(double overallVariance,
00225         double indivVariance)
00226 {
00227     double overall = Rng::gauss(0, overallVariance);
00228 
00229     for (unsigned i = size(); i--;)
00230         (*this)[ i ] *= exp(overall + Rng::gauss(0, indivVariance));
00231 }
00232 
00233 //===========================================================================
00234 
00235 void
00236 ChromosomeT< double >::recombineIntermediate(const Chromosome& dadChrom,
00237         const Chromosome& momChrom)
00238 {
00239     SIZE_CHECK(dadChrom.size() == momChrom.size())
00240 
00241     const std::vector< double >& dad = dynamic_cast< const std::vector< double >& >(dadChrom);
00242     const std::vector< double >& mom = dynamic_cast< const std::vector< double >& >(momChrom);
00243 
00244     resize(dad.size());
00245 
00246     for (unsigned i = size(); i--;)
00247         (*this)[ i ] = (dad[ i ] + mom[ i ]) / 2.;
00248 }
00249 
00250 //===========================================================================
00251 //
00252 // generalized intermediate recombination
00253 //
00254 void
00255 ChromosomeT< double >::recombineGenIntermediate(const Chromosome& dadChrom,
00256         const Chromosome& momChrom)
00257 {
00258     SIZE_CHECK(dadChrom.size() == momChrom.size())
00259 
00260     const std::vector< double >& dad = dynamic_cast< const std::vector< double >& >(dadChrom);
00261     const std::vector< double >& mom = dynamic_cast< const std::vector< double >& >(momChrom);
00262 
00263     resize(dad.size());
00264 
00265     for (unsigned i = size(); i--;)
00266         (*this)[ i ] = Rng::uni(dad[ i ], mom[ i ]);
00267 }
00268 
00269 //===========================================================================
00270 //
00271 // generalized intermediate recombination
00272 //
00273 void
00274 ChromosomeT< double >::recombineGeomIntermediate(const Chromosome& dadChrom,
00275         const Chromosome& momChrom)
00276 {
00277     SIZE_CHECK(dadChrom.size() == momChrom.size())
00278 
00279     const std::vector< double >& dad = dynamic_cast< const std::vector< double >& >(dadChrom);
00280     const std::vector< double >& mom = dynamic_cast< const std::vector< double >& >(momChrom);
00281 
00282     resize(dad.size());
00283 
00284     for (unsigned i = size(); i--;)
00285         (*this)[ i ] = sqrt(dad[ i ] * mom[ i ]);
00286 }
00287 
00288 //===========================================================================
00289 
00290 void ChromosomeT< double >::recombineIntermediate(Chromosome& mate)
00291 {
00292     SIZE_CHECK(size() == mate.size())
00293 
00294     std::vector< double >& v = *this;
00295     std::vector< double >& w = dynamic_cast< std::vector< double >& >(mate);
00296 
00297     for (unsigned i = v.size(); i--;)
00298         v[ i ] = w[ i ] = (v[ i ] + w[ i ]) / 2.;
00299 }
00300 
00301 //===========================================================================
00302 //
00303 // generalized intermediate recombination
00304 //
00305 void ChromosomeT< double >::recombineGenIntermediate(Chromosome& mate)
00306 {
00307     SIZE_CHECK(size() == mate.size())
00308 
00309     std::vector< double >& v = *this;
00310     std::vector< double >& w = dynamic_cast< std::vector< double >& >(mate);
00311 
00312     for (unsigned i = v.size(); i--;) {
00313         double a = v[ i ];
00314         double b = w[ i ];
00315         v[ i ] = Rng::uni(a, b);
00316         w[ i ] = Rng::uni(a, b);
00317     }
00318 }
00319 
00320 //===========================================================================
00321 //
00322 // generalized intermediate recombination
00323 //
00324 void ChromosomeT< double >::recombineGeomIntermediate(Chromosome& mate)
00325 {
00326     SIZE_CHECK(size() == mate.size())
00327 
00328     std::vector< double >& v = *this;
00329     std::vector< double >& w = dynamic_cast< std::vector< double >& >(mate);
00330 
00331     for (unsigned i = v.size(); i--;)
00332         v[ i ] = w[ i ] = sqrt(v[ i ] * w[ i ]);
00333 }
00334 
00335 //===========================================================================
00336 
00337 void ChromosomeT< double >::mutateRotate(ChromosomeT<double> & sigma)
00338 {
00339     int    Dimension   = (*this).size();
00340     double epsi       = 0.0001;
00341     int    sigmaCheck = 0;
00342     double tau1       = 1. / sqrt(2. * Dimension);
00343     double tau2       = 1. / sqrt(2. * sqrt(Dimension * 1.0));
00344     double beta       = 0.0873;
00345 
00346     mutateRotate(sigma, tau1, tau2, beta, sigmaCheck, epsi);
00347 
00348 }
00349 
00350 //=============================================================
00351 
00352 void ChromosomeT< double >::mutateRotate(ChromosomeT<double> & sigma_sqr,
00353         double tau1, double tau2,
00354         double beta, int sigmaCheck,
00355         double epsi)
00356 
00357 {
00358 
00359     int    Dimension   = (*this).size();
00360     double alpha = 0, *s, *ss, rdno;
00361 
00362     int i, j, k, index = 0, sign_of_sigma;
00363 
00364     s       = new double[ Dimension ];
00365     ss    = new double[ Dimension ];
00366 
00367     // here the adaptation parameters are mutated first!
00368     rdno = Rng::gauss(0, 1);
00369     for (i = 0; i < Dimension; i++) {
00370         sigma_sqr[ i ] *= exp(tau1 * rdno + tau2 * Rng::gauss(0, 1));
00371         // check that sigma is not too small!
00372         if ((sigmaCheck) && (sigma_sqr[ i ] < epsi*fabs((*this)[ i ])))
00373             sigma_sqr[ i ] = epsi * fabs((*this)[ i ]);
00374     }
00375     for (i = 0; i < Dimension - 1; i++) {
00376         for (j = i + 1; j < Dimension; j++) {
00377             index = int(Dimension * (i + 1) - i - 1 + j - 0.5 * i * (i + 1));
00378             sigma_sqr[ index ] += beta * Rng::gauss(0, 1);
00379             // check that angles are within limits!
00380             if (fabs(sigma_sqr[ index ]) > 3.141592654) {
00381                 if (sigma_sqr[ index ] >= 0) sign_of_sigma = 1; else sign_of_sigma = -1;
00382                 sigma_sqr[ index ] = sigma_sqr[ index ] - 2 * 3.141592654
00383                                      * sign_of_sigma;
00384             }
00385         }
00386     }
00387 
00388     // draw rd. numbers according to sigma values!
00389     for (k = 0; k < Dimension; k++) {
00390         s[ k ] = Rng::gauss(0, sigma_sqr[ k ]);
00391         ss[k]  = 0;
00392     }
00393 
00394     // do the funny rotation bits!
00395     for (i = 0; i < Dimension - 1; i++) {
00396         for (j = i + 1; j < Dimension; j++) {
00397             for (k = 0; k < Dimension; k++) {
00398                 alpha = sigma_sqr[ int(Dimension*(i+1) -i-1+j -0.5*i*(i+1))];
00399                 if (i == k)
00400                     ss[ k ] = s[ k ] * cos(alpha) - s[ j ] * sin(alpha);
00401                 else
00402                     if (j == k)
00403                         ss[ k ] = s[ i ] * sin(alpha) + s[ k ] * cos(alpha);
00404                     else
00405                         ss[k] = s[k];
00406             }
00407             for (k = 0; k < Dimension; k++) s[k] = ss[k];
00408         }
00409     }
00410 
00411     // change the objective parameters
00412     for (i = 0; i < Dimension; i++)
00413         (*this)[ i ] += s[ i ];
00414 
00415     delete[ ] s;
00416     delete[ ] ss;
00417 }
00418 
00419 //=============================================================
00420 
00421 void ChromosomeT< double >::initializeRotate(double SigmaMin,
00422         double SigmaMax)
00423 {
00424     int i, j;
00425     int Dimension = int(-0.5 + sqrt(0.25 + 2 * (*this).size()));
00426 
00427     for (i = 0; i < Dimension; i++)
00428         (*this)[ i ] = Rng::uni(SigmaMin, SigmaMax);;
00429 
00430     for (i = 0; i < Dimension - 1; i++)
00431         for (j = i + 1; j < Dimension; j++)
00432             (*this)[ int(Dimension*(i+1) -i-1+j -0.5*i*(i+1))] = 0;
00433 
00434 }
00435 
00436 //=============================================================
00437 
00438 void ChromosomeT< double >::initializeRotate(const ChromosomeT< double >&
00439         sigma)
00440 {
00441 
00442     initializeRotate(static_cast< const std::vector< double >& >(sigma));
00443 
00444 }
00445 //=============================================================
00446 
00447 void ChromosomeT< double >::initializeRotate(const std::vector< double >&
00448         sigma)
00449 {
00450 
00451     int i;
00452 
00453     for (i = 0; i < int(sigma.size()); i++)(* this)[ i ] = sigma[ i ];
00454 
00455 }
00456 
00457 //=============================================================
00458 
00459 void ChromosomeT< double >::showRotate()
00460 {
00461 
00462     int i, j;
00463 
00464     int n   = int((*this).size());
00465     int dim = int(- 0.5 + sqrt(2.0 * n + 0.25));
00466 
00467     std::cout << "sigma       = ";
00468     for (i = 0; i < dim; i++)
00469         std::cout << (*this)[ i ] << "  ";
00470     std::cout << std::endl;
00471     std::cout << "alpha       = ";
00472     for (i = 0; i < dim - 1; i++) {
00473         for (j = i + 1; j < dim; j++) {
00474             std::cout << (*this)[ int(dim*(i+1) -i-1+j -0.5*i*(i+1))] << "  ";
00475         }
00476         std::cout << std::endl << "             ";
00477     }
00478     std::cout << std::endl;
00479 
00480 }
00481 
00482 //=============================================================
00483 
00484 
00485 void ChromosomeT< double >::mutateMSR(double xi_prob)
00486 {
00487 
00488         int    Dimension   = (*this).size();
00489         double xi;
00490 
00491         for (int i = 0; i < Dimension; i++) {
00492                 if (Rng::coinToss(xi_prob)) xi = 1.5;
00493                 else xi = 1 / 1.5;
00494                 (*this)[ i ] *= xi;
00495         }
00496 
00497 }
00498 
00499 //=============================================================
00500 
00501 // for unconstrained problems
00502 void ChromosomeT< double >::SBX(ChromosomeT< double >& mate,
00503                                 double nc, double p)
00504 {
00505     unsigned i, n = (*this).size();
00506     double beta, x, u = 0.;
00507 
00508     if (n != mate.size()) {
00509       throw SHARKEXCEPTION ("SBX is only defined for chromosomes of equal length");
00510     }
00511 
00512     for (i = 0; i < n; i++) {
00513         if (Rng::coinToss(p)) {
00514             do {
00515                 u = Rng::uni(0, 1);
00516             }
00517             while (u == 1.);
00518             if (u <= .5)
00519                 beta = pow(2 * u, 1. / (nc + 1.));
00520             else
00521                 beta = pow(1. / (2 - 2 * u), 1. / (nc + 1.));
00522             x = (*this)[i];
00523             (*this)[i] = .5 * ((1 + beta) * x + (1 - beta) * mate[i]);
00524             mate[i]    = .5 * ((1 - beta) * x + (1 + beta) * mate[i]);
00525         }
00526     }
00527 }
00528 
00529 // for constrained problems
00530 void ChromosomeT< double >::SBX(ChromosomeT< double >& mate ,
00531                                 double lower,
00532                                 double upper,
00533                                 double nc, double p,
00534                                 double epsilon)
00535 {
00536     unsigned i, n = (*this).size();
00537     double beta, betaQ, alpha, expp, y1 = 0, y2 = 0, u = 0.;
00538 
00539     if (n != mate.size()) {
00540       throw SHARKEXCEPTION("SBX is only defined for chromosomes of equal length");
00541     }
00542 
00543     for (i = 0; i < n; i++) {
00544         if (Rng::coinToss(p)) {
00545             if (mate[i] < (*this)[i]) {
00546                 y1 = mate[i];
00547                 y2 = (*this)[i];
00548             }
00549             else {
00550                 y1 = (*this)[i];
00551                 y2 = mate[i];
00552             }
00553             if (fabs(y2 - y1) > epsilon) { //  -> from Deb's implementation, not contained in any paper: prevents division by zero
00554                 // Find beta value
00555                 if ((y1 -  lower) > (upper - y2)) {
00556                     beta = 1 + (2 * (upper - y2) / (y2 - y1));
00557                 }
00558                 else {
00559                     beta = 1 + (2 * (y1 -  lower) / (y2 - y1));
00560                 }
00561 
00562                 expp = (nc + 1.);
00563                 beta = 1. / beta;
00564 
00565                 // Find alpha
00566                 alpha = 2. - pow(beta , expp) ;
00567 
00568                 if (alpha < 0.0) {
00569                   throw SHARKEXCEPTION( "Error in ChromosomeT_double::SBX alpha<0");
00570                 }
00571 
00572                 expp = 1. / expp;
00573 
00574                 u = Rng::uni(0, 1);
00575                 //  -> from Deb's implementation, not contained in any paper
00576                 // do { u = Rng::uni(0, 1); } while(u == 1.);
00577 
00578                 if (u <= 1. / alpha) {
00579                     alpha *= u;
00580                     betaQ = pow(alpha, expp);
00581                 }
00582                 else {
00583                     alpha *= u;
00584                     alpha = 1. / (2. - alpha);
00585                     if (alpha < 0.0) {
00586                       throw SHARKEXCEPTION( "Error in ChromosomeT_double::SBX alpha<0");
00587                     }
00588                     betaQ = pow(alpha, expp);
00589                 }
00590             }
00591             else { // if genes are equal -> from Deb's implementation, not contained in any paper
00592                 betaQ = 1.;
00593             }
00594 
00595             (*this)[i]   = .5 * ((y1 + y2) - betaQ * (y2 - y1));
00596             mate[i]        = .5 * ((y1 + y2) + betaQ * (y2 - y1));
00597 
00598             //  -> from Deb's implementation, not contained in any paper
00599             if ((*this)[i] < lower)(*this)[i] = lower;
00600             if ((*this)[i] > upper)(*this)[i] = upper;
00601             if (mate[i]      < lower) mate[i]      = lower;
00602             if (mate[i]      > upper) mate[i]      = upper;
00603         }
00604     }
00605 }
00606 // for constrained problems
00607 void ChromosomeT< double >::SBX(ChromosomeT< double >& mate ,
00608                                 std::vector<double > & lower,
00609                                 std::vector<double > & upper,
00610                                 double nc, double p,
00611                                 double epsilon)
00612 {
00613     unsigned i, n = (*this).size();
00614     double beta, betaQ, alpha, expp, y1 = 0, y2 = 0, u = 0.;
00615 
00616     if (n != mate.size()) {
00617       throw SHARKEXCEPTION("SBX is only defined for chromosomes of equal length");
00618     }
00619 
00620     for (i = 0; i < n; i++) {
00621         if (Rng::coinToss(p)) {
00622             if (mate[i] < (*this)[i]) {
00623                 y1 = mate[i];
00624                 y2 = (*this)[i];
00625             }
00626             else {
00627                 y1 = (*this)[i];
00628                 y2 = mate[i];
00629             }
00630             if (fabs(y2 - y1) > epsilon) { //  -> from Deb's implementation, not contained in any paper: prevents division by zero
00631 
00632                 // Find beta value
00633                 if ((y1 - lower[i]) > (upper[i] - y2))
00634                     beta = 1 + (2 * (upper[i] - y2) / (y2 - y1));
00635                 else
00636                     beta = 1 + (2 * (y1 - lower[i]) / (y2 - y1));
00637 
00638 
00639                 expp = (nc + 1.);
00640                 beta = 1. / beta;
00641 
00642                 // Find alpha
00643                 alpha = 2. - pow(beta , expp) ;
00644 
00645                 if (alpha < 0.0) {
00646                   throw SHARKEXCEPTION( "Error in ChromosomeT_double::SBX alpha<0");
00647                 }
00648 
00649                 expp = 1. / expp;
00650 
00651                 u = Rng::uni(0, 1);
00652 
00653                 //  -> from Deb's implementation, not contained in any paper
00654                 // do { u = Rng::uni(0, 1); } while(u == 1.);
00655 
00656                 if (u <= 1. / alpha) {
00657                     alpha *= u;
00658                     betaQ = pow(alpha, expp);
00659                 }
00660                 else {
00661                     alpha *= u;
00662                     alpha = 1. / (2. - alpha);
00663                     if (alpha < 0.0) {
00664                       throw SHARKEXCEPTION( "Error in ChromosomeT_double::SBX alpha<0");
00665                     }
00666                     betaQ = pow(alpha, expp);
00667                 }
00668             }
00669             else { // if genes are equal -> from Deb's implementation, not contained in any paper
00670                 betaQ = 1.;
00671             }
00672 
00673             (*this)[i]   = .5 * ((y1 + y2) - betaQ * (y2 - y1));
00674             mate[i]        = .5 * ((y1 + y2) + betaQ * (y2 - y1));
00675 
00676             //  -> from Deb's implementation, not contained in any paper
00677             if ((*this)[i] < lower[i])(*this)[i] = lower[i];
00678             if ((*this)[i] > upper[i])(*this)[i] = upper[i];
00679             if (mate[i]      < lower[i]) mate[i]      = lower[i];
00680             if (mate[i]      > upper[i]) mate[i]      = upper[i];
00681         }
00682     }
00683 }
00684 
00685 // for unconstrained problems
00686 void ChromosomeT< double >::simpleMutatePolynomial(double lower, double upper,
00687         double nm, double p)
00688 {
00689     unsigned i, n = (*this).size();
00690     double delta,  r = 0.;
00691 
00692     for (i = 0; i < n; i++) {
00693         if (Rng::coinToss(p)) {
00694             r = Rng::uni(0, 1);
00695             if (r < .5)
00696                 delta = pow(2. * r, 1. / (nm + 1.)) - 1;
00697             else
00698                 delta = 1 - pow(2. - 2. * r, 1. / (nm + 1.));
00699             (*this)[i] += delta * (upper - lower);
00700         }
00701     }
00702 }
00703 // for unconstrained problems
00704 void ChromosomeT< double >::simpleMutatePolynomial(std::vector<double > &lower,
00705         std::vector<double > &upper,
00706         double nm, double p)
00707 {
00708     unsigned i, n = (*this).size();
00709     double delta,  r = 0.;
00710 
00711     for (i = 0; i < n; i++) {
00712         if (Rng::coinToss(p)) {
00713             r = Rng::uni(0, 1);
00714             if (r < .5)
00715                 delta = pow(2 * r, 1. / (nm + 1.)) - 1;
00716             else
00717                 delta = 1 - pow(2 - 2 * r, 1. / (nm + 1.));
00718             (*this)[i] += delta * (upper[i] - lower[i]);
00719         }
00720     }
00721 }
00722 // for constrained problems
00723 void ChromosomeT< double >::mutatePolynomial(double lower,
00724         double upper,
00725         double nm, double p)
00726 {
00727     unsigned i, n = (*this).size();
00728     double delta, deltaQ, expp,  u = 0.;
00729 
00730     for (i = 0; i < n; i++) {
00731         if (Rng::coinToss(p)) {
00732             u  = Rng::uni(0, 1);
00733 
00734             if ((*this)[i] <=  lower || (*this)[i] >= upper) { //  -> from Deb's implementation, not contained in any paper
00735                 (*this)[i] = u * (upper -  lower) +  lower;
00736 #ifdef DEBUG
00737                 std::cerr << "Warning: parameter out of bounds, random resetting ..." << std::endl;
00738 #endif
00739             }
00740             else {
00741                 // Calculate delta
00742                 if (((*this)[i] -  lower) < (upper - (*this)[i]))
00743                     delta = ((*this)[i] -  lower) / (upper -  lower);
00744                 else
00745                     delta = (upper - (*this)[i]) / (upper -  lower);
00746 
00747                 delta = 1. - delta;
00748                 expp  = (nm + 1.);
00749                 delta = pow(delta , expp);
00750                 expp  = 1. / expp;
00751 
00752                 if (u <= .5) {
00753                     deltaQ =  2. * u + (1 - 2. * u) * delta;
00754                     deltaQ = pow(deltaQ, expp) - 1. ;
00755                 }
00756                 else {
00757                     deltaQ = 2. - 2. * u + 2. * (u  - .5) * delta;
00758                     deltaQ = 1. - pow(deltaQ , expp);
00759                 }
00760 
00761                 (*this)[i] += deltaQ * (upper -  lower);
00762 
00763                 //  -> from Deb's implementation, not contained in any paper
00764                 if ((*this)[i] < lower)(*this)[i] = lower;
00765                 if ((*this)[i] > upper)(*this)[i] = upper;
00766             }
00767         }
00768     }
00769 }
00770 // for constrained problems
00771 void ChromosomeT< double >::mutatePolynomial(std::vector<double > &lower,
00772         std::vector<double > &upper,
00773         double nm, double p)
00774 {
00775     unsigned i, n = (*this).size();
00776     double delta, deltaQ, expp,  u = 0.;
00777 
00778 
00779     for (i = 0; i < n; i++) {
00780 
00781         if (Rng::coinToss(p)) {
00782             u  = Rng::uni(0, 1);
00783             if ((*this)[i] <= lower[i] || (*this)[i] >= upper[i]) { //  -> from Deb's implementation, not contained in any paper
00784                 (*this)[i] = u * (upper[i] - lower[i]) + lower[i];
00785 #ifdef DEBUG
00786                 std::cerr << "Warning: parameter out of bounds, random resetting ..." << std::endl;
00787 #endif
00788             }
00789             else {
00790                 // Calculate delta
00791                 if (((*this)[i] - lower[i]) < (upper[i] - (*this)[i]))
00792                     delta = ((*this)[i] - lower[i]) / (upper[i] - lower[i]);
00793                 else
00794                     delta = (upper[i] - (*this)[i]) / (upper[i] - lower[i]);
00795 
00796                 delta = 1. - delta;
00797                 expp  = (nm + 1.);
00798                 delta = pow(delta , expp);
00799                 expp  = 1. / expp;
00800 
00801                 if (u <= .5) {
00802                     deltaQ =  2. * u + (1 - 2. * u) * delta;
00803                     deltaQ = pow(deltaQ, expp) - 1. ;
00804                 }
00805                 else {
00806                     deltaQ = 2. - 2. * u + 2. * (u  - .5) * delta;
00807                     deltaQ = 1. - pow(deltaQ , expp);
00808                 }
00809 
00810                 (*this)[i] += deltaQ * (upper[i] - lower[i]);
00811 
00812                 //  -> from Deb's implementation, not contained in any paper
00813                 if ((*this)[i] < lower[i])
00814                     (*this)[i] = lower[i];
00815 
00816                 if ((*this)[i] > upper[i])
00817                     (*this)[i] = upper[i];
00818 
00819             }
00820         }
00821     }
00822 }
00823 
00824 
  • Shark Main Page
  • Array
  • Rng
  • LinAlg
  • FileUtil
  • EALib
  • MOO-EALib
  • ReClaM
  • Fuzzy
  • Mixture
  • Tutorials
  • FAQ