home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 42 / Amiga Format AFCD42 (Issue 126, Aug 1999).iso / -serious- / programming / c / gaplib_beta / wizards / templates / bitmatrix.c next >
Encoding:
C/C++ Source or Header  |  1999-05-14  |  2.5 KB  |  129 lines

  1.  
  2. void Init$N(struct $N *Polly)
  3. {
  4. int    x,y;
  5.  
  6. Polly->x = WIDTH$I;    $1/* Width in bits. */$
  7. Polly->y = HEIGHT$I;    $1/* Height in bits. */$
  8. Polly->xb = WIDTH$I>>3;    $1/* Width in bytes. */$
  9.  
  10. $2/* Initialize to zero or random depending on the ZINIT macro. */$
  11.  
  12. #ifdef    ZINIT$I
  13. for(x=0;x!=(WIDTH$I>>3);x++) {
  14.     for(y=0;y!=HEIGHT$I;y++) {
  15.         Polly->matrix[y][x] = 0;
  16.     }
  17. }
  18. #else
  19. for(x=0;x!=(WIDTH$I>>3);x++) {
  20.     for(y=0;y!=HEIGHT$I;y++) {
  21.         Polly->matrix[y][x] = Rnd(256);
  22.     }
  23. }
  24. #endif
  25.  
  26. }
  27.  
  28. void Mutate$N(struct $N *Polly)    $3/* Flip a random bit at random. */$
  29. {
  30. int fx,fy;
  31.  
  32. if(Rnd(1024)==512) {
  33.     fx = Rnd(Polly->x);
  34.     fy = Rnd(Polly->y);
  35.     Flip(&Polly->matrix[fy][fx>>3],fx&7);
  36. $4/*
  37.    NAME
  38.         Flip -- Flip a bit in a bitstring.
  39.  
  40.    SYNOPSIS
  41.         void Flip(void *,int);
  42.  
  43.         Flip(Ind,At);
  44.  
  45.    FUNCTION
  46.         Flips a bit in a bitstring. Bits are counted from lower addresses to
  47.         higher.
  48. */
  49. $}
  50.  
  51. }
  52.  
  53. void Cross$N(struct $N *Polly,struct $N *Tweety)
  54. {
  55. int    x,y,i,size=Polly->xb;
  56. char    *tmp;
  57.  
  58. $4/*
  59.  *
  60.  * Allocate a temporary buffer for crossover, this could be static
  61.  * if no multithreading is done.
  62.  * This would probably increase efficiency slightly.
  63.  *
  64. */$
  65.  
  66. if((tmp=malloc(size))!=NULL) {
  67.  
  68.     x = Rnd(Polly->x);    $3/* This comment is here just to irritate you! */$
  69.     y = Rnd(Polly->y);
  70.  
  71.     for(i=0;i<y;i++) {    $1/* Swap rows. */$
  72.         memcpy(tmp,Polly->matrix[i],size);
  73.         memcpy(Polly->matrix[i],Tweety->matrix[i],size);
  74.         memcpy(Tweety->matrix[i],tmp,size);
  75.     }
  76.  
  77.     free(tmp);
  78.  
  79.     for(i=0;i<Polly->y;i++) {    $1/* Swap columns. */$
  80.         Crossover(Polly->matrix[i],Tweety->matrix[i],x,size);
  81.     }
  82. $4/*
  83.    NAME
  84.         Crossover -- Perform crossover on two bitstrings.
  85.  
  86.    SYNOPSIS
  87.         void Crossover(void *,void *,int,int);
  88.  
  89.         Crossover(void *Ind1,void *Ind2,int At,int Size);
  90.  
  91.    FUNCTION
  92.         Performs one-point crossover of two bitstrings. The bitstrings must
  93.         have the same length.
  94. */
  95. $}
  96.  
  97. #ifdef    MPCROSS$I
  98. if(Rnd(1024)<128) {    $1/* One chance in eight. */$
  99.     Cross$I(Polly,Tweety);
  100. }
  101. #endif
  102.  
  103. }
  104.  
  105. $3/* Another meaningless comment. */$
  106.  
  107. double Compare$N(struct $N *Polly,struct $N *Tweety,int size)
  108. {
  109. return((double)HammingDist(Polly->matrix,Tweety->matrix,Polly->xb*Polly->y));
  110. $4/*
  111.    NAME
  112.         HammingDist -- Measure the Hamming distance between two bitstrings.
  113.  
  114.    SYNOPSIS
  115.         unsigned long int HammingDist(void *,void *,int);
  116.  
  117.         distance = HammingDist(Ind1,Ind2,Size);
  118.  
  119.    FUNCTION
  120.         Counts the number of differings bits in two bitstrings.
  121. */
  122. $}
  123.  
  124. void Kill$N(struct $N *Polly) $3/* Free resources if needed (not needed) */$
  125. {
  126. ;
  127. }
  128.  
  129.