internal void Defuzzify_WithTwoTriangles_ReturnsExpectedResult()
        {
            // Arrange
            var fuzzySet1 = new FuzzySet("left", TriangularFunction.Create(1, 2, 3));
            var fuzzySet2 = new FuzzySet("right", TriangularFunction.Create(3, 4, 5));

            var fuzzyOutput = new List <FuzzyOutput>
            {
                new FuzzyOutput(
                    Label.Create("Balance"),
                    fuzzySet1,
                    UnitInterval.One()),

                new FuzzyOutput(
                    Label.Create("Balance"),
                    fuzzySet2,
                    UnitInterval.One())
            };

            var centroidDefuzzifier = new CentroidDefuzzifier();

            // Act
            var result = centroidDefuzzifier.Defuzzify(fuzzyOutput);

            // Assert
            Assert.Equal("Balance", result.Subject.Value);
            Assert.Equal(3, result.Value);
        }
Esempio n. 2
0
        internal void IsNormal_WhenSetNotNormal_ReturnsFalse()
        {
            // Arrange
            var function = TriangularFunction.Create(2, 3, 4, 0, 0.9);
            var fuzzySet = new FuzzySet("some_fuzzy_state", function);

            // Act
            var result = fuzzySet.IsNormal;

            // Assert
            Assert.False(result);
        }
Esempio n. 3
0
        internal void Complement_VariousInputs_ReturnsExpectedResult(double input, double expected)
        {
            // Arrange
            var function = TriangularFunction.Create(2, 3, 4);
            var fuzzySet = new FuzzySet("some_fuzzy_state", function);

            // Act
            var result = fuzzySet.Complement(input);

            // Assert
            Assert.Equal(UnitInterval.Create(expected), result);
        }
Esempio n. 4
0
        internal void GetMembership_VariousInputs_ReturnsExpectedResult(double input, double expected)
        {
            // Arrange
            var function = TriangularFunction.Create(2, 3, 4);
            var fuzzySet = new FuzzySet("some_fuzzy_state", function);

            // Act
            var result = fuzzySet.GetMembership(input);

            // Assert
            Assert.Equal(UnitInterval.Create(expected), result);
            Assert.Equal("some_fuzzy_state", fuzzySet.State.Value);
        }
Esempio n. 5
0
        internal void Intersection_VariousInputs_ReturnsExpectedResult(double input, double expected)
        {
            // Arrange
            var function1 = TriangularFunction.Create(1, 3, 5);
            var function2 = TriangularFunction.Create(2, 4, 6, 0, 0.75);
            var fuzzySet1 = new FuzzySet("some_fuzzy_state1", function1);
            var fuzzySet2 = new FuzzySet("some_fuzzy_state2", function2);

            // Act
            var result = fuzzySet1.Intersection(fuzzySet2, input);

            // Assert
            Assert.Equal(UnitInterval.Create(expected), result);
        }
Esempio n. 6
0
 /// <summary>
 /// Returns a <see cref="LinguisticVariable"/> representing fan speed.
 /// </summary>
 /// <returns>
 /// The <see cref="LinguisticVariable"/>.
 /// </returns>
 public static LinguisticVariable PumpSpeed()
 {
     return(new LinguisticVariable(
                InputVariable.PumpSpeed,
                new List <FuzzySet>
     {
         new FuzzySet(TestKit.PumpSpeed.Off, SingletonFunction.Create(0)),
         new FuzzySet(TestKit.PumpSpeed.VeryLow, TrapezoidalFunction.CreateWithLeftEdge(1, 200)),
         new FuzzySet(TestKit.PumpSpeed.Low, TriangularFunction.Create(0, 500, 1000)),
         new FuzzySet(TestKit.PumpSpeed.Moderate, TriangularFunction.Create(500, 1000, 2000)),
         new FuzzySet(TestKit.PumpSpeed.High, TriangularFunction.Create(3000, 3500, 4000)),
         new FuzzySet(TestKit.PumpSpeed.VeryHigh, TrapezoidalFunction.CreateWithRightEdge(3500, 4999)),
         new FuzzySet(TestKit.PumpSpeed.AtLimit, SingletonFunction.Create(5000))
     }));
 }
