예제 #1
0
        internal void Evaluate_WithThreeGroupCompoundStatement_ReturnsExpectedResult()
        {
            // Arrange
            var evaluator = new FuzzyEvaluator();

            var evaluations = new List <Evaluation>
            {
                // Statement group 1 (assumed to evaluate to 0).
                new Evaluation(LogicOperators.If(), UnitInterval.Create(0.25)),
                new Evaluation(LogicOperators.And(), UnitInterval.Create(0.0)),
                new Evaluation(LogicOperators.And(), UnitInterval.Create(0.2)),

                // Statement group 2 (assumed to evaluate to 0.8).
                new Evaluation(LogicOperators.Or(), UnitInterval.Create(1)),
                new Evaluation(LogicOperators.And(), UnitInterval.Create(0.9)),
                new Evaluation(LogicOperators.And(), UnitInterval.Create(0.8)),

                // Statement group 3 (assumed to evaluate to 0.9).
                new Evaluation(LogicOperators.Or(), UnitInterval.Create(0.9)),
                new Evaluation(LogicOperators.And(), UnitInterval.Create(1)),
                new Evaluation(LogicOperators.And(), UnitInterval.Create(1))
            };

            // Act
            var result = evaluator.Evaluate(evaluations);

            // Assert
            Assert.Equal(UnitInterval.Create(0.9), result);
        }
예제 #2
0
        internal void Evaluate_WithThreeGroupCompoundStatementAndEinsteinFunctions_ReturnsExpectedResult()
        {
            // Arrange
            var tnorm   = TriangularNormFactory.EinsteinProduct();
            var tconorm = TriangularConormFactory.EinsteinSum();

            var evaluator = new FuzzyEvaluator(tnorm, tconorm);

            var evaluations = new List <Evaluation>
            {
                // Statement group 1
                new Evaluation(LogicOperators.If(), UnitInterval.Create(0.25)),
                new Evaluation(LogicOperators.And(), UnitInterval.Create(0.0)),
                new Evaluation(LogicOperators.And(), UnitInterval.Create(0.2)),

                // Statement group 2
                new Evaluation(LogicOperators.Or(), UnitInterval.Create(1)),
                new Evaluation(LogicOperators.And(), UnitInterval.Create(0.9)),
                new Evaluation(LogicOperators.And(), UnitInterval.Create(0.8)),

                // Statement group 3
                new Evaluation(LogicOperators.Or(), UnitInterval.Create(0.5)),
                new Evaluation(LogicOperators.And(), UnitInterval.Create(1)),
                new Evaluation(LogicOperators.And(), UnitInterval.Create(1))
            };

            // Act
            var result = evaluator.Evaluate(evaluations);

            // Assert
            Assert.Equal(0.891304347826087, result.Value);
        }
예제 #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Evaluation"/> struct.
        /// </summary>
        /// <param name="connective">
        /// The connective.
        /// </param>
        /// <param name="result">
        /// The result.
        /// </param>
        public Evaluation(IConnectiveOperator connective, UnitInterval result)
        {
            Validate.NotNull(connective, nameof(connective));

            this.Connective = connective;
            this.Result     = result;
        }
예제 #4
0
        internal void Evaluate_WithThreeGroupCompoundStatementAndOtherFunctions_ReturnsExpectedResult()
        {
            // Arrange
            var tnorm   = TriangularNormFactory.Lukasiewicz();
            var tconorm = TriangularConormFactory.ProbabilisticSum();

            var evaluator = new FuzzyEvaluator(tnorm, tconorm);

            var evaluations = new List <Evaluation>
            {
                // Statement group 1
                new Evaluation(LogicOperators.If(), UnitInterval.Create(0.25)),
                new Evaluation(LogicOperators.And(), UnitInterval.Create(0.0)),
                new Evaluation(LogicOperators.And(), UnitInterval.Create(0.2)),

                // Statement group 2
                new Evaluation(LogicOperators.Or(), UnitInterval.Create(1)),
                new Evaluation(LogicOperators.And(), UnitInterval.Create(0.9)),
                new Evaluation(LogicOperators.And(), UnitInterval.Create(0.8)),

                // Statement group 3
                new Evaluation(LogicOperators.Or(), UnitInterval.Create(0.5)),
                new Evaluation(LogicOperators.And(), UnitInterval.Create(1)),
                new Evaluation(LogicOperators.And(), UnitInterval.Create(1))
            };

            // Act
            var result = evaluator.Evaluate(evaluations);

            // Assert
            Assert.Equal(0.85, Math.Round(result.Value, 2));
        }
예제 #5
0
        /// <summary>
        /// Returns the intersection of the membership value (the minimum membership value of the fuzzy sets).
        /// </summary>
        /// <param name="other">
        /// The other.
        /// </param>
        /// <param name="x">
        /// The x.
        /// </param>
        /// <returns>
        /// The <see cref="double"/>.
        /// </returns>
        public UnitInterval Intersection(FuzzySet other, double x)
        {
            Validate.NotNull(other, nameof(other));
            Validate.NotOutOfRange(x, nameof(x));

            return(UnitInterval.Create(Math.Min(this.function.GetMembership(x).Value, other.GetMembership(x).Value)));
        }
예제 #6
0
        /// <summary>
        /// Evaluates the fuzzy condition.
        /// </summary>
        /// <param name="data">
        /// The data.
        /// </param>
        /// <param name="evaluator">
        /// The evaluator.
        /// </param>
        /// <returns>
        /// A <see cref="double"/>.
        /// </returns>
        /// <exception cref="ArgumentException">
        /// Throws if provided data does not contain all subjects.
        /// </exception>
        public Evaluation Evaluate(
            IReadOnlyDictionary <Label, DataPoint> data,
            FuzzyEvaluator evaluator)
        {
            Validate.NotNull(data, nameof(data));
            Validate.NotNull(evaluator, nameof(evaluator));

            var evaluations = new List <Evaluation>();

            foreach (var premise in this.Premises)
            {
                if (data.ContainsKey(premise.Subject))
                {
                    var input = data[premise.Subject];

                    evaluations.Add(premise.Evaluate(input));
                }
                else
                {
                    throw new InvalidOperationException(
                              $"Evaluation Failed (cannot evaluate premise '{premise.Subject}' with provided data).");
                }
            }

            return(new Evaluation(this.Connective, UnitInterval.Create(evaluator.Evaluate(evaluations) * this.Weight)));
        }
예제 #7
0
        internal void Evaluate_ValidRuleAndConditions_ReturnsExpectedFuzzyOutput()
        {
            // Arrange
            var waterTemp = StubLinguisticVariableFactory.WaterTemp();
            var fanSpeed  = StubLinguisticVariableFactory.PumpSpeed();

            var fuzzyRule = new FuzzyRuleBuilder(PumpSpeedRule.Rule0)
                            .If(ConditionBuilder.If(waterTemp.Is(WaterTemp.Frozen)))
                            .Or(ConditionBuilder.If(waterTemp.Is(WaterTemp.Freezing)))
                            .Then(fanSpeed.Is(PumpSpeed.Off))
                            .Build();

            var dataPoint = new DataPoint(waterTemp.Subject, 0);

            var data = new Dictionary <Label, DataPoint> {
                { dataPoint.Variable, dataPoint }
            };

            // Act
            var result = fuzzyRule.Evaluate(data, new FuzzyEvaluator());

            // Assert
            Assert.Equal(fanSpeed.Subject, result[0].Subject);
            Assert.Equal(fanSpeed.GetState(0), result[0].State);
            Assert.Equal(UnitInterval.One(), result[0].FiringStrength);
            Assert.Equal(fanSpeed.GetSet(PumpSpeed.Off), result[0].OutputFunction);
        }
예제 #8
0
        internal void Evaluate_WithFourGroupCompoundStatementThenConnectedByMultipleOrs_ReturnsExpectedResult()
        {
            // Arrange
            var evaluator = new FuzzyEvaluator();

            var evaluations = new List <Evaluation>
            {
                // Statement group 1 (assumed to evaluate to 0.9).
                new Evaluation(LogicOperators.If(), UnitInterval.Create(1)),
                new Evaluation(LogicOperators.And(), UnitInterval.Create(1)),
                new Evaluation(LogicOperators.And(), UnitInterval.Create(0.9)),

                // Statement group 2 (assumed to evaluate to 0.8).
                new Evaluation(LogicOperators.Or(), UnitInterval.Create(1)),
                new Evaluation(LogicOperators.And(), UnitInterval.Create(0.9)),
                new Evaluation(LogicOperators.And(), UnitInterval.Create(0.8)),

                // Statement group 3 (assumed to evaluate to 0.7).
                new Evaluation(LogicOperators.Or(), UnitInterval.Create(0.7)),
                new Evaluation(LogicOperators.And(), UnitInterval.Create(0.7)),
                new Evaluation(LogicOperators.And(), UnitInterval.Create(0.7)),

                // Then multiple or connectives (assumed to evaluate to 1).
                new Evaluation(LogicOperators.Or(), UnitInterval.Create(0.8)),
                new Evaluation(LogicOperators.Or(), UnitInterval.Create(0.9)),
                new Evaluation(LogicOperators.Or(), UnitInterval.Create(1))
            };

            // Act
            var result = evaluator.Evaluate(evaluations);

            // Assert
            Assert.Equal(UnitInterval.Create(1), result);
        }
        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);
        }
예제 #10
0
        /// <summary>
        /// Initializes a new instance of the <see cref="FuzzyPoint"/> struct.
        /// </summary>
        /// <param name="x">
        /// The X co-ordinate.
        /// </param>
        /// <param name="y">
        /// The Y co-ordinate.
        /// </param>
        public FuzzyPoint(double x, double y)
        {
            Validate.NotOutOfRange(x, nameof(x));
            Validate.NotOutOfRange(y, nameof(y), 0, 1);

            this.X = x;
            this.Y = UnitInterval.Create(y);
        }
예제 #11
0
        /// <summary>
        /// The with weight.
        /// </summary>
        /// <param name="weight">
        /// The weight.
        /// </param>
        /// <returns>
        /// The <see cref="ConditionBuilder"/>.
        /// </returns>
        public ConditionBuilder WithWeight(double weight)
        {
            Utility.Validate.NotOutOfRange(weight, nameof(weight), 0, 1);

            this.Weight = UnitInterval.Create(weight);

            return(this);
        }
예제 #12
0
 public UnitInterval Evaluate(UnitInterval membershipA, UnitInterval membershipB)
 {
     return((membershipA + membershipB) - (membershipA * membershipB) > 0
         ? UnitInterval.Create(
                (membershipA * membershipB)
                / ((membershipA + membershipB) - (membershipA * membershipB)))
         : UnitInterval.Zero());
 }
예제 #13
0
 public UnitInterval Evaluate(UnitInterval membershipA, UnitInterval membershipB)
 {
     return(1 - (membershipA * membershipB) > 0
         ? UnitInterval.Create(
                ((membershipA + membershipB) - (2 * (membershipA * membershipB)))
                / (1 - (membershipA * membershipB)))
         : UnitInterval.One());
 }
예제 #14
0
        internal void Zero_ReturnsUnitIntervalWithValueZero()
        {
            // Arrange
            // Act
            var result = UnitInterval.Zero();

            // Assert
            Assert.Equal(0, result.Value);
        }
예제 #15
0
        internal void One_ReturnsUnitIntervalWithValueOne()
        {
            // Arrange
            // Act
            var result = UnitInterval.One();

            // Assert
            Assert.Equal(1, result.Value);
        }
        internal void GetMembership_ReturnsExpectedDouble(double input, Enum label, double membershipValue)
        {
            // Arrange
            var temperature = StubLinguisticVariableFactory.WaterTemp();

            // Act
            var result = temperature.GetMembership(label, input);

            // Assert
            Assert.Equal(UnitInterval.Create(membershipValue), result);
        }
예제 #17
0
        internal void Evaluate_WithVariousValues_ReturnsExpectedResult(double membership, double expected)
        {
            // Arrange
            var isOperator = LogicOperators.Is();

            // Act
            var result = isOperator.Evaluate(UnitInterval.Create(membership));

            // Assert
            Assert.Equal(UnitInterval.Create(expected), result);
        }
        internal void MaxY_ReturnsExpectedResult()
        {
            // Arrange
            var function = SingletonFunction.Create(1);

            // Act
            var result = function.MaxY;

            // Assert
            Assert.Equal(UnitInterval.One(), result);
        }
