📄 invp_flc.c
字号:
/*
* File : invp_flc.c
*
* An example of Fuzzy Inference System,
* uk = fuzzy_controller(ek, dek)
*
* Revised by Chou, Penchen, Aug. 3,2001
* EE Dept., Da-Yeh University.
*/
float fuzzy_controller(float, float);
#include<dos.h>
#include<math.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX(i,j) (i>=j)?i:j
#define MIN(i,j) (i<=j)?i:j
#define NO_SECOND_INPUT 0
#define AND_OPERATOR 1
#define OR_OPERATOR 2
/***************************************************************/
/***************************************************************
** ==> USER CAN MODIFY THE FOLLOWING: *********
****************************************************************/
#define N_rules 25 /* Total # of RULES */
#define DA_bias 0 /* Bias appended to output for D/A conv.
If you do not know, set DA_bias to zero*/
#define IN1_min -2.3562
#define IN1_max 2.3562
#define IN2_min -1.1781
#define IN2_max 1.1781
#define OUT_min -30.0
#define OUT_max 30.0
/*-----------------------------------------------------------*/
/* Membership Functions */
/* ===>Triangles with equal sides only<=== */
float x_membership[5][3]= /* MB of input1 */
{{-2.3562,-1.5708,-0.7850},
{-1.5708,-0.7850,0},
{-0.7850,0,0.7850},
{0,0.7850,1.5708},
{0.7850,1.5708,2.3562}};
float y_membership[5][3]= /* MB of input2 */
{{-1.1781,-0.7850,-0.3927},
{-0.7850,-0.3927,0},
{-0.3927,0,0.3927},
{0,0.3927,0.7850},
{0.3927,0.7850,1.1781}};
float z_membership[5][3]= /* MB of output1 */
{{-30,-20,-10},
{-20,-10,0},
{-10,0,10},
{0,10,20},
{10,20,30}};
/* RULES DEFINITION *
-----------------------------------------------------------------*/
/* ===>{1,1,1,2}, the last 2 is OR operation between two inputs.
Use 1 for AND.
-----------------------------------------------------------------*/
int Rule[N_rules][4]=
{{1,1,5,1},
{1,2,5,1},
{1,3,5,1},
{1,4,4,1},
{1,5,3,1},
{2,1,5,1},
{2,2,4,1},
{2,3,4,1},
{2,4,3,1},
{2,5,2,1},
{3,1,5,1},
{3,2,4,1},
{3,3,3,1},
{3,4,2,1},
{3,5,1,1},
{4,1,4,1},
{4,2,3,1},
{4,3,2,1},
{4,4,2,1},
{4,5,1,1},
{5,1,2,1},
{5,2,2,1},
{5,3,1,1},
{5,4,1,1},
{5,5,1,1}};
/******************************************************************
** [==> NO TOUCH in the following TO THE END.] **
******************************************************************/
/* Global variables */
/* grade_reg array saves w1 and w2 of input x and y respectively.
grade stores all min(w1,w2) in N_rules of locations. */
float grade_reg[2], grade[N_rules];
float *ptr_MB; /* membership functions (MB) pointer */
/*****************************************************************
** FUNCTIONS DEFINITIONS **
******************************************************************
[1]. COMPUTE WEIGHT OF INPUT **
******************************************************************/
float compute_grade(float *MB, int set, float input_data)
{
float weight;
if ((input_data< *(MB+3*set+0)) || (input_data>*(MB+3*set+2)))
weight=0;
else
if (input_data> *(MB+3*set+1)) /* ON THE RIGHT SIDE OF A TRIANGLE */
weight=(*(MB+3*set+2)-input_data)/
(*(MB+3*set+2)-*(MB+3*set+1));
else
weight=(input_data-*(MB+3*set+0))/ /* ON THE LEFT SIDE OF A TRIANGLE */
(*(MB+3*set+1)-*(MB+3*set+0));
//printf("weight=%.2f\n",weight);
weight=MAX(0.0,weight); /* MAKE SURE TO BE WITHIN 0 TO 1 */
weight=MIN(1.0,weight);
//printf("weight=%.2f\n",weight);
return weight;
}
/**************************************************************
[2]. COMPUTE WEIGHTED AVERAGE(CENTROID) OF AREA MADE BY
OUTPUT FUZZY SET.
***************************************************************/
float fuzzy_controller(float input1, float input2)
{
float accu_num,accu_den,output,signal;
int which_rule, set;
/* CLEAR VARIABLES */
accu_num=0;accu_den=0;output=0;
for (which_rule=0;which_rule<=N_rules-1;which_rule++)
{
signal=input1; /* Data in */
signal=MIN(signal, IN1_max);
signal=MAX(signal, IN1_min);
//printf("Data modified=%f\n",signal);
set=Rule[which_rule][0]-1; ptr_MB=&x_membership;
grade_reg[0]=compute_grade(ptr_MB,set,signal); /* Save data in grade_reg[0] */
//printf("%2d, Grade1=%6.2f, ",n+1,grade_reg[0]);
signal=input2;
signal=MIN(signal, IN2_max);
signal=MAX(signal, IN2_min);
set=Rule[which_rule][1]-1; ptr_MB=&y_membership;
grade_reg[1]=compute_grade(ptr_MB,set,signal); /* Save data in grade_reg[1] */
//printf("Grade2=%6.2f\n",grade_reg[1]);
/* Check and/or operator */
/* If the sencond input is empty (i.e. 0), then only one grade */
if (Rule[which_rule][1]==NO_SECOND_INPUT)
grade[which_rule]=grade_reg[0];
else
if (Rule[which_rule][3]==AND_OPERATOR)
grade[which_rule]=MIN(grade_reg[0],grade_reg[1]);
else /* Otherwise, it is an OR_OPERATOR */
grade[which_rule]=MAX(grade_reg[0],grade_reg[1]);
/* ACCUMULATED GRADES AND AREA GRADES CALCULATIONS */
//printf("grade[%i]=%.2f\n",which_rule,grade[which_rule]);
accu_num=accu_num+(grade[which_rule]*z_membership[Rule[which_rule][2]-1][1]);
accu_den=accu_den+grade[which_rule];
//printf("num[%i]=%5.2f,den[%i]=%5.2f\n",which_rule,accu_num,which_rule,accu_den);
}
/* IN CASE, AVOIDING DIVIDED BY ZERO */
if (fabs(accu_den)>0.001)
output=accu_num/accu_den;
else
output=0.0;
//printf("output=%f",output);
output=MIN(output, OUT_max); /* BOUNDED OUTPUT CHECK POINTS */
output=MAX(output, OUT_min);
return (output+DA_bias);
}
/************************************************************************
END of FUNCTIONS.
*************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -