# 模糊推理系统的简单实现

1.获取数值输入

2.通过数据库（Database）将数值输入转为语意含义

3.验证规则库（Rulebase）中的哪些规则被输入激活

4.组合被激活的规则，得到模糊输出（Fuzzy Output）

5.去模糊化（实现IDefuzzifier接口）

// 隶属度函数（距离） FuzzySet fsNear = new FuzzySet("Near", new TrapezoidalFunction(15, 50, TrapezoidalFunction.EdgeType.Right)); FuzzySet fsMedium = new FuzzySet("Medium", new TrapezoidalFunction(15, 50, 60, 100)); FuzzySet fsFar = new FuzzySet("Far", new TrapezoidalFunction(60, 100, TrapezoidalFunction.EdgeType.Left));//绘制图像 int length1 =(int) (chart1.RangeX.Max - chart1.RangeX.Min); double[,] NearValues = new double[length1, 2]; for (int i = (int)chart1.RangeX.Min; i < chart1.RangeX.Max; i++) {     NearValues[i, 0] = i;     NearValues[i, 1] = fsNear.GetMembership(i); } double[,] MediumValues = new double[length1, 2]; for (int i = (int)chart1.RangeX.Min; i < chart1.RangeX.Max; i++) {     MediumValues[i, 0] = i;     MediumValues[i, 1] = fsMedium.GetMembership(i); } double[,] FarValues = new double[length1, 2]; for (int i = (int)chart1.RangeX.Min; i < chart1.RangeX.Max; i++) {     FarValues[i, 0] = i;     FarValues[i, 1] = fsFar.GetMembership(i); } chart1.UpdateDataSeries("Near", NearValues); chart1.UpdateDataSeries("Medium", MediumValues); chart1.UpdateDataSeries("Far", FarValues);// 距离（输入） LinguisticVariable lvFront = new LinguisticVariable("FrontalDistance", 0, 120); lvFront.AddLabel(fsNear); lvFront.AddLabel(fsMedium); lvFront.AddLabel(fsFar);// 隶属度函数 FuzzySet fsZero = new FuzzySet("Zero", new TrapezoidalFunction(-10, 5, 5, 10)); FuzzySet fsLP = new FuzzySet("LittlePositive", new TrapezoidalFunction(5, 10, 20, 25)); FuzzySet fsP = new FuzzySet("Positive", new TrapezoidalFunction(20, 25, 35, 40)); FuzzySet fsVP = new FuzzySet("VeryPositive", new TrapezoidalFunction(35, 40, TrapezoidalFunction.EdgeType.Left));//绘制图像 int length2 = (int)(chart2.RangeX.Max - chart2.RangeX.Min); double[,] ZeroValues = new double[length2, 2]; for (int i = (int)chart2.RangeX.Min; i < chart2.RangeX.Max; i++) {     ZeroValues[i + 10, 0] = i;     ZeroValues[i + 10, 1] = fsZero.GetMembership(i); } double[,] LittlePositiveValues = new double[length2, 2]; for (int i = (int)chart2.RangeX.Min; i < chart2.RangeX.Max; i++) {     LittlePositiveValues[i + 10, 0] = i;     LittlePositiveValues[i + 10, 1] = fsLP.GetMembership(i); } double[,] PositiveValues = new double[length2, 2]; for (int i = (int)chart2.RangeX.Min; i < chart2.RangeX.Max; i++) {     PositiveValues[i + 10, 0] = i;     PositiveValues[i + 10, 1] = fsP.GetMembership(i); } double[,] VeryPositiveValues = new double[length2, 2]; for (int i = (int)chart2.RangeX.Min; i < chart2.RangeX.Max; i++) {     VeryPositiveValues[i + 10, 0] = i;     VeryPositiveValues[i + 10, 1] = fsVP.GetMembership(i); } chart2.UpdateDataSeries("Zero", ZeroValues); chart2.UpdateDataSeries("LittlePositive", LittlePositiveValues); chart2.UpdateDataSeries("Positive", PositiveValues); chart2.UpdateDataSeries("VeryPositive", VeryPositiveValues);//角度 LinguisticVariable lvAngle = new LinguisticVariable("Angle", -10, 50); lvAngle.AddLabel(fsZero); lvAngle.AddLabel(fsLP); lvAngle.AddLabel(fsP); lvAngle.AddLabel(fsVP);//设置数据库 Database fuzzyDB = new Database(); fuzzyDB.AddVariable(lvFront); fuzzyDB.AddVariable(lvAngle);// 创建模糊推理系统 InferenceSystem IS = new InferenceSystem(fuzzyDB, new CentroidDefuzzifier(1000));// 直行规则 IS.NewRule("Rule 1", "IF FrontalDistance IS Far THEN Angle IS Zero"); // 左转规则 IS.NewRule("Rule 2", "IF FrontalDistance IS Near THEN Angle IS Positive");//开始推理// 设定输入 IS.SetInput("FrontalDistance",float.Parse(inputBox.Text));//打印输出 try { float newAngle = IS.Evaluate("Angle");     outputBox.Text = newAngle.ToString(); } catch (Exception ex) {     MessageBox.Show("error  ===>  "+ex.Message); }

FuzzyOutput fuzzyOutput = IS.ExecuteInference("Angle");foreach (FuzzyOutput.OutputConstraint oc in fuzzyOutput.OutputList) {     Console.WriteLine(oc.Label + " - " + oc.FiringStrength.ToString()); }

# 模糊推理系统的扩展和完善

1.最大平均法 (modified mean of maxima defuzzifier)

2.修正型最大平均法 (modified mean of maxima defuzzifier)

3.中心平均法 (modified center average defuzzifier)

4.修正型重心法 (modified center average defuzzifier)

5.权重式平均法(weighted average method)结合了启动强度，使用更为广泛

AForge.Net也有神经网络相关的东西，所以使用起来很方便的。

+ 订阅