📄 fis.c
字号:
#endif
fisError("\n");
#endif
}
}
}
/* Assign a other function pointers */
void
#ifdef __STDC__
fisAssignFunctionPointer(FIS *fis)
#else
fisAssignFunctionPointer(fis)
FIS *fis;
#endif
{
/* assign andMethod function pointer */
if (strcmp(fis->andMethod, "prod") == 0)
fis->andFcn = fisProduct;
else if (strcmp(fis->andMethod, "min") == 0)
fis->andFcn = fisMin;
else {
#ifdef MATLAB_MEX_FILE
{
double function_type;
function_type = fisCallMatlabExist(fis->andMethod);
if (function_type == 0) {
printf("AND function '%s' does not exist!\n", fis->andMethod);
fisError("Exiting ...");
}
if (function_type == 1) {
printf("AND function '%s' is a MATLAB variable!\n", fis->andMethod);
fisError("Exiting ...");
}
fis->userDefinedAnd = 1;
}
#else
#ifndef NO_PRINTF
printf("Given andMethod %s is unknown.\n", fis->andMethod);
#endif
fisError("Legal andMethod: min, prod");
#endif
}
/* assign orMethod function pointer */
if (strcmp(fis->orMethod, "probor") == 0)
fis->orFcn = fisProbOr;
else if (strcmp(fis->orMethod, "max") == 0)
fis->orFcn = fisMax;
else {
#ifdef MATLAB_MEX_FILE
{
double function_type;
function_type = fisCallMatlabExist(fis->orMethod);
if (function_type == 0) {
printf("OR function '%s' does not exist!\n", fis->orMethod);
fisError("Exiting ...");
}
if (function_type == 1) {
printf("OR function '%s' is a MATLAB variable!\n", fis->orMethod);
fisError("Exiting ...");
}
fis->userDefinedOr = 1;
}
#else
#ifndef NO_PRINTF
printf("Given orMethod %s is unknown.\n", fis->orMethod);
#endif
fisError("Legal orMethod: max, probor");
#endif
}
/* assign impMethod function pointer */
if (strcmp(fis->impMethod, "prod") == 0)
fis->impFcn = fisProduct;
else if (strcmp(fis->impMethod, "min") == 0)
fis->impFcn = fisMin;
else {
#ifdef MATLAB_MEX_FILE
{
double function_type;
function_type = fisCallMatlabExist(fis->impMethod);
if (function_type == 0) {
printf("IMPLICATION function '%s' does not exist!\n", fis->impMethod);
fisError("Exiting ...");
}
if (function_type == 1) {
printf("IMPLICATION function '%s' is a MATLAB variable!\n", fis->impMethod);
fisError("Exiting ...");
}
fis->userDefinedImp = 1;
}
#else
#ifndef NO_PRINTF
printf("Given impMethod %s is unknown.\n", fis->impMethod);
#endif
fisError("Legal impMethod: min, prod");
#endif
}
/* assign aggMethod function pointer */
if (strcmp(fis->aggMethod, "max") == 0)
fis->aggFcn = fisMax;
else if (strcmp(fis->aggMethod, "probor") == 0)
fis->aggFcn = fisProbOr;
else if (strcmp(fis->aggMethod, "sum") == 0)
fis->aggFcn = fisSum;
else {
#ifdef MATLAB_MEX_FILE
{
double function_type;
function_type = fisCallMatlabExist(fis->aggMethod);
if (function_type == 0) {
printf("AGGREGATE function '%s' does not exist!\n", fis->aggMethod);
fisError("Exiting ...");
}
if (function_type == 1) {
printf("AGGREGATE function '%s' is a MATLAB variable!\n", fis->aggMethod);
fisError("Exiting ...");
}
fis->userDefinedAgg = 1;
}
#else
#ifndef NO_PRINTF
printf("Given aggMethod %s is unknown.\n", fis->aggMethod);
#endif
fisError("Legal aggMethod: max, probor, sum");
#endif
}
/* assign defuzzification function pointer */
if (strcmp(fis->defuzzMethod, "centroid") == 0)
fis->defuzzFcn = defuzzCentroid;
else if (strcmp(fis->defuzzMethod, "bisector") == 0)
fis->defuzzFcn = defuzzBisector;
else if (strcmp(fis->defuzzMethod, "mom") == 0)
fis->defuzzFcn = defuzzMeanOfMax;
else if (strcmp(fis->defuzzMethod, "som") == 0)
fis->defuzzFcn = defuzzSmallestOfMax;
else if (strcmp(fis->defuzzMethod, "lom") == 0)
fis->defuzzFcn = defuzzLargestOfMax;
else if (strcmp(fis->defuzzMethod, "wtaver") == 0)
;
else if (strcmp(fis->defuzzMethod, "wtsum") == 0)
;
else {
#ifdef MATLAB_MEX_FILE
{
double function_type;
function_type = fisCallMatlabExist(fis->defuzzMethod);
if (function_type == 0) {
printf("DEFUZZIFICATION function '%s' does not exist!\n", fis->defuzzMethod);
fisError("Exiting ...");
}
if (function_type == 1) {
printf("DEFUZZIFICATION function '%s' is a MATLAB variable!\n", fis->defuzzMethod);
fisError("Exiting ...");
}
fis->userDefinedDefuzz = 1;
}
#else
#ifndef NO_PRINTF
printf("Given defuzzification method %s is unknown.\n", fis->defuzzMethod);
#endif
fisError("Legal defuzzification methods: centroid, bisector, mom, som, lom, wtaver, wtsum");
#endif
}
}
#ifndef NO_PRINTF
static void
#ifdef __STDC__
fisPrintData(FIS *fis)
#else
fisPrintData(fis)
FIS *fis;
#endif
{
int i, j, k;
if (fis == NULL)
fisError("Given fis pointer is NULL, no data to print!");
printf("fis_name = %s\n", fis->name);
printf("fis_type = %s\n", fis->type);
printf("in_n = %d\n", fis->in_n);
printf("out_n = %d\n", fis->out_n);
printf("in_mf_n: ");
for (i = 0; i < fis->in_n; i++)
printf("%d ", fis->input[i]->mf_n);
printf("\n");
printf("out_mf_n: ");
for (i = 0; i < fis->out_n; i++)
printf("%d ", fis->output[i]->mf_n);
printf("\n");
printf("rule_n = %d\n", fis->rule_n);
printf("andMethod = %s\n", fis->andMethod);
printf("orMethod = %s\n", fis->orMethod);
printf("impMethod = %s\n", fis->impMethod);
printf("aggMethod = %s\n", fis->aggMethod);
printf("defuzzMethod = %s\n", fis->defuzzMethod);
/*
for (i = 0; i < fis->in_n; i++) {
printf("Input variable %d = %s\n", i+1, fis->input[i]->name);
for (j = 0; j < fis->input[i]->mf_n; j++)
printf("\t Label for MF %d = %s\n", j+1, fis->input[i]->mf[j]->label);
}
for (i = 0; i < fis->out_n; i++) {
printf("Output variable %d = %s\n", i+1, fis->output[i]->name);
for (j = 0; j < fis->output[i]->mf_n; j++)
printf("\t Label for MF %d = %s\n", j+1, fis->output[i]->mf[j]->label);
}
*/
for (i = 0; i < fis->in_n; i++)
printf("Bounds for input variable %d: [%6.3f %6.3f]\n", i+1,
fis->input[i]->bound[0], fis->input[i]->bound[1]);
for (i = 0; i < fis->out_n; i++)
printf("Bounds for output variable %d: [%6.3f %6.3f]\n", i+1,
fis->output[i]->bound[0], fis->output[i]->bound[1]);
for (i = 0; i < fis->in_n; i++) {
printf("MF for input variable %d (%s):\n", i+1, fis->input[i]->name);
for (j = 0; j < fis->input[i]->mf_n; j++)
printf("\t Type for MF %d = %s\n", j+1, fis->input[i]->mf[j]->type);
}
for (i = 0; i < fis->out_n; i++) {
printf("MF for output variable %d (%s):\n", i+1, fis->output[i]->name);
for (j = 0; j < fis->output[i]->mf_n; j++)
printf("\t Type for MF %d = %s\n", j+1, fis->output[i]->mf[j]->type);
}
printf("Rule list:\n");
for (i = 0; i < fis->rule_n; i++) {
for (j = 0; j < fis->in_n + fis->out_n; j++)
printf("%d ", fis->rule_list[i][j]);
printf("\n");
}
printf("Rule weights:\n");
for (i = 0; i < fis->rule_n; i++)
printf("%f\n", fis->rule_weight[i]);
printf("AND-OR indicator:\n");
for (i = 0; i < fis->rule_n; i++)
printf("%d\n", fis->and_or[i]);
for (i = 0; i < fis->in_n; i++) {
printf("MF parameters for input variable %d (%s):\n",
i+1, fis->input[i]->name);
for (j = 0; j < fis->input[i]->mf_n; j++) {
printf("\tParameters for MF %d (%s) (%s): ",
j+1, fis->input[i]->mf[j]->label,
fis->input[i]->mf[j]->type);
for (k = 0; k < MF_PARA_N; k++)
printf("%6.3f ", fis->input[i]->mf[j]->para[k]);
printf("\n");
}
}
if (strcmp(fis->type, "mamdani") == 0) {
for (i = 0; i < fis->out_n; i++) {
printf("MF parameters for output variable %d (%s):\n",
i+1, fis->output[i]->name);
for (j = 0; j < fis->output[i]->mf_n; j++) {
printf("\tParameters for MF %d (%s) (%s): ",
j+1, fis->output[i]->mf[j]->label,
fis->output[i]->mf[j]->type);
for (k = 0; k < MF_PARA_N; k++)
printf("%6.3f ", fis->output[i]->mf[j]->para[k]);
printf("\n");
}
}
} else if (strcmp(fis->type, "sugeno") == 0) {
for (i = 0; i < fis->out_n; i++) {
printf("Sugeno model parameters for output variable %d (%s):\n",
i+1, fis->output[i]->name);
for (j = 0; j < fis->output[i]->mf_n; j++) {
printf("\tSugeno model parameters for list %d (%s) (%s): ",
j+1, fis->output[i]->mf[j]->label,
fis->output[i]->mf[j]->type);
for (k = 0; k < fis->in_n + 1; k++)
printf("%6.3f ", fis->output[i]->mf[j]->sugeno_coef[k]);
printf("\n");
}
}
} else {
printf("fis->type = %s\n", fis->type);
fisError("Unknown fis type!");
}
}
#endif
static void
#ifdef __STDC__
fisFreeMfList(MF *mf_list, int n)
#else
fisFreeMfList(mf_list, n)
MF *mf_list;
int n;
#endif
{
int i;
for (i = 0; i < n; i++) {
free(mf_list[i].sugeno_coef);
free(mf_list[i].value_array);
}
free(mf_list);
}
static void
#ifdef __STDC__
fisFreeIoList(IO *io_list, int n)
#else
fisFreeIoList(io_list, n)
IO *io_list;
int n;
#endif
{
int i;
for (i = 0; i < n; i++) {
if (io_list[i].mf_n > 0) /* check if no MF at all */
fisFreeMfList(io_list[i].mf[0], io_list[i].mf_n);
free(io_list[i].mf);
}
free(io_list);
}
void
#ifdef __STDC__
fisFreeFisNode(FIS *fis)
#else
fisFreeFisNode(fis)
FIS *fis;
#endif
{
if (fis == NULL)
return;
fisFreeIoList(fis->input[0], fis->in_n);
free(fis->input);
fisFreeIoList(fis->output[0], fis->out_n);
free(fis->output);
#ifdef FREEMAT
FREEMAT((void **)fis->rule_list, fis->rule_n);
#else
fisFreeMatrix((void **)fis->rule_list, fis->rule_n);
#endif
free(fis->rule_weight);
free(fis->and_or);
free(fis->firing_strength);
free(fis->rule_output);
free(fis->BigOutMfMatrix);
free(fis->BigWeightMatrix);
free(fis->mfs_of_rule);
free(fis);
}
/* Compute arrays of MF values (for Mamdani model only) */
/* This is done whenever new parameters are loaded */
void
#ifdef __STDC__
fisComputeOutputMfValueArray(FIS *fis, int numofpoints)
#else
fisComputeOutputMfValueArray(fis)
FIS *fis;
int numofpoints
#endif
{
int i, j, k;
double x, lx, ux, dx;
MF *mf_node;
for (i = 0; i < fis->out_n; i++) {
lx = fis->output[i]->bound[0];
ux = fis->output[i]->bound[1];
dx = (ux - lx)/(numofpoints - 1);
for (j = 0; j < fis->output[i]->mf_n; j++) {
mf_node = fis->output[i]->mf[j];
if (!mf_node->userDefined)
for (k = 0; k < numofpoints; k++) {
x = lx + k*dx;
mf_node->value_array[k] =
(*mf_node->mfFcn)(x, mf_node->para);
}
else { /* user defined MF */
#ifdef MATLAB_MEX_FILE
/* this is non-vector version */
/*
for (k = 0; k < numofpoints; k++) {
x = lx + k*dx;
mf_node->value_array[k] =
fisCallMatlabMf(x, mf_node->para,
mf_node->type);
}
*/
/* this is vector version */
{
double *X;
X = (double *)calloc(numofpoints, sizeof(double));
/* double X[numofpoints]; */
for (k = 0; k < numofpoints; k++)
X[k] = lx + k*dx;
fisCallMatlabMf2(X, mf_node->para,
mf_node->type, numofpoints, mf_node->value_array);
free(X);
}
#else
#ifndef NO_PRINTF
printf("Cannot find MF type %s!\n", mf_node->type);
#endif
fisError("Exiting ...");
#endif
}
}
}
}
/* Copyright (c) 1994-98 by The MathWorks, Inc. */
/* $Revision: $ $Date: $ */
/* copy string (the first 'length' characters) from array to target string */
static void
#ifdef __STDC__
fisGetString2(char *target, double *array, int max_leng)
#else
fisGetString2(target, array, max_leng)
char *target;
double *array;
int max_leng;
#endif
{
int i;
int actual_leng;
/* Find the actual length of the string */
/* If the string is not ended with 0, take max_leng */
for (actual_leng = 0; actual_leng < max_leng; actual_leng++)
if (array[actual_leng] == 0)
break;
if (actual_leng + 1 > STR_LEN) {
#ifndef NO_PRINTF
printf("actual_leng = %d\n", actual_leng);
printf("STR_LEN = %d\n", STR_LEN);
#endif
fisError("String too long!");
}
for (i = 0; i < actual_leng; i++)
target[i] = (int)array[i];
target[actual_leng] = 0;
}
/* Check if there are abnormal situations is the FIS data structure */
/* Things being checked:
1. MF indices out of bound.
2. Rules with no premise part.
3. Sugeno system with negated consequent.
4. Sugeno system with zero consequent.
*/
void
#ifdef __STDC__
fisCheckDataStructure(FIS *fis)
#else
fisCheckDataStructure(fis)
FIS *fis;
#endif
{
int i, j, mf_index;
int found;
/* check if MF indices are out of bound */
for (i = 0; i < fis->rule_n; i++) {
for (j = 0; j < fis->in_n; j++) {
mf_index = fis->rule_list[i][j];
if (ABS(mf_index) > fis->input[j]->mf_n) {
#ifndef NO_PRINTF
printf("MF index for input %d in rule %d is out of bound.\n",
j+1, i+1);
#endif
fisFreeFisNode(fis);
fisError("Exiting ...");
}
}
for (j = 0; j < fis->out_n; j++) {
mf_index = fis->rule_list[i][fis->in_n+j];
if (ABS(mf_index) > fis->output[j]->mf_n) {
#ifndef NO_PRINTF
printf("MF index for output %d in rule %d is out of bound.\n",
j+1, i+1);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -