home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / virus / ddj0491.zip / MORROW.ZIP / GENE.C < prev    next >
C/C++ Source or Header  |  1989-09-03  |  3KB  |  172 lines

  1. /***
  2. *       GASystem
  3. *       Mike Morrow
  4. *       September 1989
  5. ***/
  6.  
  7.  
  8.  
  9. #include "ga.h"
  10. #include "param.h"
  11. #include "util.h"
  12.  
  13.  
  14. extern void *calloc();
  15.  
  16. /**
  17. *    These variables hold parameters that the user may set by interacting with the
  18. *    symbol table.  The default values are hardwired here.
  19. **/
  20. static int  param_gene_len = 10;
  21. static int  param_gene_lowa = 'A';
  22. static int  param_gene_hia = 'Z';
  23.  
  24.  
  25.  
  26.  
  27. static CONST PARAMSPEC geneparams[] =
  28. {
  29.     {"LEN",         (void *) ¶m_gene_len,     MODEINT,    FLAG_INIT},
  30.     {"LOWA",         (void *) ¶m_gene_lowa,     MODEINT,    FLAG_INIT},
  31.     {"HIA",         (void *) ¶m_gene_hia,     MODEINT,    FLAG_INIT},
  32.  
  33.  
  34.  
  35.     {(char *) 0,     (void *) 0,                 (MODE) 0,        0}
  36. };
  37.  
  38.  
  39. /**
  40. *    This routine inserts information about the parameter vars
  41. *    into the symbol table.
  42. **/
  43. genepinit()
  44. {
  45.     CONST PARAMSPEC *p;
  46.     
  47.     for(p = geneparams; p->param_name; p++)
  48.         paramins(p, SYMVAR);
  49. }
  50.  
  51.  
  52. /**
  53. *    Generate a new gene.
  54. **/
  55. GENE *genenew()
  56. {
  57.  
  58.     GENE *new;
  59.     unsigned int i;
  60.     SEQ_COMPONENT range;
  61.     
  62.     new = calloc(1, sizeof(GENE));
  63.     if(! new)
  64.         return new;
  65.         
  66.     new->gene_seq = calloc(param_gene_len, sizeof(SEQ_COMPONENT));
  67.     if(! new->gene_seq)
  68.     {
  69.         free(new);
  70.         return (GENE *) 0;
  71.     }
  72.     
  73.     new->gene_fit = (FIT_TYPE) 0;
  74.     
  75.     /**
  76.     *    Generate a random SEQ pattern
  77.     **/
  78.     range = param_gene_hia - param_gene_lowa + 1;
  79.     for(i = 0; i < param_gene_len; i++)
  80.         new->gene_seq[i] = param_gene_lowa +
  81.                 (SEQ_COMPONENT) randnum((int) range);
  82.     
  83.     
  84.     return new;
  85. }
  86.  
  87.  
  88. /**
  89. *    Perform 2-point crossover on g1 and g2.
  90. **/
  91. void genecross(g1, g2)
  92. GENE *g1, *g2;
  93. {
  94.     int xpnt1, xpnt2;
  95.     register SEQ_COMPONENT *g1ptr, *g1end, *g2ptr;
  96.     
  97.     xpnt1 = randnum(param_gene_len);
  98.     xpnt2 = xpnt1 + randnum(param_gene_len - xpnt1);
  99.  
  100.     /**
  101.     *    Everything between xpnt1 and xpnt2 will be swapped.
  102.     **/
  103.     g1ptr = &(g1->gene_seq[xpnt1]);    
  104.     g1end = &(g1->gene_seq[xpnt2]);
  105.     g2ptr = &(g2->gene_seq[xpnt1]);
  106.     
  107.     while(g1ptr <= g1end)
  108.     {
  109.         register SEQ_COMPONENT tmp;
  110.         
  111.         tmp = *g2ptr;
  112.         *g2ptr++ = *g1ptr;
  113.         *g1ptr++ = tmp;
  114.     }
  115. }
  116.  
  117. /**
  118. *    Mutate a random allele in the gene g.
  119. **/
  120. void genemutate(g)
  121. GENE *g;
  122. {
  123.     SEQ_COMPONENT range, new;
  124.     int where;
  125.     
  126.     where = randnum(param_gene_len);
  127.     range = param_gene_hia - param_gene_lowa + 1;
  128.     new = param_gene_lowa + (SEQ_COMPONENT) randnum((int) range);
  129.  
  130.     g->gene_seq[where] = new;
  131. }
  132.     
  133.  
  134.  
  135. /**
  136. *    Duplicate the SEQ of g2 into g1.  Also the fitness rating is transferred.
  137. **/
  138. void genedupl(g1, g2)
  139. GENE *g1, *g2;
  140. {
  141.     memcpy(g1->gene_seq, g2->gene_seq, param_gene_len * sizeof(SEQ_COMPONENT));
  142.     g1->gene_fit = g2->gene_fit;
  143. }
  144.  
  145.  
  146. /**
  147. *    Apply the objective function to GENE g.
  148. **/
  149. FIT_TYPE genefitness(g)
  150. GENE *g;
  151. {
  152.     return g->gene_fit = objective(g->gene_seq, param_gene_len);
  153. }
  154.  
  155.  
  156.  
  157. void geneshow(g)
  158. GENE *g;
  159. {
  160.     objshow(g->gene_seq, param_gene_len, g->gene_fit);
  161. }
  162.  
  163.  
  164. /**
  165. *    Compare the fitness of two genes.  Return convention is like strcmp().
  166. **/
  167. int genecomp(g1, g2)
  168. GENE **g1, **g2;
  169. {
  170.     return (*g2)->gene_fit - (*g1)->gene_fit;
  171. }
  172.