📄 datstruc.c
字号:
for (i = 0; i < fis->in_n; i++)
for (j = 0; j < fis->input[i]->mf_n; j++) {
mf_type = fis->input[i]->mf[j]->type;
mf_para = fis->input[i]->mf[j]->para;
for (k = 0; k < fisGetMfParaN(mf_type); k++)
fis->para[tmp++] = mf_para[k];
}
/* copy output parameters from FIS data structure */
for (i = 0; i < fis->out_n; i++) {
for (j = 0; j < fis->output[i]->mf_n; j++) {
mf_para = fis->output[i]->mf[j]->sugeno_coef;
/* ========== */
if (fis->order==1)
for (k = 0; k < fis->in_n+1; k++)
fis->para[tmp++] = mf_para[k];
else
for (k = 0; k < 1; k++)
fis->para[tmp++] = mf_para[k];
}
}
}
static void
#ifdef __STDC__
anfisAssignForwardFunction(FIS *fis)
#else
anfisAssignForwardFunction(fis)
FIS *fis;
#endif
{
int i, j, start, stop;
double (*nodeFcn)();
for (i = 0; i < 7; i++) {
switch(i) {
case 0:
nodeFcn = anfisInputNode;
break;
case 1:
nodeFcn = anfisMfNode;
break;
case 2:
nodeFcn = anfisInvNode;
break;
case 3:
nodeFcn = anfisAndOrNode;
break;
case 4:
nodeFcn = anfisRuleOutputNode;
break;
case 5:
nodeFcn = anfisSummationNode;
break;
case 6:
nodeFcn = anfisDivisionNode;
break;
default:
fisError("Unknown layer!");
}
start = fis->layer[i]->index;
stop = start + fis->layer_size[i] - 1;
for (j = start; j <= stop; j++)
fis->node[j]->nodeFcn = nodeFcn;
}
}
static void
#ifdef __STDC__
anfisFreeAnNode(NODE *node)
#else
anfisFreeAnNode(node)
NODE *node;
#endif
{
int i;
FAN *now, *next;
FREEARRAY(node->input);
/* free fanin list */
now = node->fanin;
for (i = 0; i < node->fanin_n; i++) {
next = now->next;
FREEARRAY(now);
now = next;
}
/* free fanout list */
now = node->fanout;
for (i = 0; i < node->fanout_n; i++) {
next = now->next;
FREEARRAY(now);
now = next;
}
/*
FREEARRAY(node);
*/
/* node->para, node->de_dp, and node->do_dp
are freed in anfisFreeAnfis() */
}
static void
#ifdef __STDC__
anfisFreeAnfis(FIS *fis)
#else
anfisFreeAnfis(fis)
FIS *fis;
#endif
{
int i;
FREEARRAY(fis->in_mf_n);
FREEARRAY(fis->out_mf_n);
FREEARRAY(fis->para);
FREEARRAY(fis->trn_best_para);
FREEARRAY(fis->chk_best_para);
FREEARRAY(fis->de_dp);
FREEARRAY(fis->do_dp);
for (i = 0; i < fis->node_n; i++)
anfisFreeAnNode(fis->node[i]);
FREEARRAY(fis->node[0]);
FREEARRAY(fis->node);
FREEMAT((void **)fis->trn_data, fis->trn_data_n);
FREEMAT((void **)fis->chk_data, fis->chk_data_n);
FREEARRAY(fis->ss_array);
FREEARRAY(fis->trn_error);
FREEARRAY(fis->chk_error);
FREEARRAY(fis->kalman_io_pair);
FREEMAT((void **)fis->tmp_node_output, fis->trn_data_n);
/* the following is for kalman matrices */
{
int in_n, out_n;
if (fis->method==1 || fis->method==0)
in_n = ((fis->order)*(fis->in_n)+1)*fis->rule_n;
else{
if (fis->method==2)
in_n = fis->para_n;
}
out_n = fis->out_n;
FREEMAT((void **)fis->kalman_para, in_n);
FREEMAT((void **)fis->S, in_n);
FREEMAT((void **)fis->P, in_n);
FREEMAT((void **)fis->a, in_n);
FREEMAT((void **)fis->b, out_n);
FREEMAT((void **)fis->a_t, 1);
FREEMAT((void **)fis->b_t, 1);
FREEMAT((void **)fis->tmp1, in_n);
FREEMAT((void **)fis->tmp2, 1);
FREEMAT((void **)fis->tmp3, 1);
FREEMAT((void **)fis->tmp4, in_n);
FREEMAT((void **)fis->tmp5, 1);
FREEMAT((void **)fis->tmp6, in_n);
FREEMAT((void **)fis->tmp7, in_n);
}
}
/* Initial variables, for off-line learning only */
static void
#ifdef __STDC__
anfisSetVariable(FIS *fis)
#else
anfisSetVariable(fis)
FIS *fis;
#endif
{
int i;
int in_n, out_n;
fis->ss_array = (double *)calloc(fis->epoch_n, sizeof(double));
for (i = 0; i < fis->epoch_n; i++)
fis->ss_array[i] = -1;
fis->trn_error = (double *)calloc(fis->epoch_n, sizeof(double));
for (i = 0; i < fis->epoch_n; i++)
fis->trn_error[i] = -1;
if (fis->chk_data_n != 0) {
fis->chk_error = (double *)calloc(fis->epoch_n,sizeof(double));
for (i = 0; i < fis->epoch_n; i++)
fis->chk_error[i] = -1;
}
if (fis->method==1 || fis->method==0)
in_n = ((fis->order)*(fis->in_n)+1)*fis->rule_n;
else if (fis->method==2)
in_n = fis->para_n;
out_n = fis->out_n;
fis->kalman_io_pair = (double *)calloc
(in_n+fis->out_n, sizeof(double));
fis->kalman_para = (double **)fisCreateMatrix
(in_n, 1, sizeof(double));
fis->tmp_node_output = (double **)fisCreateMatrix
(fis->trn_data_n, fis->in_n+2*fis->total_in_mf_n+fis->rule_n,
sizeof(double));
fis->lambda = 1;
fis->min_trn_error = pow(2.0, 31.0)-1;
fis->min_chk_error = pow(2.0, 31.0)-1;
fis->last_dec_ss = 0;
fis->last_inc_ss = 0;
/* allocate static memory for Kalman filter algorithm */
{
/* ========== */
fis->S = (double **)fisCreateMatrix(in_n, in_n, sizeof(double));
fis->P = (double **)fisCreateMatrix(in_n, out_n, sizeof(double));
fis->a = (double **)fisCreateMatrix(in_n, 1, sizeof(double));
fis->b = (double **)fisCreateMatrix(out_n, 1, sizeof(double));
fis->a_t = (double **)fisCreateMatrix(1, in_n, sizeof(double));
fis->b_t = (double **)fisCreateMatrix(1, out_n, sizeof(double));
fis->tmp1 = (double **)fisCreateMatrix(in_n, 1, sizeof(double));
fis->tmp2 = (double **)fisCreateMatrix(1, 1, sizeof(double));
fis->tmp3 = (double **)fisCreateMatrix(1, in_n, sizeof(double));
fis->tmp4 = (double **)fisCreateMatrix(in_n, in_n, sizeof(double));
fis->tmp5 = (double **)fisCreateMatrix(1, out_n, sizeof(double));
fis->tmp6 = (double **)fisCreateMatrix(in_n, out_n, sizeof(double));
fis->tmp7 = (double **)fisCreateMatrix(in_n, out_n, sizeof(double));
}
}
/* Initial variables, for on-line learning only */
static void
#ifdef __STDC__
anfisSetVariable1(FIS *fis)
#else
anfisSetVariable1(fis)
FIS *fis;
#endif
{
fis->kalman_io_pair = (double *)calloc
((fis->in_n+1)*fis->rule_n+fis->out_n, sizeof(double));
fis->kalman_para = (double **)fisCreateMatrix
((fis->in_n+1)*fis->rule_n, 1, sizeof(double));
fis->display_anfis_info = 1; /* always display anfis info */
/* allocate static memory for Kalman filter algorithm */
{
int in_n = (fis->in_n+1)*fis->rule_n;
int out_n = fis->out_n;
fis->S = (double **)fisCreateMatrix(in_n, in_n, sizeof(double));
fis->P = (double **)fisCreateMatrix(in_n, out_n, sizeof(double));
fis->a = (double **)fisCreateMatrix(in_n, 1, sizeof(double));
fis->b = (double **)fisCreateMatrix(out_n, 1, sizeof(double));
fis->a_t = (double **)fisCreateMatrix(1, in_n, sizeof(double));
fis->b_t = (double **)fisCreateMatrix(1, out_n, sizeof(double));
fis->tmp1 = (double **)fisCreateMatrix(in_n, 1, sizeof(double));
fis->tmp2 = (double **)fisCreateMatrix(1, 1, sizeof(double));
fis->tmp3 = (double **)fisCreateMatrix(1, in_n, sizeof(double));
fis->tmp4 = (double **)fisCreateMatrix(in_n, in_n, sizeof(double));
fis->tmp5 = (double **)fisCreateMatrix(1, out_n, sizeof(double));
fis->tmp6 = (double **)fisCreateMatrix(in_n, out_n, sizeof(double));
fis->tmp7 = (double **)fisCreateMatrix(in_n, out_n, sizeof(double));
}
}
/* check the validity of the FIS structure for learning */
static void
#ifdef __STDC__
anfisCheckFisForLearning(FIS *fis)
#else
anfisCheckFisForLearning(fis)
FIS *fis;
#endif
{
int i, j;
/* user-defined operators are not allow */
if (fis->userDefinedAnd || fis->userDefinedOr ||
fis->userDefinedImp || fis->userDefinedAgg ||
fis->userDefinedDefuzz) {
fisFreeFisNode(fis);
fisError("User-defined operators are not allowed!");
}
/* user-defined MF are not allow */
for (i = 0; i < fis->in_n; i++)
for (j = 0; j < fis->input[i]->mf_n; j++)
if (fis->input[i]->mf[j]->userDefined) {
fisFreeFisNode(fis);
fisError("User-defined MF's are not allowed!");
}
for (i = 0; i < fis->out_n; i++)
for (j = 0; j < fis->output[i]->mf_n; j++)
if (fis->output[i]->mf[j]->userDefined) {
fisFreeFisNode(fis);
fisError("User-defined MF's are not allowed!");
}
/* must be sugeno type */
if (strcmp(fis->type, "sugeno")) {
fisFreeFisNode(fis);
fisError("Given FIS matrix is not of Sugeno type!");
}
/* must have only one output */
if (fis->out_n != 1) {
fisFreeFisNode(fis);
fisError("Given FIS has more than one output!");
}
/* must use weighted average for deriving final output */
if (strcmp(fis->defuzzMethod, "wtaver")) {
fisFreeFisNode(fis);
fisError("ANFIS only supports weighted average (wtaver).");
}
/* must have more than one rule */
if (fis->rule_n <= 1) {
fisFreeFisNode(fis);
fisError("Need at least two rules for ANFIS learning!");
}
/* output MF no. must be the same as rule no. */
if (fis->output[0]->mf_n != fis->rule_n) {
fisFreeFisNode(fis);
printf("Number of output MF's is not equal to number of rules -->\n");
fisError("Parameter sharing in FIS is not allowed!");
}
/* ========== */
/* output MF must be of linear */
/*
for (i = 0; i < fis->output[0]->mf_n; i++)
if (strcmp(fis->output[0]->mf[i]->type, "linear")) {
fisFreeFisNode(fis);
fisError("Each rule's output must be of linear type!");
}
*/
/* output MF must be either all linear or all constant */
{
int all_linear = 1;
int all_constant = 1;
for (i = 0; i < fis->output[0]->mf_n; i++)
if (strcmp(fis->output[0]->mf[i]->type, "linear")) {
all_linear = 0;
break;
}
for (i = 0; i < fis->output[0]->mf_n; i++)
if (strcmp(fis->output[0]->mf[i]->type, "constant")) {
all_constant = 0;
break;
}
if (all_linear == 0 && all_constant == 0) {
fisFreeFisNode(fis);
fisError("Rules' outputs must be either of all linear"
" or all constant!");
}
/*
if (all_linear == 1)
printf("Rule outputs are all linear.\n");
if (all_constant == 1)
printf("Rule outputs are all constant.\n");
*/
}
/* rule weight must be one */
for (i = 0; i < fis->rule_n; i++)
if (fis->rule_weight[i] != 1) {
fis->rule_weight[i] = 1;
printf("Warning: weight of rule %d is set to 1"
" for ANFIS learning!\n", i+1);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -