-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBPSim.c
247 lines (217 loc) · 4.23 KB
/
BPSim.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include"BPSim.h"
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
//全局变量定义
double *data=NULL;
int layer_all1 = 0;
int *nodes_all1 = NULL;
int num_all = 0;
struct simnetnode **simnetwork=NULL;
float simxminmax[SIMROW_X][2];
float simyminmax[SIMROW_Y][2];
//神经网络节点建立函数
int simNetSetUp(void)
{
int i = 0,j=0,k=0,n=0;
//神经网络层数
layer_all1 = (int)data[n];
n++;
//神经网络每层节点数
nodes_all1 = (int *)malloc(sizeof(int)*layer_all1);
for (i = 0; i < layer_all1; i++)
{
nodes_all1[i] = data[n];
n++;
}
//申请层数
simnetwork = (struct simnetnode**)malloc(sizeof(struct simnetnode*)*(layer_all1));
//申请神经网络每层节点
for (i = 0; i < layer_all1; i++)
{
simnetwork[i] = (struct simnetnode*)malloc(sizeof(struct simnetnode)*nodes_all1[i]);
}
//映射参数
for (i = 0; i < SIMROW_X; i++)
{
simxminmax[i][0] = data[n];
n++;
simxminmax[i][1] = data[n];
n++;
}
for (i = 0; i < SIMROW_Y; i++)
{
simyminmax[i][0] = data[n];
n++;
simyminmax[i][1] = data[n];
n++;
}
//神经网络节点参数初始化
for (i = 0; i < layer_all1; i++)
{
for (j = 0; j < nodes_all1[i]; j++)
{
if (i != layer_all1 - 1)
{
simnetwork[i][j].w = (double*)malloc(sizeof(double)*nodes_all1[i + 1]);
for (k = 0; k < nodes_all1[i + 1]; k++)
{
simnetwork[i][j].w[k] = data[n];
n++;
}
simnetwork[i][j].b = data[n];
n++;
}
}
}
return 0;
}
int sim(float x[SIMROW_X], float y[SIMROW_Y])
{
if ((SIMROW_X != nodes_all1[0]) && (SIMROW_Y != nodes_all1[layer_all1 - 1]))
{
printf("输入或输出的数量不正确\n");
return 1;
}
int i = 0, j = 0, k = 0;
for (i = 0; i < layer_all1; i++)
{
//如果是输入层
if (i == 0)
{
for (j = 0; j < nodes_all1[i]; j++)
{
simnetwork[i][j].input = simDataChange(x[j],j,1);
simnetwork[i][j].output = simInspirit(simnetwork[i][j].input, simnetwork[i][j].b, 1);
}
}
else
{
float s = 0;
for (j = 0; j < nodes_all1[i]; j++)
{
//输入
s = 0;
for (k = 0; k < nodes_all1[i - 1]; k++)
{
s += (simnetwork[i-1][k].output*simnetwork[i-1][k].w[j]);
}
simnetwork[i][j].input = s;
if (i<(layer_all1-1))
simnetwork[i][j].output = simInspirit(simnetwork[i][j].input, simnetwork[i][j].b, 1);
else
{
simnetwork[i][j].output = simnetwork[i][j].input;
y[j] = simDataRecover(simnetwork[i][j].output,j,1);
}
}
}
}
}
//mode1==0,激励函数(sig)
//y=1/(1+exp(x))
//mode1==1,激励函数(正切tansig)
//y=2/(1+e(-2x))-1
double simInspirit(double x, double b, int mode1)
{
double y = 0;
x -= b;
if (mode1 == 0)
{
y = 1 + exp(-x);
y = 1 / y;
}
else if (mode1 == 1)
{
y = 1 + exp(-2 * x);
y = 2 / y - 1;
}
return y;
}
int readData(void)
{
FILE *fpread = fopen("data.txt", "r");
if (fpread == NULL)
{
return 1;
}
double k = 0;
num_all = 0;
while (fscanf(fpread, "%lf", &k) != EOF)
num_all++;
fclose(fpread);
int i = 0;
data = (double*)malloc(num_all*sizeof(double));
fpread = fopen("data.txt", "r");
while (fscanf(fpread, "%lf", &data[i]) != EOF)
{
i++;
}
return 0;
}
int simInit()
{
if (readData())
{
printf("读取错误\n");
return 1;
}
if (simNetSetUp())
{
printf("神经网络建立错误\n");
return 2;
}
return 0;
}
//数据映射
double simDataChange(double x,int j,int mode1)
{
float dis = simxminmax[j][1] - simxminmax[j][0];
x -= simxminmax[j][0];
x /= dis;
if (mode1 == 1)
{
x *= 2;
x -= 1;
}
return x;
}
//数据恢复
double simDataRecover(double y, int j,int mode1)
{
float dis = simyminmax[j][1] - simyminmax[j][0];
if (mode1 == 1)
//压缩到0-1区间
{
y += 1;
y /= 2;
}
y *= dis;
y += simyminmax[j][0];
return y;
}
int simFree(void)
{
int i = 0, j = 0, k = 0;
//释放data
free(data);
data = NULL;
for (i = 0; i < layer_all1; i++)
{
for (j = 0; j < nodes_all1[i]; j++)
{
if (i != layer_all1 - 1)
{
free(simnetwork[i][j].w);
simnetwork[i][j].w = NULL;
}
}
free(simnetwork[i]);
simnetwork[i] = NULL;
}
free(simnetwork);
simnetwork = NULL;
free(nodes_all1);
nodes_all1 = NULL;
return 0;
}