// Testing basic funcionality of linguistic variables private void runLingVarTestButton_Click(object sender, EventArgs e) { ClearDataSeries( ); // create a linguistic variable to represent temperature LinguisticVariable lvTemperature = new LinguisticVariable("Temperature", 0, 80); // create the linguistic labels (fuzzy sets) that compose the temperature TrapezoidalFunction function1 = new TrapezoidalFunction(10, 15, TrapezoidalFunction.EdgeType.Right); FuzzySet fsCold = new FuzzySet("Cold", function1); TrapezoidalFunction function2 = new TrapezoidalFunction(10, 15, 20, 25); FuzzySet fsCool = new FuzzySet("Cool", function2); TrapezoidalFunction function3 = new TrapezoidalFunction(20, 25, 30, 35); FuzzySet fsWarm = new FuzzySet("Warm", function3); TrapezoidalFunction function4 = new TrapezoidalFunction(30, 35, TrapezoidalFunction.EdgeType.Left); FuzzySet fsHot = new FuzzySet("Hot", function4); // adding labels to the variable lvTemperature.AddLabel(fsCold); lvTemperature.AddLabel(fsCool); lvTemperature.AddLabel(fsWarm); lvTemperature.AddLabel(fsHot); // get membership of some points to the cool fuzzy set double[][,] chartValues = new double[4][, ]; for (int i = 0; i < 4; i++) { chartValues[i] = new double[160, 2]; } // showing the shape of the linguistic variable - the shape of its labels memberships from start to end int j = 0; for (float x = 0; x < 80; x += 0.5f, j++) { double y1 = lvTemperature.GetLabelMembership("Cold", x); double y2 = lvTemperature.GetLabelMembership("Cool", x); double y3 = lvTemperature.GetLabelMembership("Warm", x); double y4 = lvTemperature.GetLabelMembership("Hot", x); chartValues[0][j, 0] = x; chartValues[0][j, 1] = y1; chartValues[1][j, 0] = x; chartValues[1][j, 1] = y2; chartValues[2][j, 0] = x; chartValues[2][j, 1] = y3; chartValues[3][j, 0] = x; chartValues[3][j, 1] = y4; } // plot membership to a chart chart.UpdateDataSeries("COLD", chartValues[0]); chart.UpdateDataSeries("COOL", chartValues[1]); chart.UpdateDataSeries("WARM", chartValues[2]); chart.UpdateDataSeries("HOT", chartValues[3]); }
public void DrawChartAndSelectSection() { if (Type == VariableType.Input) { ClearChart(); float delta = (_range.Max - _range.Min) / _sampling; foreach (var label in _sets) { for (float x = _range.Min; x <= _range.Max; x += delta) { _chart.Series.Where(xx => xx.Name == label.Name).FirstOrDefault().Points.AddXY(x, _fVariable.GetLabelMembership(label.Name, x)); } } _chartLine.X = (double)ResultValue; _chart.Update(); } }
private void Form1_Load(object sender, EventArgs e) { /* * LinguisticVariable lvTemperature = new LinguisticVariable("Temperature", 0, 80); * * TrapezoidalFunction function1 = new TrapezoidalFunction(10, 15, TrapezoidalFunction.EdgeType.Right); * FuzzySet fsCold = new FuzzySet("Cold", function1); * TrapezoidalFunction function2 = new TrapezoidalFunction(10, 15, 20, 25); * FuzzySet fsCool = new FuzzySet("Cool", function2); * TrapezoidalFunction function3 = new TrapezoidalFunction(20, 25, 30, 35); * FuzzySet fsWarm = new FuzzySet("Warm", function3); * TrapezoidalFunction function4 = new TrapezoidalFunction(30, 35, TrapezoidalFunction.EdgeType.Left); * FuzzySet fsHot = new FuzzySet("Hot", function4); * * lvTemperature.AddLabel(fsCold); * lvTemperature.AddLabel(fsCool); * lvTemperature.AddLabel(fsWarm); * lvTemperature.AddLabel(fsHot); * * textBox1.Text += "Cold; Cool; Warm; Hot" + Environment.NewLine; * for (float x = 0; x < 80; x += 0.2f) * { * float y1 = lvTemperature.GetLabelMembership("Cold", x); * float y2 = lvTemperature.GetLabelMembership("Cool", x); * float y3 = lvTemperature.GetLabelMembership("Warm", x); * float y4 = lvTemperature.GetLabelMembership("Hot", x); * * textBox1.Text += y1.ToString() + " " + y2.ToString() + " " + y3.ToString() + " " + y4.ToString() + Environment.NewLine; * } */ /* * 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)); * * 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)); * * 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", 20); * float a = IS.Evaluate("Angle"); // -> 29.999 * * try * { * FuzzyOutput fuzzyOutput = IS.ExecuteInference("Angle"); // -> 0.87 Positive * * foreach (FuzzyOutput.OutputConstraint oc in fuzzyOutput.OutputList) * { * Console.WriteLine(oc.Label + " - " + oc.FiringStrength.ToString()); * } * } * catch (Exception) * { * }*/ FuzzySet fsxM2 = new FuzzySet("xM2", new TrapezoidalFunction(-150, -80, TrapezoidalFunction.EdgeType.Right)); FuzzySet fsxM1 = new FuzzySet("xM1", new TrapezoidalFunction(-90f, -65f, 0f)); FuzzySet fsxS = new FuzzySet("xS", new TrapezoidalFunction(-30f, 0, 30f)); FuzzySet fsxD1 = new FuzzySet("xD1", new TrapezoidalFunction(0f, 65f, 90f)); FuzzySet fsxD2 = new FuzzySet("xD2", new TrapezoidalFunction(80, 150, TrapezoidalFunction.EdgeType.Left)); LinguisticVariable lvX = new LinguisticVariable("XDist", -150, 150); lvX.AddLabel(fsxM2); lvX.AddLabel(fsxM1); lvX.AddLabel(fsxS); lvX.AddLabel(fsxD1); lvX.AddLabel(fsxD2); FuzzySet fsfM3 = new FuzzySet("fM3", new TrapezoidalFunction(-190, -140, -90)); FuzzySet fsfM2 = new FuzzySet("fM2", new TrapezoidalFunction(-140f, -90f, -45f)); FuzzySet fsfM1 = new FuzzySet("fM1", new TrapezoidalFunction(-90f, -45f, 0f)); FuzzySet fsfS = new FuzzySet("fS", new TrapezoidalFunction(-30f, 0, 30f)); FuzzySet fsfD1 = new FuzzySet("fD1", new TrapezoidalFunction(0f, 45f, 90f)); FuzzySet fsfD2 = new FuzzySet("fD2", new TrapezoidalFunction(45f, 90f, 140f)); FuzzySet fsfD3 = new FuzzySet("fD3", new TrapezoidalFunction(90, 140, 190)); LinguisticVariable lvF = new LinguisticVariable("FAngle", -190, 190); lvF.AddLabel(fsfM3); lvF.AddLabel(fsfM2); lvF.AddLabel(fsfM1); lvF.AddLabel(fsfS); lvF.AddLabel(fsfD1); lvF.AddLabel(fsfD2); lvF.AddLabel(fsfD3); FuzzySet fsaM3 = new FuzzySet("aM3", new TrapezoidalFunction(-45, -30, TrapezoidalFunction.EdgeType.Right)); FuzzySet fsaM2 = new FuzzySet("aM2", new TrapezoidalFunction(-45f, -30f, -5f)); FuzzySet fsaM1 = new FuzzySet("aM1", new TrapezoidalFunction(-30f, -17f, -5f)); FuzzySet fsaS = new FuzzySet("aS", new TrapezoidalFunction(-5f, 0, 5f)); FuzzySet fsaD1 = new FuzzySet("aD1", new TrapezoidalFunction(5f, 17f, 30f)); FuzzySet fsaD2 = new FuzzySet("aD2", new TrapezoidalFunction(5f, 30f, 45f)); FuzzySet fsaD3 = new FuzzySet("aD3", new TrapezoidalFunction(30, 45, TrapezoidalFunction.EdgeType.Left)); LinguisticVariable lvA = new LinguisticVariable("OutAngle", -45, 45); lvA.AddLabel(fsaM3); lvA.AddLabel(fsaM2); lvA.AddLabel(fsaM1); lvA.AddLabel(fsaS); lvA.AddLabel(fsaD1); lvA.AddLabel(fsaD2); lvA.AddLabel(fsaD3); Database fuzzyDB = new Database(); fuzzyDB.AddVariable(lvX); fuzzyDB.AddVariable(lvF); fuzzyDB.AddVariable(lvA); /* * float x = -100, F = 180, A = 45; * float Px = 0, Pf = 0, Pa = 0; * int Nx = -1, Nf = -1, Na = -1; * * float _t = lvX.GetLabelMembership("xM2", x); * if (_t > Px) * { * Px = _t; * Nx = 0; * } * * _t = lvX.GetLabelMembership("xM1", x); * if (_t > Px) * { * Px = _t; * Nx = 1; * } * * _t = lvX.GetLabelMembership("xS", x); * if (_t > Px) * { * Px = _t; * Nx = 2; * } * * _t = lvX.GetLabelMembership("xD1", x); * if (_t > Px) * { * Px = _t; * Nx = 3; * } * * _t = lvX.GetLabelMembership("xD2", x); * if (_t > Px) * { * Px = _t; * Nx = 4; * }*/ InferenceSystem IS = new InferenceSystem(fuzzyDB, new CentroidDefuzzifier(1000)); IS.NewRule("Rule 1", "IF XDist IS xM2 AND FAngle IS fD3 THEN OutAngle IS aD3"); //Rule r = IS.GetRule("Rule 1"); //Clause cl = r.Output; IS.SetInput("XDist", -100); IS.SetInput("FAngle", 180); float fu = lvX.GetLabelMembership("xM2", -100); // 0.29 float fuu = lvF.GetLabelMembership("fD3", 180); // 0.2 float a = IS.Evaluate("OutAngle"); // -> 38.2 try { FuzzyOutput fuzzyOutput = IS.ExecuteInference("OutAngle"); // -> 0.2 ad3 foreach (FuzzyOutput.OutputConstraint oc in fuzzyOutput.OutputList) { Console.WriteLine(oc.Label + " - " + oc.FiringStrength.ToString()); } } catch (Exception) { } }
private void _getMemberships(float probability, out float y1, out float yX, out float y2) { y1 = _lvlProbabilidad.GetLabelMembership("UNO", probability); yX = _lvlProbabilidad.GetLabelMembership("EQUIS", probability); y2 = _lvlProbabilidad.GetLabelMembership("DOS", probability); }
static void Main(string[] args) { LinguisticVariable luminosidade = new LinguisticVariable("luminosidade", 0, 14); TrapezoidalFunction tf_escura = new TrapezoidalFunction(1, 5, TrapezoidalFunction.EdgeType.Right); FuzzySet escura = new FuzzySet("escura", tf_escura); TrapezoidalFunction tf_pouca = new TrapezoidalFunction(1, 5, 6, 8); FuzzySet pouca = new FuzzySet("pouca", tf_pouca); TrapezoidalFunction tf_media = new TrapezoidalFunction(6, 8, 10, 11); FuzzySet media = new FuzzySet("media", tf_media); TrapezoidalFunction tf_extrema = new TrapezoidalFunction(9, 12, TrapezoidalFunction.EdgeType.Left); FuzzySet extrema = new FuzzySet("extrema", tf_extrema); luminosidade.AddLabel(escura); luminosidade.AddLabel(pouca); luminosidade.AddLabel(media); luminosidade.AddLabel(extrema); LinguisticVariable profundidade = new LinguisticVariable("profundidade", (float)0, (float)1.4); TrapezoidalFunction tf_rasa = new TrapezoidalFunction((float)0.1, (float)0.3, TrapezoidalFunction.EdgeType.Right); FuzzySet rasa = new FuzzySet("rasa", tf_rasa); TrapezoidalFunction tf_medprof = new TrapezoidalFunction((float)0.1, (float)0.3, (float)0.6, (float)1); FuzzySet mediaprof = new FuzzySet("media", tf_medprof); TrapezoidalFunction tf_profundo = new TrapezoidalFunction((float)0.6, (float)1, (float)1.2); FuzzySet profundo = new FuzzySet("profundo", tf_profundo); TrapezoidalFunction tf_muitoprof = new TrapezoidalFunction((float)1, (float)1.2, TrapezoidalFunction.EdgeType.Left); FuzzySet muitoprof = new FuzzySet("muito profundo", tf_muitoprof); profundidade.AddLabel(rasa); profundidade.AddLabel(mediaprof); profundidade.AddLabel(profundo); profundidade.AddLabel(muitoprof); float x = 0; float y = 0; float profmedia_result = 0; float luminopouca_result = 0; float profrasa_result = 0; float luminoextrema_result = 0; float profmuito_result = 0; float luminomed_result = 0; float luminoesc_result = 0; Console.WriteLine("Qual a luminosidade?"); string n1 = Console.ReadLine(); x = (float)Convert.ToDouble(n1); Console.WriteLine("Qual a profundidade?"); string n2 = Console.ReadLine(); y = (float)Convert.ToDouble(n2); profmedia_result = profundidade.GetLabelMembership("media", y); luminopouca_result = luminosidade.GetLabelMembership("pouca", x); profrasa_result = profundidade.GetLabelMembership("rasa", y); luminoextrema_result = luminosidade.GetLabelMembership("extrema", x); profmuito_result = profundidade.GetLabelMembership("muito profundo", y); luminomed_result = luminosidade.GetLabelMembership("media", x); luminoesc_result = luminosidade.GetLabelMembership("escura", x); Console.WriteLine("Profundidade média e luminosidade pouca = " + Math.Min(profmedia_result, luminopouca_result)); Console.WriteLine("Profundidade rasa ou Luminosidade extrema =" + Math.Max(profrasa_result, luminoextrema_result)); Console.WriteLine("Profundidade muito profundo ou profundidade média e luminosidade escura = " + Math.Min(Math.Max(profmuito_result, profmedia_result), luminoesc_result)); Console.WriteLine("Luminosidade extrema e luminosidade média = " + Math.Min(luminoextrema_result, luminomed_result)); Console.ReadKey(); }