📄 fis.c
字号:
for (i = 0; i < fis->in_n; i++)
fis->input[i]->value = input[i];
/* evaluate FIS */
fisEvaluate(fis, 101);
/* dispatch output */
for (i = 0; i < fis->out_n; i++)
output[i] = fis->output[i]->value;
}
/* Copyright 1994-2002 The MathWorks, Inc. */
/* $Revision: $ $Date: $ */
/* compare strings to skip Version field */
int compareString(char *string1, char *string2)
{
int i;
for (i = 0; i<7; i++)
{
if (string1[i] != string2[i])
{
return 0;
}
}
return 1;
}
/* return the next valid line without comments */
static char *
#ifdef __STDC__
getNextLine(char *buf, FILE *fp)
#else
getNextLine(buf, fp)
char *buf;
FILE *fp;
#endif
{
char *returned_value;
int i, j;
returned_value = fgets(buf, STR_LEN, fp);
if (NULL == returned_value)
return(NULL);
/* skip if it starts with '%' or '\n' */
/* skip if it stars with 'V' to protect against version field
from writefis.m which was added for Fuzzy Logic 2.0*/
if ((buf[0] == '%') || (buf[0] == '\n') || compareString("Version",buf) == 1 )
return(getNextLine(buf, fp));
/* get rid of trailing comment or new line */
for (i = 0; buf[i]!='%' && buf[i]!='\n' && i < STR_LEN; i++);
/*
printf("%s\n", buf);
printf("i = %d\n", i);
*/
for (j = i; j < STR_LEN; j++)
buf[j] = 0;
return(returned_value);
}
/* find number x in "******=x" */
static DOUBLE
#ifdef __STDC__
getNumber(char *buf, FILE *fp)
#else
getNumber(buf, fp)
char *buf;
FILE *fp;
#endif
{
int tmp;
char string[STR_LEN];
DOUBLE num;
if (getNextLine(buf, fp) == NULL)
fisError("getNumber: Incomplete FIS file!");
tmp = sscanf(buf, " %[^=] = %lf ", string, &num);
if (tmp != 2) {
PRINTF("Error format in FIS file when parsing\n");
PRINTF("\"%s\"\n", buf);
fisError("Error in getNumber().");
}
/*
printf("getNumber --> %s%lf\n", string, num);
printf("getNumber --> %lf\n", num);
*/
return(num);
}
/* find string x in "*******='x'" */
static void
#ifdef __STDC__
getString(char *buf, FILE *fp, DOUBLE *array)
#else
getString(buf, fp, array)
char *buf;
FILE *fp;
DOUBLE *array;
#endif
{
int i;
char string1[STR_LEN];
char string2[STR_LEN];
int tmp;
if (getNextLine(buf, fp) == NULL)
fisError("getString: Incomplete FIS file!");
tmp = sscanf(buf, " %[^'] '%[^']' ", string1, string2);
if (tmp != 2) {
PRINTF("Error format in FIS file when parsing\n");
PRINTF("\"%s\"\n", buf);
fisError("Error in getString().");
}
/* copy it to output array */
for (i = 0; i < (int)strlen(string2); i++)
array[i] = string2[i];
/*
printf("getString --> %s\n", string2);
*/
}
/* put a string "a b c" to an array [a b c]*/
/* return number of elements */
static int
#ifdef __STDC__
getArray(char *string, DOUBLE *array)
#else
getArray(string, array)
char *string;
DOUBLE *array;
#endif
{
int i;
int start, end, index;
char tmp[STR_LEN];
start = 0; /* start of a number */
end = 0; /* end of a number */
index = 0; /* index of array */
while (start <= (int)strlen(string)-1) {
/* find end */
for (end = start; end < (int)strlen(string); end++)
if (string[end] == ' ')
break;
for (i = start; i <= end; i++)
tmp[i-start] = string[i];
tmp[i-start] = 0;
array[index++] = atof(tmp);
/* find new start */
for (start = end; start < (int)strlen(string); start++)
if (string[start] != ' ')
break;
}
/*
printf("%s\n", string);
fisPrintArray(array, 8);
*/
return(index);
}
static void
#ifdef __STDC__
getMfN(char *filename, int in_n, DOUBLE *in_mf_n, int out_n, DOUBLE *out_mf_n)
#else
getMfN(filename, in_n, in_mf_n, out_n, out_mf_n)
char *filename;
int in_n;
DOUBLE *in_mf_n;
int out_n;
DOUBLE *out_mf_n;
#endif
{
int i, tmp;
char buf[STR_LEN];
FILE *fp = fisOpenFile(filename, "r");
for (i = 0; i < in_n+out_n; i++) {
while (1) {
if (getNextLine(buf, fp) == NULL)
fisError("Not enough NumMFs in FIS file!");
if (sscanf(buf, " NumMFs = %d ", &tmp) == 1)
break;
}
if (i < in_n)
in_mf_n[i] = tmp;
else
out_mf_n[i-in_n] = tmp;
}
fclose(fp);
/*
fisPrintArray(in_mf_n, in_n);
fisPrintArray(out_mf_n, out_n);
*/
}
/* return an empty FIS matrix with right size */
static DOUBLE **
#ifdef __STDC__
returnEmptyFismatrix(char *filename, int *row_n_p, int *col_n_p)
#else
returnEmptyFismatrix(filename, row_n_p, col_n_p)
char *filename;
int *row_n_p;
int *col_n_p;
#endif
{
int in_n, out_n, rule_n, total_in_mf_n, total_out_mf_n;
int row_n, col_n;
char buf[STR_LEN], fisType[STR_LEN];
char fisName[STR_LEN], IoName[STR_LEN];
char tmp1[STR_LEN], tmp2[STR_LEN], tmp3[STR_LEN], tmp4[STR_LEN];
FILE *fp;
DOUBLE *in_mf_n;
DOUBLE *out_mf_n;
DOUBLE **fismatrix;
/* find the row_n */
fp = fisOpenFile(filename, "r");
/* find in_n */
while (1) {
if (getNextLine(buf, fp) == NULL)
fisError("Cannot find NumInputs in FIS file!");
if (sscanf(buf, " NumInputs = %d ", &in_n) == 1)
break;
}
/* find out_n */
while (1) {
if (getNextLine(buf, fp) == NULL)
fisError("Cannot find NumOutputs in FIS file!");
if (sscanf(buf, " NumOutputs = %d ", &out_n) == 1)
break;
}
/* find rule_n */
while (1) {
if (getNextLine(buf, fp) == NULL)
fisError("Cannot find NumRules in FIS file!");
if (sscanf(buf, " NumRules = %d ", &rule_n) == 1)
break;
}
fclose(fp);
in_mf_n = (DOUBLE *)fisCalloc(in_n, sizeof(DOUBLE));
out_mf_n = (DOUBLE *)fisCalloc(out_n, sizeof(DOUBLE));
getMfN(filename, in_n, in_mf_n, out_n, out_mf_n);
total_in_mf_n = fisArrayOperation(in_mf_n, in_n, fisSum);
total_out_mf_n = fisArrayOperation(out_mf_n, out_n, fisSum);
row_n = 11 + 2*(in_n+out_n) +
3*(total_in_mf_n + total_out_mf_n) + rule_n;
FREE(in_mf_n);
FREE(out_mf_n);
/* find the col_n */
fp = fisOpenFile(filename, "r");
/* find FIS name */
while (1) {
if (getNextLine(buf, fp) == NULL)
fisError("Cannot find FIS Name in FIS file!");
if (sscanf(buf, " Name = '%[^']' ", fisName) == 1)
break;
}
col_n = (int)strlen(fisName);
col_n = MAX(col_n, 8); /* 'centroid' defuzzification */
/* find FIS type */
while (1) {
if (getNextLine(buf, fp) == NULL)
fisError("Cannot find FIS Type in FIS file!");
if (sscanf(buf, " Type = '%[^']' ", fisType) == 1)
break;
}
/* find IO names, MF labels, MF types */
while (getNextLine(buf, fp) != NULL) {
if (sscanf(buf, " Name = '%[^']' ", IoName) == 1)
col_n = MAX(col_n, (int)strlen(IoName));
if (sscanf(buf, " %[^'] '%[^']' : '%[^']' , [ %[^]] ",
tmp1, tmp2, tmp3, tmp4) == 4) {
col_n = MAX(col_n, (int)strlen(tmp2));
col_n = MAX(col_n, (int)strlen(tmp3));
}
}
if (!strcmp(fisType, "mamdani"))
col_n = MAX(col_n, MF_PARA_N);
else if (!strcmp(fisType, "sugeno"))
col_n = MAX(col_n, in_n+out_n+2);
else
fisError("Unknown FIS type!");
fclose(fp);
/*
printf("row_n = %d\n", row_n);
printf("col_n = %d\n", col_n);
*/
*row_n_p = row_n;
*col_n_p = col_n;
fismatrix = (DOUBLE **)fisCreateMatrix(row_n, col_n, sizeof(DOUBLE));
return(fismatrix);
}
/* return a FIS matrix with all information */
static DOUBLE **
#ifdef __STDC__
returnFismatrix(char *fis_file, int *row_n_p, int *col_n_p)
#else
returnFismatrix(fis_file, row_n_p, col_n_p)
char *fis_file;
int *row_n_p;
int *col_n_p;
#endif
{
int i, j, k;
FILE *fp;
char buf[STR_LEN];
char str1[STR_LEN], str2[STR_LEN], str3[STR_LEN], str4[STR_LEN];
char fisType[STR_LEN];
int in_n, out_n, rule_n;
int mf_n;
int now;
DOUBLE **fismatrix;
DOUBLE *in_mf_n, *out_mf_n;
fismatrix = returnEmptyFismatrix(fis_file, row_n_p, col_n_p);
fp = fisOpenFile(fis_file, "r");
/* looping till it finds "[System]" */
while (1) {
if (getNextLine(buf, fp) == NULL)
fisError("Cannot find [System] in FIS file!");
if (!strcmp(buf, "[System]")) /* found it! */
break;
}
/* get FIS information */
now = 0;
getString(buf, fp, fismatrix[now++]); /* name */
getString(buf, fp, fismatrix[now++]); /* type */
for (i = 0; i < STR_LEN && fismatrix[1][i] != 0; i++)
fisType[i] = (int) fismatrix[1][i];
fisType[i] = 0;
in_n = (int)getNumber(buf, fp);
out_n = (int)getNumber(buf, fp);
fismatrix[now][0] = (DOUBLE) in_n;
fismatrix[now][1] = (DOUBLE) out_n;
now++;
/* create in_mf_n and out_mf_n */
in_mf_n = (DOUBLE *)fisCalloc(in_n, sizeof(DOUBLE));
out_mf_n = (DOUBLE *)fisCalloc(out_n, sizeof(DOUBLE));
getMfN(fis_file, in_n, in_mf_n, out_n, out_mf_n);
for (i = 0; i < in_n; i++)
fismatrix[now][i] = in_mf_n[i];
now++;
for (i = 0; i < out_n; i++)
fismatrix[now][i] = out_mf_n[i];
now++;
rule_n = (int)getNumber(buf, fp);
fismatrix[now++][0] = (DOUBLE) rule_n;
getString(buf, fp, fismatrix[now++]); /* and method */
getString(buf, fp, fismatrix[now++]); /* or method */
getString(buf, fp, fismatrix[now++]); /* imp method */
getString(buf, fp, fismatrix[now++]); /* agg method */
getString(buf, fp, fismatrix[now++]); /* defuzz method */
fclose(fp);
/*
printf("in_n = %d, out_n = %d, rule_n = %d\n", in_n, out_n, rule_n);
*/
/* get input & output labels */
/* get rid of FIS name */
fp = fisOpenFile(fis_file, "r");
while (1) {
if (getNextLine(buf, fp) == NULL)
fisError("Cannot find the first Name in FIS file!");
if (sscanf(buf, " Name = '%[^']' ", str1) == 1)
break;
}
for (i = 0; i < in_n+out_n; i++) {
while (1) {
if (getNextLine(buf, fp) == NULL)
fisError("Not enough Name in FIS file!");
if (sscanf(buf, " Name = '%[^']' ", str1) == 1)
break;
}
for (j = 0; j < (int)strlen(str1); j++)
fismatrix[now][j] = str1[j];
now++;
}
fclose(fp);
/* get input & output ranges */
fp = fisOpenFile(fis_file, "r");
for (i = 0; i < in_n+out_n; i++) {
while (1) {
if (getNextLine(buf, fp) == NULL)
fisError("Not enough Range in FIS file!");
if (sscanf(buf, " Range = [ %[^]] ", str1) == 1)
break;
}
if (getArray(str1, fismatrix[now++]) != 2)
fisError("Error in parsing I/O ranges.");
}
fclose(fp);
/* get input and output MF labels */
fp = fisOpenFile(fis_file, "r");
for (i = 0; i < in_n+out_n; i++) {
mf_n = i < in_n? in_mf_n[i]:out_mf_n[i-in_n];
for (j = 0; j < mf_n; j++) {
while (1) {
if (getNextLine(buf, fp) == NULL)
fisError("Not enough MF Labels in FIS file!");
if (sscanf(buf, " %[^']'%[^']' : '%[^']' , [ %[^]] ",
str1, str2, str3, str4) == 4)
break;
}
for (k = 0; k < (int)strlen(str2); k++)
fismatrix[now][k] = str2[k];
now++;
}
}
fclose(fp);
/* get input and output MF types */
fp = fisOpenFile(fis_file, "r");
for (i = 0; i < in_n+out_n; i++) {
mf_n = i < in_n? in_mf_n[i]:out_mf_n[i-in_n];
for (j = 0; j < mf_n; j++) {
while (1) {
if (getNextLine(buf, fp) == NULL)
fisError("Not enough MF types in FIS file!");
if (sscanf(buf, " %[^']'%[^']' : '%[^']' , [ %[^]] ",
str1, str2, str3, str4) == 4)
break;
}
for (k = 0; k < (int)strlen(str3); k++)
fismatrix[now][k] = str3[k];
now++;
}
}
fclose(fp);
/* get input & output MF parameters */
fp = fisOpenFile(fis_file, "r");
for (i = 0; i < in_n+out_n; i++) {
mf_n = i < in_n? in_mf_n[i]:out_mf_n[i-in_n];
for (j = 0; j < mf_n; j++) {
while (1) {
if (getNextLine(buf, fp) == NULL)
fisError("Not enough MF parameters in FIS file!");
if (sscanf(buf, " %[^']'%[^']' : '%[^']' , [ %[^]] ",
str1, str2
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -