home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / SNNSV32.ZIP / SNNSv3.2 / tools / sources / bignet.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-25  |  4.7 KB  |  169 lines

  1. /*     @(#)bignet.c    1.9 2/28/94  */
  2.  
  3. /*****************************************************************************
  4.   FILE           : bignet.c
  5.   SHORTNAME      : bignet.c
  6.   SNNS VERSION   : 3.2
  7.  
  8.   PURPOSE        :  SNNS-Network Generator for special 3 Layer Feedforward Networks
  9.   NOTES          :
  10.  
  11.   AUTHOR         : Niels Mache
  12.   DATE           : 01.10.90
  13.  
  14.   CHANGED BY     : Sven Doering
  15.   IDENTIFICATION : @(#)bignet.c    1.9 2/28/94
  16.   SCCS VERSION   : 1.9
  17.   LAST CHANGE    : 2/28/94
  18.  
  19.              Copyright (c) 1990-1994  SNNS Group, IPVR, Univ. Stuttgart, FRG
  20.  
  21. ******************************************************************************/
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24.  
  25. /*  SNNS-Kernel constants and data type definitions  */
  26. #include "glob_typ.h"
  27. /*  SNNS-Kernel User-Interface Function Prototypes   */
  28. #include "kr_ui.h"
  29.  
  30.  
  31.  
  32. static void  errChk( err_code )
  33. int  err_code;
  34. {
  35.   if (err_code != KRERR_NO_ERROR)  {
  36.     printf( "%s\n", krui_error( err_code ));
  37.     exit( 1 );
  38.   }
  39. }
  40.  
  41. int main()
  42. {
  43.   int    ret_code, i, j, unit_no;
  44.   int   IUnits, OUnits, HUnits;
  45.   char     netname[80], file_name[80];
  46.   struct PosType    unit_pos;
  47.   float  initialize_params[5];
  48.  
  49.  
  50.   printf( "\n%s\n", krui_getVersion() );
  51.   printf( "----  Network Generator for 3 Layer Feedforward Networks  ----\n\n" );
  52.  
  53.   printf( "No. of input units: " );
  54.   scanf( "%i", &IUnits );
  55.   printf( "No. of output units: " );
  56.   scanf( "%i", &OUnits );
  57.   printf( "No. of hidden units: " );
  58.   scanf( "%i", &HUnits );
  59.  
  60.   if ((IUnits <= 0) || (OUnits <= 0) || (HUnits <= 0))  {
  61.      printf( "\nInvalid no. of units !\n" );
  62.      exit( 1 );
  63.   }
  64.  
  65.   /*  Allocate units (the user may or may not use this function,
  66.       there is no need to do this but it's useful for the kernel memory
  67.       management to call this function before creating units)
  68.   */
  69.   printf( "\nCreate Units now ...\n" );
  70.   ret_code = krui_allocateUnits( OUnits + HUnits + IUnits );
  71.   errChk( ret_code );
  72.  
  73.   /*  Create standard (input) Units  */
  74.   unit_pos.x = 0;
  75.   for (i = 1; i <= IUnits; i++)  {
  76.     unit_no = krui_createDefaultUnit();
  77.     if (unit_no < 0)  errChk( unit_no );
  78.     ret_code = krui_setUnitTType( unit_no, INPUT );
  79.     errChk( ret_code );
  80.  
  81.     unit_pos.y = i;
  82.     krui_setUnitPosition( unit_no, &unit_pos );
  83.   }
  84.  
  85.   /*  Create standard (hidden) Units  */
  86.   unit_pos.x = 10;
  87.   for (i = 1; i <= HUnits; i++)  {
  88.     unit_no = krui_createDefaultUnit();
  89.     if (unit_no < 0)  errChk( unit_no );
  90.     ret_code = krui_setUnitTType( unit_no, HIDDEN );
  91.     errChk( ret_code );
  92.  
  93.     unit_pos.y = i;
  94.     krui_setUnitPosition( unit_no, &unit_pos );
  95.   }
  96.  
  97.   /*  Create standard (output) Units  */
  98.   unit_pos.x = 20;
  99.   for (i = 1; i <= OUnits; i++)  {
  100.     unit_no = krui_createDefaultUnit();
  101.     if (unit_no < 0)  errChk( unit_no );
  102.     ret_code = krui_setUnitTType( unit_no, OUTPUT );
  103.     errChk( ret_code );
  104.  
  105.     unit_pos.y = i;
  106.     krui_setUnitPosition( unit_no, &unit_pos );
  107.   }
  108.  
  109.  
  110.   /* Make Connections now */
  111.   /* Make connections between hidden units and output units first !  */
  112. printf("make connections\n");
  113.  
  114.   for (i = IUnits + HUnits + 1; i <= IUnits + HUnits + OUnits; i++)
  115.     {  /*  Make output unit to current unit  */
  116.     ret_code = krui_setCurrentUnit( i );
  117.     errChk( ret_code );
  118.  
  119.     for (j = IUnits + 1; j <= IUnits + HUnits; j++)
  120.       {  /*  connect current (output) unit with hidden unit. REMEMBER: The hidden unit #j
  121.          is the predecessor of the (output) unit #i (it is a backward connection) */
  122.       ret_code = krui_createLink( j, 0.0 );
  123.       errChk( ret_code );
  124.     }
  125.   }
  126.  
  127.   /* Make connections between input units and hidden units  */
  128.   for (i = IUnits + 1; i <= IUnits + HUnits; i++)
  129.     {  /*  Make hidden unit to current unit  */
  130.     ret_code = krui_setCurrentUnit( i );
  131.     errChk( ret_code );
  132.  
  133.     for (j = 1; j <= IUnits; j++)
  134.       {  /*  (backward) connect current (hidden) unit with input unit  */
  135.       ret_code = krui_createLink( j, 0.0 );
  136.       errChk( ret_code );
  137.     }
  138.   }
  139.  
  140.   /*  initialize the pseudo random generator with a really random value  */
  141.   krui_setSeedNo( 0 );
  142.  
  143.   /*  randomize all link weights in the range [-1.0,+1.0] */
  144.   ret_code = krui_setInitialisationFunc( "Randomize_Weights" );
  145.   errChk( ret_code );
  146.  
  147.   initialize_params[ 0 ] = -1.0;  /*  lower bound  */
  148.   initialize_params[ 1 ] = 1.0;   /*  upper bound  */
  149.   ret_code = krui_initializeNet( initialize_params, 2 );
  150.   errChk( ret_code );
  151.   /*  set the update function  */
  152.   ret_code = krui_setUpdateFunc( "Topological_Order" );
  153.   errChk( ret_code );
  154.  
  155.   /*  save the network    */
  156.   printf( "\nName of the network: " );
  157.   scanf( "%s", netname );
  158.   printf( "Filename of the network to save: " );
  159.   scanf( "%s", file_name );
  160.   printf( "Saving the network ...\n" );
  161.   ret_code = krui_saveNet( file_name, netname );
  162.   errChk( ret_code );
  163.  
  164.   /*  before exiting: delete network  */
  165.   krui_deleteNet();
  166.  
  167.   return( 0 );
  168. }
  169.