-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinitpara.c
159 lines (135 loc) · 4.25 KB
/
initpara.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
//#include "ancfis.h"
#define RANDOM_INIT 1 //random parameter initialization = 1, see initpara.c
/*
This file deals with the initial generation of parameter of layer 1 and 4. --- should be layer 5
These parameters MUST be generated before starting the forward pass.
In layer 1, we need dsin(at+b)+c -> {a,b,c,d}
In layer 4, we need w(p|x|+r) -> {p,r}
There are two possible modes of parameter generation based on the flag
'RANDOM_INIT' in ancfis.h. If it is set to '0', then there will be a
deterministic generation of parameters {a,b,c,d}. If it is set to '1', {a,b,c,d}
will be randomly generated between [0,1] with constraints on d and c.
There are 2 functions:
1. initpara
2. rdm
*/
/***********************************************
Description:
Returns a random number between 0 and 1.
Inputs:
*ran_point - pointer to a double, not an array
Outputs:
a random value
Called from:
initpara in initpara.c
************************************************/
rnd()
{
int stime;
long ltime;
/*get the current calendar time*/
ltime=time(NULL);
stime=(unsigned) ltime/2;
srand(stime);
}
/***********************************************
Description:
Generates initial parameters for a,b,c,d based off the trn data set.
These values are written to a file named "para.ini". This file is read
by get_parameter("para.ini") in input.c.
Note that the training data should be NORMALIZED before being input to here.
Layer 1 parameters are generated by:
a = pow(scale_factor, -1*floor(mf_n/2.0))
eg for 3 membership functions and scale_factor 10, a = 0.1, 1, 10
b = 0
c = (max-min)/2
d = 1-c;
Layer 4 parameters are all initialized to 0.
There is also an optional random parameter generation if
the value RANDOM_INIT is set to 1 in 'ancfis.h'
Inputs:
*data_file - name of the traning data file
data_n - number of rows in the data file
input_n - number of inputs (not input vector length)
col_n - number of columns in the data file (includes the output column(s))
mf_n - number of membership functions
Outputs:
A file called para.ini that contains the initial parameter values.
Called from:
mexFunction in ancfismex.c
************************************************/
void initpara(char *data_file, int data_n, int input_n, int col_n, int mf_n) {
int i, j;
FILE *fp, *para_file;
double **min_max; /* 1st col = min, 2nd col = max */
double rule_n;
double a, b, c, d;
//double max_d;
double tmp, scale_factor,rr=0.0;
scale_factor = 1.5;
rnd();
rule_n = pow((double)mf_n, (double)input_n);
min_max = (double **)create_matrix(data_n, 2, sizeof(double));
for (i = 0 ; i < data_n ; i++) {
min_max[i][0] = DBL_MAX;
min_max[i][1] = DBL_MIN;
}
fp = fopen(data_file, "r");
//find max and min of each row
for(i = 0 ; i < data_n ; i++) {
for(j = 0 ; j < col_n ; j++) {
fscanf(fp, "%lf", &tmp);
if(tmp < min_max[i][0])
min_max[i][0] = tmp;
if(tmp > min_max[i][1])
min_max[i][1] = tmp;
}
}
//mexPrintf("min and max of each column:\n\n");
//print_matrix(min_max, data_n, 2);
//mexPrintf("\n");
fclose(fp);
if((para_file = fopen("para.ini", "w")) == NULL){
printf("Cannot write 'config'.\n");
}
/*
mexPrintf("Premise parameters:\n\n");
*/
//d*sin(a*x+b)+c
if(RANDOM_INIT==0 ) { //set this in ancfis.h
for(i = 0; i < input_n ; i++) {
b = 0;
c = (min_max[i][1] - min_max[i][0])/2;
d = 1 - c;
a = pow(scale_factor, -1*floor(mf_n/2.0)); //eg for mf_n = 3, a = 0.1, 1, 10
for(j = 0; j < mf_n; j++) {
fprintf(para_file, "%4.10lf %4.10lf %4.10lf %4.10lf\n", a, b, c, d);
//mexPrintf("%4.10lf %4.10lf %4.10lf %4.10lf\n", a, b, c, d);
a *= scale_factor;
}
}
} else {
c = 0.0;
d = 0.0;
for(i = 0; i < input_n ; i++)
{ //for now only 1 input
for(j = 0; j < mf_n; j++)
{
do
{
c = (rand()/(float)RAND_MAX)*(rand()/(float)RAND_MAX); //between 0 and 1
d = (rand()/(float)RAND_MAX)*(rand()/(float)RAND_MAX);
}while( d+c> 1.0||d>c);
a = rand()/(float)RAND_MAX;
b = rand()/(float)RAND_MAX;
fprintf(para_file, "%4.10lf %4.10lf %4.10lf %4.10lf\n", a, b, c, d);
}
}
}
for(i = 0; i < (rule_n*input_n); i++) {
for(j = 0; j < (In_vect_n*input_n+1); j++) //px + r (r is the +1)
fprintf(para_file, "%d ", 0);
fprintf(para_file, "\n");
}
fclose(para_file);
}