示例#1
0
        internal void Evaluate_WithVariousValidValues_ReturnsExpectedResult(
            double membershipA,
            double membershipB,
            double expected)
        {
            // Arrange
            var maximumTConorm = TriangularConormFactory.MaximumTConorm();

            // Act
            var result = maximumTConorm.Evaluate(UnitInterval.Create(membershipA), UnitInterval.Create(membershipB));

            // Assert
            Assert.Equal(UnitInterval.Create(expected), result);
        }
示例#2
0
        internal void Or_WhenFunctionsDefault_ReturnsExpectedResult()
        {
            // Arrange
            var tNorm   = TriangularNormFactory.MinimumTNorm();
            var tConorm = TriangularConormFactory.MaximumTConorm();

            var evaluator = new FuzzyEvaluator(tNorm, tConorm);

            var membershipA = UnitInterval.Create(0.25);
            var membershipB = UnitInterval.Create(0.5);

            // Act
            var result = evaluator.Or(membershipA, membershipB);

            // Assert
            Assert.Equal(UnitInterval.Create(0.5), result);
        }
示例#3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="FuzzyEvaluator"/> class.
 /// </summary>
 public FuzzyEvaluator()
 {
     this.triangularNorm   = TriangularNormFactory.MinimumTNorm();
     this.triangularConorm = TriangularConormFactory.MaximumTConorm();
 }
示例#4
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);
        }