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

  1.  
  2. /*
  3.     adamain.c
  4.  
  5.  
  6.    Dwayne Phillips
  7.    February 1992
  8.  
  9.    This file contains the main calling
  10.    routine for the Adaline (Adaptive
  11.    Linear Element) program.
  12.  
  13.   
  14.   
  15.    Notes:
  16.   
  17.    the inputs array x has N+2 elements
  18.      x[0] is always 1
  19.      then there are 1 to N elements
  20.      then the last element is the target
  21.    the weights array w has N+1 elements
  22.      w[0] is the bias
  23.      then there are 1 to N elements
  24.   
  25. */
  26.  
  27.  
  28.  
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <malloc.h>
  32.  
  33.  
  34. main(argc, argv)
  35.   int argc;
  36.   char *argv[];
  37. {
  38.    char  inputs_file[80], weights_file[80];
  39.    FILE  *inputs, *weights;
  40.    int   working_mode  = 0,
  41.          training_mode = 0,
  42.          input_mode    = 0,
  43.          files_ok      = 1;
  44.    long *w, *x, N;
  45.  
  46.  
  47.    if(argc < 5){
  48.       printf("\n\nUsage: adaline inputs_file weights_file ");
  49.       printf("size_of_vectors mode");
  50.       printf("\n   where mode=t (training)");
  51.       printf("\n   where mode=i (input data)");
  52.       printf("\n   where mode=w (working)");
  53.       exit(1);
  54.    }
  55.  
  56.    strcpy(inputs_file, argv[1]);
  57.    strcpy(weights_file, argv[2]);
  58.  
  59.    N = atoi(argv[3]);
  60.  
  61.    if(argv[4][0] == 't'  ||  argv[4][0] == 'T')
  62.       training_mode = 1;
  63.    if(argv[4][0] == 'i'  ||  argv[4][0] == 'I')
  64.       input_mode = 1;
  65.    if(argv[4][0] == 'w'  ||  argv[4][0] == 'W')
  66.       working_mode = 1;
  67.  
  68.  
  69.           /* the last element of the inputs vector will
  70.              hold the target */
  71.    x = (long *) malloc((N+2) * sizeof(long));
  72.    w = (long *) malloc((N+1) * sizeof(long));
  73.  
  74.  
  75.  
  76.  
  77.       /* I N P U T   M O D E */
  78.  
  79.    if(input_mode){
  80.       if( (inputs = fopen(inputs_file, "w+b")) == '\0'){
  81.          printf("\n\nERROR - cannot open input vector file\n");
  82.          exit(0);
  83.       }
  84.       else
  85.          get_straight_input_vectors(inputs, x, N);
  86.    }  /* ends if input_mode */
  87.  
  88.  
  89.  
  90.  
  91.       /*  T R A I N I N G   M O D E */
  92.  
  93.    if(training_mode){
  94.  
  95.       if( (inputs = fopen(inputs_file, "r+b")) == '\0'){
  96.          printf("\n\nERROR - cannot open input vector file\n");
  97.          files_ok = 0;
  98.          exit(0);
  99.       }
  100.  
  101.       if( (weights = fopen(weights_file, "w+b")) == '\0'){
  102.          printf("\n\nERROR - cannot open weights vector file\n");
  103.          files_ok = 0;
  104.          exit(0);
  105.       }
  106.  
  107.       if(files_ok)
  108.          train_the_adaline(inputs, weights, x, w, N);
  109.  
  110.    } /* ends training_mode */
  111.  
  112.  
  113.  
  114.  
  115.       /* W O R K I N G   M O D E */
  116.  
  117.    if(working_mode){
  118.  
  119.       if( (weights = fopen(weights_file, "r+b")) == '\0'){
  120.          printf("\n\nERROR - cannot open weights vector file\n");
  121.          exit(0);
  122.       }
  123.       else
  124.          process_new_case(weights, x, w, N);
  125.  
  126.    }  /* ends if working_mode */
  127.  
  128.    free(x);
  129.    free(w);
  130.  
  131.  
  132. }  /* ends main */
  133.