Exemplo n.º 1
0
        public void ValueWhenXIsOutOfRange()
        {
            TriangularFunction func = new TriangularFunction(1.0, 2.0);

            Assert.AreEqual(func.Value(0.8), 0);
            Assert.AreEqual(func.Value(2.1), 0);
        }
        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);
        }
        public void IntersectionfFuzzySets()
        {
            TriangularFunction  func1 = new TriangularFunction(1.0, 2.0);
            TrapezoidalFunction func2 = new TrapezoidalFunction(1.0, 2.0, 1.8, 1.5);
            FuzzySet            set1  = new FuzzySet(func1);
            FuzzySet            set2  = new FuzzySet(func2);

            Assert.AreEqual(set1.Intersection(set2, 1.0), 0);
        }
        public void InequalityOfFuzzySets()
        {
            TriangularFunction  func1 = new TriangularFunction(1.0, 2.0);
            TrapezoidalFunction func2 = new TrapezoidalFunction(1.0, 2.0, 1.8, 1.5);
            FuzzySet            set1  = new FuzzySet(func1);
            FuzzySet            set2  = new FuzzySet(func2);

            Assert.IsFalse(set1.Equals(set2, 1.8));
        }
Exemplo n.º 5
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);
        }
Exemplo n.º 6
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);
        }
Exemplo n.º 7
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);
        }
Exemplo n.º 8
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);
        }
Exemplo n.º 9
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))
     }));
 }
        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);
        }
Exemplo n.º 11
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));
 }
 static ScenarioValueFunctions()
 {
     ClearRunAtBaseScenarioValueFunction
         = new ReverseLogisticFunction(leftAsymptoticX: -50, rightAsymptoticX: 30, minAsymptoticY: 0, maxAsymptoticY: 100000);  // one hundred thousand
     LockDownEnemyTankForOtherTankToDestroyValueFunction
         = new ReverseLogisticFunction(leftAsymptoticX: -120, rightAsymptoticX: 120, minAsymptoticY: 0, maxAsymptoticY: 10000); // ten thousand
     AvoidBlockingFriendlyTankFunction
         = new RampFunction(leftX: 4, rightX: 8, minY: -100, maxY: 0);
     // Note that the function falls off very slowly, meaning that there is a slight incentive to stay closer together
     AvoidWalkingIntoABulletFunction
         = new ReverseLogisticFunction(leftAsymptoticX: 0, rightAsymptoticX: 6, minAsymptoticY: 1, maxAsymptoticY: -VALUE_OF_A_TANK);  // fifty thousand
     // slack is number of ticks until bullet collides, 6 ticks is enough time to cross the path of the bullet
     ShootBulletHeadOnFunction
         = new ReverseLogisticFunction(leftAsymptoticX: 0, rightAsymptoticX: 1, minAsymptoticY: 0, maxAsymptoticY: VALUE_OF_A_TANK);  // fifty thousand
     // slack is number of ticks until bullet collides
     DodgeBulletFunction
         = new ReverseLogisticFunction(leftAsymptoticX: 0, rightAsymptoticX: 1, minAsymptoticY: 0, maxAsymptoticY: VALUE_OF_A_TANK);  // fifty thousand
     // slack is number of ticks till reaching a survival point less number of ticks until bullet collides
     ProlongEnemyDisarmamentFunction
         = new TriangularFunction(startX: -500, modeX: -81, endX: 0, maxY: 30000); // thirty thousand
     // use this to fake a linear decline, rather than the logistic function decline
     // was: ReverseLogisticFunction(leftAsymptoticX: -80, rightAsymptoticX: 0, minAsymptoticY: 0, maxAsymptoticY: 30000);
     AttackEnemyBaseFunction
         = new ReverseLogisticFunction(leftAsymptoticX: 0, rightAsymptoticX: 300, minAsymptoticY: 0, maxAsymptoticY: 100000);  // hundred thousand
     // this is a low value, designed purely to get the tanks away from their base and into the game
     AttackEnemyBaseAttackActionFunction
         = new ReverseLogisticFunction(leftAsymptoticX: 0, rightAsymptoticX: 300, minAsymptoticY: 0, maxAsymptoticY: 30000);  // thirty thousand
     // This function boosts the action along the calculated shortest path.
     // This helps to break deadlocks between adjacent choices, probably caused by logistic curve not being granular enough
     GrappleWithEnemyTankAttackDiffFunction
         = new ReverseLogisticFunction(leftAsymptoticX: -5, rightAsymptoticX: -1, minAsymptoticY: 0, maxAsymptoticY: 20000);  // ten thousand
     GrappleWithEnemyTankAttackFunction
         = new ReverseLogisticFunction(leftAsymptoticX: 0, rightAsymptoticX: 40, minAsymptoticY: 0, maxAsymptoticY: 5000);
     GrappleWithEnemyTankAttackActionFunction
         = new ReverseLogisticFunction(leftAsymptoticX: 0, rightAsymptoticX: 4, minAsymptoticY: 0, maxAsymptoticY: 10000);
     AttackDisarmedEnemyTankSlackUntilRearmedFunction
         = new ReverseLogisticFunction(leftAsymptoticX: -25, rightAsymptoticX: 0, minAsymptoticY: 0, maxAsymptoticY: 30000);     // thirty thousand
     AttackDisarmedEnemyTankAttackActionFunction
         = new ReverseLogisticFunction(leftAsymptoticX: -25, rightAsymptoticX: 0, minAsymptoticY: 0, maxAsymptoticY: 5000);      // five thousand
     AttackLockedDownEnemyTankFunction
         = new ReverseLogisticFunction(leftAsymptoticX: 0, rightAsymptoticX: 200, minAsymptoticY: 10000, maxAsymptoticY: 40000); // ten thousand to forty thousand
 }
        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));
        }
Exemplo n.º 15
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);
        }
Exemplo n.º 16
0
        public void RangeWhenAandBAreOfDifferentSigns()
        {
            TriangularFunction func = new TriangularFunction(-1.0, 2.0);

            Assert.AreEqual(func.Range(), 3.0);
        }
Exemplo n.º 17
0
        public void ValueWhenXIsOnTheEdgeOfTheFunction()
        {
            TriangularFunction func = new TriangularFunction(1.0, 2.0);

            Assert.AreEqual(func.Value(2.0), 0);
        }
Exemplo n.º 18
0
        public void ValueWhenXIsIsInTheMiddleOfTheTriangle()
        {
            TriangularFunction func = new TriangularFunction(1.0, 2.0);

            Assert.AreEqual(func.Value(1.5), 1);
        }
Exemplo n.º 19
0
        public void ValueWhenXIsIsBetweenCandB()
        {
            TriangularFunction func = new TriangularFunction(0.0, 4.0);

            Assert.AreEqual(func.Value(3.0), 0.5);
        }
Exemplo n.º 20
0
        public void HeightDefaultsTo1()
        {
            TriangularFunction func = new TriangularFunction(0.0, 4.0);

            Assert.AreEqual(func.GetHeight(), 1.0);
        }
Exemplo n.º 21
0
        public void AllowsToChangeHeight()
        {
            TriangularFunction func = new TriangularFunction(0.0, 4.0, 0.5);

            Assert.AreEqual(func.GetHeight(), 0.5);
        }
Exemplo n.º 22
0
        public void RangeWhenAandBAreNegative()
        {
            TriangularFunction func = new TriangularFunction(-1.0, -2.0);

            Assert.AreEqual(func.Range(), 1.0);
        }