⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nnccpn.c

📁 这是一个CPN算法的源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
{
  INT  n,i;
  REAL Length, Length_;

  for (n=0; n<NUM_DATA; n++) {
    Length  = 0;
    Length_ = 0;
    for (i=0; i<N; i++) {
      Length  += sqr(Input [n][i]);
      Length_ += sqr(Input_[n][i]);
    }
    Length  = sqrt(Length);
    Length_ = sqrt(Length_);
    for (i=0; i<N; i++) {
      Input [n][i] /= Length;
      Input_[n][i] /= Length_;
    }
  }
}


void InitializeApplication(NET* Net)
{
  INT n,i,j;

  for (n=0; n<NUM_DATA; n++) {
    for (i=0; i<Y; i++) {
      for (j=0; j<X; j++) {
        Input [n][i*X+j] = (Pattern [n][i][j] == 'O') ? HI : LO;
        Input_[n][i*X+j] = (Pattern_[n][i][j] == 'O') ? HI : LO;
      }
    }
  }
  NormalizeInput();
  for (n=0; n<NUM_DATA; n++) {
    Output[n][0] = sin(n * 0.25 * PI);
    Output[n][1] = cos(n * 0.25 * PI);
  }
  f = fopen("CPN.txt", "w");
}


void WriteInput(NET* Net, REAL* Input)
{
  INT i;
   
  for (i=0; i<N; i++) {
    if (i%X == 0) {
      fprintf(f, "\n");
    }
    fprintf(f, "%c", (Input[i] != LO) ? 'O' : ' ');
  }
  fprintf(f, " -> ");
}


void WriteOutput(NET* Net, REAL* Output)
{
  REAL Angle;

  Angle = (atan2(Output[0], Output[1]) / PI) * 180;
  if (Angle < 0)
    Angle = Angle + 360;

  fprintf(f, "%0.0f癨n", Angle);
}


void FinalizeApplication(NET* Net)
{
  fclose(f);
}


/******************************************************************************
                          I N I T I A L I Z A T I O N
 ******************************************************************************/


void GenerateNetwork(NET* Net)
{
  INT i;

  Net->InputLayer   = (LAYER*) malloc(sizeof(LAYER));
  Net->InstarLayer  = (LAYER*) malloc(sizeof(LAYER));
  Net->OutstarLayer = (LAYER*) malloc(sizeof(LAYER));

  Net->InputLayer->Units    = N;
  Net->InputLayer->Output   = (REAL*)  calloc(N, sizeof(REAL));
      
  Net->InstarLayer->Units   = C;
  Net->InstarLayer->Output  = (REAL*)  calloc(C, sizeof(REAL));
  Net->InstarLayer->Weight  = (REAL**) calloc(C, sizeof(REAL*));
  Net->InstarLayer->Winner  = (BOOL*)  calloc(C, sizeof(BOOL));
      
  Net->OutstarLayer->Units  = M;
  Net->OutstarLayer->Output = (REAL*)  calloc(M, sizeof(REAL));
  Net->OutstarLayer->Weight = (REAL**) calloc(M, sizeof(REAL*));
      
  for (i=0; i<C; i++) {
    Net->InstarLayer->Weight[i] = (REAL*) calloc(N, sizeof(REAL));
  }
  for (i=0; i<M; i++) {
    Net->OutstarLayer->Weight[i] = (REAL*) calloc(C, sizeof(REAL));
  }

  Net->Winners = 1;
  Net->Alpha   = 0.1;
  Net->Beta    = 0.1;
}


void SetInput(NET* Net, REAL* Input, BOOL Protocoling)
{
  INT i;
   
  for (i=0; i<Net->InputLayer->Units; i++) {
    Net->InputLayer->Output[i] = Input[i];
  }
  if (Protocoling) {
    WriteInput(Net, Input);
  }
}


void GetOutput(NET* Net, REAL* Output, BOOL Protocoling)
{
  INT i;
   
  for (i=0; i<Net->OutstarLayer->Units; i++) {
    Output[i] = Net->OutstarLayer->Output[i];
  }
  if (Protocoling) {
    WriteOutput(Net, Output);
  }
}


/******************************************************************************
                     P R O P A G A T I N G   S I G N A L S
 ******************************************************************************/


void PropagateToInstars(NET* Net)
{
  INT  w,i,j;
  REAL Sum, SumWinners, MaxOut;
  INT  Winner;

  for (i=0; i<Net->InstarLayer->Units; i++) {
    Sum = 0;
    for (j=0; j<Net->InputLayer->Units; j++) {
      Sum += Net->InstarLayer->Weight[i][j] * Net->InputLayer->Output[j];
    }
    Net->InstarLayer->Output[i] = Sum;
    Net->InstarLayer->Winner[i] = FALSE;
  }
  SumWinners = 0;
  for (w=0; w<Net->Winners; w++) {
    MaxOut = MIN_REAL;
    for (i=0; i<Net->InstarLayer->Units; i++) {
      if (NOT Net->InstarLayer->Winner[i] AND Net->InstarLayer->Output[i] > MaxOut)
        MaxOut = Net->InstarLayer->Output[Winner = i];
    }
    Net->InstarLayer->Winner[Winner] = TRUE;
    SumWinners += Net->InstarLayer->Output[Winner];
  }
  for (i=0; i<Net->InstarLayer->Units; i++) {
    if (Net->InstarLayer->Winner[i])
      Net->InstarLayer->Output[i] = Net->InstarLayer->Output[i] / SumWinners;
    else
      Net->InstarLayer->Output[i] = 0;
  }
}


void PropagateToOutstars(NET* Net)
{
  INT  i,j;
  REAL Sum;

  for (i=0; i<Net->OutstarLayer->Units; i++) {
    Sum = 0;
    for (j=0; j<Net->InstarLayer->Units; j++) {  
      Sum += Net->OutstarLayer->Weight[i][j] * Net->InstarLayer->Output[j];
    }
    Net->OutstarLayer->Output[i] = Sum;
  }
}


void PropagateNet(NET* Net)
{
  PropagateToInstars(Net);
  PropagateToOutstars(Net);
}


/******************************************************************************
                        T R A I N I N G   T H E   N E T
 ******************************************************************************/


INT Winner(NET* Net)
{
  INT i;

  for (i=0; i<Net->InstarLayer->Units; i++) {
    if (Net->InstarLayer->Winner[i])
      return i;
  }
}


void TrainInstars(NET* Net, INT Epochs)
{
  INT n,m,i,j;
   
  for (i=0; i<Net->InstarLayer->Units; i++) {
    for (j=0; j<Net->InputLayer->Units; j++) {
      Net->InstarLayer->Weight[i][j] = Input[i][j];
    }
  }
  Net->Winners = 1;
  for (m=0; m<Epochs*NUM_DATA; m++) {
    n = RandomEqualINT(0, NUM_DATA-1);
    SetInput(Net, Input[n], FALSE);
    PropagateToInstars(Net);
    i = Winner(Net);
    for (j=0; j<Net->InputLayer->Units; j++) {
      Net->InstarLayer->Weight[i][j] +=
        Net->Alpha * (Input[n][j] - Net->InstarLayer->Weight[i][j]);
    }
  }
}


void TrainOutstars(NET* Net, INT Epochs)
{
  INT n,m,i,j;
   
  for (i=0; i<Net->OutstarLayer->Units; i++) {
    for (j=0; j<Net->InstarLayer->Units; j++) {
      Net->OutstarLayer->Weight[i][j] = Output[j][i];
    }
  }
  Net->Winners = 1;
  for (m=0; m<Epochs*NUM_DATA; m++) {
    n = RandomEqualINT(0, NUM_DATA-1);
    SetInput(Net, Input[n], FALSE);
    PropagateToInstars(Net);
    j = Winner(Net);
    for (i=0; i<Net->OutstarLayer->Units; i++) {
      Net->OutstarLayer->Weight[i][j] +=
        Net->Beta * (Output[n][i] - Net->OutstarLayer->Weight[i][j]);
    }
  }
}


/******************************************************************************
                      S I M U L A T I N G   T H E   N E T
 ******************************************************************************/


void SimulateNet(NET* Net, REAL* Input)
{
  REAL Output[M];

  SetInput(Net, Input, TRUE);
  PropagateNet(Net);
  GetOutput(Net, Output, TRUE);
}


/******************************************************************************
                                    M A I N
 ******************************************************************************/


void main()
{
  NET Net;
  INT n;

  InitializeRandoms();
  GenerateNetwork(&Net);
  InitializeApplication(&Net);
  TrainInstars(&Net, 0);         /* weights are computed for this application */
  TrainOutstars(&Net, 0);        /* weights are computed for this application */

  Net.Winners = 2;
  for (n=0; n<NUM_DATA; n++) {
    SimulateNet(&Net, Input[n]);
  }
  for (n=0; n<NUM_DATA; n++) {
    SimulateNet(&Net, Input_[n]);
  }

  FinalizeApplication(&Net);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -