⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 defuzz.c

📁 模糊逻辑工具箱 模糊逻辑工具箱 模糊逻辑工具箱 模糊逻辑工具箱 模糊逻辑工具箱
💻 C
字号:
/* Copyright (c) 1994-98 by 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 
#ifdef __STDC__
defuzzCentroid(FIS *fis, int m, double *mf, int numofpoints)
#else
defuzzCentroid(fis, m, mf)
FIS *fis;
int m;
double *mf;
int numofpoints;
#endif
{
	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) {
#ifndef NO_PRINTF
		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");
#endif
		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 
#ifdef __STDC__
defuzzBisector(FIS *fis, int m, double *mf, int numofpoints)
#else
defuzzBisector(fis, m, mf)
FIS *fis;
int m;
double *mf;
int numofpoints;
#endif
{
	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) {
#ifndef NO_PRINTF
		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");
#endif
		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 
#ifdef __STDC__
defuzzMeanOfMax(FIS *fis, int m, double *mf, int numofpoints)
#else
defuzzMeanOfMax(fis, m, mf)
FIS *fis;
int m;
double *mf;
int numofpoints;
#endif
{
	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 
#ifdef __STDC__
defuzzSmallestOfMax(FIS *fis, int m, double *mf, int numofpoints)
#else
defuzzSmallestOfMax(fis, m, mf)
FIS *fis;
int m;
double *mf;
int numofpoints;
#endif
{
	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 
#ifdef __STDC__
defuzzLargestOfMax(FIS *fis, int m, double *mf, int numofpoints)
#else
defuzzLargestOfMax(fis, m, mf)
FIS *fis;
int m;
double *mf;
int numofpoints;
#endif
{
	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);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -