public void Constructor_Fails() { var term = new FuzzyTerm("Term", new MembershipFunction()); var var = new FuzzyVariable("Variable", null, term); Assert.AreEqual("variable", Assert.Throws<ArgumentNullException>(() => new ValueExpression(null, term)).ParamName); Assert.AreEqual("value", Assert.Throws<ArgumentNullException>(() => new ValueExpression(var, null)).ParamName); }
public void ToStringTest() { var membershipFunction = new MembershipFunction(); var sut = new FuzzyTerm("MyFuzzyTerm", membershipFunction); Assert.AreEqual("MyFuzzyTerm", sut.ToString()); }
public void Constructor() { var membershipFunction = new MembershipFunction(); var sut = new FuzzyTerm("MyFuzzyTerm", membershipFunction); Assert.AreEqual("MyFuzzyTerm", sut.Term); Assert.AreEqual(membershipFunction, sut.MembershipFunction); }
public void Constructor() { var term = new FuzzyTerm("Term", new MembershipFunction()); var var = new FuzzyVariable("Variable", null, term); var expr = new ValueExpression(var, term); var sut = new NotExpression(expr); Assert.AreEqual(expr, sut.Expression); }
public void GetHashCodeTest() { var numVariable = new NumericVariable("MyNumVariable"); var fuzzyTerm = new FuzzyTerm("FuzzyTerm", new MembershipFunction{ { -2, 0 }, { 2, 1 } }); var sut = new FuzzyVariable("MyFuzzyVariable", numVariable, fuzzyTerm); Assert.AreEqual(sut.Identifier.GetHashCode(), sut.GetHashCode()); }
public void ToStringTest() { var numVariable = new NumericVariable("MyNumVariable"); var fuzzyTerm1 = new FuzzyTerm("FuzzyTerm1", new MembershipFunction{ { -2, 0 }, { 2, 1 } }); var fuzzyTerm2 = new FuzzyTerm("FuzzyTerm2", new MembershipFunction{ { -2, 0 }, { 2, 1 } }); var sut = new FuzzyVariable("MyFuzzyVariable", numVariable, fuzzyTerm1, fuzzyTerm2); Assert.AreEqual("MyFuzzyVariable = { FuzzyTerm1, FuzzyTerm2 }", sut.ToString()); }
public void Constructor() { var numVariable = new NumericVariable("MyNumVariable"); var fuzzyTerm = new FuzzyTerm("MyFuzzyTerm", new MembershipFunction{ { -2, 0 }, { 2, 1 } }); var sut = new FuzzyVariable("MyFuzzyVariable", numVariable, fuzzyTerm); Assert.AreEqual("MyFuzzyVariable", sut.Identifier); Assert.AreEqual(numVariable, sut.NumericVariable); Assert.AreEqual(1, sut.FuzzyTerms.Count()); Assert.AreEqual(fuzzyTerm, sut.FuzzyTerms.ElementAt(0)); }
public void Apply_Complex_Scenario([Values("Max", "Min", "Sum", "Average")] string strategyName) { var strategy = createMergingStrategy(strategyName); var termA1 = new FuzzyTerm("TermA1", new MembershipFunction()); var termA2 = new FuzzyTerm("TermA2", new MembershipFunction()); var varA = new FuzzyVariable("Variable A", null, termA1, termA2); var termB1 = new FuzzyTerm("TermB1", new MembershipFunction()); var termB2 = new FuzzyTerm("TermB2", new MembershipFunction()); var termB3 = new FuzzyTerm("TermB3", new MembershipFunction()); var varB = new FuzzyVariable("Variable B", null, termB1, termB2, termB3); var valueA1 = new FuzzyValue(varA, new Dictionary<FuzzyTerm, double> { { termA1, 0.6 } }); var valueA2 = new FuzzyValue(varA, new Dictionary<FuzzyTerm, double> { { termA2, 0.1 } }); var valueA3 = new FuzzyValue(varA, new Dictionary<FuzzyTerm, double> { { termA1, 0.8 }, { termA2, 0.6 } }); var valueB1 = new FuzzyValue(varB, new Dictionary<FuzzyTerm, double> { { termB1, 0.2 } }); var valueB2 = new FuzzyValue(varB, new Dictionary<FuzzyTerm, double> { { termB2, 0.3 } }); var valueB3 = new FuzzyValue(varB, new Dictionary<FuzzyTerm, double> { { termB1, 0.8 }, { termB3, 0.6 } }); var valueB4 = new FuzzyValue(varB, new Dictionary<FuzzyTerm, double> { { termB3, 0.7 } }); var valueB5 = new FuzzyValue(varB, new Dictionary<FuzzyTerm, double> { { termB2, 0.8 }, { termB3, 0.6 } }); var valueB6 = new FuzzyValue(varB, new Dictionary<FuzzyTerm, double> { { termB1, 0.3 }, { termB2, 0.5 }, { termB3, 0.7 } }); var sut = new FuzzyValueMerger(strategy); var result = sut.Apply(new List<FuzzyValue> { valueA1, valueA2, valueA3, valueB1, valueB2, valueB3, valueB4, valueB5, valueB6 }).ToList(); Assert.AreEqual(2, result.Count); Assert.IsTrue(result.Exists(v => v.AssociatedVariable.Equals(varA))); Assert.IsTrue(result.Exists(v => v.AssociatedVariable.Equals(varB))); // check for varA var varAValues = result.Find(v => v.AssociatedVariable.Equals(varA)); Assert.AreEqual(2, varAValues.Values.Count); Assert.IsTrue(varAValues.Values.ContainsKey(termA1)); Assert.IsTrue(varAValues.Values.ContainsKey(termA2)); Assert.AreEqual(strategy.Merge(new List<double> { valueA1.Values[termA1], valueA3.Values[termA1] }), varAValues.Values[termA1]); Assert.AreEqual(strategy.Merge(new List<double> { valueA2.Values[termA2], valueA3.Values[termA2] }), varAValues.Values[termA2]); // check for varB var varBValues = result.Find(v => v.AssociatedVariable.Equals(varB)); Assert.AreEqual(3, varBValues.Values.Count); Assert.IsTrue(varBValues.Values.ContainsKey(termB1)); Assert.IsTrue(varBValues.Values.ContainsKey(termB2)); Assert.IsTrue(varBValues.Values.ContainsKey(termB3)); Assert.AreEqual(strategy.Merge(new List<double> { valueB1.Values[termB1], valueB3.Values[termB1], valueB6.Values[termB1] }), varBValues.Values[termB1]); Assert.AreEqual(strategy.Merge(new List<double> { valueB2.Values[termB2], valueB5.Values[termB2], valueB6.Values[termB2] }), varBValues.Values[termB2]); Assert.AreEqual(strategy.Merge(new List<double> { valueB3.Values[termB3], valueB4.Values[termB3], valueB5.Values[termB3], valueB6.Values[termB3] }), varBValues.Values[termB3]); }
public void GetHashCodeTest() { var numVariable = new NumericVariable("MyNumVariable"); var fuzzyTerm1 = new FuzzyTerm("FuzzyTerm1", new MembershipFunction{ { -2, 0 }, { 2, 1 } }); var fuzzyTerm2 = new FuzzyTerm("FuzzyTerm2", new MembershipFunction{ { -2, 0 }, { 2, 1 } }); var fuzVariable = new FuzzyVariable("MyFuzzyVariable", numVariable, fuzzyTerm1, fuzzyTerm2); var sut = new FuzzyValue(fuzVariable,new Dictionary<FuzzyTerm, double>{{fuzzyTerm1, 1}}); Assert.AreEqual(fuzVariable.GetHashCode(), sut.GetHashCode()); }
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 AndExpression(exprA, exprB); Assert.AreEqual(exprA, sut.LeftExpression); Assert.AreEqual(exprB, sut.RightExpression); }
public void Apply_Unit_Test() { var numVar = new NumericVariable("Num Variable", 0, 42); var msf1 = new MembershipFunction { { 1, 0 } }; var msf2 = new MembershipFunction { { 2, 0 } }; var term1 = new FuzzyTerm("Term1", msf1); var term2 = new FuzzyTerm("Term2", msf2); const double value1 = 0.3; const int value2 = 06; var fuzzyVariable = new FuzzyVariable("Var", numVar, term1, term2); var fuzzyValue = new FuzzyValue(fuzzyVariable, new Dictionary<FuzzyTerm, double> {{term1, value1}, {term2, value2}}); var scaledMsf1 = new MembershipFunction {{0, 0}}; var scaledMsf2 = new MembershipFunction {{1, 1}}; var mergedValue = new MembershipFunction {{2, 1}}; const double expectedDefuzzifiedValue = 42; var mocks = new MockRepository(); var scaleStrategy = mocks.StrictMock<IMsfScalingStrategy>(); var mergeStrategy = mocks.StrictMock<IMsfMergingStrategy>(); var defuzzifyStrategy = mocks.StrictMock<IDefuzzifyStrategy>(); // 1. Scale the membership functions according to the fuzzy value Expect.Call(scaleStrategy.Apply(msf1, value1)).Return(scaledMsf1).Repeat.Once(); Expect.Call(scaleStrategy.Apply(msf2, value2)).Return(scaledMsf2).Repeat.Once(); // 2. Merge all membership functions into single one Expect.Call(mergeStrategy.Apply(new[] {scaledMsf1, scaledMsf2})).IgnoreArguments().Return(mergedValue).Repeat.Once(); // 3. Create a defuzzified value for the result of the previous merge. Expect.Call(defuzzifyStrategy.Apply(numVar, mergedValue)).Return(expectedDefuzzifiedValue).Repeat.Once(); mocks.ReplayAll(); var sut = new Defuzzifier(scaleStrategy, mergeStrategy, defuzzifyStrategy); var result = sut.Apply(fuzzyValue); Assert.AreEqual(fuzzyVariable, result.Variable); Assert.AreEqual(mergedValue, result.MembershipFunction); Assert.AreEqual(expectedDefuzzifiedValue, result.Value); mocks.VerifyAll(); }
public void EqualsTest() { var numVariable = new NumericVariable("MyNumVariable"); var fuzzyTerm1 = new FuzzyTerm("FuzzyTerm1", new MembershipFunction{ { -2, 0 }, { 2, 1 } }); var fuzzyTerm2 = new FuzzyTerm("FuzzyTerm2", new MembershipFunction{ { -2, 0 }, { 2, 1 } }); var sut1 = new FuzzyVariable("MyFuzzyVariable", numVariable, fuzzyTerm1); var sut2 = new FuzzyVariable("MyFuzzyVariable", numVariable, fuzzyTerm2); // ReSharper disable SuspiciousTypeConversion.Global Assert.IsFalse(sut1.Equals("MyIdentifier")); // ReSharper restore SuspiciousTypeConversion.Global Assert.IsTrue(sut1.Equals(sut2)); }
private void runTest(Action<Scope, FuzzyValue, FuzzyValue> runAndAssert) { var num1 = new NumericVariable("Variable1"); var num2 = new NumericVariable("Variable2"); var term11 = new FuzzyTerm("Var1_Term1", new MembershipFunction()); var term12 = new FuzzyTerm("Var1_Term2", new MembershipFunction()); var term21 = new FuzzyTerm("Var2_Term1", new MembershipFunction()); var term22 = new FuzzyTerm("Var2_Term2", new MembershipFunction()); var var1 = new FuzzyVariable("Variable1", num1, term11, term12); var var2 = new FuzzyVariable("Variable2", num2, term21, term22); var value1 = new FuzzyValue(var1, new Dictionary<FuzzyTerm, double> {{term11, 0.4}, {term12, 0.3}}); var value2 = new FuzzyValue(var2, new Dictionary<FuzzyTerm, double> {{term21, 0.1}, {term22, 0.2}}); var sut = new Scope(value1, value2); runAndAssert(sut, value1, value2); }
public void SetCurrentScope( [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 term = new FuzzyTerm("Term", new MembershipFunction()); var var = new FuzzyVariable("Variable", null, term); var value = new FuzzyValue(var, new Dictionary<FuzzyTerm, double> { { term, 0.2 } }); var scope = new Scope(value); var sut = new RuleEvaluation(evaluationStrategy, merger) { InputScope = scope }; Assert.AreEqual(scope, sut.InputScope); }
public void ToStringTest() { var numVariable = new NumericVariable("MyNumVariable"); var fuzzyTerm1 = new FuzzyTerm("FuzzyTerm1", new MembershipFunction{ { -2, 0 }, { 2, 1 } }); var fuzzyTerm2 = new FuzzyTerm("FuzzyTerm2", new MembershipFunction{ { -2, 0 }, { 2, 1 } }); var fuzVariable = new FuzzyVariable("MyFuzzyVariable", numVariable, fuzzyTerm1, fuzzyTerm2); var sut = new FuzzyValue(fuzVariable, new Dictionary<FuzzyTerm, double> { {fuzzyTerm1, 1}, {fuzzyTerm2, 0.5} }); Assert.AreEqual(fuzVariable.Identifier + " = ( FuzzyTerm1=1, FuzzyTerm2=0,5 )", sut.ToString()); }
public void Accept() { var term = new FuzzyTerm("Term", new MembershipFunction()); var var = new FuzzyVariable("Variable", null, term); var mocks = new MockRepository(); var visitor = mocks.StrictMock<IExpressionVisitor<int>>(); var sut = new ValueExpression(var, term); Expect.Call(visitor.Visit(sut)).Return(42); mocks.ReplayAll(); var result = sut.Accept(visitor); Assert.AreEqual(42, result); mocks.VerifyAll(); }
public void Test() { var nVar = new NumericVariable("Km/h", 0); var slow = new FuzzyTerm("Slow", new MembershipFunction{ { 30, 1 }, { 50, 0 } }); var medium = new FuzzyTerm("Medium", new MembershipFunction{ { 40, 0 }, { 70, 1 }, { 110, 0 } }); var fast = new FuzzyTerm("Fast", new MembershipFunction{ { 70, 0 }, { 90, 1 } }); var speed = new FuzzyVariable("Speed", nVar, slow, medium, fast); var sut = new Fuzzifier(speed); var result = sut.Apply(new NumericValue(nVar, 90)); Assert.AreEqual(1, result.Count); var speedValue = result[0]; Assert.AreEqual(speed, speedValue.AssociatedVariable); Assert.AreEqual(2, speedValue.Values.Count); Assert.AreEqual(0.5, speedValue.Values[medium]); Assert.AreEqual(1, speedValue.Values[fast]); }
public void Constructor() { var numVariable = new NumericVariable("MyNumVariable"); var fuzzyTerm1 = new FuzzyTerm("FuzzyTerm1", new MembershipFunction{ { -2, 0 }, { 2, 1 } }); var fuzzyTerm2 = new FuzzyTerm("FuzzyTerm2", new MembershipFunction{ { -2, 0 }, { 2, 1 } }); var fuzVariable = new FuzzyVariable("MyFuzzyVariable", numVariable, fuzzyTerm1, fuzzyTerm2); var sut = new FuzzyValue(fuzVariable, new Dictionary<FuzzyTerm, double> { {fuzzyTerm1, 1}, {fuzzyTerm2, 0.5} }); Assert.AreEqual(fuzVariable, sut.AssociatedVariable); Assert.AreEqual(2, sut.Values.Count); Assert.AreEqual(1, sut.Values[fuzzyTerm1]); Assert.AreEqual(0.5, sut.Values[fuzzyTerm2]); }
public void Apply_With_Merging_of_Values([Values("Max", "Min", "Sum", "Average")] string strategyName) { var strategy = createMergingStrategy(strategyName); var term1 = new FuzzyTerm("Term1", new MembershipFunction()); var term2 = new FuzzyTerm("Term2", new MembershipFunction()); var varA = new FuzzyVariable("Variable A", null, term1, term2); var value1 = new FuzzyValue(varA, new Dictionary<FuzzyTerm, double> { { term1, 0.6 } }); var value2 = new FuzzyValue(varA, new Dictionary<FuzzyTerm, double> { { term1, 0.8 }, { term2, 0.6 } }); var sut = new FuzzyValueMerger(strategy); var result = sut.Apply(new List<FuzzyValue> { value1, value2 }); Assert.AreEqual(1, result.Count); Assert.AreEqual(varA, result[0].AssociatedVariable); Assert.AreEqual(2, result[0].Values.Count); Assert.IsTrue(result[0].Values.ContainsKey(term1)); Assert.IsTrue(result[0].Values.ContainsKey(term2)); Assert.AreEqual(strategy.Merge(new List<double>{value1.Values[term1], value2.Values[term1]}), result[0].Values[term1]); Assert.AreEqual(value2.Values[term2], result[0].Values[term2]); }
public void Apply_One_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 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 value = new FuzzyValue(varA, new Dictionary<FuzzyTerm, double> { { termA, 0.4 } }); var sut = new RuleEvaluation(evaluationStrategy, merger); var result = sut.Apply(new Scope(value), new[] {new FuzzyImplication(new ValueExpression(varA, termA), new ValueExpression(varB, termB))}); Assert.AreEqual(2, result.Count); Assert.AreEqual(varA, result[0].AssociatedVariable); Assert.AreEqual(1, result[0].Values.Count); Assert.AreEqual(0.4, result[0].Values[termA]); Assert.AreEqual(varB, result[1].AssociatedVariable); Assert.AreEqual(1, result[1].Values.Count); Assert.AreEqual(0.4, result[1].Values[termB]); }
public void Visit_Or_Expression( [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 term1 = new FuzzyTerm("Term1", new MembershipFunction()); var term2 = new FuzzyTerm("Term2", new MembershipFunction()); var var = new FuzzyVariable("Variable", null, term1, term2); var value = new FuzzyValue(var, new Dictionary<FuzzyTerm, double> { { term1, 0.2 }, { term2, 0.8 } }); var sut = new RuleEvaluation(evaluationStrategy, merger) { InputScope = new Scope(value) }; var result = sut.Visit(new OrExpression(new ValueExpression(var, term1), new ValueExpression(var, term2))); Assert.AreEqual(evaluationStrategy.Or(value.Values[term1], value.Values[term2]), result); }
public void Visit_Value_Expression_Fails_Variable_Not_Found( [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 termA = new FuzzyTerm("Term A", new MembershipFunction()); var varA = new FuzzyVariable("Variable A", null, termA); var termB = new FuzzyTerm("Term B", new MembershipFunction()); var varB = new FuzzyVariable("Variable B", null); var value = new FuzzyValue(varA, new Dictionary<FuzzyTerm, double> { { termA, 0.2 } }); var sut = new RuleEvaluation(evaluationStrategy, merger) { InputScope = new Scope(value) }; Assert.Throws<UnresolvedVariableException>(() => sut.Visit(new ValueExpression(varB, termB))); }
public void Visit_Implication_Expression( [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 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 value = new FuzzyValue(varA, new Dictionary<FuzzyTerm, double> { { termA, 0.4 } }); var sut = new RuleEvaluation(evaluationStrategy, merger) { InputScope = new Scope(value) }; var result = sut.Visit(new FuzzyImplication(new ValueExpression(varA, termA), new ValueExpression(varB, termB))); Assert.AreEqual(0.4, result); }
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 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); }
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)); }