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 / boundedrealvector.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-05-14  |  1.1 KB  |  71 lines

  1.  
  2.  
  3. void Init$N(struct $N *Polly)
  4. {
  5. int i;
  6. for(i=0;i!=VLENGTH$I;i++) {
  7.     Polly->v[i] = InRand(Constraints$I[i][0],Constraints$I[i][1]);
  8. }
  9. }
  10.  
  11. void Mutate$N(struct $N *Polly)
  12. {
  13.  
  14.     int i;
  15.  
  16. for(i=0;i!=VLENGTH$I;i++) {
  17.     if(Rnd(1024)==512) {
  18.         Polly->v[i] = InRand(Constraints$I[i][0],Constraints$I[i][1]);
  19.     }
  20. }
  21.  
  22. }
  23.  
  24. $1/* Multipoint crossover for this genome is a numerical crossover. */
  25. $
  26. void Cross$N(struct $N *Polly, struct $N *Tweety)
  27. {
  28. int i;
  29. #ifndef    MPCROSS$I
  30. double t;
  31. i = Rnd(VLENGTH$I+1);
  32. for(;i<VLENGTH$I;i++) {
  33.     t = Polly->v[i];
  34.     Polly->v[i] = Tweety->v[i];
  35.     Tweety->v[i] = t;
  36. }
  37. #else
  38. double    delta,dpos,avg;
  39.  
  40. for(i=0;i!=VLENGTH$I;i++) {
  41.     delta = fabs(Polly->v[i]-Tweety->v[i]);
  42.     if(delta>DBL_EPSILON) {
  43.         dpos = InRand(-delta,delta);
  44.         avg = (Polly->v[i]+Tweety->v[i])/2.0;
  45.         Polly->v[i] = avg+dpos;
  46.         Tweety->v[i] = avg-dpos;
  47.     }
  48. }
  49. #endif
  50. }
  51.  
  52. $1/* Standard euclidian length of the difference vector. */
  53. $
  54. double Compare$N(struct $N *Polly,struct $N *Tweety)
  55. {
  56. double    l=0,t;
  57. int i;
  58.  
  59. for(i=0;i!=VLENGTH$I;i++) {
  60.     t = Polly->v[i]-Tweety->v[i];
  61.     l += t*t;
  62. }
  63.  
  64. return(sqrt(l));
  65. }
  66.  
  67. void Kill$N(struct %N *Polly)
  68. {
  69. ;
  70. }
  71.