00001
00040
00041
00042
00043 #ifndef __CHROMOSOMET_H
00044 #define __CHROMOSOMET_H
00045
00046 #ifdef _WIN32
00047
00048
00049 #pragma warning(disable: 4786)
00050 #endif
00051
00052 #include <algorithm>
00053 #include <typeinfo>
00054 #include <cstring>
00055
00056 #include <SharkDefs.h>
00057 #include <Rng/GlobalRng.h>
00058 #include <EALib/Interval.h>
00059 #include <EALib/Chromosome.h>
00060 #include <EALib/PVMinterface.h>
00061
00062
00063
00067 template < class T >
00068 class ChromosomeT_base : public Chromosome, public std::vector< T >
00069 {
00070 public:
00071 ChromosomeT_base()
00072 { }
00073 explicit ChromosomeT_base(unsigned l) : std::vector< T >(l)
00074 { }
00075 ChromosomeT_base(unsigned l, const T& v) : std::vector< T >(l, v)
00076 { }
00077 ChromosomeT_base(const std::vector< T >& v) : std::vector< T >(v)
00078 { }
00079
00080 const char* typeOfAlleles() const
00081 {
00082 return typeid(T).name();
00083 }
00084 unsigned sizeOfAlleles() const
00085 {
00086 return sizeof(T);
00087 }
00088
00089 unsigned size() const
00090 {
00091 return static_cast< const std::vector< T > * >(this)->size();
00092 }
00093
00094 Chromosome& operator = (const Chromosome& c)
00095 {
00096 static_cast< std::vector< T > * >(this)->operator = (dynamic_cast< const std::vector< T >& >(c));
00097 return *this;
00098 }
00099
00100 Chromosome& operator = (const std::vector< T >& c)
00101 {
00102 static_cast< std::vector< T > * >(this)->operator = (c);
00103 return *this;
00104 }
00105
00106
00107
00108
00109 Chromosome& operator = (const ChromosomeT_base< T >& c)
00110 {
00111 static_cast< std::vector< T > * >(this)->operator = (c);
00112 return *this;
00113 }
00114
00115 Chromosome& operator = (const T& c)
00116 {
00117 for (unsigned i = this->size(); i--; (*this)[ i ] = c);
00118 return *this;
00119 }
00120
00121
00122
00123
00124
00125 void resize(unsigned n)
00126 {
00127 if (n < size())
00128 {
00129 static_cast< std::vector< T > * >(this)->erase(this->begin() + n, this->end());
00130 }
00131 else if (n > size())
00132 {
00133 static_cast< std::vector< T > * >(this)->insert(this->end(), n - size(), T());
00134 }
00135 }
00136
00137
00138
00139
00140
00141
00142 void duplicate(unsigned start,
00143 unsigned stop,
00144 unsigned dest)
00145 {
00146 RANGE_CHECK(start < size() &&
00147 stop < size() &&
00148 dest < size())
00149
00150 if (start > stop) stop += size();
00151
00152 for (unsigned i = start, j = dest; i <= stop; i++, j++)
00153 (*this)[ j % size()] = (*this)[ i % size()];
00154 }
00155
00156
00157
00158
00159
00160
00161 void invert(unsigned start,
00162 unsigned stop,
00163 unsigned granularity = 1)
00164 {
00165 unsigned i, j, k, l;
00166
00167 RANGE_CHECK(start < size() &&
00168 stop < size())
00169
00170 if (start > stop) stop += size();
00171
00172 i = start;
00173 j = stop - granularity + 1;
00174 l = (stop - start + 1) / (2 * granularity);
00175
00176 while (l--)
00177 {
00178 for (k = granularity; k--;)
00179 swap((i + k) % size(), (j + k) % size());
00180 i += granularity;
00181 j -= granularity;
00182 }
00183 }
00184
00185
00186
00187
00188
00189
00190 void invert(unsigned granularity = 1)
00191 {
00192 invert(0, size() - 1, granularity);
00193 }
00194
00195
00196
00197
00198
00199
00200 void transcribe(unsigned start,
00201 unsigned stop,
00202 const Chromosome& chrom)
00203 {
00204 const std::vector< T >& c = dynamic_cast< const std::vector< T >& >(chrom);
00205
00206 RANGE_CHECK(start < c.size() &&
00207 stop < c.size())
00208
00209 if (start > stop) stop += c.size();
00210
00211 resize(stop - start + 1);
00212
00213 for (unsigned i = start, j = 0; i <= stop; i++, j++)
00214 (*this)[ j ] = c[ i % c.size()];
00215 }
00216
00217
00218
00219
00220
00221
00222 void swap(unsigned i, unsigned j)
00223 {
00224 RANGE_CHECK(i < size() && j < size())
00225 #ifdef __NO_BITPACKING__
00226 std::swap((*this)[ i ], (*this)[ j ]);
00227 #else
00228
00229
00230
00231
00232 T t = (*this)[ i ];
00233 (*this)[ i ] = (*this)[ j ];
00234 (*this)[ j ] = t;
00235 #endif
00236 }
00237
00238
00239
00240
00241
00242
00243 void shuffle()
00244 {
00245 for (unsigned i = this->size(); i--;)
00246 swap(i, Rng::discrete(0, size() - 1));
00247 }
00248
00249
00250
00251
00252
00253
00254 void replace(unsigned i, const T& v)
00255 {
00256 RANGE_CHECK(i < size())
00257 (*this)[ i ] = v;
00258 }
00259
00260
00261
00262
00263
00264
00265 void replace(unsigned i, const Chromosome& chrom)
00266 {
00267 const std::vector< T >& v = dynamic_cast< const std::vector< T >& >(chrom);
00268 RANGE_CHECK(i + v.size() <= size())
00269 std::copy(v.begin(), v.end(), this->begin() + i);
00270 }
00271
00272
00273
00274
00275
00276
00277 void insert(unsigned i, const T& allele)
00278 {
00279 RANGE_CHECK(i <= size())
00280 std::vector< T >::insert(this->begin() + i, 1, allele);
00281 }
00282
00283
00284
00285
00286
00287
00288 void insert(unsigned i, const Chromosome& chrom)
00289 {
00290 const std::vector< T >& v = dynamic_cast< const std::vector< T >& >(chrom);
00291 RANGE_CHECK(i <= size())
00292 static_cast< std::vector< T > * >(this)->insert(this->begin() + i, v.begin(), v.end());
00293 }
00294
00295
00296
00297
00298
00299
00300 void append(const T& allele)
00301 {
00302 std::vector< T >::push_back(allele);
00303 }
00304
00305
00306
00307
00308
00309
00310 void append(const Chromosome& chrom)
00311 {
00312 const std::vector< T >& v = dynamic_cast< const std::vector< T >& >(chrom);
00313 static_cast< std::vector< T > * >(this)->insert(this->end(), v.begin(), v.end());
00314 }
00315
00316
00317
00318
00319
00320
00321 void remove(unsigned i)
00322 {
00323 RANGE_CHECK(i < size())
00324 static_cast< std::vector< T > * >(this)->erase(this->begin() + i);
00325 }
00326
00327
00328
00329
00330
00331
00332 void remove(unsigned i, unsigned k)
00333 {
00334 if (i <= k)
00335 {
00336 RANGE_CHECK(k < size())
00337 static_cast< std::vector< T > * >(this)->erase(this->begin() + i, this->begin() + k);
00338 }
00339 }
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350 void rotateRight(unsigned n = 1)
00351 {
00352
00353
00354
00355
00356 n = n % size();
00357 #ifdef __NO_BITPACKING__
00358 std::rotate(this->begin(), this->end() - n, this->end());
00359 #else
00360 std::cerr << "cannot rotate bool chromosome represented as bit string by "
00361 << n << " bits!" << std::endl;
00362 #endif
00363 }
00364
00365
00366
00367
00368
00369
00370 void rotateLeft(unsigned n = 1)
00371 {
00372
00373
00374
00375
00376 n = n % size();
00377 #ifdef __NO_BITPACKING__
00378 std::rotate(begin(), begin() + n, end());
00379 #else
00380 std::cerr << "cannot rotate bool chromosome represented as bit string by "
00381 << n << " bits!" << std::endl;
00382 #endif
00383 }
00384
00385
00386
00387
00388
00389
00390 #ifndef _WIN32
00391
00392
00393
00394 void crossover(const Chromosome& dadChrom,
00395 const Chromosome& momChrom,
00396 const std::vector< unsigned >& points)
00397 {
00398 SIZE_CHECK(dadChrom.size() == momChrom.size())
00399
00400 resize(dadChrom.size());
00401
00402 if (size() > 0)
00403 {
00404 unsigned i, j, p, max, swp;
00405 const std::vector< T >& dad = dynamic_cast< const std::vector< T >& >(dadChrom);
00406 const std::vector< T >& mom = dynamic_cast< const std::vector< T >& >(momChrom);
00407
00408
00409
00410
00411 std::vector< unsigned > pts(points);
00412 for (i = pts.size(); i--;)
00413 {
00414 for (max = pts[ i ], p = j = i; j--;)
00415 if (pts[ j ] > max) p = j;
00416 RANGE_CHECK(max <= size())
00417 if (p != i) std::swap(pts[ i ], pts[ p ]);
00418 }
00419
00420 for (swp = i = j = 0; i < pts.size(); i++, swp ^= 1)
00421 {
00422 if (swp)
00423 for (; j < pts[ i ]; j++)
00424 (*this)[ j ] = mom[ j ];
00425 else
00426 for (; j < pts[ i ]; j++)
00427 (*this)[ j ] = dad[ j ];
00428 }
00429
00430 if (swp)
00431 for (; j < size(); j++)
00432 (*this)[ j ] = mom[ j ];
00433 else
00434 for (; j < size(); j++)
00435 (*this)[ j ] = dad[ j ];
00436 }
00437 }
00438
00439
00440
00441
00442
00443
00444 void crossover(Chromosome& mate, const std::vector< unsigned >& points)
00445 {
00446 SIZE_CHECK(this->size() == mate.size())
00447
00448 if (size() > 0)
00449 {
00450 unsigned i, j, p, max, swp;
00451 std::vector< T >& mate1 = *this;
00452 std::vector< T >& mate2 = dynamic_cast< std::vector<T>& >(mate);
00453
00454
00455
00456
00457 std::vector< unsigned > pts(points);
00458 for (i = pts.size(); i--;)
00459 {
00460 for (max = pts[ i ], p = j = i; j--;)
00461 if (pts[ j ] > max) p = j;
00462 RANGE_CHECK(max <= size())
00463 if (p != i) std::swap(pts[ i ], pts[ p ]);
00464 }
00465
00466 for (swp = pts.size() & 1, i = j = 0; i < pts.size(); i++, swp ^= 1)
00467 {
00468 if (swp)
00469 for (; j < pts[ i ]; j++)
00470 {
00471 #ifdef __NO_BITPACKING__
00472 std::swap(mate1[ j ], mate2[ j ]);
00473 #else
00474
00475
00476
00477
00478 T t = mate1[ j ];
00479 mate1[ j ] = mate2[ j ];
00480 mate2[ j ] = t;
00481 #endif
00482 }
00483 else
00484 j = pts[ i ];
00485 }
00486 }
00487 }
00488 #endif // !_WIN32
00489
00490
00491
00492
00493
00494
00495 void crossover(const Chromosome& dadChrom,
00496 const Chromosome& momChrom,
00497 const std::vector< bool >& pos)
00498 {
00499 SIZE_CHECK(dadChrom.size() == momChrom.size())
00500
00501 resize(dadChrom.size());
00502
00503 if (size())
00504 {
00505 const std::vector< T >& dad = dynamic_cast< const std::vector< T >& >(dadChrom);
00506 const std::vector< T >& mom = dynamic_cast< const std::vector< T >& >(momChrom);
00507
00508 unsigned i, m;
00509 bool swp = false;
00510 for (i = 0, m = Shark::min(size(), (unsigned) pos.size()); i < m; i++)
00511 {
00512
00513
00514
00515
00516 swp = swp != pos[ i ];
00517 (*this)[ i ] = swp ? mom[ i ] : dad[ i ];
00518 }
00519 }
00520 }
00521
00522
00523
00524
00525
00526
00527 void crossover(Chromosome& mate, const std::vector< bool >& pos)
00528 {
00529 SIZE_CHECK(this->size() == mate.size())
00530
00531 if (size())
00532 {
00533 std::vector< T >& mate1 = *this;
00534 std::vector< T >& mate2 = dynamic_cast< std::vector<T>& >(mate);
00535
00536 unsigned i, m;
00537 bool swp = false;
00538 for (i = 0, m = Shark::min(size(), (unsigned) pos.size()); i < m; ++i)
00539
00540 if( ( swp = ( swp != pos[ i ] ) ) )
00541 {
00542 #ifdef __NO_BITPACKING__
00543 std::swap(mate1[ i ], mate2[ i ]);
00544 #else
00545
00546
00547
00548
00549 T t = mate1[ i ];
00550 mate1[ i ] = mate2[ i ];
00551 mate2[ i ] = t;
00552 #endif
00553 }
00554 }
00555 }
00556
00557
00558
00559
00560
00561
00562 void crossover(const Chromosome& dadChrom,
00563 const Chromosome& momChrom,
00564 unsigned npoints,
00565 unsigned align = 1,
00566 bool chromswap = 0)
00567 {
00568 std::vector< bool > pos(dadChrom.size(), false);
00569 unsigned startpos = chromswap ? 0 : 1;
00570
00571 #ifdef __NO_BITPACKING__
00572 for (unsigned i = 0; i < npoints; i++)
00573 pos[ Rng::discrete(startpos,(pos.size()-1)/align)*align ] ^= true;
00574 #else
00575
00576
00577
00578 for (unsigned i = 0; i < npoints; i++)
00579 {
00580 unsigned j = Rng::discrete(startpos, (pos.size() - 1) / align) * align;
00581 pos[ j ] = ! pos[ j ];
00582 }
00583 #endif
00584
00585 crossover(dadChrom, momChrom, pos);
00586 }
00587
00588
00589
00590
00591
00592
00593 void crossover(Chromosome& mate,
00594 unsigned npoints,
00595 unsigned align = 1,
00596 bool chromswap = 0)
00597 {
00598 std::vector< bool > pos(mate.size(), false);
00599 unsigned startpos = chromswap ? 0 : 1;
00600
00601 #ifdef __NO_BITPACKING__
00602 for (unsigned i = 0; i < npoints; i++)
00603 pos[ Rng::discrete(startpos,(pos.size()-1)/align)*align] ^= true;
00604 #else
00605
00606
00607
00608 for (unsigned i = 0; i < npoints; i++)
00609 {
00610 unsigned j = Rng::discrete(startpos, (pos.size() - 1) / align) * align;
00611 pos[ j ] = ! pos[ j ];
00612 }
00613 #endif
00614
00615 crossover(mate, pos);
00616 }
00617
00618
00619
00620
00621
00622
00623 void crossover(const Chromosome& dadChrom,
00624 const Chromosome& momChrom,
00625 const Chromosome& posChrom)
00626 {
00627 crossover(dadChrom, momChrom,
00628 dynamic_cast< const std::vector< bool >& >(posChrom));
00629 }
00630
00631
00632
00633
00634
00635
00636 void crossover(Chromosome& mate, const Chromosome& posChrom)
00637 {
00638 crossover(mate,
00639 dynamic_cast< const std::vector< bool >& >(posChrom));
00640 }
00641
00642
00643
00644
00645
00646
00647 void crossoverUniform(const Chromosome& dadChrom,
00648 const Chromosome& momChrom,
00649 const std::vector< bool >& pos)
00650 {
00651 SIZE_CHECK(dadChrom.size() == momChrom.size())
00652
00653 resize(dadChrom.size());
00654
00655 if (size() > 0)
00656 {
00657 const std::vector< T >& dad = dynamic_cast< const std::vector< T >& >(dadChrom);
00658 const std::vector< T >& mom = dynamic_cast< const std::vector< T >& >(momChrom);
00659
00660 for (unsigned i = Shark::min(size(), (unsigned) pos.size()); i--;)
00661 (*this)[ i ] = pos[ i ] ? mom[ i ] : dad[ i ];
00662 }
00663 }
00664
00665
00666
00667
00668
00669
00670 void crossoverUniform(Chromosome& mateChrom,
00671 const std::vector< bool >& pos)
00672 {
00673 SIZE_CHECK(this->size() == mateChrom.size())
00674
00675 if (size())
00676 {
00677 std::vector< T >& mate = dynamic_cast< std::vector< T >& >(mateChrom);
00678
00679 for (unsigned i = Shark::min(size(), (unsigned) pos.size()); i--;)
00680 if (pos[ i ])
00681 {
00682 #ifdef __NO_BITPACKING__
00683 std::swap((*this)[ i ], mate[ i ]);
00684 #else
00685
00686
00687
00688
00689 T t = (*this)[ i ];
00690 (*this)[ i ] = mate[ i ];
00691 mate[ i ] = t;
00692 #endif
00693 }
00694 }
00695 }
00696
00697
00698
00699
00700
00701
00702 void crossoverUniform(const Chromosome& dadChrom,
00703 const Chromosome& momChrom)
00704 {
00705 SIZE_CHECK(dadChrom.size() == momChrom.size())
00706
00707 resize(dadChrom.size());
00708
00709 if (size() > 0)
00710 {
00711 const std::vector< T >& dad = dynamic_cast< const std::vector< T >& >(dadChrom);
00712 const std::vector< T >& mom = dynamic_cast< const std::vector< T >& >(momChrom);
00713
00714 for (unsigned i = this->size(); i--;)
00715 (*this)[ i ] = Rng::coinToss(0.5) ? dad[ i ] : mom[ i ];
00716 }
00717 }
00718
00719
00720
00721
00722
00723
00724 void crossoverUniform(Chromosome& mateChrom)
00725 {
00726 SIZE_CHECK(this->size() == mateChrom.size())
00727
00728 if (size())
00729 {
00730 std::vector< T >& mate = dynamic_cast< std::vector< T >& >(mateChrom);
00731
00732 for (unsigned i = size(); i--;)
00733 if (Rng::coinToss(0.5))
00734 {
00735 #ifdef __NO_BITPACKING__
00736 std::swap((*this)[ i ], mate[ i ]);
00737 #else
00738
00739
00740
00741
00742 T t = (*this)[ i ];
00743 (*this)[ i ] = mate[ i ];
00744 mate[ i ] = t;
00745 #endif
00746 }
00747 }
00748 }
00749
00750
00751
00752
00753
00754
00755 void crossoverUniform(const Chromosome& dadChrom,
00756 const Chromosome& momChrom,
00757 const Chromosome& posChrom)
00758 {
00759 crossoverUniform(dadChrom, momChrom,
00760 dynamic_cast< const std::vector< bool >& >(posChrom));
00761 }
00762
00763
00764
00765
00766
00767
00768 void crossoverUniform(Chromosome& mateChrom,
00769 const Chromosome& posChrom)
00770 {
00771 crossoverUniform(mateChrom,
00772 dynamic_cast< const std::vector< bool >& >(posChrom));
00773 }
00774
00775
00776
00777
00778
00779
00780 void recombineDiscrete(const Chromosome& dad,
00781 const Chromosome& mom)
00782 {
00783 crossoverUniform(dad, mom);
00784 }
00785
00786
00787
00788
00789
00790
00791 void recombineDiscrete(Chromosome& mate)
00792 {
00793 crossoverUniform(mate);
00794 }
00795
00796 #ifndef __NO_GENERIC_IOSTREAM
00797
00798
00799
00800
00801 void writeTo(std::ostream& os) const
00802 {
00803 os << "ChromosomeT<"
00804 << typeid(T).name()
00805 << ">("
00806 << this->size() << ")"
00807 << std::endl;
00808 for (unsigned i = 0; i < size(); i++)
00809 {
00810 if (i)
00811 {
00812 os << '\t';
00813 }
00814 os << (*this)[ i ];
00815 }
00816 os << std::endl;
00817 }
00818
00819
00820
00821
00822
00823 void readFrom(std::istream& is)
00824 {
00825 std::string s;
00826
00827 is >> s;
00828 is.get();
00829
00830 if (is.good() &&
00831 s.substr(0, 12) == "ChromosomeT<" &&
00832 s.find('>') != std::string::npos &&
00833 s.substr(12, s.find('>') - 12) == typeid(T).name())
00834 {
00835
00836 resize(atoi(s.substr(s.find('>') + 2).c_str()));
00837 for (unsigned i = 0; i < size(); i++)
00838 {
00839 T t;
00840 is >> t;
00841 (*this)[ i ] = t;
00842 }
00843 }
00844 else
00845 #if (defined(__GNUC__) && __GNUC__ > 2)
00846
00847 is.setf(std::ios::fmtflags(std::ios::failbit));
00848 #else
00849 is.setf(std::ios::failbit);
00850 #endif
00851 }
00852 #endif // !__NO_GENERIC_IOSTREAM
00853
00854 protected:
00855 Chromosome* clone() const
00856 {
00857 return new ChromosomeT_base< T >(*this);
00858 }
00859 Chromosome* empty() const
00860 {
00861 return new ChromosomeT_base< T >;
00862 }
00863 };
00864
00865
00869 template < class T >
00870 class ChromosomeT : public ChromosomeT_base< T >
00871 {
00872 public:
00873 ChromosomeT()
00874 { }
00875 explicit ChromosomeT(unsigned l)
00876 : ChromosomeT_base< T >(l)
00877 { }
00878 ChromosomeT(unsigned l, const T& v)
00879 : ChromosomeT_base< T >(l, v)
00880 { }
00881 ChromosomeT(const std::vector< T >& v)
00882 : ChromosomeT_base< T >(v)
00883 { }
00884
00885 protected:
00886 Chromosome* clone() const
00887 {
00888 return new ChromosomeT< T >(*this);
00889 }
00890 Chromosome* empty() const
00891 {
00892 return new ChromosomeT< T >;
00893 }
00894
00895 #ifndef __NO_GENERIC_IOSTREAM
00896
00897
00898
00899
00900 void writeTo(std::ostream& os) const
00901 {
00902 os << "ChromosomeT<" << typeid(T).name() << ">(" << this->size() << ")" << std::endl;
00903 for (unsigned i = 0; i < this->size(); i++)
00904 {
00905 if (i) os << '\t';
00906 os << (*this)[ i ];
00907 }
00908 os << std::endl;
00909 }
00910
00911
00912
00913
00914
00915 void readFrom(std::istream& is)
00916 {
00917 std::string s;
00918 is >> s;
00919 is.get();
00920
00921 if (is.good() &&
00922 s.substr(0, 12) == "ChromosomeT<" &&
00923 s.find('>') != std::string::npos &&
00924 s.substr(12, s.find('>') - 12) == typeid(T).name())
00925 {
00926
00927 ((std::vector<T>*)this)->resize(atoi(s.substr(s.find('>') + 2).c_str()));
00928 for (unsigned i = 0; i < this->size(); i++)
00929 is >> (*this)[ i ];
00930 }
00931 else
00932 #if (defined(__GNUC__) && __GNUC__ > 2)
00933
00934 is.setf(std::ios::fmtflags(std::ios::failbit));
00935 #else
00936 is.setf(std::ios::failbit);
00937 #endif
00938 }
00939 #endif // !__NO_GENERIC_IOSTREAM
00940
00941 };
00942
00943
00947 template < class T >
00948 class ChromosomeT_num : public ChromosomeT_base< T >
00949 {
00950 private:
00951 static void initialize(T& v, T min, T max)
00952 {
00953 if (T(0.5) == 0.5)
00954 v = T(Rng::uni(double(min), double(max)));
00955 else
00956 v = T(Rng::discrete(long(min), long(max)));
00957 }
00958
00959 static void cutOff(T& v, T min, T max)
00960 {
00961 if (v < min) v = min;
00962 if (v > max) v = max;
00963 }
00964
00965 public:
00966 ChromosomeT_num()
00967 { }
00968 explicit ChromosomeT_num(unsigned l)
00969 : ChromosomeT_base< T >(l)
00970 { }
00971 ChromosomeT_num(unsigned l, const T& v)
00972 : ChromosomeT_base< T >(l, v)
00973 { }
00974 ChromosomeT_num(const std::vector< T >& v)
00975 : ChromosomeT_base< T >(v)
00976 { }
00977
00978
00979
00980
00981
00982
00983 void initialize(T min, T max)
00984 {
00985 for (unsigned i = this->size(); i--;)
00986 initialize((*this)[ i ], min, max);
00987 }
00988
00989
00990
00991
00992
00993
00994
00995
00996
00997
00998
00999
01000
01001
01002
01003
01004
01005
01006
01007
01008
01009
01010
01011
01012 void initialize(const std::vector<T>& min,
01013 const std::vector<T>& max)
01014 {
01015 SIZE_CHECK(min.size() == max.size())
01016
01017 ((std::vector<T>*)this)->resize(min.size());
01018
01019 for (unsigned i = this->size(); i--;)
01020 initialize((*this)[i], min[i], max[i]);
01021 }
01022
01023
01024
01025
01026
01027
01028 void cutOff(T min, T max)
01029 {
01030 for (unsigned i = this->size(); i--;)
01031 cutOff((*this)[ i ], min, max);
01032 }
01033
01034
01035
01036
01037
01038
01039 void cutOff(const Chromosome& minChrom,
01040 const Chromosome& maxChrom)
01041 {
01042 SIZE_CHECK(minChrom.size() == maxChrom.size())
01043
01044 SIZE_CHECK((*this).size() == minChrom.size())
01045
01046 if (this->size())
01047 {
01048 const std::vector< T >& min = dynamic_cast< const std::vector< T >& >(minChrom);
01049 const std::vector< T >& max = dynamic_cast< const std::vector< T >& >(maxChrom);
01050
01051 for (unsigned i = this->size(); i--;)
01052 cutOff((*this)[ i ], min[ i ], max[ i ]);
01053 }
01054 }
01055
01056
01057
01058
01059
01060
01061 void mutateUniform(T min, T max, double p)
01062 {
01063 for (unsigned i = this->size(); i--;)
01064 if (Rng::coinToss(p))
01065 initialize((*this)[ i ], min, max);
01066 }
01067
01068
01069
01070
01071
01072
01073 void mutateUniform(T min, T max,
01074 const std::vector< double >& p,
01075 bool cycle = false)
01076 {
01077 RANGE_CHECK(p.size() <= this->size())
01078
01079 for (unsigned i = cycle ? this->size() : p.size(); i--;)
01080 if (Rng::coinToss(p[ i % p.size()]))
01081 initialize((*this)[ i ], min, max);
01082 }
01083
01084
01085
01086
01087
01088
01089 void mutateUniform(T min, T max,
01090 const Chromosome& p,
01091 bool cycle = false)
01092 {
01093 mutateUniform(min, max,
01094 dynamic_cast< const std::vector< double >& >(p),
01095 cycle);
01096 }
01097
01098
01099
01100
01101
01102
01103 void mutateUniform(const Chromosome& minChrom,
01104 const Chromosome& maxChrom,
01105 double p)
01106 {
01107 SIZE_CHECK(this->size() == minChrom.size())
01108 SIZE_CHECK(this->size() == maxChrom.size())
01109
01110 if (this->size() > 0)
01111 {
01112 const std::vector< T >& min = dynamic_cast< const std::vector< T >& >(minChrom);
01113 const std::vector< T >& max = dynamic_cast< const std::vector< T >& >(maxChrom);
01114
01115 for (unsigned i = this->size(); i--;)
01116 if (Rng::coinToss(p))
01117 initialize((*this)[ i ], min[ i ], max[ i ]);
01118 }
01119 }
01120
01121
01122
01123
01124
01125
01126 void mutateUniform(const Chromosome& minChrom,
01127 const Chromosome& maxChrom,
01128 const std::vector< double >& p,
01129 bool cycle = false)
01130 {
01131 SIZE_CHECK(this->size() == minChrom.size())
01132 SIZE_CHECK(this->size() == maxChrom.size())
01133 RANGE_CHECK(p.size() <= this->size())
01134
01135 if (this->size())
01136 {
01137 const std::vector< T >& min = dynamic_cast< const std::vector< T >& >(minChrom);
01138 const std::vector< T >& max = dynamic_cast< const std::vector< T >& >(maxChrom);
01139
01140 for (unsigned i = cycle ? this->size() : p.size(); i--;)
01141 if (Rng::coinToss(p[ i % p.size()]))
01142 initialize((*this)[ i ], min[ i ], max[ i ]);
01143 }
01144 }
01145
01146
01147
01148
01149
01150
01151 void mutateUniform(const Chromosome& min,
01152 const Chromosome& max,
01153 const Chromosome& p,
01154 bool cycle = false)
01155 {
01156 mutateUniform(min, max,
01157 dynamic_cast< const std::vector< double >& >(p),
01158 cycle);
01159 }
01160
01161
01162 bool operator == (const Chromosome& c) const
01163 {
01164
01165
01166
01167
01168 return static_cast< const std::vector< T >& >(*this) == dynamic_cast< const std::vector< T >& >(c);
01169 }
01170
01171 bool operator < (const Chromosome& c) const
01172 {
01173 return static_cast< const std::vector< T >& >(*this) < dynamic_cast< const std::vector< T >& >(c);
01174 }
01175 };
01176
01177
01181 template < >
01182 class ChromosomeT< double > : public ChromosomeT_num< double >
01183 {
01184 public:
01185 ChromosomeT()
01186 { }
01187 explicit ChromosomeT(unsigned l)
01188 : ChromosomeT_num< double >(l)
01189 { }
01190 ChromosomeT(unsigned l, const double& v)
01191 : ChromosomeT_num< double >(l, v)
01192 { }
01193 ChromosomeT(const std::vector< double >& v)
01194 : ChromosomeT_num< double >(v)
01195 { }
01196 ~ChromosomeT();
01197
01198 void initializeRotate(const ChromosomeT< double >&);
01199 void initializeRotate(const std::vector< double >&);
01200 void initializeRotate(double, double);
01201
01202 void showRotate();
01203
01204 void decodeBinary(const Chromosome& chrom,
01205 const Interval& range,
01206 unsigned nbits,
01207 bool useGray = false);
01208
01209 void accumulate(const std::vector< double >&, double);
01210 void accumulate(const Chromosome&, double);
01211
01212 void mutateCauchy(double scale);
01213 void mutateCauchy(const std::vector< double >& scale, bool = false);
01214 void mutateCauchy(const Chromosome& scale, bool = false);
01215 void mutateCauchy(const ChromosomeT< double >& scale, bool = false);
01216
01217 void mutateNormal(double variance);
01218 void mutateNormal(const std::vector< double >& variances, bool = false);
01219 void mutateNormal(const Chromosome& variances, bool = false);
01220 void mutateNormal(const ChromosomeT< double >& variances, bool = false);
01221
01222 void mutateNormalRotAngles(const std::vector< double >&, const std::vector< double >&);
01223 void mutateNormalRotAngles(const Chromosome&, const Chromosome&);
01224
01225 void mutateLogNormal(double, double);
01226 void mutateMSR(double);
01227
01228 void mutateRotate(ChromosomeT<double> &);
01229 void mutateRotate(ChromosomeT<double> &, double, double, double, int, double);
01230
01231 void recombineIntermediate(const Chromosome&, const Chromosome&);
01232 void recombineGenIntermediate(const Chromosome&, const Chromosome&);
01233 void recombineGeomIntermediate(const Chromosome&, const Chromosome&);
01234
01235 void recombineIntermediate(Chromosome&);
01236 void recombineGenIntermediate(Chromosome&);
01237 void recombineGeomIntermediate(Chromosome&);
01238
01239
01240 void SBX(ChromosomeT< double >&, double, double = .5);
01241 void SBX(ChromosomeT< double >&, double, double, double, double = .5, double = 1.E-12);
01242 void SBX(ChromosomeT< double >&, std::vector<double > &, std::vector<double > &, double, double = .5, double = 1.E-12);
01243
01244 void simpleMutatePolynomial(double, double, double, double);
01245 void simpleMutatePolynomial(std::vector<double > &, std::vector<double > &, double, double);
01246 void mutatePolynomial(double, double, double, double);
01247 void mutatePolynomial(std::vector<double > &, std::vector<double > &, double, double);
01248
01249 protected:
01250 Chromosome* clone() const
01251 {
01252 return new ChromosomeT< double >(*this);
01253 }
01254 Chromosome* empty() const
01255 {
01256 return new ChromosomeT< double >;
01257 }
01258
01261 int pvm_pkchrom()
01262 {
01263
01264 unsigned i;
01265
01266 unsigned *s = new unsigned;
01267 *s = this->size();
01268 pvm_pkuint(s, 1, 1);
01269 delete s;
01270
01271 double *u = new double[this->size()];
01272 for (i = 0; i < this->size(); i++)
01273 u[i] = (*this)[i];
01274 pvm_pkdouble(u, this->size(), 1);
01275 delete[] u;
01276
01277 return 1;
01278 };
01279
01282 int pvm_upkchrom()
01283 {
01284
01285 unsigned i;
01286
01287 unsigned *s = new unsigned;
01288 pvm_upkuint(s, 1, 1);
01289 (*this).resize(*s);
01290 delete s;
01291
01292 double *u = new double[this->size()];
01293 pvm_upkdouble(u, this->size(), 1);
01294 for (i = 0; i < this->size(); i++)
01295 (*this)[i] = u[i];
01296 delete[] u;
01297
01298 return 1;
01299 };
01300
01301
01302 #ifndef __NO_GENERIC_IOSTREAM
01303 void writeTo(std::ostream& os) const
01304 {
01305 os << "ChromosomeT<" << typeid(double).name() << ">(" << size() << ")" << std::endl;
01306 for (unsigned i = 0; i < size(); i++)
01307 {
01308 if (i) os << '\t';
01309 os << (*this)[ i ];
01310 }
01311 os << std::endl;
01312 }
01313
01314 void readFrom(std::istream& is)
01315 {
01316 std::string s;
01317
01318 is >> s;
01319 is.get();
01320
01321 if (is.good() &&
01322 s.substr(0, 12) == "ChromosomeT<" &&
01323 s.find('>') != std::string::npos &&
01324 s.substr(12, s.find('>') - 12) == typeid(double).name())
01325 {
01326
01327 resize(atoi(s.substr(s.find('>') + 2).c_str()));
01328 for (unsigned i = 0; i < size(); i++)
01329 is >> (*this)[ i ];
01330 }
01331 else
01332 #if (defined(__GNUC__) && __GNUC__ > 2)
01333
01334 is.setf(std::ios::fmtflags(std::ios::failbit));
01335 #else
01336 is.setf(std::ios::failbit);
01337 #endif
01338 }
01339
01340 #endif // !__NO_GENERIC_IOSTREAM
01341
01342 };
01343
01344
01348 template < >
01349 class ChromosomeT< char > : public ChromosomeT_num< char >
01350 {
01351 public:
01352 ChromosomeT()
01353 { }
01354 explicit ChromosomeT(unsigned l)
01355 : ChromosomeT_num< char >(l)
01356 { }
01357 ChromosomeT(unsigned l, const char& v)
01358 : ChromosomeT_num< char >(l, v)
01359 { }
01360 ChromosomeT(const std::vector< char >& v)
01361 : ChromosomeT_num< char >(v)
01362 { }
01363
01364 protected:
01365 Chromosome* clone() const
01366 {
01367 return new ChromosomeT< char >(*this);
01368 }
01369 Chromosome* empty() const
01370 {
01371 return new ChromosomeT< char >;
01372 }
01373
01376 int pvm_pkchrom()
01377 {
01378
01379 unsigned i;
01380
01381 unsigned *s = new unsigned;
01382 *s = this->size();
01383 pvm_pkuint(s, 1, 1);
01384 delete s;
01385
01386 char *u = new char[this->size()];
01387 for (i = 0; i < this->size(); i++)
01388 u[i] = (*this)[i];
01389 pvm_pkbyte(u, this->size(), 1);
01390 delete[] u;
01391
01392 return 1;
01393 };
01394
01395
01398 int pvm_upkchrom()
01399 {
01400
01401 unsigned i;
01402
01403 unsigned *s = new unsigned;
01404 pvm_upkuint(s, 1, 1);
01405 (*this).resize(*s);
01406 delete s;
01407
01408 char *u = new char[this->size()];
01409 pvm_upkbyte(u, this->size(), 1);
01410 for (i = 0; i < this->size(); i++)
01411 (*this)[i] = u[i];
01412 delete[] u;
01413
01414 return 1;
01415 };
01416
01417 #ifndef __NO_GENERIC_IOSTREAM
01418 void writeTo(std::ostream& os) const
01419 {
01420 os << "ChromosomeT<" << typeid(char).name() << ">(" << size() << ")" << std::endl;
01421 for (unsigned i = 0; i < size(); i++)
01422 os.put((*this)[ i ]);
01423 os << std::endl;
01424 }
01425
01426 void readFrom(std::istream& is)
01427 {
01428 std::string s;
01429
01430 is >> s;
01431 is.get();
01432
01433 if (is.good() &&
01434 s.substr(0, 12) == "ChromosomeT<" &&
01435 s.find('>') != std::string::npos &&
01436 s.substr(12, s.find('>') - 12) == typeid(char).name())
01437 {
01438
01439 resize(atoi(s.substr(s.find('>') + 2).c_str()));
01440 for (unsigned i = 0; i < size(); i++)
01441 is.get((*this)[ i ]);
01442 }
01443 else
01444 #if (defined(__GNUC__) && __GNUC__ > 2)
01445
01446 is.setf(std::ios::fmtflags(std::ios::failbit));
01447 #else
01448 is.setf(std::ios::failbit);
01449 #endif
01450 }
01451
01452 #endif // !__NO_GENERIC_IOSTREAM
01453
01454 };
01455
01456
01460 template < >
01461 class ChromosomeT< int > : public ChromosomeT_num< int >
01462 {
01463 public:
01464 ChromosomeT()
01465 { }
01466 explicit ChromosomeT< int >(unsigned l)
01467 : ChromosomeT_num< int >(l)
01468 { }
01469 ChromosomeT(unsigned l, const int& v)
01470 : ChromosomeT_num< int >(l, v)
01471 { }
01472 ChromosomeT(const std::vector< int >& v)
01473 : ChromosomeT_num< int >(v)
01474 { }
01475
01476 void mutateDiffGeom(double s);
01477 void mutateDiffGeom(const std::vector< double >& s, bool = false);
01478 void mutateDiffGeom(const Chromosome& s, bool = false);
01479 void mutateDiffGeom(const ChromosomeT< double >& s, bool = false);
01480
01481 protected:
01482 Chromosome* clone() const
01483 {
01484 return new ChromosomeT< int >(*this);
01485 }
01486 Chromosome* empty() const
01487 {
01488 return new ChromosomeT< int >;
01489 }
01490
01491
01494 int pvm_pkchrom()
01495 {
01496
01497 unsigned i;
01498
01499 unsigned *s = new unsigned;
01500 *s = this->size();
01501 pvm_pkuint(s, 1, 1);
01502 delete s;
01503
01504 int *u = new int[this->size()];
01505 for (i = 0; i < this->size(); i++)
01506 u[i] = (*this)[i];
01507 pvm_pkint(u, this->size(), 1);
01508 delete[] u;
01509
01510 return 1;
01511 };
01512
01513
01516 int pvm_upkchrom()
01517 {
01518
01519 unsigned i;
01520
01521 unsigned *s = new unsigned;
01522 pvm_upkuint(s, 1, 1);
01523 (*this).resize(*s);
01524 delete s;
01525
01526 int *u = new int[this->size()];
01527 pvm_upkint(u, this->size(), 1);
01528 for (i = 0; i < this->size(); i++)
01529 (*this)[i] = u[i];
01530 delete[] u;
01531
01532 return 1;
01533 };
01534
01535
01536 #ifndef __NO_GENERIC_IOSTREAM
01537 void writeTo(std::ostream& os) const
01538 {
01539 os << "ChromosomeT<" << typeid(int).name() << ">(" << size() << ")" << std::endl;
01540 for (unsigned i = 0; i < size(); i++)
01541 {
01542 if (i) os << '\t';
01543 os << (*this)[ i ];
01544 }
01545 os << std::endl;
01546 }
01547
01548 void readFrom(std::istream& is)
01549 {
01550 std::string s;
01551
01552 is >> s;
01553 is.get();
01554
01555 if (is.good() &&
01556 s.substr(0, 12) == "ChromosomeT<" &&
01557 s.find('>') != std::string::npos &&
01558 s.substr(12, s.find('>') - 12) == typeid(int).name())
01559 {
01560
01561 resize(atoi(s.substr(s.find('>') + 2).c_str()));
01562 for (unsigned i = 0; i < size(); i++)
01563 is >> (*this)[ i ];
01564 }
01565 else
01566 #if (defined(__GNUC__) && __GNUC__ > 2)
01567
01568 is.setf(std::ios::fmtflags(std::ios::failbit));
01569 #else
01570 is.setf(std::ios::failbit);
01571 #endif
01572 }
01573
01574 #endif // !__NO_GENERIC_IOSTREAM
01575
01576 };
01577
01578
01582 template < >
01583 class ChromosomeT< bool > : public ChromosomeT_base< bool >
01584 {
01585 public:
01586 ChromosomeT()
01587 { }
01588 explicit ChromosomeT(unsigned l)
01589 : ChromosomeT_base< bool >(l)
01590 { }
01591 ChromosomeT(unsigned l, const bool& v)
01592 : ChromosomeT_base< bool >(l, v)
01593 { }
01594 ChromosomeT(const std::vector< bool >& v)
01595 : ChromosomeT_base< bool >(v)
01596 { }
01597
01598 void initialize();
01599 void initialize(const unsigned pos);
01600
01601
01602 void encode(double val,
01603 const Interval& range,
01604 unsigned nbits,
01605 bool useGray = false);
01606 double decode(const Interval& range,
01607 bool useGray = false) const;
01608
01609 void encodeBinary(const std::vector< double >& chrom,
01610 const Interval& range,
01611 unsigned nbits,
01612 bool useGray = false);
01613
01614 void encodeBinary(const Chromosome& chrom,
01615 const Interval& range,
01616 unsigned nbits,
01617 bool useGray = false);
01618
01619 void flip(double p);
01620 void flip(const std::vector< double >& p,
01621 bool cycle = false);
01622 void flip(const Chromosome& p,
01623 bool cycle = false);
01624
01625 bool operator == (const Chromosome& c) const;
01626 bool operator < (const Chromosome& c) const;
01627
01628 protected:
01629 Chromosome* clone() const
01630 {
01631 return new ChromosomeT< bool >(*this);
01632 }
01633 Chromosome* empty() const
01634 {
01635 return new ChromosomeT< bool >;
01636 }
01637
01638
01641 int pvm_pkchrom()
01642 {
01643
01644 unsigned i;
01645
01646 unsigned *s = new unsigned;
01647 *s = this->size();
01648 pvm_pkuint(s, 1, 1);
01649 delete s;
01650
01651 unsigned *u = new unsigned[this->size()];
01652 for (i = 0; i < this->size(); i++)
01653 u[i] = (*this)[i];
01654 pvm_pkuint(u, this->size(), 1);
01655 delete[] u;
01656
01657 return 1;
01658 };
01659
01662 int pvm_upkchrom()
01663 {
01664
01665 unsigned i;
01666
01667 unsigned *s = new unsigned;
01668 pvm_upkuint(s, 1, 1);
01669 (*this).resize(*s);
01670 delete s;
01671
01672 unsigned *u = new unsigned[this->size()];
01673 pvm_upkuint(u, this->size(), 1);
01674 for (i = 0; i < this->size(); i++)
01675 (*this)[i] = (u[i] != 0);
01676 delete[] u;
01677
01678 return 1;
01679 };
01680
01681 #ifndef __NO_GENERIC_IOSTREAM
01682 void writeTo(std::ostream& os) const
01683 {
01684 os << "ChromosomeT<" << typeid(bool).name() << ">(" << size() << ")" << std::endl;
01685 for (unsigned i = 0; i < size(); i++)
01686 os.put((*this)[ i ] ? '1' : '0');
01687 os << std::endl;
01688 }
01689
01690 void readFrom(std::istream& is)
01691 {
01692 std::string s;
01693 std::string::size_type pos = 0;
01694
01695
01696
01697 is >> s;
01698 is.get();
01699
01700 if (is.good() &&
01701 s.substr(0, 12) == "ChromosomeT<" &&
01702 (pos = s.find('>')) != std::string::npos &&
01703 s.substr(12, pos - 12) == typeid(bool).name())
01704 {
01705 resize(atoi(s.substr(pos + 2).c_str()));
01706 char c;
01707 for (unsigned i = 0; i < size(); i++)
01708 {
01709 is.get(c);
01710 (*this)[ i ] = c != '0';
01711 }
01712 }
01713 else
01714 #if (defined(__GNUC__) && __GNUC__ > 2)
01715
01716 is.setf(std::ios::fmtflags(std::ios::failbit));
01717 #else
01718 is.setf(std::ios::failbit);
01719 #endif
01720 }
01721
01722 #endif // !__NO_GENERIC_IOSTREAM
01723
01724 };
01725
01726
01727
01728 #endif
01729