📄 sfsup.c
字号:
out = 0;
return(out);
}
/* pi membership function */
static DOUBLE fisPiMf(DOUBLE x, DOUBLE *params)
{
return(fisSMf(x, params)*fisZMf(x, params+2));
}
/* all membership function */
static DOUBLE fisAllMf(DOUBLE x, DOUBLE *params)
{
return(1);
}
/* returns the number of parameters of MF */
static int fisGetMfParaN(char *mfType)
{
if (strcmp(mfType, "trimf") == 0)
return(3);
if (strcmp(mfType, "trapmf") == 0)
return(4);
if (strcmp(mfType, "gaussmf") == 0)
return(2);
if (strcmp(mfType, "gauss2mf") == 0)
return(4);
if (strcmp(mfType, "sigmf") == 0)
return(2);
if (strcmp(mfType, "dsigmf") == 0)
return(4);
if (strcmp(mfType, "psigmf") == 0)
return(4);
if (strcmp(mfType, "gbellmf") == 0)
return(3);
if (strcmp(mfType, "smf") == 0)
return(2);
if (strcmp(mfType, "zmf") == 0)
return(2);
if (strcmp(mfType, "pimf") == 0)
return(4);
PRINTF("Given MF type (%s) is unknown.\n", mfType);
exit(1);
return(0); /* get rid of compiler warning */
}
/***********************************************************************
T-norm and T-conorm operators
**********************************************************************/
/* Copyright 1994-2002 The MathWorks, Inc. */
/* $Revision: $ $Date: $ */
static DOUBLE fisMin(DOUBLE x, DOUBLE y)
{return((x) < (y) ? (x) : (y));}
static DOUBLE fisMax(DOUBLE x, DOUBLE y)
{return((x) > (y) ? (x) : (y));}
static DOUBLE fisProduct(DOUBLE x, DOUBLE y)
{return(x*y);}
static DOUBLE fisProbOr(DOUBLE x, DOUBLE y)
{return(x + y - x*y);}
static DOUBLE fisSum(DOUBLE x, DOUBLE y)
{return(x + y);}
/* apply given function to an array */
static DOUBLE fisArrayOperation(DOUBLE *array, int size, DOUBLE (*fcn)())
{
int i;
DOUBLE out;
if (size == 0)
fisError("Given size is zero!");
out = array[0];
for (i = 1; i < size; i++)
out = (*fcn)(out, array[i]);
return(out);
}
/* Copyright 1994-2002 The MathWorks, Inc. */
/* $Revision: $ $Date: $ */
/***********************************************************************
Defuzzification methods
**********************************************************************/
/* return the center of area of combined output MF (specified by mf)
of output m */
/* numofpoints is the number of partition for integration */
static DOUBLE defuzzCentroid(FIS *fis, int m, DOUBLE *mf, int numofpoints)
{
DOUBLE min = fis->output[m]->bound[0];
DOUBLE max = fis->output[m]->bound[1];
DOUBLE step = (max - min)/(numofpoints - 1);
DOUBLE total_mf = 0;
DOUBLE sum = 0;
int i;
for (i = 0; i < numofpoints; i++){
total_mf += mf[i];
sum += mf[i]*(min + step*i);
}
if (total_mf == 0) {
PRINTF("Total area is zero in defuzzCentroid() for output %d.\n", m+1);
PRINTF("Average of the range of this output variable is used as the output value.\n\n");
return((fis->output[m]->bound[0] + fis->output[m]->bound[1])/2);
}
return(sum/total_mf);
}
/* return the bisector of area of mf */
static DOUBLE defuzzBisector(FIS *fis, int m, DOUBLE *mf, int numofpoints)
{
DOUBLE min = fis->output[m]->bound[0];
DOUBLE max = fis->output[m]->bound[1];
DOUBLE step = (max - min)/(numofpoints - 1);
DOUBLE area, sub_area;
int i;
/* find the total area */
area = 0;
for (i = 0; i < numofpoints; i++)
area += mf[i];
if (area == 0) {
PRINTF("Total area is zero in defuzzBisector() for output %d.\n", m+1);
PRINTF("Average of the range of this output variable is used as the output value.\n");
return((fis->output[m]->bound[0] + fis->output[m]->bound[1])/2);
}
sub_area = 0;
for (i = 0; i < numofpoints; i++) {
sub_area += mf[i];
if (sub_area >= area/2)
break;
}
return(min + step*i);
}
/* Returns the mean of maximizing x of mf */
static DOUBLE defuzzMeanOfMax(FIS *fis, int m, DOUBLE *mf, int numofpoints)
{
DOUBLE min = fis->output[m]->bound[0];
DOUBLE max = fis->output[m]->bound[1];
DOUBLE step = (max - min)/(numofpoints - 1);
DOUBLE mf_max;
DOUBLE sum;
int count;
int i;
mf_max = fisArrayOperation(mf, numofpoints, fisMax);
sum = 0;
count = 0;
for (i = 0; i < numofpoints; i++)
if (mf[i] == mf_max) {
count++;
sum += i;
}
return(min+step*sum/count);
}
/* Returns the smallest (in magnitude) maximizing x of mf */
static DOUBLE defuzzSmallestOfMax(FIS *fis, int m, DOUBLE *mf, int numofpoints)
{
DOUBLE min = fis->output[m]->bound[0];
DOUBLE max = fis->output[m]->bound[1];
DOUBLE step = (max - min)/(numofpoints - 1);
DOUBLE mf_max;
int i, min_index = 0;
DOUBLE min_distance = pow(2.0, 31.0)-1;
DOUBLE distance; /* distance to the origin */
mf_max = fisArrayOperation(mf, numofpoints, fisMax);
for (i = 0; i < numofpoints; i++)
if (mf[i] == mf_max) {
distance = ABS(min + step*i);
if (min_distance > distance) {
min_distance = distance;
min_index = i;
}
}
return(min + step*min_index);
}
/* Returns the largest (in magnitude) maximizing x of mf */
static DOUBLE defuzzLargestOfMax(FIS *fis, int m, DOUBLE *mf, int numofpoints)
{
DOUBLE min = fis->output[m]->bound[0];
DOUBLE max = fis->output[m]->bound[1];
DOUBLE step = (max - min)/(numofpoints - 1);
DOUBLE mf_max;
int i, max_index = 0;
DOUBLE max_distance = -(pow(2.0, 31.0)-1);
DOUBLE distance; /* distance to the origin */
mf_max = fisArrayOperation(mf, numofpoints, fisMax);
for (i = 0; i < numofpoints; i++)
if (mf[i] == mf_max) {
distance = ABS(min + step*i);
if (max_distance < distance) {
max_distance = distance;
max_index = i;
}
}
return(min + step*max_index);
}
/* Copyright 1994-2002 The MathWorks, Inc. */
/* $Revision: $ $Date: $ */
#ifdef MATLAB_MEX_FILE
/***********************************************************************
MATLAB function calls
**********************************************************************/
/* V4 --> v5
mxFreeMatrix --> mxDestroyArray
Matrix --> mxArray;
mxCreateFull(*, *, 0) --> mxCreateDoubleMatrix(*, *, mxREAL)
mexCallMATLAB(*, *, prhs, *) --> mexCallMATLAB(*, *, (mxArray **)prhs, *)
*/
/* execute MATLAB MF function, scalar version */
static DOUBLE fisCallMatlabMf(DOUBLE x, int nparams, DOUBLE *params, char *mf_type)
{
int i;
mxArray *PARA = mxCreateDoubleMatrix(nparams, 1, mxREAL);
mxArray *X = mxCreateDoubleMatrix(1, 1, mxREAL);
mxArray *OUT;
DOUBLE out;
mxArray *prhs[2];
/* data transfer */
for (i = 0; i < nparams; i++)
mxGetPr(PARA)[i] = params[i];
mxGetPr(X)[0] = x;
prhs[0] = X; prhs[1] = PARA;
/* call matlab MF function */
mexCallMATLAB(1, &OUT, 2, (mxArray **)prhs, mf_type);
out = mxGetScalar(OUT);
/* free allocated matrix */
mxDestroyArray(X);
mxDestroyArray(PARA);
mxDestroyArray(OUT);
/* return output */
return(out);
}
/* execute MATLAB MF function, vector version */
/* this is used in fisComputeOutputMfValueArray() */
static void fisCallMatlabMf2(DOUBLE *x, int nparams, DOUBLE *params, char *mf_type, int leng, DOUBLE *out)
{
int i;
mxArray *PARA = mxCreateDoubleMatrix(nparams, 1, mxREAL);
mxArray *X = mxCreateDoubleMatrix(leng, 1, mxREAL);
mxArray *OUT;
mxArray *prhs[2];
/* transfer data in */
for (i = 0; i < nparams; i++)
mxGetPr(PARA)[i] = params[i];
for (i = 0; i < leng; i++)
mxGetPr(X)[i] = x[i];
prhs[0] = X; prhs[1] = PARA;
/* call matlab MF function */
mexCallMATLAB(1, &OUT, 2, (mxArray **)prhs, mf_type);
/* transfer data out */
for (i = 0; i < leng; i++)
out[i] = mxGetPr(OUT)[i];
/* free allocated matrix */
mxDestroyArray(X);
mxDestroyArray(PARA);
mxDestroyArray(OUT);
}
/* use MATLAB 'exist' to check the type of a variable or function */
static DOUBLE fisCallMatlabExist(char *variable)
{
DOUBLE out;
mxArray *VARIABLE = mxCreateString(variable);
mxArray *OUT;
/* call matlab 'exist' */
mexCallMATLAB(1, &OUT, 1, &VARIABLE, "exist");
out = mxGetScalar(OUT);
/* free allocated matrix */
mxDestroyArray(VARIABLE);
mxDestroyArray(OUT);
/* return output */
return(out);
}
/* execute MATLAB function with a vector input */
/* qualified MATLAB functions are min, sum, max, etc */
static DOUBLE fisCallMatlabFcn(DOUBLE *x, int leng, char *func)
{
DOUBLE out;
mxArray *X = mxCreateDoubleMatrix(leng, 1, mxREAL);
mxArray *OUT;
int i;
/* transfer data */
for (i = 0; i < leng; i++)
mxGetPr(X)[i] = x[i];
/* call matlab function */
mexCallMATLAB(1, &OUT, 1, &X, func);
out = mxGetScalar(OUT);
/* free allocated matrix */
mxDestroyArray(X);
mxDestroyArray(OUT);
/* return output */
return(out);
}
/* execute MATLAB function with a matrix input */
/* qualified MATLAB functions are min, sum, max, etc */
static void fisCallMatlabFcn1(DOUBLE *x, int m, int n, char *func, DOUBLE *out)
{
mxArray *X, *OUT;
int i;
/* allocate memory */
X = mxCreateDoubleMatrix(m, n, mxREAL);
/* transfer data in */
for (i = 0; i < m*n; i++)
mxGetPr(X)[i] = x[i];
/* call matlab function */
mexCallMATLAB(1, &OUT, 1, &X, func);
/* transfer data out */
if (m == 1)
out[0] = mxGetScalar(OUT);
else
for (i = 0; i < n; i++)
out[i] = mxGetPr(OUT)[i];
/* free allocated matrix */
mxDestroyArray(X);
mxDestroyArray(OUT);
}
/* execute MATLAB function with two matrix inputs */
/* qualified MATLAB functions are min, sum, max, etc */
static void fisCallMatlabFcn2(DOUBLE *x, DOUBLE *y, int m, int n, char *func, DOUBLE *out)
{
mxArray *X, *Y, *OUT, *prhs[2];
int i;
/* allocate memory */
X = mxCreateDoubleMatrix(m, n, mxREAL);
Y = mxCreateDoubleMatrix(m, n, mxREAL);
prhs[0] = X;
prhs[1] = Y;
/* transfer data in */
for (i = 0; i < m*n; i++) {
mxGetPr(X)[i] = x[i];
mxGetPr(Y)[i] = y[i];
}
/* call matlab function */
mexCallMATLAB(1, &OUT, 2, (mxArray **)prhs, func);
/* transfer data out */
for (i = 0; i < m*n; i++)
out[i] = mxGetPr(OUT)[i];
/* free allocated matrix */
mxDestroyArray(X);
mxDestroyArray(Y);
mxDestroyArray(OUT);
}
/* execute MATLAB function for defuzzification */
static DOUBLE fisCallMatlabDefuzz(DOUBLE *x, DOUBLE *mf, int leng, char *defuzz_fcn)
{
DOUBLE out;
mxArray *X = mxCreateDoubleMatrix(leng, 1, mxREAL);
/* MF is used as type word in fis.h */
/* gcc is ok with MF being used here, but cc needs a different name */
mxArray *MF_ = mxCreateDoubleMatrix(leng, 1, mxREAL);
mxArray *OUT;
mxArray *prhs[2];
int i;
/* transfer data */
for (i = 0; i < leng; i++) {
mxGetPr(X)[i] = x[i];
mxGetPr(MF_)[i] = mf[i];
}
/* call matlab function */
prhs[0] = X;
prhs[1] = MF_;
mexCallMATLAB(1, &OUT, 2, (mxArray **)prhs, defuzz_fcn);
out = mxGetScalar(OUT);
/* free allocated matrix */
mxDestroyArray(X);
mxDestroyArray(MF_);
mxDestroyArray(OUT);
/* return output */
return(out);
}
#else
# define fisCallMatlabMf(x,nparams,params,mf_type) /* do nothing */
# define fisCallMatlabMf2(x,nparams,params, mf_type, leng, out) /* do nothing */
# define fisCallMatlabExist(variable) /* do nothing */
# define fisCallMatlabFcn(x, leng, func) /* do nothing */
# define fisCallMatlabFcn1(x, m, n, func, out) /* do nothing */
# define fisCallMatlabFcn2(x, y, m, n, func, out) /* do nothing */
# define fisCallMatlabDefuzz(x, mf, leng, defuzz_fcn) /* do nothing */
#endif /* MATLAB_MEX_FILE */
/***********************************************************************
Data structure: construction, printing, and destruction
**********************************************************************/
/* Copyright 1994-2002 The MathWorks, Inc. */
/* $Revision: $ $Date: $ */
IO *fisBuildIoList(int node_n, int *mf_n)
{
IO *io_list;
int i, j;
io_list = (IO *)fisCalloc(node_n, sizeof(IO));
for (i = 0; i < node_n; i++) {
io_list[i].mf_n = mf_n[i];
io_list[i].mf = (MF **)fisCalloc(mf_n[i], sizeof(MF *));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -