/usr/include/ga/GA1DArrayGenome.C is in libga-dev 2.4.7-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 | // $Header$
/* ----------------------------------------------------------------------------
array1.C
mbwall 25feb95
Copyright (c) 1995 Massachusetts Institute of Technology
all rights reserved
DESCRIPTION:
Source file for the 1D array genome.
---------------------------------------------------------------------------- */
#ifndef _ga_array1_C_
#define _ga_array1_C_
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ga/garandom.h>
#include <ga/GA1DArrayGenome.h>
#include <ga/GAMask.h>
template <class T> int
GA1DArrayIsHole(const GA1DArrayGenome<T>&, const GA1DArrayGenome<T>&,
int, int, int);
/* ----------------------------------------------------------------------------
1DArrayGenome
---------------------------------------------------------------------------- */
template <class T> const char *
GA1DArrayGenome<T>::className() const {return "GA1DArrayGenome";}
template <class T> int
GA1DArrayGenome<T>::classID() const {return GAID::ArrayGenome;}
// Set all the initial values to NULL or zero, then allocate the space we'll
// need (using the resize method). We do NOT call the initialize method at
// this point - initialization must be done explicitly by the user of the
// genome (eg when the population is created or reset). If we called the
// initializer routine here then we could end up with multiple initializations
// and/or calls to dummy initializers (for example when the genome is
// created with a dummy initializer and the initializer is assigned later on).
// Besides, we default to the no-initialization initializer by calling the
// default genome constructor.
template <class T>
GA1DArrayGenome<T>::
GA1DArrayGenome(unsigned int length, GAGenome::Evaluator f, void * u) :
GAArray<T>(length),
GAGenome(DEFAULT_1DARRAY_INITIALIZER,
DEFAULT_1DARRAY_MUTATOR,
DEFAULT_1DARRAY_COMPARATOR) {
evaluator(f);
userData(u);
nx=minX=maxX=length;
crossover(DEFAULT_1DARRAY_CROSSOVER);
}
// This is the copy initializer. We set everything to the default values, then
// copy the original. The Array creator takes care of zeroing the data.
template <class T>
GA1DArrayGenome<T>::
GA1DArrayGenome(const GA1DArrayGenome<T> & orig) :
GAArray<T>(orig.sz), GAGenome() {
GA1DArrayGenome<T>::copy(orig);
}
// Delete whatever we own.
template <class T>
GA1DArrayGenome<T>::~GA1DArrayGenome() { }
// This is the class-specific copy method. It will get called by the super
// class since the superclass operator= is set up to call ccopy (and that is
// what we define here - a virtual function). We should check to be sure that
// both genomes are the same class and same dimension. This function tries
// to be smart about they way it copies. If we already have data, then we do
// a memcpy of the one we're supposed to copy. If we don't or we're not the
// same size as the one we're supposed to copy, then we adjust ourselves.
// The Array takes care of the resize in its copy method.
template <class T> void
GA1DArrayGenome<T>::copy(const GAGenome & orig){
if(&orig == this) return;
const GA1DArrayGenome<T>* c = DYN_CAST(const GA1DArrayGenome<T>*, &orig);
if(c) {
GAGenome::copy(*c);
GAArray<T>::copy(*c);
nx = c->nx; minX = c->minX; maxX = c->maxX;
}
}
template <class T> GAGenome *
GA1DArrayGenome<T>::clone(GAGenome::CloneMethod flag) const {
GA1DArrayGenome<T> *cpy = new GA1DArrayGenome<T>(nx);
if(flag == CONTENTS){
cpy->copy(*this);
}
else{
cpy->GAGenome::copy(*this);
cpy->maxX = maxX; cpy->minX = minX;
}
return cpy;
}
// Resize the genome.
// A negative value for the length means that we should randomly set the
// length of the genome (if the resize behaviour is resizeable). If
// someone tries to randomly set the length and the resize behaviour is fixed
// length, then we don't do anything.
// We pay attention to the values of minX and maxX - they determine what kind
// of resizing we are allowed to do. If a resize is requested with a length
// less than the min length specified by the behaviour, we set the minimum
// to the length. If the length is longer than the max length specified by
// the behaviour, we set the max value to the length.
// We return the total size (in bits) of the genome after resize.
// We don't do anything to the new contents!
template <class T> int
GA1DArrayGenome<T>::resize(int len)
{
if(len == STA_CAST(int,nx)) return nx;
if(len == GAGenome::ANY_SIZE)
len = GARandomInt(minX, maxX);
else if(len < 0)
return nx; // do nothing
else if(minX == maxX)
minX=maxX=len;
else{
if(len < STA_CAST(int,minX)) len=minX;
if(len > STA_CAST(int,maxX)) len=maxX;
}
nx = GAArray<T>::size(len);
_evaluated = gaFalse;
return this->sz;
}
#ifdef GALIB_USE_STREAMS
// We don't define this one apriori. Do it in a specialization.
template <class T> int
GA1DArrayGenome<T>::read(STD_ISTREAM &) {
GAErr(GA_LOC, className(), "read", gaErrOpUndef);
return 1;
}
// When we write the data to a stream we do it with spaces between elements.
// Also, there is no newline at the end of the stream of digits.
template <class T> int
GA1DArrayGenome<T>::write(STD_OSTREAM & os) const {
for(unsigned int i=0; i<nx; i++)
os << gene(i) << " ";
return 0;
}
#endif
// Set the resize behaviour of the genome. A genome can be fixed
// length, resizeable with a max and min limit, or resizeable with no limits
// (other than an implicit one that we use internally).
// A value of 0 means no resize, a value less than zero mean unlimited
// resize, and a positive value means resize with that value as the limit.
template <class T> int
GA1DArrayGenome<T>::
resizeBehaviour(unsigned int lower, unsigned int upper)
{
if(upper < lower){
GAErr(GA_LOC, className(), "resizeBehaviour", gaErrBadResizeBehaviour);
return resizeBehaviour();
}
minX = lower; maxX = upper;
if(nx > upper) GA1DArrayGenome<T>::resize(upper);
if(nx < lower) GA1DArrayGenome<T>::resize(lower);
return resizeBehaviour();
}
template <class T> int
GA1DArrayGenome<T>::resizeBehaviour() const {
int val = maxX;
if(maxX == minX) val = FIXED_SIZE;
return val;
}
template <class T> int
GA1DArrayGenome<T>::equal(const GAGenome & c) const {
const GA1DArrayGenome<T> & b = DYN_CAST(const GA1DArrayGenome<T> &, c);
return((this == &c) ? 1 : ((nx != b.nx) ? 0 : GAArray<T>::equal(b,0,0,nx)));
}
/* ----------------------------------------------------------------------------
1DArrayAlleleGenome
These genomes contain an allele set. When we create a new genome, it owns
its own, independent allele set. If we clone a new genome, the new one gets a
link to our allele set (so we don't end up with zillions of allele sets). Same
is true for the copy constructor.
The array may have a single allele set or an array of allele sets, depending
on which creator was called. Either way, the allele set cannot be changed
once the array is created.
---------------------------------------------------------------------------- */
template <class T> const char *
GA1DArrayAlleleGenome<T>::className() const {return "GA1DArrayAlleleGenome";}
template <class T> int
GA1DArrayAlleleGenome<T>::classID() const {return GAID::ArrayAlleleGenome;}
template <class T>
GA1DArrayAlleleGenome<T>::
GA1DArrayAlleleGenome(unsigned int length, const GAAlleleSet<T> & s,
GAGenome::Evaluator f, void * u) :
GA1DArrayGenome<T>(length, f, u){
naset = 1;
aset = new GAAlleleSet<T>[1];
aset[0] = s;
initializer(GA1DArrayAlleleGenome<T>::DEFAULT_1DARRAY_ALLELE_INITIALIZER);
mutator(GA1DArrayAlleleGenome<T>::DEFAULT_1DARRAY_ALLELE_MUTATOR);
comparator(GA1DArrayAlleleGenome<T>::DEFAULT_1DARRAY_ALLELE_COMPARATOR);
crossover(GA1DArrayAlleleGenome<T>::DEFAULT_1DARRAY_ALLELE_CROSSOVER);
}
template <class T>
GA1DArrayAlleleGenome<T>::
GA1DArrayAlleleGenome(const GAAlleleSetArray<T> & sa,
GAGenome::Evaluator f, void * u) :
GA1DArrayGenome<T>(sa.size(), f, u) {
naset = sa.size();
aset = new GAAlleleSet<T>[naset];
for(int i=0; i<naset; i++)
aset[i] = sa.set(i);
initializer(GA1DArrayAlleleGenome<T>::DEFAULT_1DARRAY_ALLELE_INITIALIZER);
mutator(GA1DArrayAlleleGenome<T>::DEFAULT_1DARRAY_ALLELE_MUTATOR);
comparator(GA1DArrayAlleleGenome<T>::DEFAULT_1DARRAY_ALLELE_COMPARATOR);
crossover(GA1DArrayAlleleGenome<T>::DEFAULT_1DARRAY_ALLELE_CROSSOVER);
}
// The copy constructor creates a new genome whose allele set refers to the
// original's allele set.
template <class T>
GA1DArrayAlleleGenome<T>::
GA1DArrayAlleleGenome(const GA1DArrayAlleleGenome<T>& orig) :
GA1DArrayGenome<T>(orig.sz) {
naset = 0;
aset = (GAAlleleSet<T>*)0;
GA1DArrayAlleleGenome<T>::copy(orig);
}
// Delete the allele set
template <class T>
GA1DArrayAlleleGenome<T>::~GA1DArrayAlleleGenome(){
delete [] aset;
}
// This implementation of clone does not make use of the contents/attributes
// capability because this whole interface isn't quite right yet... Just
// clone the entire thing, contents and all.
template <class T> GAGenome *
GA1DArrayAlleleGenome<T>::clone(GAGenome::CloneMethod) const {
return new GA1DArrayAlleleGenome<T>(*this);
}
template <class T> void
GA1DArrayAlleleGenome<T>::copy(const GAGenome& orig){
if(&orig == this) return;
const GA1DArrayAlleleGenome<T> * c =
DYN_CAST(const GA1DArrayAlleleGenome<T>*, &orig);
if(c) {
GA1DArrayGenome<T>::copy(*c);
if(naset != c->naset){
delete [] aset;
naset = c->naset;
aset = new GAAlleleSet<T>[naset];
}
for(int i=0; i<naset; i++)
aset[i].link(c->aset[i]);
}
}
// If we resize to a larger length then we need to set the contents to a valid
// value (ie one of our alleles).
template <class T> int
GA1DArrayAlleleGenome<T>::resize(int len){
unsigned int oldx = this->nx;
GA1DArrayGenome<T>::resize(len);
if(this->nx > oldx){
for(unsigned int i=oldx; i<this->nx; i++)
this->a[i] = aset[i % naset].allele();
}
return len;
}
// Define these so they can easily be specialized as needed.
#ifdef GALIB_USE_STREAMS
template <class T> int
GA1DArrayAlleleGenome<T>::read(STD_ISTREAM& is){
return GA1DArrayGenome<T>::read(is);
}
template <class T> int
GA1DArrayAlleleGenome<T>::write(STD_OSTREAM& os) const {
return GA1DArrayGenome<T>::write(os);
}
#endif
template <class T> int
GA1DArrayAlleleGenome<T>::equal(const GAGenome & c) const {
return GA1DArrayGenome<T>::equal(c);
}
/* ----------------------------------------------------------------------------
Operator definitions
---------------------------------------------------------------------------- */
// The random initializer sets the elements of the array based on the alleles
// set. We choose randomly the allele for each element.
template <class ARRAY_TYPE> void
GA1DArrayAlleleGenome<ARRAY_TYPE>::UniformInitializer(GAGenome & c)
{
GA1DArrayAlleleGenome<ARRAY_TYPE> &child=
DYN_CAST(GA1DArrayAlleleGenome<ARRAY_TYPE> &, c);
child.resize(GAGenome::ANY_SIZE); // let chrom resize if it can
for(int i=child.length()-1; i>=0; i--)
child.gene(i, child.alleleset(i).allele());
}
// Random initializer for order-based genome. Loop through the genome
// and assign each element the next allele in the allele set. Once each
// element has been initialized, scramble the contents by swapping elements.
// This assumes that there is only one allele set for the array.
template <class ARRAY_TYPE> void
GA1DArrayAlleleGenome<ARRAY_TYPE>::OrderedInitializer(GAGenome & c)
{
GA1DArrayAlleleGenome<ARRAY_TYPE> &child=
DYN_CAST(GA1DArrayAlleleGenome<ARRAY_TYPE> &, c);
child.resize(GAGenome::ANY_SIZE); // let chrom resize if it can
int length = child.length()-1;
int n=0;
int i;
for(i=length; i>=0; i--){
child.gene(i, child.alleleset().allele(n++));
if(n >= child.alleleset().size()) n = 0;
}
for(i=length; i>=0; i--)
child.swap(i, GARandomInt(0, length));
}
// Randomly pick elements in the array then set the element to any of the
// alleles in the allele set for this genome. This will work for any number
// of allele sets for a given array.
template <class ARRAY_TYPE> int
GA1DArrayAlleleGenome<ARRAY_TYPE>::FlipMutator(GAGenome & c, float pmut)
{
GA1DArrayAlleleGenome<ARRAY_TYPE> &child=
DYN_CAST(GA1DArrayAlleleGenome<ARRAY_TYPE> &, c);
register int n, i;
if(pmut <= 0.0) return(0);
float nMut = pmut * STA_CAST(float,child.length());
if(nMut < 1.0){ // we have to do a flip test on each bit
nMut = 0;
for(i=child.length()-1; i>=0; i--){
if(GAFlipCoin(pmut)){
child.gene(i, child.alleleset(i).allele());
nMut++;
}
}
}
else{ // only flip the number of bits we need to flip
for(n=0; n<nMut; n++){
i = GARandomInt(0, child.length()-1);
child.gene(i, child.alleleset(i).allele());
}
}
return(STA_CAST(int,nMut));
}
// Randomly swap elements in the array.
template <class ARRAY_TYPE> int
GA1DArrayGenome<ARRAY_TYPE>::SwapMutator(GAGenome & c, float pmut)
{
GA1DArrayGenome<ARRAY_TYPE> &child=DYN_CAST(GA1DArrayGenome<ARRAY_TYPE>&, c);
register int n, i;
if(pmut <= 0.0) return(0);
float nMut = pmut * STA_CAST(float,child.length());
int length = child.length()-1;
if(nMut < 1.0){ // we have to do a flip test on each bit
nMut = 0;
for(i=length; i>=0; i--){
if(GAFlipCoin(pmut)){
child.swap(i, GARandomInt(0, length));
nMut++;
}
}
}
else{ // only flip the number of bits we need to flip
for(n=0; n<nMut; n++)
child.swap(GARandomInt(0, length), GARandomInt(0, length));
}
return(STA_CAST(int,nMut));
}
// The comparator is supposed to return a number that indicates how similar
// two genomes are, so here we just compare elements and return a number that
// indicates how many elements match. If they are different lengths then we
// return -1 to indicate that we could not calculate the differences.
// This assumes that there is an operator == defined for the object in the
// elements of the array.
template <class ARRAY_TYPE> float
GA1DArrayGenome<ARRAY_TYPE>::
ElementComparator(const GAGenome& a, const GAGenome& b)
{
const GA1DArrayGenome<ARRAY_TYPE>& sis=
DYN_CAST(const GA1DArrayGenome<ARRAY_TYPE>&, a);
const GA1DArrayGenome<ARRAY_TYPE>& bro=
DYN_CAST(const GA1DArrayGenome<ARRAY_TYPE>&, b);
if(sis.length() != bro.length()) return -1;
if(sis.length() == 0) return 0;
float count = 0.0;
for(int i=sis.length()-1; i>=0; i--)
count += ((sis.gene(i) == bro.gene(i)) ? 0 : 1);
return count/sis.length();
}
#define SWAP(a,b) {unsigned int tmp=a; a=b; b=tmp;}
// Randomly take bits from each parent. For each bit we flip a coin to see if
// that bit should come from the mother or the father. If strings are
// different lengths then we need to use the mask to get things right.
template <class T> int
GA1DArrayGenome<T>::
UniformCrossover(const GAGenome& p1, const GAGenome& p2,
GAGenome* c1, GAGenome* c2){
const GA1DArrayGenome<T> &mom=DYN_CAST(const GA1DArrayGenome<T> &, p1);
const GA1DArrayGenome<T> &dad=DYN_CAST(const GA1DArrayGenome<T> &, p2);
int n=0;
int i;
if(c1 && c2){
GA1DArrayGenome<T> &sis=DYN_CAST(GA1DArrayGenome<T> &, *c1);
GA1DArrayGenome<T> &bro=DYN_CAST(GA1DArrayGenome<T> &, *c2);
if(sis.length() == bro.length() &&
mom.length() == dad.length() &&
sis.length() == mom.length()){
for(i=sis.length()-1; i>=0; i--){
if(GARandomBit()){
sis.gene(i, mom.gene(i));
bro.gene(i, dad.gene(i));
}
else{
sis.gene(i, dad.gene(i));
bro.gene(i, mom.gene(i));
}
}
}
else{
GAMask mask;
int start;
int max = (sis.length() > bro.length()) ? sis.length() : bro.length();
int min = (mom.length() < dad.length()) ? mom.length() : dad.length();
mask.size(max);
for(i=0; i<max; i++)
mask[i] = GARandomBit();
start = (sis.length() < min) ? sis.length()-1 : min-1;
for(i=start; i>=0; i--)
sis.gene(i, (mask[i] ? mom.gene(i) : dad.gene(i)));
start = (bro.length() < min) ? bro.length()-1 : min-1;
for(i=start; i>=0; i--)
bro.gene(i, (mask[i] ? dad.gene(i) : mom.gene(i)));
}
n = 2;
}
else if(c1 || c2){
GA1DArrayGenome<T> &sis = (c1 ?
DYN_CAST(GA1DArrayGenome<T> &, *c1) :
DYN_CAST(GA1DArrayGenome<T> &, *c2));
if(mom.length() == dad.length() && sis.length() == mom.length()){
for(i=sis.length()-1; i>=0; i--)
sis.gene(i, (GARandomBit() ? mom.gene(i) : dad.gene(i)));
}
else{
int min = (mom.length() < dad.length()) ? mom.length() : dad.length();
min = (sis.length() < min) ? sis.length() : min;
for(i=min-1; i>=0; i--)
sis.gene(i, (GARandomBit() ? mom.gene(i) : dad.gene(i)));
}
n = 1;
}
return n;
}
// Single point crossover for 1D array genomes. Pick a single point then
// copy genetic material from each parent. We must allow for resizable genomes
// so be sure to check the behaviours before we do the crossovers. If resizing
// is allowed then the children will change depending on where the site is
// located. It is also possible to have a mixture of resize behaviours, but
// we won't worry about that at this point. If this happens we just say that
// we cannot handle that and post an error message.
template <class T> int
GA1DArrayGenome<T>::
OnePointCrossover(const GAGenome& p1, const GAGenome& p2,
GAGenome* c1, GAGenome* c2){
const GA1DArrayGenome<T> &mom=DYN_CAST(const GA1DArrayGenome<T> &, p1);
const GA1DArrayGenome<T> &dad=DYN_CAST(const GA1DArrayGenome<T> &, p2);
int nc=0;
unsigned int momsite, momlen;
unsigned int dadsite, dadlen;
if(c1 && c2){
GA1DArrayGenome<T> &sis=DYN_CAST(GA1DArrayGenome<T> &, *c1);
GA1DArrayGenome<T> &bro=DYN_CAST(GA1DArrayGenome<T> &, *c2);
if(sis.resizeBehaviour() == GAGenome::FIXED_SIZE &&
bro.resizeBehaviour() == GAGenome::FIXED_SIZE){
if(mom.length() != dad.length() ||
sis.length() != bro.length() ||
sis.length() != mom.length()){
GAErr(GA_LOC, mom.className(), "one-point cross", gaErrSameLengthReqd);
return nc;
}
momsite = dadsite = GARandomInt(0, mom.length());
momlen = dadlen = mom.length() - momsite;
}
else if(sis.resizeBehaviour() == GAGenome::FIXED_SIZE ||
bro.resizeBehaviour() == GAGenome::FIXED_SIZE){
GAErr(GA_LOC, mom.className(), "one-point cross", gaErrSameBehavReqd);
return nc;
}
else{
momsite = GARandomInt(0, mom.length());
dadsite = GARandomInt(0, dad.length());
momlen = mom.length() - momsite;
dadlen = dad.length() - dadsite;
sis.resize(momsite+dadlen);
bro.resize(dadsite+momlen);
}
sis.copy(mom, 0, 0, momsite);
sis.copy(dad, momsite, dadsite, dadlen);
bro.copy(dad, 0, 0, dadsite);
bro.copy(mom, dadsite, momsite, momlen);
nc = 2;
}
else if(c1 || c2){
GA1DArrayGenome<T> &sis = (c1 ?
DYN_CAST(GA1DArrayGenome<T> &, *c1) :
DYN_CAST(GA1DArrayGenome<T> &, *c2));
if(sis.resizeBehaviour() == GAGenome::FIXED_SIZE){
if(mom.length() != dad.length() || sis.length() != mom.length()){
GAErr(GA_LOC, mom.className(), "one-point cross", gaErrSameLengthReqd);
return nc;
}
momsite = dadsite = GARandomInt(0, mom.length());
momlen = dadlen = mom.length() - momsite;
}
else{
momsite = GARandomInt(0, mom.length());
dadsite = GARandomInt(0, dad.length());
momlen = mom.length() - momsite;
dadlen = dad.length() - dadsite;
sis.resize(momsite+dadlen);
}
if(GARandomBit()){
sis.copy(mom, 0, 0, momsite);
sis.copy(dad, momsite, dadsite, dadlen);
}
else{
sis.copy(dad, 0, 0, dadsite);
sis.copy(mom, dadsite, momsite, momlen);
}
nc = 1;
}
return nc;
}
// Two point crossover for the 1D array genome. Similar to the single point
// crossover, but here we pick two points then grab the sections based upon
// those two points.
// When we pick the points, it doesn't matter where they fall (one is not
// dependent upon the other). Make sure we get the lesser one into the first
// position of our site array.
template <class T> int
GA1DArrayGenome<T>::
TwoPointCrossover(const GAGenome& p1, const GAGenome& p2,
GAGenome* c1, GAGenome* c2){
const GA1DArrayGenome<T> &mom=DYN_CAST(const GA1DArrayGenome<T> &, p1);
const GA1DArrayGenome<T> &dad=DYN_CAST(const GA1DArrayGenome<T> &, p2);
int nc=0;
unsigned int momsite[2], momlen[2];
unsigned int dadsite[2], dadlen[2];
if(c1 && c2){
GA1DArrayGenome<T> &sis=DYN_CAST(GA1DArrayGenome<T> &, *c1);
GA1DArrayGenome<T> &bro=DYN_CAST(GA1DArrayGenome<T> &, *c2);
if(sis.resizeBehaviour() == GAGenome::FIXED_SIZE &&
bro.resizeBehaviour() == GAGenome::FIXED_SIZE){
if(mom.length() != dad.length() ||
sis.length() != bro.length() ||
sis.length() != mom.length()){
GAErr(GA_LOC, mom.className(), "two-point cross", gaErrSameLengthReqd);
return nc;
}
momsite[0] = GARandomInt(0, mom.length());
momsite[1] = GARandomInt(0, mom.length());
if(momsite[0] > momsite[1]) SWAP(momsite[0], momsite[1]);
momlen[0] = momsite[1] - momsite[0];
momlen[1] = mom.length() - momsite[1];
dadsite[0] = momsite[0];
dadsite[1] = momsite[1];
dadlen[0] = momlen[0];
dadlen[1] = momlen[1];
}
else if(sis.resizeBehaviour() == GAGenome::FIXED_SIZE ||
bro.resizeBehaviour() == GAGenome::FIXED_SIZE){
return nc;
}
else{
momsite[0] = GARandomInt(0, mom.length());
momsite[1] = GARandomInt(0, mom.length());
if(momsite[0] > momsite[1]) SWAP(momsite[0], momsite[1]);
momlen[0] = momsite[1] - momsite[0];
momlen[1] = mom.length() - momsite[1];
dadsite[0] = GARandomInt(0, dad.length());
dadsite[1] = GARandomInt(0, dad.length());
if(dadsite[0] > dadsite[1]) SWAP(dadsite[0], dadsite[1]);
dadlen[0] = dadsite[1] - dadsite[0];
dadlen[1] = dad.length() - dadsite[1];
sis.resize(momsite[0]+dadlen[0]+momlen[1]);
bro.resize(dadsite[0]+momlen[0]+dadlen[1]);
}
sis.copy(mom, 0, 0, momsite[0]);
sis.copy(dad, momsite[0], dadsite[0], dadlen[0]);
sis.copy(mom, momsite[0]+dadlen[0], momsite[1], momlen[1]);
bro.copy(dad, 0, 0, dadsite[0]);
bro.copy(mom, dadsite[0], momsite[0], momlen[0]);
bro.copy(dad, dadsite[0]+momlen[0], dadsite[1], dadlen[1]);
nc = 2;
}
else if(c1 || c2){
GA1DArrayGenome<T> &sis = (c1 ?
DYN_CAST(GA1DArrayGenome<T> &, *c1) :
DYN_CAST(GA1DArrayGenome<T> &, *c2));
if(sis.resizeBehaviour() == GAGenome::FIXED_SIZE){
if(mom.length() != dad.length() || sis.length() != mom.length()){
GAErr(GA_LOC, mom.className(), "two-point cross", gaErrSameLengthReqd);
return nc;
}
momsite[0] = GARandomInt(0, mom.length());
momsite[1] = GARandomInt(0, mom.length());
if(momsite[0] > momsite[1]) SWAP(momsite[0], momsite[1]);
momlen[0] = momsite[1] - momsite[0];
momlen[1] = mom.length() - momsite[1];
dadsite[0] = momsite[0];
dadsite[1] = momsite[1];
dadlen[0] = momlen[0];
dadlen[1] = momlen[1];
}
else{
momsite[0] = GARandomInt(0, mom.length());
momsite[1] = GARandomInt(0, mom.length());
if(momsite[0] > momsite[1]) SWAP(momsite[0], momsite[1]);
momlen[0] = momsite[1] - momsite[0];
momlen[1] = mom.length() - momsite[1];
dadsite[0] = GARandomInt(0, dad.length());
dadsite[1] = GARandomInt(0, dad.length());
if(dadsite[0] > dadsite[1]) SWAP(dadsite[0], dadsite[1]);
dadlen[0] = dadsite[1] - dadsite[0];
dadlen[1] = dad.length() - dadsite[1];
sis.resize(momsite[0]+dadlen[0]+momlen[1]);
}
if(GARandomBit()){
sis.copy(mom, 0, 0, momsite[0]);
sis.copy(dad, momsite[0], dadsite[0], dadlen[0]);
sis.copy(mom, momsite[0]+dadlen[0], momsite[1], momlen[1]);
}
else{
sis.copy(dad, 0, 0, dadsite[0]);
sis.copy(mom, dadsite[0], momsite[0], momlen[0]);
sis.copy(dad, dadsite[0]+momlen[0], dadsite[1], dadlen[1]);
}
nc = 1;
}
return nc;
}
// Even and odd crossover for the array works just like it does for the
// binary strings. For even crossover we take the 0th element and every other
// one after that from the mother. The 1st and every other come from the
// father. For odd crossover, we do just the opposite.
template <class T> int
GA1DArrayGenome<T>::
EvenOddCrossover(const GAGenome& p1, const GAGenome& p2,
GAGenome* c1, GAGenome* c2){
const GA1DArrayGenome<T> &mom=DYN_CAST(const GA1DArrayGenome<T> &, p1);
const GA1DArrayGenome<T> &dad=DYN_CAST(const GA1DArrayGenome<T> &, p2);
int nc=0;
int i;
if(c1 && c2){
GA1DArrayGenome<T> &sis=DYN_CAST(GA1DArrayGenome<T> &, *c1);
GA1DArrayGenome<T> &bro=DYN_CAST(GA1DArrayGenome<T> &, *c2);
if(sis.length() == bro.length() &&
mom.length() == dad.length() &&
sis.length() == mom.length()){
for(i=sis.length()-1; i>=1; i-=2){
sis.gene(i, mom.gene(i));
bro.gene(i, dad.gene(i));
sis.gene(i-1, dad.gene(i-1));
bro.gene(i-1, mom.gene(i-1));
}
if(i==0){
sis.gene(0, mom.gene(0));
bro.gene(0, dad.gene(0));
}
}
else{
int start;
int min = (mom.length() < dad.length()) ? mom.length() : dad.length();
start = (sis.length() < min) ? sis.length()-1 : min-1;
for(i=start; i>=0; i--)
sis.gene(i, ((i%2 == 0) ? mom.gene(i) : dad.gene(i)));
start = (bro.length() < min) ? bro.length()-1 : min-1;
for(i=start; i>=0; i--)
bro.gene(i, ((i%2 == 0) ? dad.gene(i) : mom.gene(i)));
}
nc = 2;
}
else if(c1 || c2){
GA1DArrayGenome<T> &sis = (c1 ?
DYN_CAST(GA1DArrayGenome<T> &, *c1) :
DYN_CAST(GA1DArrayGenome<T> &, *c2));
if(mom.length() == dad.length() && sis.length() == mom.length()){
for(i=sis.length()-1; i>=1; i-=2){
sis.gene(i, mom.gene(i));
sis.gene(i-1, dad.gene(i-1));
}
if(i==0){
sis.gene(0, mom.gene(0));
}
}
else{
int min = (mom.length() < dad.length()) ? mom.length() : dad.length();
min = (sis.length() < min) ? sis.length()-1 : min-1;
for(i=min; i>=0; i--)
sis.gene(i, ((i%2 == 0) ? mom.gene(i) : dad.gene(i)));
}
nc = 1;
}
return nc;
}
// Partial match crossover for the 1D array genome. This uses the partial
// matching algorithm described in Goldberg's book.
// Parents and children must be the same size for this crossover to work. If
// they are not, we post an error message.
// We make sure that b will be greater than a.
template <class T> int
GA1DArrayGenome<T>::
PartialMatchCrossover(const GAGenome& p1, const GAGenome& p2,
GAGenome* c1, GAGenome* c2){
const GA1DArrayGenome<T> &mom=DYN_CAST(const GA1DArrayGenome<T> &, p1);
const GA1DArrayGenome<T> &dad=DYN_CAST(const GA1DArrayGenome<T> &, p2);
int nc=0;
int a = GARandomInt(0, mom.length());
int b = GARandomInt(0, dad.length());
if(b < a) SWAP(a,b);
int i,j,index;
if(mom.length() != dad.length()){
GAErr(GA_LOC, mom.className(), "parial match cross", gaErrBadParentLength);
return nc;
}
if(c1 && c2){
GA1DArrayGenome<T> &sis=DYN_CAST(GA1DArrayGenome<T> &, *c1);
GA1DArrayGenome<T> &bro=DYN_CAST(GA1DArrayGenome<T> &, *c2);
sis.GAArray<T>::copy(mom);
for(i=a, index=a; i<b; i++, index++){
for(j=0; j<sis.length()-1 && sis.gene(j) != dad.gene(index); j++);
sis.swap(i,j);
}
bro.GAArray<T>::copy(dad);
for(i=a, index=a; i<b; i++, index++){
for(j=0; j<bro.length()-1 && bro.gene(j) != mom.gene(index); j++);
bro.swap(i,j);
}
nc = 2;
}
else if(c1 || c2){
GA1DArrayGenome<T> &sis = (c1 ?
DYN_CAST(GA1DArrayGenome<T> &, *c1) :
DYN_CAST(GA1DArrayGenome<T> &, *c2));
const GA1DArrayGenome<T> *parent1, *parent2;
if(GARandomBit()) { parent1 = &mom; parent2 = &dad; }
else { parent1 = &dad; parent2 = &mom; }
sis.GAArray<T>::copy(*parent1);
for(i=a, index=a; i<b; i++, index++){
for(j=0; j<sis.length()-1 && sis.gene(j) != parent2->gene(index); j++);
sis.swap(i,j);
}
nc = 1;
}
return nc;
}
// This function determines whether or not an indexed position is a hole that
// we can substitute into. It does a linear search to find the holes (yuk).
template <class T> int
GA1DArrayIsHole(const GA1DArrayGenome<T> &c, const GA1DArrayGenome<T> &dad,
int index, int a, int b){
for(int i=a; i<b; i++)
if(c.gene(index) == dad.gene(i)) return 1;
return 0;
}
// Order crossover for the 1D array genome. This uses the order crossover
// described in Goldberg's book.
// Parents and children must be the same length.
// We make sure that b will be greater than a.
// This implementation isn't terribly smart. For example, I do a linear
// search rather than caching and doing binary search or smarter hash tables.
// First we copy the mother into the sister. Then move the 'holes' into the
// crossover section and maintain the ordering of the non-hole elements.
// Finally, put the 'holes' in the proper order within the crossover section.
// After we have done the sister, we do the brother.
template <class T> int
GA1DArrayGenome<T>::
OrderCrossover(const GAGenome& p1, const GAGenome& p2,
GAGenome* c1, GAGenome* c2){
const GA1DArrayGenome<T> &mom=DYN_CAST(const GA1DArrayGenome<T> &, p1);
const GA1DArrayGenome<T> &dad=DYN_CAST(const GA1DArrayGenome<T> &, p2);
int nc=0;
int a = GARandomInt(0, mom.length());
int b = GARandomInt(0, mom.length());
if(b < a) SWAP(a,b);
int i,j, index;
if(mom.length() != dad.length()){
GAErr(GA_LOC, mom.className(), "order cross", gaErrBadParentLength);
return nc;
}
if(c1 && c2){
GA1DArrayGenome<T> &sis=DYN_CAST(GA1DArrayGenome<T> &, *c1);
GA1DArrayGenome<T> &bro=DYN_CAST(GA1DArrayGenome<T> &, *c2);
// Copy the parent
sis.GAArray<T>::copy(mom);
// Move all the 'holes' into the crossover section
for(i=0, index=b; i<sis.size(); i++, index++){
if(index >= sis.size()) index=0;
if(GA1DArrayIsHole(sis,dad,index,a,b)) break;
}
for(; i<sis.size()-b+a; i++, index++){
if(index >= sis.size()) index=0;
j=index;
do{
j++;
if(j >= sis.size()) j=0;
} while(GA1DArrayIsHole(sis,dad,j,a,b));
sis.swap(index,j);
}
// Now put the 'holes' in the proper order within the crossover section.
for(i=a; i<b; i++){
if(sis.gene(i) != dad.gene(i)){
for(j=i+1; j<b; j++)
if(sis.gene(j) == dad.gene(i)) sis.swap(i,j);
}
}
// Now do the other child
bro.GAArray<T>::copy(dad);
// Move all the 'holes' into the crossover section
for(i=0, index=b; i<bro.size(); i++, index++){
if(index >= bro.size()) index=0;
if(GA1DArrayIsHole(bro,mom,index,a,b)) break;
}
for(; i<bro.size()-b+a; i++, index++){
if(index >= bro.size()) index=0;
j=index;
do{
j++;
if(j >= bro.size()) j=0;
} while(GA1DArrayIsHole(bro,mom,j,a,b));
bro.swap(index,j);
}
// Now put the 'holes' in the proper order within the crossover section.
for(i=a; i<b; i++){
if(bro.gene(i) != mom.gene(i)){
for(j=i+1; j<b; j++)
if(bro.gene(j) == mom.gene(i)) bro.swap(i,j);
}
}
nc = 2;
}
else if(c1 || c2){
GA1DArrayGenome<T> &sis = (c1 ?
DYN_CAST(GA1DArrayGenome<T> &, *c1) :
DYN_CAST(GA1DArrayGenome<T> &, *c2));
const GA1DArrayGenome<T> *parent1, *parent2;
if(GARandomBit()) { parent1 = &mom; parent2 = &dad; }
else { parent1 = &dad; parent2 = &mom; }
sis.GAArray<T>::copy(*parent1);
for(i=0, index=b; i<sis.size(); i++, index++){
if(index >= sis.size()) index=0;
if(GA1DArrayIsHole(sis,*parent2,index,a,b)) break;
}
for(; i<sis.size()-b+a; i++, index++){
if(index >= sis.size()) index=0;
j=index;
do{
j++;
if(j >= sis.size()) j=0;
} while(GA1DArrayIsHole(sis,*parent2,j,a,b));
sis.swap(index,j);
}
for(i=a; i<b; i++){
if(sis.gene(i) != parent2->gene(i)){
for(j=i+1; j<b; j++)
if(sis.gene(j) == parent2->gene(i)) sis.swap(i,j);
}
}
nc = 1;
}
return nc;
}
// Cycle crossover for the 1D array genome. This is implemented as described
// in goldberg's book. The first is picked from mom, then cycle using dad.
// Finally, fill in the gaps with the elements from dad.
// We allocate space for a temporary array in this routine. It never frees
// the memory that it uses, so you might want to re-think this if you're really
// memory-constrained (similar to what we do with the uniform crossover when
// the children are resizeable).
// Allocate space for an array of flags. We use this to keep track of whether
// the child's contents came from the mother or the father. We don't free the
// space here, but it is not a memory leak.
// The first step is to cycle through mom & dad to get the cyclic part of
// the crossover. Then fill in the rest of the sis with dad's contents that
// we didn't use in the cycle. Finally, do the same thing for the other child.
// Notice that this implementation makes serious use of the operator= for the
// objects in the array. It also requires the operator != and == comparators.
template <class T> int
GA1DArrayGenome<T>::
CycleCrossover(const GAGenome& p1, const GAGenome& p2,
GAGenome* c1, GAGenome* c2){
const GA1DArrayGenome<T> &mom=DYN_CAST(const GA1DArrayGenome<T> &, p1);
const GA1DArrayGenome<T> &dad=DYN_CAST(const GA1DArrayGenome<T> &, p2);
int nc=0;
int i, current=0;
if(mom.length() != dad.length()){
GAErr(GA_LOC, mom.className(), "cycle cross", gaErrBadParentLength);
return nc;
}
if(c1 && c2){
GAMask mask;
GA1DArrayGenome<T> &sis=DYN_CAST(GA1DArrayGenome<T> &, *c1);
GA1DArrayGenome<T> &bro=DYN_CAST(GA1DArrayGenome<T> &, *c2);
mask.size(sis.length());
mask.clear();
sis.gene(0, mom.gene(0));
mask[0] = 1;
while(dad.gene(current) != mom.gene(0)){
for(i=0; i<sis.size(); i++){
if(mom.gene(i) == dad.gene(current)){
sis.gene(i, mom.gene(i));
mask[i] = 1;
current = i;
break;
}
}
}
for(i=0; i<sis.size(); i++)
if(mask[i] == 0) sis.gene(i, dad.gene(i));
mask.clear();
bro.gene(0, dad.gene(0));
mask[0] = 1;
while(mom.gene(current) != dad.gene(0)){
for(i=0; i<bro.size(); i++){
if(dad.gene(i) == mom.gene(current)){
bro.gene(i, dad.gene(i));
mask[i] = 1;
current = i;
break;
}
}
}
for(i=0; i<bro.size(); i++)
if(mask[i] == 0) bro.gene(i, mom.gene(i));
nc = 2;
}
else if(c1 || c2){
GA1DArrayGenome<T> &sis = (c1 ?
DYN_CAST(GA1DArrayGenome<T> &, *c1) :
DYN_CAST(GA1DArrayGenome<T> &, *c2));
const GA1DArrayGenome<T> *parent1, *parent2;
if(GARandomBit()) { parent1 = &mom; parent2 = &dad; }
else { parent1 = &dad; parent2 = &mom; }
GAMask mask;
mask.size(sis.length());
mask.clear();
sis.gene(0, parent1->gene(0));
mask[0] = 1;
while(parent2->gene(current) != parent1->gene(0)){
for(i=0; i<sis.size(); i++){
if(parent1->gene(i) == parent2->gene(current)){
sis.gene(i, parent1->gene(i));
mask[i] = 1;
current = i;
break;
}
}
}
for(i=0; i<sis.size(); i++)
if(mask[i] == 0) sis.gene(i, parent2->gene(i));
nc = 1;
}
return nc;
}
#undef SWAP
#endif
|