public DefuzzifiedValue Apply(FuzzyValue fuzzyValue) { var scaledMembershipFunctions = fuzzyValue.Values.Select(kvp => _msfScalingStrategy.Apply(kvp.Key.MembershipFunction, kvp.Value)).ToList(); var mergeMembershipFunction = _msfMergingStrategy.Apply(scaledMembershipFunctions); var value = _defuzzifyStrategy.Apply(fuzzyValue.AssociatedVariable.NumericVariable, mergeMembershipFunction); return new DefuzzifiedValue(fuzzyValue.AssociatedVariable, mergeMembershipFunction, value); }
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 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 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); }
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 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 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 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 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 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_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 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 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]); }