SOM.C
上传用户:gaoxuliang
上传日期:2013-01-26
资源大小:64k
文件大小:14k
开发平台:

Visual C++

  1. /******************************************************************************
  2.                       ===================
  3.         Network:      Self-Organizing Map
  4.                       ===================
  5.         Application:  Control
  6.                       Pole Balancing Problem
  7.         Author:       Karsten Kutza
  8.         Date:         6.6.96
  9.         Reference:    T. Kohonen
  10.                       Self-Organized Formation
  11.                       of Topologically Correct Feature Maps
  12.                       Biological Cybernetics, 43, pp. 59-69, 1982
  13.  ******************************************************************************/
  14. /******************************************************************************
  15.                             D E C L A R A T I O N S
  16.  ******************************************************************************/
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19. #include <math.h>
  20. typedef int           BOOL;
  21. typedef int           INT;
  22. typedef double        REAL;
  23. #define FALSE         0
  24. #define TRUE          1
  25. #define NOT           !
  26. #define AND           &&
  27. #define OR            ||
  28. #define MIN_REAL      -HUGE_VAL
  29. #define MAX_REAL      +HUGE_VAL
  30. #define MIN(x,y)      ((x)<(y) ? (x) : (y))
  31. #define MAX(x,y)      ((x)>(y) ? (x) : (y))
  32. #define PI            (2*asin(1))
  33. #define sqr(x)        ((x)*(x))
  34. typedef struct {                     /* A LAYER OF A NET:                     */
  35.         INT           Units;         /* - number of units in this layer       */
  36.         REAL*         Output;        /* - output of ith unit                  */
  37.         REAL**        Weight;        /* - connection weights to ith unit      */
  38.         REAL*         StepSize;      /* - size of search steps of ith unit    */
  39.         REAL*         dScoreMean;    /* - mean score delta of ith unit        */
  40. } LAYER;
  41. typedef struct {                     /* A NET:                                */
  42.         LAYER*        InputLayer;    /* - input layer                         */
  43.         LAYER*        KohonenLayer;  /* - Kohonen layer                       */
  44.         LAYER*        OutputLayer;   /* - output layer                        */
  45.         INT           Winner;        /* - last winner in Kohonen layer        */
  46.         REAL          Alpha;         /* - learning rate for Kohonen layer     */
  47.         REAL          Alpha_;        /* - learning rate for output layer      */
  48.         REAL          Alpha__;       /* - learning rate for step sizes        */
  49.         REAL          Gamma;         /* - smoothing factor for score deltas   */
  50.         REAL          Sigma;         /* - parameter for width of neighborhood */
  51. } NET;
  52. /******************************************************************************
  53.         R A N D O M S   D R A W N   F R O M   D I S T R I B U T I O N S
  54.  ******************************************************************************/
  55. void InitializeRandoms()
  56. {
  57.   srand(4715);
  58. }      
  59. REAL RandomEqualREAL(REAL Low, REAL High)
  60. {
  61.   return ((REAL) rand() / RAND_MAX) * (High-Low) + Low;
  62. }      
  63. REAL RandomNormalREAL(REAL Mu, REAL Sigma)
  64. {
  65.   REAL x,fx;
  66.   do {
  67.     x = RandomEqualREAL(Mu-3*Sigma, Mu+3*Sigma);
  68.     fx = (1 / (sqrt(2*PI)*Sigma)) * exp(-sqr(x-Mu) / (2*sqr(Sigma)));
  69.   } while (fx < RandomEqualREAL(0, 1));
  70.   return x;
  71. }      
  72. /******************************************************************************
  73.                A P P L I C A T I O N - S P E C I F I C   C O D E
  74.  ******************************************************************************/
  75. #define ROWS          25
  76. #define COLS          25
  77. #define N             2
  78. #define C             (ROWS * COLS)
  79. #define M             1
  80. #define TRAIN_STEPS   10000
  81. #define BALANCED      100
  82. FILE*                 f;
  83. void InitializeApplication(NET* Net)
  84. {
  85.   INT i;
  86.    
  87.   for (i=0; i<Net->KohonenLayer->Units; i++) {
  88.     Net->KohonenLayer->StepSize[i] = 1;
  89.     Net->KohonenLayer->dScoreMean[i] = 0;
  90.   }
  91.   f = fopen("SOM.txt", "w");
  92. }
  93. void WriteNet(NET* Net)
  94. {
  95.   INT  r,c;
  96.   REAL x,y,z;
  97.   fprintf(f, "nnn");
  98.   for (r=0; r<ROWS; r++) {
  99.     for (c=0; c<COLS; c++) {
  100.       x = Net->KohonenLayer->Weight[r*ROWS+c][0];
  101.       y = Net->KohonenLayer->Weight[r*ROWS+c][1];
  102.       z = Net->OutputLayer->Weight[0][r*ROWS+c];
  103.       fprintf(f, "([%5.1f