home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_10_09 / 1009088a < prev    next >
Text File  |  1992-05-11  |  6KB  |  280 lines

  1.  
  2.  
  3. /*
  4.    madaline.c
  5.  
  6.    Dwayne Phillips
  7.    February 1992
  8.  
  9.    The functions in this file implement the multiple
  10.    adaptive linear element - Madaline - and the 
  11.    Madaline I learning law.
  12.  
  13.    Contents:
  14.    display_2d_weights
  15.    initialize_2d_weights
  16.    madaline_output
  17.    process_new_madaline
  18.    train_the_madaline
  19.    which_adaline
  20.  
  21. */
  22.  
  23.  
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <math.h>
  27.  
  28.  
  29.  
  30.  
  31. /*
  32.    void initialize_2d_weights(w, N, A)
  33.  
  34.    This function initializes the weights in th
  35.    2-dimensional weight vector.  They are set
  36.    to 0 2 4 6 0 2 4 6 ...
  37. */
  38.  
  39. long  initialize_2d_weights(w, N, A)
  40.    long  **w, N, A;
  41. {
  42.    int i, j;
  43.    for(i=0; i<A; i++)
  44.       for(j=0; j<N+1; j++)
  45.          w[i][j] = ((i+j+1) % 4) * 2;
  46. }  /* ends initialize_2d_weights */
  47.  
  48.  
  49.  
  50.  
  51. /*
  52.    long  display_2d_weights(w, N, A)
  53.  
  54.    This function displays the 2-dimemsional
  55.    weights to the screen.
  56. */
  57.  
  58. long  display_2d_weights(w, N, A)
  59.    long  **w, N, A;
  60. {
  61.    int i, j;
  62.    for(i=0; i<A; i++){
  63.       printf("\n>>");
  64.       for(j=0; j<N+1; j++)
  65.          printf("%10ld ", w[i][j]);
  66.    }
  67. }  /* ends display_2d_weights */
  68.  
  69.  
  70.  
  71.  
  72. /*
  73.    long  train_the_madaline(inputs, weights, x, w,
  74.                             nets, outs, N, A, choice)
  75.  
  76.    x[N+2]  nets[A]  outs[A]  w[A][N+1]
  77.  
  78.    This function trains the Madaline network using
  79.    the Madaline I training law.  It loops through 
  80.    the input vectors.  If an output is wrong, it
  81.    trains the weights for that input.  If a single 
  82.    output is wrong from all of the inputs, then
  83.    the training loop repeats.  This continues until
  84.    all of the answers are correct, or until the training
  85.    goes beyond a limit.
  86. */
  87.  
  88. long  train_the_madaline(inputs, weights, x, w,
  89.                          nets, outs, N, A, choice)
  90.    char  choice;
  91.    FILE  *inputs, *weights;
  92.    long  A, N, nets[], outs[], x[], **w;
  93. {
  94.    char  string[80];
  95.    float eta = 0.5;
  96.    int   adjusting = 1,
  97.          counter   = 0,
  98.          i,
  99.          j,
  100.          s;
  101.  
  102.    long  net,
  103.          one_net,
  104.          output,
  105.          target,
  106.          this_one;
  107.  
  108.    initialize_2d_weights(w, N, A);
  109.    printf("\nEnter number of input vectors >>");
  110.    gets(string);
  111.    s = atoi(string);
  112.  
  113.    while(adjusting){
  114.       printf("\nwhile adjusting counter=%d", counter);
  115.       counter++;
  116.       adjusting = 0;
  117.       fseek(inputs, 0L, SEEK_SET);
  118.  
  119.       for(i=0; i<s; i++){
  120.          fread(x, (N+2)*sizeof(long ), 1, inputs);
  121.  
  122.          for(j=0; j<A; j++){
  123.             calculate_net(&one_net, N, w[j], x);
  124.             nets[j] = one_net;
  125.             outs[j] = calculate_output(nets[j]);
  126.          }
  127.  
  128.          output = madaline_output(outs, choice, A);
  129.          target = x[N+1];
  130.  
  131.          if(output != target){
  132.             printf(" %d-t ", i);
  133.             adjusting = 1;
  134.             this_one  = which_adaline(target, outs, nets, A);
  135.             printf("%ldx ", this_one);
  136.             train_weights(target, nets[this_one], eta,
  137.                              w[this_one], x, N);
  138.          } /* ends if output != target */
  139.          else
  140.              printf(" %d-n ", i);
  141.  
  142.       } /* ends loop over s vectors */
  143.  
  144.       if(counter > 299){
  145.          printf("\n\nTOO MUCH TRAINING - quiting");
  146.          adjusting = 0;
  147.       }
  148.  
  149.  
  150.    }  /* ends while adjusting */
  151.  
  152.    printf("\n\nwent through the training cycle %d times",counter);
  153.    fclose(inputs);
  154.    for(i=0; i<A; i++)
  155.       fwrite(w[i], (N+1)*sizeof(long ), 1, weights);
  156.    fclose(weights);
  157.  
  158.    display_2d_weights(w, N, A);
  159.  
  160. }  /* ends train_the_madaline */
  161.  
  162.  
  163.  
  164.  
  165.  
  166. /*
  167.    long  madaline_output(outputs, choice, A)
  168.  
  169.    This function sets the single output for the 
  170.    Madaline network.  It can use either the
  171.    AND, OR, or Majority vote.
  172. */
  173.  
  174.  
  175. long  madaline_output(outputs, choice, A)
  176.    long  A, outputs[];
  177.    char  choice;
  178. {
  179.    int   i,
  180.          minus = 0,
  181.          plus  = 0;
  182.    long  result = -1;
  183.  
  184.  
  185.       /* use the AND method */
  186.    if(choice == 'a' ||
  187.       choice == 'A'){
  188.       result = 1;
  189.       for(i=0; i<A; i++)
  190.          if(outputs[i] == -1)
  191.             result = -1;
  192.    }
  193.  
  194.       /* use the OR method */
  195.    if(choice == 'o' ||
  196.       choice == 'O'){
  197.       for(i=0; i<A; i++)
  198.          if(outputs[i] == 1)
  199.             result = 1;
  200.    }
  201.  
  202.       /* use the Majority vote method */
  203.    if(choice == 'm' ||
  204.       choice == 'M'){
  205.       for(i=0; i<A; i++){
  206.          if(outputs[i] ==  1) plus++;
  207.          if(outputs[i] == -1) minus++;
  208.       }
  209.       if(plus > minus) result = 1;
  210.    }
  211.  
  212.    return(result);
  213. }  /* ends madaline_output */
  214.  
  215.  
  216.  
  217.  
  218. /*
  219.    long  which_adaline(target, outs, nets, A)
  220.  
  221.    Find the nets that has the smallest absolute value
  222.    and the corresponding outs does not equal the target.
  223. */
  224.  
  225. long  which_adaline(target, outs, nets, A)
  226.    long  A, nets[], outs[], target;
  227. {
  228.    int   i;
  229.    long  result, sum;
  230.  
  231.    sum = 32000;
  232.    for(i=0; i<A; i++){
  233.       if(outs[i] != target){
  234.          if(abs(nets[i]) < sum){
  235.             result = i;
  236.             sum    = abs(nets[i]);
  237.          }
  238.       }
  239.    }
  240.  
  241.    return(result);
  242.      }  /* ends which_adaline */
  243.  
  244.  
  245.  
  246.  
  247. /*
  248.    long  process_new_madaline(weights, x, w, nets, 
  249.                               outs, choice, N, A)
  250.    This function implements the working mode of the 
  251.    network.  It takes in a new input vector and
  252.    calculates the answer.
  253. */
  254.  
  255. long  process_new_madaline(weights, x, w, nets, 
  256.                            outs, choice, N, A)
  257.    char   choice;
  258.    FILE   *weights;
  259.    long   A, N, nets[], outs[], x[], **w;
  260. {
  261.    int   i, j;
  262.    long  one_net, output;
  263.  
  264.    for(i=0; i<A; i++)
  265.       fread(w[i], (N+1)*sizeof(long ), 1, weights);
  266.    fclose(weights);
  267.    display_2d_weights(w, N, A);
  268.  
  269.    get_straight_input_from_user(x, N);
  270.  
  271.    for(j=0; j<A; j++){
  272.       calculate_net(&one_net, N, w[j], x);
  273.       nets[j] = one_net;
  274.       outs[j] = calculate_output(nets[j]);
  275.    }
  276.  
  277.    output = madaline_output(outs, choice, A);
  278.    printf("\n\noutput is %ld \n", output);
  279. }  /* ends process_new_madaline */
  280.