Exemple #1
0
        public void T6Implicate()
        {
            double axis = 0;

            LinguisticVariable =
                LinguisticVariable.fromJson(JsonLingVar);
            LinguisticVariable.RangeCalibration(1, 0.01);
            ExternalLVSetUp();
            string LogMsg = "[Implicate Test Result]\n==================================\n";
            string TmpLog;

            LinguisticVariable.ApplyRule(LingVars);
            LinguisticVariable.Implicate(1);
            foreach (LinguisticRule rule in LinguisticVariable.linguisticRules)
            {
                TmpLog  = "Linguistic : " + rule.membershipValue.linguistic + "\n";
                TmpLog += "Implication Method : " + FuzzyImplication.nameOf(rule.implicationM) + "\n";
                TmpLog += "Axis\t| Implication\n";
                axis    = rule.implData.StartAxis;
                foreach (double implRes in rule.implData.data)
                {
                    TmpLog += axis + "\t| " + implRes + "\n";
                    axis   += rule.implData.spacing;
                }
                LogMsg += TmpLog + "==================================\n";
                TmpLog  = "";
            }
            Debug.Log(LogMsg);
        }
Exemple #2
0
        /*
         * public JSONObject encodeCompleteJson()
         * {
         *  JSONObject encoded = this.encodeLinguisticJson();
         *  encoded.AddField("Rule", Eval.double2Float(this.fuzzyValue));
         *  return encoded;
         * }
         */
        public JSONObject encodeLinguisticJson()
        {
            JSONObject encoded = new JSONObject(JSONObject.Type.OBJECT);

            encoded.AddField("Value", this.membershipValue.linguistic);
            encoded.AddField("Operator", FuzzyOperator.nameOf(this.fOperator));
            encoded.AddField("Implication",
                             FuzzyImplication.nameOf(this.implicationM));
            encoded.AddField("Rule", this.rule);
            return(encoded);
        }
Exemple #3
0
        public void setup()
        {
            TestJsonRule =
                @"
{
    ""Value"" : """ + testRuleValue + @""",
    ""Operator"" : """ + FuzzyOperator.nameOf(TestOperator) + @""",
    ""Implication"" : """ + FuzzyImplication.nameOf(TestImplication) + @""",
    ""Rule"" : """ + testActualRule + @"""
}
";
        }
        public void Constructor()
        {
            var num1 = new NumericVariable("Variable1");
            var num2 = new NumericVariable("Variable2");
            var term1 = new FuzzyTerm("Var1_Term1", new MembershipFunction());
            var term2 = new FuzzyTerm("Var2_Term1", new MembershipFunction());
            var var1 = new FuzzyVariable("Variable1", num1, term1);
            var var2 = new FuzzyVariable("Variable2", num2, term2);
            var rule1 = new FuzzyImplication(new ValueExpression(var1, term1), new ValueExpression(var1, term1));
            var rule2 = new FuzzyImplication(new ValueExpression(var2, term2), new ValueExpression(var2, term2));

            var sut = new Iteration(new[] { rule1, rule2 });
            Assert.AreEqual(rule1, sut.Implications[0]);
            Assert.AreEqual(rule2, sut.Implications[1]);
        }
        public void Constructor()
        {
            var termA = new FuzzyTerm("TermA", new MembershipFunction());
            var termB = new FuzzyTerm("TermB", new MembershipFunction());
            var varA = new FuzzyVariable("Variable A", null, termA);
            var varB = new FuzzyVariable("Variable B", null, termB);

            var exprA = new ValueExpression(varA, termA);
            var exprB = new ValueExpression(varB, termB);

            var sut = new FuzzyImplication(exprA, exprB);

            Assert.AreEqual(exprA, sut.Premise);
            Assert.AreEqual(exprB, sut.Conclusion);
        }
Exemple #6
0
        // JSON Encode and Decode //
        public static LinguisticRule fromJson(string JsonData)
        {
            JSONObject     LRJSO  = new JSONObject(JsonData);
            LinguisticRule Result = new LinguisticRule(
                LRJSO.GetField("Value").str,
                LRJSO.GetField("Rule").str);

            Result.implicationMethod =
                FuzzyImplication.TryParse(
                    LRJSO.GetField("Implication").str);
            Result._operator =
                FuzzyOperator.TryParse(
                    LRJSO.GetField("Operator").str);
            return(Result);
        }
Exemple #7
0
 public void T4LinguisticEncode()
 {
     testRule = LinguisticRule.fromJson(TestJsonRule);
     Assert.AreEqual(
         testRuleValue,
         testRule.encodeLinguisticJson().GetField("Value").str
         );
     Assert.AreEqual(
         TestImplication,
         FuzzyImplication.TryParse(
             testRule.encodeLinguisticJson().
             GetField("Implication").str)
         );
     Assert.AreEqual(
         FuzzyOperator.nameOf(TestOperator),
         testRule.encodeLinguisticJson().
         GetField("Operator").str
         );
     Assert.AreEqual(
         testActualRule,
         testRule.encodeLinguisticJson().GetField("Rule").str
         );
 }
        public void Accept()
        {
            var termA = new FuzzyTerm("TermA", new MembershipFunction());
            var termB = new FuzzyTerm("TermB", new MembershipFunction());
            var varA = new FuzzyVariable("Variable A", null, termA);
            var varB = new FuzzyVariable("Variable B", null, termB);

            var mocks = new MockRepository();

            var visitor = mocks.StrictMock<IExpressionVisitor<int>>();

            var sut = new FuzzyImplication(new ValueExpression(varA, termA), new ValueExpression(varB, termB));

            Expect.Call(visitor.Visit(sut)).Return(42);

            mocks.ReplayAll();

            var result = sut.Accept(visitor);

            Assert.AreEqual(42, result);

            mocks.VerifyAll();
        }
        public void Apply_Many_Implication_With_Value_Override(
            [Values("Algebraic", "Drastic", "Einstein", "Hamacher", "Lukasiewicz", "MinMax")] string evalStrategyName,
            [Values("Average", "Max", "Min", "Sum")] string mergeStrategy)
        {
            var evaluationStrategy = createEvaluationStrategy(evalStrategyName);
            var merger = createFuzzyValueMerger(mergeStrategy);

            var termA1 = new FuzzyTerm("TermA1", new MembershipFunction());
            var termA2 = new FuzzyTerm("TermA1", new MembershipFunction());
            var varA = new FuzzyVariable("Variable A", null, termA1, termA2);

            var value = new FuzzyValue(varA, new Dictionary<FuzzyTerm, double> { { termA1, 0.2 }, { termA2, 0.6 } });

            var sut = new RuleEvaluation(evaluationStrategy, merger) { InputScope = new Scope(value) };

            var impl1 = new FuzzyImplication(new AndExpression(new ValueExpression(varA, termA1), new ValueExpression(varA, termA2)), new ValueExpression(varA, termA1));

            var result = sut.Apply(new Scope(value), new[] { impl1 });

            var val = evaluationStrategy.And(0.2, 0.6);

            Assert.AreEqual(1, result.Count);
            Assert.AreEqual(varA, result[0].AssociatedVariable);
            Assert.AreEqual(2, result[0].Values.Count);
            // A = A1 got overridden:
            Assert.AreEqual(val, result[0].Values[termA1]);
            // A = A2 stays the same:
            Assert.AreEqual(0.6, result[0].Values[termA2]);
        }
        public void Apply_Many_Implication_With_Value_Merging(
            [Values("Algebraic", "Drastic", "Einstein", "Hamacher", "Lukasiewicz", "MinMax")] string evalStrategyName,
            [Values("Average", "Max", "Min", "Sum")] string mergeStrategy)
        {
            var evaluationStrategy = createEvaluationStrategy(evalStrategyName);
            var merger = createFuzzyValueMerger(mergeStrategy);

            var termA1 = new FuzzyTerm("TermA1", new MembershipFunction());
            var termA2 = new FuzzyTerm("TermA1", new MembershipFunction());
            var termB1 = new FuzzyTerm("TermB1", new MembershipFunction());
            var varA = new FuzzyVariable("Variable A", null, termA1, termA2);
            var varB = new FuzzyVariable("Variable B", null, termB1);

            var value = new FuzzyValue(varA, new Dictionary<FuzzyTerm, double> { { termA1, 0.5 }, { termA2, 0.6 } });

            var sut = new RuleEvaluation(evaluationStrategy, merger) { InputScope = new Scope(value) };

            var impl1 = new FuzzyImplication(new ValueExpression(varA, termA1), new ValueExpression(varB, termB1));
            var impl2 = new FuzzyImplication(new ValueExpression(varA, termA2), new ValueExpression(varB, termB1));

            var result = sut.Apply(new Scope(value), new[] { impl1, impl2 });

            var mergedValue = merger.Apply(new FuzzyValue[]
            {
                new FuzzyValue(varB, new Dictionary<FuzzyTerm, double> { { termB1, 0.5 } }),
                new FuzzyValue(varB, new Dictionary<FuzzyTerm, double> { { termB1, 0.6 } })
            });

            Assert.AreEqual(2, result.Count);
            Assert.AreEqual(varA, result[0].AssociatedVariable);
            Assert.AreEqual(2, result[0].Values.Count);
            Assert.AreEqual(0.5, result[0].Values[termA1]);
            Assert.AreEqual(0.6, result[0].Values[termA2]);


            Assert.AreEqual(varB, result[1].AssociatedVariable);
            Assert.AreEqual(1, result[1].Values.Count);
            Assert.AreEqual(mergedValue[0].Values[termB1], result[1].Values[termB1]);
        }
        public void Apply_Many_Implication(
            [Values("Algebraic", "Drastic", "Einstein", "Hamacher", "Lukasiewicz", "MinMax")] string evalStrategyName,
            [Values("Average", "Max", "Min", "Sum")] string mergeStrategy)
        {
            var evaluationStrategy = createEvaluationStrategy(evalStrategyName);
            var merger = createFuzzyValueMerger(mergeStrategy);

            var termA1 = new FuzzyTerm("TermA1", new MembershipFunction());
            var termA2 = new FuzzyTerm("TermA1", new MembershipFunction());
            var termB1 = new FuzzyTerm("TermB1", new MembershipFunction());
            var termB2 = new FuzzyTerm("TermB2", new MembershipFunction());
            var varA = new FuzzyVariable("Variable A", null, termA1, termA2);
            var varB = new FuzzyVariable("Variable B", null, termB1, termB2);

            var value = new FuzzyValue(varA, new Dictionary<FuzzyTerm, double> { { termA1, 0.4 }, { termA2, 0.6 } });

            var sut = new RuleEvaluation(evaluationStrategy, merger);

            var impl1 = new FuzzyImplication(new ValueExpression(varA, termA1), new ValueExpression(varB, termB1));
            var impl2 = new FuzzyImplication(new ValueExpression(varA, termA2), new ValueExpression(varB, termB2));

            var result = sut.Apply(new Scope(value), new[] {impl1, impl2}).ToList();

            Assert.AreEqual(2, result.Count);
            var valA = result.Find(val => val.AssociatedVariable.Equals(varA));
            Assert.IsNotNull(valA);
            Assert.AreEqual(2, valA.Values.Count);
            Assert.AreEqual(0.4, valA.Values[termA1]);
            Assert.AreEqual(0.6, valA.Values[termA2]);

            var valB = result.Find(val => val.AssociatedVariable.Equals(varB));
            Assert.IsNotNull(valB);
            Assert.AreEqual(2, valB.Values.Count);
            Assert.AreEqual(0.4, valB.Values[termB1]);
            Assert.AreEqual(0.6, valB.Values[termB2]);
        }
        public void ComplexExpression()
        {
            var A = new FuzzyVariable("A", new NumericVariable("a"));
            var B = new FuzzyVariable("B", new NumericVariable("b"));
            var C = new FuzzyVariable("C", new NumericVariable("c"));

            var x = new FuzzyTerm("x", new MembershipFunction());
            var y = new FuzzyTerm("y", new MembershipFunction());
            var z = new FuzzyTerm("z", new MembershipFunction());


            var ruleExpr = new FuzzyImplication(
                new OrExpression(
                    new NotExpression(
                        new AndExpression(
                            new ValueExpression(A, x),
                            new ValueExpression(B, y)
                        )
                    ),
                    new AndExpression(
                        new ValueExpression(A, y),
                        new ValueExpression(B, x)
                    )
                ),
                new ValueExpression(C, z)
            );

            var sut = new GetInvolvedVariables();

            var result = sut.Visit(ruleExpr);
            Assert.AreEqual(3, result.Count);
            Assert.IsTrue(result.Contains(A));
            Assert.IsTrue(result.Contains(B));
            Assert.IsTrue(result.Contains(C));

            result = ruleExpr.Accept(sut);
            Assert.AreEqual(3, result.Count);
            Assert.IsTrue(result.Contains(A));
            Assert.IsTrue(result.Contains(B));
            Assert.IsTrue(result.Contains(C));
        }
        public void VisitRule()
        {
            var variable1 = new FuzzyVariable("MyFuzzyVariable1", new NumericVariable("MyNumVariable1"));
            var variable2 = new FuzzyVariable("MyFuzzyVariable2", new NumericVariable("MyNumVariable2"));
            var valueExpr1 =
                new ValueExpression(
                    variable1,
                    new FuzzyTerm("MyTerm1", new MembershipFunction()));
            var valueExpr2 =
                new ValueExpression(
                    variable2,
                    new FuzzyTerm("MyTerm2", new MembershipFunction()));

            var ruleExpr = new FuzzyImplication(valueExpr1, valueExpr2);

            var sut = new GetInvolvedVariables();

            var result = sut.Visit(ruleExpr);
            Assert.AreEqual(2, result.Count);
            Assert.IsTrue(result.Contains(variable1));
            Assert.IsTrue(result.Contains(variable2));

            result = ruleExpr.Accept(sut);
            Assert.AreEqual(2, result.Count);
            Assert.IsTrue(result.Contains(variable1));
            Assert.IsTrue(result.Contains(variable2));
        }
        public void VisitRule()
        {
            var valueExpr1 =
                new ValueExpression(
                    new FuzzyVariable("MyFuzzyVariable1", new NumericVariable("MyNumVariable1")),
                    new FuzzyTerm("MyTerm1", new MembershipFunction()));
            var valueExpr2 =
                new ValueExpression(
                    new FuzzyVariable("MyFuzzyVariable2", new NumericVariable("MyNumVariable2")),
                    new FuzzyTerm("MyTerm2", new MembershipFunction()));

            var ruleExpr = new FuzzyImplication(valueExpr1, valueExpr2);

            var sut = new ToStringVisitor();

            var result = sut.Visit(ruleExpr);
            Assert.AreEqual("MyFuzzyVariable1=MyTerm1 => MyFuzzyVariable2=MyTerm2", result);

            result = ruleExpr.Accept(sut);
            Assert.AreEqual("MyFuzzyVariable1=MyTerm1 => MyFuzzyVariable2=MyTerm2", result);
        }
        public void ComplexExpression()
        {
            var A = new FuzzyVariable("A", new NumericVariable("a"));
            var B = new FuzzyVariable("B", new NumericVariable("b"));
            var C = new FuzzyVariable("C", new NumericVariable("c"));

            var x = new FuzzyTerm("x", new MembershipFunction());
            var y = new FuzzyTerm("y", new MembershipFunction());
            var z = new FuzzyTerm("z", new MembershipFunction());


            var ruleExpr = new FuzzyImplication(
                new OrExpression(
                    new NotExpression(
                        new AndExpression(
                            new ValueExpression(A, x),
                            new ValueExpression(B, y)
                        )
                    ),
                    new AndExpression(
                        new ValueExpression(A, y),
                        new ValueExpression(B, x)
                    )
                ),
                new ValueExpression(C, z)
            );

            var sut = new ToStringVisitor();

            var result = sut.Visit(ruleExpr);
            Assert.AreEqual("!( A=x && B=y ) || ( A=y && B=x ) => C=z", result);

            result = ruleExpr.Accept(sut);
            Assert.AreEqual("!( A=x && B=y ) || ( A=y && B=x ) => C=z", result);
        }