Exemple #1
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);
        }
Exemple #2
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));
        }
Exemple #3
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);
        }
Exemple #4
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);
        }
Exemple #5
0
        /// <summary>
        /// Adds a <see cref="Condition"/> to the <see cref="FuzzyRule"/> to be built.
        /// </summary>
        /// <param name="condition">
        /// The condition to be added.
        /// </param>
        /// <returns>
        /// The <see cref="FuzzyRuleBuilder"/>.
        /// </returns>
        public FuzzyRuleBuilder If(ConditionBuilder condition)
        {
            Validate.NotNull(condition, nameof(condition));

            condition.Connective = LogicOperators.If();
            this.conditions.Add(condition.Build());

            return(this);
        }
        internal void ToString_ReturnsExpectedString()
        {
            // Arrange
            var waterTemp = StubLinguisticVariableFactory.WaterTemp();
            var premise   = new Premise(
                LogicOperators.If(),
                waterTemp,
                LogicOperators.Is(),
                FuzzyState.Create(WaterTemp.Cold));

            // Act
            var result = premise.ToString();

            // Assert
            Assert.Equal("IF WaterTemp IS cold", result);
        }
Exemple #7
0
        /// <summary>
        /// The create.
        /// </summary>
        /// <param name="proposition">
        /// The proposition.
        /// </param>
        /// <returns>
        /// The <see cref="ConditionBuilder"/>.
        /// </returns>
        public static ConditionBuilder If(Proposition proposition)
        {
            Utility.Validate.NotNull(proposition, nameof(proposition));

            var conditionBuilder = new ConditionBuilder {
                Connective = LogicOperators.If()
            };

            conditionBuilder.Premises.Add(new Premise(
                                              LogicOperators.If(),
                                              proposition.Variable,
                                              proposition.Evaluator,
                                              proposition.State));

            return(conditionBuilder);
        }
Exemple #8
0
        internal void Evaluate_WithTwoEvaluationsOrConnective_ReturnsExpectedResult()
        {
            // Arrange
            var evaluator = new FuzzyEvaluator();

            var evaluations = new List <Evaluation>
            {
                new Evaluation(LogicOperators.If(), UnitInterval.Create(0.25)),
                new Evaluation(LogicOperators.Or(), UnitInterval.Create(1))
            };

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

            // Assert
            Assert.Equal(UnitInterval.Create(1), result);
        }
Exemple #9
0
        /// <summary>
        /// Returns the final evaluation result of the aggregated collection of evaluations.
        /// </summary>
        /// <param name="evaluations">
        /// The evaluations.
        /// </param>
        /// <returns>
        /// A <see cref="UnitInterval"/>.
        /// </returns>
        public UnitInterval Evaluate(IEnumerable <Evaluation> evaluations)
        {
            var result = UnitInterval.One();

            foreach (var evaluation in evaluations)
            {
                if (evaluation.Connective.Equals(LogicOperators.And()) ||
                    evaluation.Connective.Equals(LogicOperators.If()))
                {
                    result = this.And(result, evaluation.Result);
                }

                if (evaluation.Connective.Equals(LogicOperators.Or()))
                {
                    result = this.Or(result, evaluation.Result);
                }
            }

            return(result);
        }
Exemple #10
0
        private static void ValidateFuzzyRule(IList <Condition> conditions, IList <Conclusion> conclusions)
        {
            Validate.CollectionNotNullOrEmpty(conditions, nameof(conditions));
            Validate.CollectionNotNullOrEmpty(conclusions, nameof(conclusions));

            if (conditions[0].Connective.ToString() != LogicOperators.If().ToString())
            {
                throw new InvalidOperationException(
                          $"Invalid FuzzyRule (the connective of the first condition must be an IF). Value = {conditions[0].Connective}.");
            }

            var remainingConditions = new List <Condition>(conditions);

            remainingConditions.RemoveAt(0);

            if (remainingConditions.Any(conclusion => conclusion.Connective.Equals(LogicOperators.If())))
            {
                throw new InvalidOperationException(
                          $"Invalid FuzzyRule (only the connective of the first condition can be an IF).");
            }
        }