internal void IsConvex_WhenPointsAllDecreasing_ReturnsTrue() { // Arrange var function = TrapezoidalFunction.CreateWithLeftEdge(0, 3); var fuzzySet = new FuzzySet("some_fuzzy_state", function); // Act var result = fuzzySet.IsConvex; // Assert Assert.True(result); }
/// <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)) })); }
internal void CreateWithLeftEdge_GetMembershipWithVariousInputs_ReturnsExpectedResult( double x1, double x2, double input, double expected) { // Arrange var function = TrapezoidalFunction.CreateWithLeftEdge(x1, x2); // Act var result = function.GetMembership(input); // Assert Assert.Equal(UnitInterval.Create(expected), result); }
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); }