Esempio n. 7
0
 /// <summary>
 /// Returns a <see cref="LinguisticVariable"/> representing water temperature.
 /// </summary>
 /// <returns>
 /// The <see cref="LinguisticVariable"/>.
 /// </returns>
 public static LinguisticVariable WaterTemp()
 {
     return(new LinguisticVariable(
                InputVariable.WaterTemp,
                new List <FuzzySet>
     {
         new FuzzySet(TestKit.WaterTemp.Frozen, SingletonFunction.Create(0)),
         new FuzzySet(TestKit.WaterTemp.Freezing, TriangularFunction.Create(0, 5, 10)),
         new FuzzySet(TestKit.WaterTemp.Cold, TrapezoidalFunction.Create(5, 10, 15, 20)),
         new FuzzySet(TestKit.WaterTemp.Warm, TrapezoidalFunction.Create(15, 25, 35, 40)),
         new FuzzySet(TestKit.WaterTemp.Hot, TrapezoidalFunction.Create(35, 60, 80, 100)),
         new FuzzySet(TestKit.WaterTemp.Boiling, TrapezoidalFunction.CreateWithRightEdge(95, 100))
     },
                -20,
                200));
 }
        internal void GetMembership_VariousInputs_ReturnsExpectedResult(
            double x1,
            double x2,
            double x3,
            double input,
            double expected)
        {
            // Arrange
            var function = TriangularFunction.Create(x1, x2, x3);

            // Act
            var result = function.GetMembership(input);

            // Assert
            Assert.Equal(UnitInterval.Create(expected), result);
        }
        internal void Defuzzify_WithTriangle_ReturnsExpectedResult()
        {
            // Arrange
            var fuzzySet = new FuzzySet("centre", TriangularFunction.Create(-1, 0, 1));

            var fuzzyOutput = new List <FuzzyOutput>
            {
                new FuzzyOutput(
                    Label.Create("Balance"),
                    fuzzySet,
                    UnitInterval.Create(0.5))
            };

            var centroidDefuzzifier = new CentroidDefuzzifier();

            // Act
            var result = centroidDefuzzifier.Defuzzify(fuzzyOutput);

            // Assert
            Assert.Equal("Balance", result.Subject.Value);
            Assert.Equal(0, result.Value);
        }
        internal void Defuzzify_WhenSubjectsUnrelated_Throws()
        {
            // Arrange
            var fuzzySet = new FuzzySet("centre", TriangularFunction.Create(-1, 0, 1));

            var fuzzyOutput = new List <FuzzyOutput>
            {
                new FuzzyOutput(
                    Label.Create("Subject1"),
                    fuzzySet,
                    UnitInterval.One()),
                new FuzzyOutput(
                    Label.Create("Subject2"),
                    fuzzySet,
                    UnitInterval.One())
            };

            var centroidDefuzzifier = new CentroidDefuzzifier();

            // Act
            // Assert
            Assert.Throws <InvalidOperationException>(() => centroidDefuzzifier.Defuzzify(fuzzyOutput));
        }
Esempio n. 11
0
        internal void RunMamdaniInference(double foodInput, double serviceInput, double expected)
        {
            // Define the input and output linguistic variables.
            var foodQuality = new LinguisticVariable(
                InputVariable.FoodQuality,
                new List <FuzzySet>
            {
                new FuzzySet(FoodQuality.Poor, TrapezoidalFunction.CreateWithLeftEdge(0, 5)),
                new FuzzySet(FoodQuality.Average, TriangularFunction.Create(0, 5, 10)),
                new FuzzySet(FoodQuality.Good, TrapezoidalFunction.CreateWithRightEdge(5, 10))
            });

            var serviceQuality = new LinguisticVariable(
                InputVariable.FoodQuality,
                new List <FuzzySet>
            {
                new FuzzySet(ServiceQuality.Poor, TrapezoidalFunction.CreateWithLeftEdge(0, 5)),
                new FuzzySet(ServiceQuality.Average, TriangularFunction.Create(0, 5, 10)),
                new FuzzySet(ServiceQuality.Good, TrapezoidalFunction.CreateWithRightEdge(5, 10))
            });

            var tipAmount = new LinguisticVariable(
                OutputVariable.TipAmount,
                new List <FuzzySet>
            {
                new FuzzySet(TipAmount.Low, TrapezoidalFunction.CreateWithLeftEdge(0, 13)),
                new FuzzySet(TipAmount.Medium, TriangularFunction.Create(0, 13, 25)),
                new FuzzySet(TipAmount.High, TrapezoidalFunction.CreateWithRightEdge(13, 25))
            });

            // Define the rules for the fuzzy inference engine.
            var rule1 = new FuzzyRuleBuilder(TippingProblem.Rule1)
                        .If(foodQuality.Is(FoodQuality.Poor))
                        .Or(serviceQuality.Is(ServiceQuality.Poor))
                        .Then(tipAmount.Is(TipAmount.Low))
                        .Build();

            var rule2 = new FuzzyRuleBuilder(TippingProblem.Rule2)
                        .If(serviceQuality.Is(ServiceQuality.Average))
                        .Then(tipAmount.Is(TipAmount.Medium))
                        .Build();

            var rule3 = new FuzzyRuleBuilder(TippingProblem.Rule3)
                        .If(foodQuality.Is(FoodQuality.Good))
                        .Or(serviceQuality.Is(ServiceQuality.Good))
                        .Then(tipAmount.Is(TipAmount.High))
                        .Build();

            // Construct the fuzzy inference engine.
            var tnorm       = TriangularNormFactory.MinimumTNorm();
            var tconorm     = TriangularConormFactory.MaximumTConorm();
            var defuzzifier = new CentroidDefuzzifier();
            var fuzzyEngine = new MamdaniInferenceEngine(tnorm, tconorm, defuzzifier);

            // Add the rules to the rulebase.
            fuzzyEngine.Rulebase.AddRule(rule1);
            fuzzyEngine.Rulebase.AddRule(rule2);
            fuzzyEngine.Rulebase.AddRule(rule3);

            // Prepare database to receive inputs.
            fuzzyEngine.Database.AddVariable(Label.Create(InputVariable.FoodQuality));
            fuzzyEngine.Database.AddVariable(Label.Create(InputVariable.ServiceQuality));

            // Generate input data.
            var foodData    = new DataPoint(InputVariable.FoodQuality, foodInput);
            var serviceData = new DataPoint(InputVariable.ServiceQuality, serviceInput);

            // Feed inference engine the data.
            fuzzyEngine.Database.UpdateData(foodData);
            fuzzyEngine.Database.UpdateData(serviceData);

            // Compute the inference engine.
            var result = fuzzyEngine.Compute();

            Assert.Equal(OutputVariable.TipAmount.ToString(), result[0].Subject.Value);
            Assert.Equal(expected, result[0].Value);
        }