#include <stdio.h>
#include <math.h>
#include <local/simu.h>
#define Pixel 42
#define Pattern 5
#define Test 40
#define IN 42
#define CELL 30
#define OUT 3
#define Number 3
#define Long 5
#define Figure 0.75
#define C_Code 'A'
#define Study 1000000
#define Rlow -0.30
#define Rhigh 0.30
#define urand() (drand48() * (Rhigh - Rlow) + Rlow)
double PATTERN[Pattern][Pixel]={{0,0,1,1,0,0,
0,1,0,0,1,0,
1,0,0,0,0,1,
1,0,0,0,0,1,
1,1,1,1,1,1,
1,0,0,0,0,1,
1,0,0,0,0,1},
{1,1,1,1,1,0,
1,0,0,0,0,1,
1,0,0,0,0,1,
1,1,1,1,1,0,
1,0,0,0,0,1,
1,0,0,0,0,1,
1,1,1,1,1,0},
{0,0,1,1,1,0,
0,1,0,0,0,1,
1,0,0,0,0,0,
1,0,0,0,0,0,
1,0,0,0,0,0,
0,1,0,0,0,1,
0,0,1,1,1,0},
{1,1,1,1,0,0,
0,1,0,0,1,0,
0,1,0,0,0,1,
0,1,0,0,0,1,
0,1,0,0,0,1,
0,1,0,0,1,0,
1,1,1,1,0,0},
{0,0,1,1,1,1,
0,0,1,0,0,0,
0,1,0,0,0,0,
0,1,1,1,1,0,
0,1,0,0,0,0,
1,0,0,0,0,0,
1,1,1,1,1,0}};
double Feedback1[CELL], Feedback2[CELL], Out[Test][OUT];
double teach1, teach2, teach3;
char Reference[Long];
char Cycle[Number][Long] = {{'A','B','C','D','E'},
{'D','C','B','A',0},
{'E','D','C',0,0}};
int Cycle_L[Number] = {5,4,3};
char Character[Pattern] = {'A','B','C','D','E'};
int TEST[Test] = {'E','D','C','B','A','B','C','D','E','C',
'B','A','B','C','E','D','C','B','A','C',
'D','E','D','C','D','A','D','E','A','B',
'C','D','E','D','C','B','A','B','C','E'};
Layer Layer1; /* 入力層 */
Layer Layer2; /* 中間層1 */
Layer Layer3; /* 中間層2 */
Layer Layer4; /* フィードバック層1 */
Layer Layer5; /* フィードバック層2 */
Layer Layer6; /* 出力層 */
Layer Layer7; /* 閾値 */
double drand48();
main()
{
srand48(1);
Establishment(); /* 初期設定 */
Learning(); /* 学習 */
Practice(); /* 実行 */
Output_Data(); /* 結果出力 */
}
Establishment()
{
int i,j;
Layer1 = Neurons(IN);
Coefficient_of_Layer(Layer1, 0.0, 0.0, 0.0, 1.0);
Layer2 = Neurons(CELL);
Coefficient_of_Layer(Layer2, 0.0, 0.0, 0.0, 1.0);
Layer3 = Neurons(CELL);
Coefficient_of_Layer(Layer3, 0.0, 0.0, 0.0, 1.0);
Layer4 = Neurons(CELL);
Coefficient_of_Layer(Layer4, 0.0, 0.0, 0.0, 1.0);
Layer5 = Neurons(CELL);
Coefficient_of_Layer(Layer5, 0.0, 0.0, 0.0, 1.0);
Layer6 = Neurons(OUT);
Coefficient_of_Layer(Layer6, 0.0, 0.0, 0.0, 1.0);
Layer7 = Neurons(1);
Coefficient_of_Layer(Layer7, 0.0, 0.0, 0.0, 1.0);
Teach_Signal_Manager(Layer6);
for(i = 0 ; i < IN ; i++)
for(j = 0 ; j < CELL ; j++)
Connect(Layer1, i, Layer2, j, urand());
for(i = 0 ; i < CELL ; i++)
for(j = 0 ; j < CELL ; j++)
{
Connect(Layer4, i, Layer2, j, urand());
Connect(Layer2, i, Layer3, j, urand());
Connect(Layer5, i, Layer3, j, urand());
}
for(i = 0 ; i < CELL ; i++)
for(j = 0 ; j < OUT ; j++)
Connect(Layer3, i, Layer6, j, urand());
for(i = 0 ; i < CELL ; i++)
{
Connect(Layer7, 0, Layer2, i, urand());
Connect(Layer7, 0, Layer3, i, urand());
}
for(i = 0 ; i < OUT ; i++)
Connect(Layer7, 0, Layer6, i, urand());
Change_Coefficient_W(0.2, 0.15);
}
Learning()
{
int i, j;
for(i = 0 ; i < CELL ; i++)
{
Feedback1[i] = 0.0;
Feedback2[i] = 0.0;
}
for(i = 0 ; i < Study ; i++)
{
Put_Output(Layer7, 0, 1.0);
Pattern_Input(); /* パターンの入力 */
Teach_Signal(); /* 教師信号の決定 */
for(j = 0 ; j < CELL ; j++)
{
Put_Output(Layer4, j, Feedback1[j]);
Put_Output(Layer5, j, Feedback2[j]);
}
Output_Calculation(Layer2);
Output_Calculation(Layer3);
Output_Calculation(Layer6);
Put_Teach_Signal(Layer6,teach1,teach2,teach3);
Error_Calculation(Layer6);
Error_Calculation(Layer3);
Error_Calculation(Layer2);
Change_Weight(Layer6);
Change_Weight(Layer3);
Change_Weight(Layer2);
for(j = 0 ; j < CELL ; j++)
{
Feedback1[j] = Feedback1[j] * Figure + Get_Output(Layer2, j);
Feedback2[j] = Feedback2[j] * Figure + Get_Output(Layer3, j);
}
}
}
Pattern_Input()
{
int pattern;
int i, x;
static int a = -1, b;
x = (int)(drand48() * 10);
if(x % 2 == 0 && a == -1)
{
pattern = (int)(drand48() * Pattern);
for(i = 0 ; i < Pixel ; i++)
Put_Output(Layer1, i, PATTERN[pattern][i]);
}
else
{
if(a < 0)
{
a = (int)(drand48() * Number);
b = 0;
}
pattern = (int)(Cycle[a][b] - C_Code);
for(i = 0 ; i < Pixel ; i++)
Put_Output(Layer1, i, PATTERN[pattern][i]);
b++;
if(b >= Cycle_L[a]) a = -1;
}
Reference[0] = Character[pattern];
}
Teach_Signal()
{
int i, x;
teach1 = 0.0; teach2 = 0.0; teach3 = 0.0;
x = Number;
for(i = 0 ; i < Number ; i++)
{
if(strncmp(Reference,Cycle[i],Cycle_L[i]) == 0)
{
x = i;
break;
}
}
switch(x)
{
case 0 : teach1 = 1.0;
break;
case 1 : teach2 = 1.0;
break;
case 2 : teach3 = 1.0;
break;
}
for(i = Long - 1 ; i > 0 ; i--)
Reference[i] = Reference[i - 1];
}
Practice()
{
int pattern;
int i, j;
for(i = 0 ; i < Test ; i++)
{
Put_Output(Layer7, 0, 1.0);
pattern = (int)(TEST[i] - C_Code);
for(j = 0 ; j < Pixel ; j++)
Put_Output(Layer1, j, PATTERN[pattern][j]);
for(j = 0 ; j < CELL ; j++)
{
Put_Output(Layer4, j, Feedback1[j]);
Put_Output(Layer5, j, Feedback2[j]);
}
Output_Calculation(Layer2);
Output_Calculation(Layer3);
Output_Calculation(Layer6);
for(j = 0 ; j < CELL ; j++)
{
Feedback1[j] = Feedback1[j] * Figure + Get_Output(Layer2, j);
Feedback2[j] = Feedback2[j] * Figure + Get_Output(Layer3, j);
}
for(j = 0 ; j < OUT ; j++)
Out[i][j] = Get_Output(Layer6,j);
}
}
Output_Data()
{
int i, j;
FILE *fp;
if((fp = fopen("acycle24.1000000.dat", "w")) == NULL)
{
fprintf(fp, " Can't open file !!\n");
exit(-1);
}
fprintf(fp, "学習回数 = %d\n", Study);
fprintf(fp, "周期パターン :");
for(i = 0 ; i < Number ;i++)
{
fprintf(fp, " %d. ", i + 1);
for(j = Cycle_L[i] - 1 ; j >= 0 ; j--)
fputc(Cycle[i][j], fp);
}
fprintf(fp, "\n 1. 2. 3.\n");
for(i = 0 ; i < Test ; i++)
fprintf(fp, " %c %f %f %f\n",
TEST[i], Out[i][0], Out[i][1], Out[i][2]);
}