예제 #19
0
        internal void ToString_ReturnsExpectedResults()
        {
            // Arrange
            // Act
            var result1 = UnitInterval.Zero().ToString();
            var result2 = UnitInterval.One().ToString();

            // Assert
            Assert.Equal("0", result1);
            Assert.Equal("1", result2);
        }
        internal void GetMembership_VariousInputs_ReturnsExpectedResult(double x, double input, double expected)
        {
            // Arrange
            var function = SingletonFunction.Create(x);

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

            // Assert
            Assert.Equal(UnitInterval.Create(expected), result);
        }
예제 #21
0
        internal void Equals_WhenValueNull_ReturnsFalse()
        {
            // Arrange
            var unitInterval = UnitInterval.One();

            // Act
            var result = unitInterval.Equals(null);

            // Assert
            Assert.False(result);
        }
예제 #22
0
        internal void GetHashCode_ReturnsExpectedResults()
        {
            // Arrange
            // Act
            var result1 = UnitInterval.Zero().GetHashCode();
            var result2 = UnitInterval.One().GetHashCode();

            // Assert
            Assert.Equal(0, result1);
            Assert.Equal(1072693248, result2);
        }
예제 #23
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);
        }
예제 #24
0
        /// <summary>
        /// Initializes a new instance of the <see cref="FuzzyOutput"/> struct.
        /// </summary>
        /// <param name="subject">
        /// The subject.
        /// </param>
        /// <param name="outputFunction">
        /// The output function.
        /// </param>
        /// <param name="firingStrength">
        /// The firing strength [0, 1].
        /// </param>
        public FuzzyOutput(
            Label subject,
            FuzzySet outputFunction,
            UnitInterval firingStrength)
        {
            Validate.NotNull(subject, nameof(subject));
            Validate.NotNull(firingStrength, nameof(firingStrength));
            Validate.NotNull(outputFunction, nameof(outputFunction));

            this.Subject        = subject;
            this.OutputFunction = outputFunction;
            this.FiringStrength = firingStrength;
        }
예제 #25
0
        internal void Equals_WhenValueNotEqual_ReturnsFalse()
        {
            // Arrange
            var unitInterval = UnitInterval.One();

            // Act
            var result1 = unitInterval.Equals(UnitInterval.Zero());
            var result2 = unitInterval.Equals(0);

            // Assert
            Assert.False(result1);
            Assert.False(result2);
        }
예제 #26
0
        internal void Equals_WhenValueEqual_ReturnsTrue()
        {
            // Arrange
            var unitInterval = UnitInterval.One();

            // Act
            var result1 = unitInterval.Equals(UnitInterval.One());
            var result2 = unitInterval.Equals(1.0);

            // Assert
            Assert.True(result1);
            Assert.True(result2);
        }
예제 #27
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);
        }
예제 #28
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Condition"/> class.
        /// </summary>
        /// <param name="connective">
        /// The connective.
        /// </param>
        /// <param name="premises">
        /// The premises.
        /// </param>
        /// <param name="weight">
        /// The weight.
        /// </param>
        /// <returns>
        /// The <see cref="Condition"/>.
        /// </returns>
        public Condition(
            IConnectiveOperator connective,
            IList <Premise> premises,
            UnitInterval weight)
        {
            Validate.NotNull(connective, nameof(connective));
            Validate.CollectionNotNullOrEmpty(premises, nameof(premises));
            Validate.NotNull(weight, nameof(weight));

            this.Connective = connective;
            this.Premises   = premises;
            this.Weight     = weight;
        }
예제 #29
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);
        }
예제 #30
0
        internal void Evaluate_WithVariousValidValues_ReturnsExpectedResult(
            double membershipA,
            double membershipB,
            double expected)
        {
            // Arrange
            var algebraicProduct = TriangularNormFactory.AlgebraicProduct();

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

            // Assert
            Assert.Equal(UnitInterval.Create(expected), result);
        }