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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
| #include<stdio.h>
#include<math.h>
FILE *stream;
FILE *gmdh;
FILE *observed;
FILE *estimate;
double x[1000][30]; // array of independent variables
double y[1000];// array of dependent variables
double data[1000];
double ev[1000][30];
double ysave[1000];
double zz[60];
unsigned short int itree[100][100];
double tree[100][100][6];
unsigned short int itr[436];
unsigned short int iter;
unsigned short int m; // no of independent variables
unsigned short int n; // no of data points
unsigned short int nt; // no of data points in training set
unsigned short int niter; // no of levels GMDH performs before stopping if = 0 decidesitself
double fi; // fractional increase in the number of variables at each iteration [0,1]
double dmin = 100.0;
double xtx[7][7]; //function alg, sys, inter, sort
double xty[6];
unsigned short int index[436];
double xwork[10000];
double ywork[1000];
double zzz[6];
unsigned short int l, nc, mm;
double d[435];
unsigned short int cit, ci, cj, cl, nn, nz, nzz, n1, jj, jj1, jj2, jj3;
double wk, cy, work[750];
unsigned short int coeff();
void gmdh1();
double stat();
void sort();
void conv();
void comp6();
void comp();
unsigned short int round(double);
double exp(double);
double pow(double, double);
void swap(double*, double*);
void swapint(unsigned short int*, unsigned short int*);
void main(void)
{
unsigned short int i,j,k, mal, nnt;
double yy;
double er;
double perer;
double qqq;
yy = er = perer = 0.0;
for (i = 0; i <= 435; i++)
{
d[i] = 0.0;
index[i] = 0;
}
//obtain values for the variables m, n, niter and fi : e.g., 3 27 0 0.5
printf("number of independent variables:");
scanf("%d", &m);
printf("number of data points: ");
scanf("%d", &n);
printf("number of levels GMDH performs before stopping (if =0 GMDH decides itself):");
scanf("%d", &niter);
printf("fractional increase in the number of variables at each iteration [0,1]: ");
scanf("%lf", &fi);
qqq = 0.75 * double(n);
nt = round(qqq);
printf("no of data points training set: %d", nt);
//read data values from file nlor.dat and store to x[m][n] and y[n]
stream = fopen("data.txt", "r");
for (i = 1; i < 20000; i++)
fscanf(stream, "%lf\n", &data[i]);
fclose(stream);
for (i = 1; i < (n+1); i++)
{
for (j = 1; j < (m+1); j++)
x[i][j] = data[(i-1)*(m+1)+j];
y[i] = data[i*(m+1)];
}
for (i = 1; i < (n+1); i++)
ysave[i] = y[i];
//write data values x[n][m], y[n] to file gmdh.txt
gmdh = fopen("gmdh.txt", "w");
for (j = 1; j < (m+1); j++)
fprintf(gmdh,"X%d ", j);
fprintf(gmdh,"Y\n\n");
for (i = 1; i < (n+1); i++)
{
for (j = 1; j < (m+1); j++)
fprintf(gmdh,"%lf ", x[i][j]);
fprintf(gmdh,"%lf\n", y[i]);
}
//make a copy of array x[n][m] to ev[n][m]
for (i = 1; i < n+1; i++)
for (j = 1; j < (m+1); j++)
ev[i][j] = x[i][j];
gmdh1();
fprintf(gmdh, "\ncase no. observed value estimate error percent error\n");
observed = fopen("bserved.txt", "w");
estimate = fopen("estimate.txt", "w");
for (i = 1; i < (n+1); i++)
{
for (j = 1; j < (m+1); j++)
zz[j] = ev[i][j];
// call subroutine to evaluate the Ivakhnenko polynomial
comp();
{
er = fabs(ysave[i] - cy);
if (ysave[i] != 0)
perer = 100.0*er / ysave[i];
fprintf(gmdh, "\n%d %lf %lf %lf %lf", i, ysave[i], cy, er, perer);
fprintf(observed, "%lf\n", ysave[i]);
fprintf(estimate, "%lf\n", cy);
}
//the Ivakhnenko polynomial is printed only if it is a simple quadratic
if (iter > 1)
return;
fprintf(gmdh, "\n Ivakhnenko polynomial\n");
fprintf(gmdh, "\n y = a + b*u + c*v + d*u*u + e*v*v + f*u*v\n");
fprintf(gmdh, "a = %f, b = %f, c = %f, d = %f, e = %f, f = %f", tree[1][1][1], tree
[1][1][2], tree[1][1][3], tree[1][1][4], tree[1][1][5], tree[1][1][6]);
fprintf(gmdh, "\n u = x(%d), v = x(%d)", itr[2], itr[3]);
}
void gmdh1() ;
{
double poly[6][100];
double work[1000][100];
unsigned short int ind[435];
unsigned short int ma[20];
double rms;
double ww, st, sum, sum1, sum2, test ;
unsigned short int iflag, q, ntp1, mm1, ip1, j, i, h, z, k ;
ntp1 = nt + 1;
nc = n - nt;
mm = m;
iter = 1;
while (q == 0)
{
l = 1;
mm1 = m - 1;
// Stage: # 1: 1st & 2ndvariables of [m*(m-1)/2] pairs for training to define xty[] andxtx[][] for regression
for (z = 1; z < (mm1+1); z++) //1st index of two variables to be used
{
ip1 = z + 1;
for (h = ip1; h < (m+1); h++) //2nd index of two variables to be used
{
for (i = 1; i < 7; i++)
{
xty[i] = 0.0; //initialize vector Y
for (j = 1; j < 7; j++)
xtx[i][j] = 0.0; //initialize array X
}
xtx[1][1] = double(nt);
for (k = 1; k <(nt+1); k++) //only training data points areused
{
xtx[1][2] = xtx[1][2] + x[k][z];
xtx[1][3] = xtx[1][3] + x[k][h];
xtx[1][4] = xtx[1][4] + pow(x[k][z],2.0);
xtx[1][5] = xtx[1][5] + pow(x[k][h], 2.0);
xtx[1][6] = xtx[1][6] + (x[k][z]*x[k][h]);
xtx[2][1] = xtx[2][1] + x[k][z];
xtx[2][2] = xtx[2][2] + pow(x[k][z], 2.0);
xtx[2][3] = xtx[2][3] + (x[k][z]*x[k][h]);
xtx[2][4] = xtx[2][4] + pow(x[k][z], 3.0);
xtx[2][5] = xtx[2][5] + (x[k][z]*pow(x[k][h], 2.0));
xtx[2][6] = xtx[2][6] + (pow(x[k][z], 2.0)*x[k][h]);
xtx[3][1] = xtx[3][1] + x[k][h];
xtx[3][2] = xtx[3][2] + (x[k][z]*x[k][h]);
xtx[3][3] = xtx[3][3] + pow(x[k][h], 2.0);
xtx[3][4] = xtx[3][4] + (pow(x[k][z], 2.0)*x[k][h]);
xtx[3][5] = xtx[3][5] + pow(x[k][h], 3.0);
xtx[3][6] = xtx[3][6] + (x[k][z]*pow(x[k][h], 2.0));
xtx[4][1] = xtx[4][1] + pow(x[k][z], 2.0);
xtx[4][2] = xtx[4][2] + pow(x[k][z], 3.0);
xtx[4][3] = xtx[4][3] + (pow(x[k][z], 2.0)*x[k][h]);
xtx[4][4] = xtx[4][4] + pow(x[k][z], 4.0);
xtx[4][5] = xtx[4][5] + pow((x[k][z]*x[k][h]), 2.0);
xtx[4][6] = xtx[4][6] + (pow(x[k][z], 3.0)*x[k][h]);
xtx[5][1] = xtx[5][1] + pow(x[k][h], 2.0);
xtx[5][2] = xtx[5][2] + (x[k][z]*pow(x[k][h], 2.0));
xtx[5][3] = xtx[5][3] + pow(x[k][h], 3.0);
xtx[5][4] = xtx[5][4] + pow((x[k][z]*x[k][h]), 2.0);
xtx[5][5] = xtx[5][5] + pow(x[k][h], 4.0);
xtx[5][6] = xtx[5][6] + (x[k][z]*pow(x[k][h], 3.0));
xtx[6][1] = xtx[6][1] + (x[k][z]*x[k][h]);
xtx[6][2] = xtx[6][2] + (pow(x[k][z], 2.0)*x[k][h]);
xtx[6][3] = xtx[6][3] + (x[k][z]*pow(x[k][h], 2.0));
xtx[6][4] = xtx[6][4] + (pow(x[k][z], 3.0)*x[k][h]);
xtx[6][5] = xtx[6][5] + (x[k][z]*pow(x[k][h], 3.0));
xtx[6][6] = xtx[6][6] + pow((x[k][z]*x[k][h]), 2.0);
}
for (k = 1; k < (nt+1); k++)
{
xty[1] = xty[1] + y[k];
xty[2] = xty[2] + (x[k][z]*y[k]);
xty[3] = xty[3] + (x[k][h]*y[k]);
xty[4] = xty[4] + ((pow(x[k][z],2.0))*y[k]);
xty[5] = xty[5] + ((pow(x[k][h], 2.0))*y[k]);
xty[6] = xty[6] + (x[k][z]*x[k][h]*y[k]);
}
// Stage: # 2: Compute the coefficients xyx[] via regression analysis using function coeff()
iflag = coeff(); // compute the coefficients xyx[] via regressionanalysis
if (iflag == 0)
{
for (i = 1; i < 7; i++)
{
poly[i][l] = xty[i]; // the coefficients in poly[][]
//fprintf(gmdh, ''\npoly[%d][%d] = %g'', i,l,xty[i]);
}
// Stage: # 3: Construct new variables z1,z2,. . . ,zm(m?1)/2
for (k = 1; k < (n+1); k++)
{
ww = poly[1][l] + poly[2][l]*x[k][z] + poly[3][l]*x[k][h];
ww = ww + poly[4][l]*pow(x[k][z], 2.0)+ poly[5][l]*pow(x[k][h], 2.0);
ww = ww + poly[6][l]*x[k][z]*x[k][h];
work[k][l] = ww;
}
ind[l] = 100*(z+10) + (h+10); //key for tree generation
if (l == nt)
goto end;
l = l + 1; // increment counter for # of polynomials,zi
}
}
}
//completed construction of m*(m-1) / 2 new variables
l = l - 1;
end: ;
// Stage: # 4: Use checking data set to compute the goodness of fit statistics
for (i = 1; i < (nc+1); i++) //only checking data considered
ywork[i] = y[nt+i]; //y
for (j = 1; j < (l+1); j++) // for each new variable, zi
{
for (i = 1; i < (nc+1); i++)
xwork[i] = work[nt+i][j]; //x
//compute the goodness of fit statistics
st = stat(); //external criterion
//fprintf(gmdh, ''\nd[%d] = st = %g\n'',j, st);
d[j] = st; //save external criterion values (EC) for each newvariable, zi
index[j] = j; //generate index
}
// Stage: # 5: Sort values of the statistics from low to high
if (l > 0)
{
sort(); //sort index according to best EC sorting
//fprintf(gmdh, ''\nSORT\n'');
for (j = 1; j < = l; j++)
fprintf(gmdh, "\nd[%d] = %lf", index[j], d[index[j]]); //savebest index and EC
}
rms = fi * double(m);
m = m + round(rms);
if (m > l)
m = l; //kluge lower bound
//the largest number of var is set to 75
if (m > 75)
m = 75; //kluge upper bound
if (m < mm)
m = mm; //kluge with defined bound
// Stage: # 6: Grow tree from
for (j = 1; j < (m+1); j++)
{
itree[iter][j] = ind[index[j]]; //define tree using keys for best
index
fprintf(gmdh, "nitree[%d][%d] = %d", iter, j, ind[index[j]]);
for (k = 1; k < 7; k++)
{
tree[iter][j][k] = poly[k][index[j]];
fprintf(gmdh, "\ntree[%d][%d][%d] = %g", iter, j,
k, tree [iter][j][k]);
}
}
//test for convergence of gmdh algorithm
if (niter = 0)
{
test = d[index[1]] - dmin + 0.0000005;
//fprintf(gmdh, "\ntest = %lf > %lf", d[index[1]], dmin);
if (test > 0.0)
{
conv(); //convergence test
return;
}
}
else
{
if (iter = niter)
{
conv();//convergence test
return;
}
}
// Stage: # 7: Determine minimum external criterion checking error and coefficient of
correlation
dmin = d[index[1]]; //minimum external criterion (EC) value
fprintf(gmdh, "\nLevel number = %d", iter);
fprintf(gmdh, "\nNo. variables saved = %d\nrmin value(summed over checking set) = %f\n", m, dmin);
ma[iter] = m;
iter = iter + 1;
for (i = 1; i < (n+1); i++)
for (j = 1; j < (m+1); j++)
x[i][j] = work[i][index[j]];
sum = 0.0;
for (i = 1; i < (nt+1); i++)
sum = sum + y[i];
sum = sum / nt;
sum1 = 0.0;
sum2 = 0.0;
for (i = 1; i < (nt+1); i++)
{
sum1 = sum1 + pow((sum - x[i][1]), 2.0);
sum2 = sum2 + pow((y[i] - sum), 2.0);
}
sum = sum1 / sum2;
fprintf(gmdh, "\nsum = %f\n", sum);
}
}
void comp()
{
unsigned short int n11, q;
unsigned short int j;
cit = iter;
itr[1] = 1;
ci = 1;
// Step 1: Generate vector 'itr' from 'itree'
comp6();
// Step 2: Extract coefficients in 'itree' using information in 'itr'
iter = cit;
nz = (unsigned short int)pow(2.0, double(iter -1));
nzz = nz;
n1 = (unsigned short int)pow(2.0, double(iter));
for(j = 1; j < (nzz + 1); j++)
{
jj1 = itr[nz];
jj2 = itr[n1];
jj3 = itr[n1 + 1];
wk = (tree[1][jj1][1] + (tree[1][jj1][2] * zz[jj2]) + (tree[1][jj1][3] * zz
[jj3]));
wk = wk + tree[1][jj1][4] * pow(zz[jj2], 2.0) + tree[1][jj1][5]*pow(zz[jj3],
2.0);
wk = wk + tree[1][jj1][6]*zz[jj2]*zz[jj3];
work[j] = wk;
nz = nz + 1;
n1 = n1 + 2;
}
iter = iter - 1;
if (iter == 0)
{
cy = work[1];
iter = cit;
return;
}
ci = 2;
q = 0;
while (q == 0)
{
nz = (unsigned short int)pow(2.0, double(iter-1));
n1 = (unsigned short int)pow(2.0, double(iter));
nzz = nz;
n11 = n1;
for (j = 1; j < (nzz+1); j++)
{
jj = 2*j - 1;
jj1 = itr[nz];
jj2 = itr[n1];
jj3 = itr[n1 + 1];
wk = tree[1][jj1][1] + tree[1][jj1][2]*work[jj] + tree[1][jj1][3]*
work[jj+1];
wk = wk + tree[1][jj1][4]*pow(work[jj], 2.0) + tree[1][jj1][5]*
pow(work[jj+1], 2.0);
wk = wk + tree[1][jj1][6]*work[jj]*work[jj+1];
work[n11+j] = wk;
nz = nz + 1;
n1 = n1 + 2;
}
iter = iter - 1;
if (iter == 0)
{
cy = work[3];
iter = cit;
return;
}
for (j = 1; j < (nzz+1); j++)
work[j] = work[n11+j];
ci = ci + 1;
}
}
void comp6() // Step 1: Generate vector 'itr' from 'itree'
{
unsigned short int xx, iz;
unsigned short int q, r;
q = 0; r = 0;
while (r == 0)
{
cl = 0;
nn = (unsigned short int)pow(2.0, double(ci-1));
n1 = (unsigned short int)pow(2.0, double(ci));
nz = (unsigned short int)pow(2.0, double(ci+1)) - 1;
cj = n1;
while (q == 0)
{
jj = itr[nn + cl];
xx = itree[iter][jj];
itr[cj] = itree[iter][jj] / 100 - 10;
iz = itree[iter][jj] / 100;
itr[cj+1] = xx - 100*iz - 10;
cj = cj + 2;
if (cj > nz)
break;
else
cl = cl + 1;
}
if (iter == 1)
return;
iter = iter - 1;
ci = ci + 1;
}
}
void coeff()
{
unsigned short int xx, iz;
unsigned short int q, r;
q = 0; r = 0;
while (r == 0)
{
cl = 0;
nn = (unsigned short int)pow(2.0, double(ci-1));
n1 = (unsigned short int)pow(2.0, double(ci));
nz = (unsigned short int)pow(2.0, double(ci+1)) - 1;
cj = n1;
////////////////////////
while (q == 0)
{
jj = itr[nn + cl];
xx = itree[iter][jj];
itr[cj] = itree[iter][jj] / 100 - 10;
iz = itree[iter][jj] / 100;
itr[cj+1] = xx - 100*iz - 10;
cj = cj + 2;
if (cj > nz)
break;
else
cl = cl + 1;
}
if (iter == 1)
return;
iter = iter - 1;
ci = ci + 1;
}
} |