public double Apply(NumericVariable numericVariable, MembershipFunction msf) { if (msf.Count == 0) return (numericVariable.MaxValue - numericVariable.MinValue) / 2.0; if (msf[0].Y > 0 && msf[0].X > numericVariable.MinValue) { msf.Add(numericVariable.MinValue, msf[0].Y); } if (msf[msf.Count - 1].Y > 0 && msf[msf.Count - 1].X < numericVariable.MaxValue) { msf.Add(numericVariable.MaxValue, msf[msf.Count - 1].Y); } var numerator = 0d; var denominator = 0d; for (var i = 0; i < msf.Count - 1; i++) { var value1 = msf[i]; var value2 = msf[i + 1]; var min = Math.Min(value1.Y, value2.Y); var max = Math.Max(value1.Y, value2.Y); var line = new LineSegment(value1, value2); var m = line.Gradient.Value; var b = line.B.Value; numerator += ((m / 3.0) * (Math.Pow(value2.X, 3) - Math.Pow(value1.X, 3))) + ((b / 2.0) * (Math.Pow(value2.X, 2) - Math.Pow(value1.X, 2))); denominator += ((max + min) / 2.0) * (value2.X - value1.X); } return numerator / denominator; }
public double Apply(NumericVariable numericVariable, MembershipFunction msf) { var maximum = double.NegativeInfinity; var left = 0d; var right = 0d; foreach (var item in msf) { if (item.Value > maximum) { maximum = item.Value; left = item.Key; right = item.Key; } else if(Math.Abs(item.Value - maximum) < 0.00000000001) { right = item.Key; } } var first = msf[0]; if (Math.Abs(first.Y - maximum) < 0.00000000001) { left = numericVariable.MinValue; } var last = msf[msf.Count -1]; if (Math.Abs(last.Y - maximum) < 0.00000000001) { right = numericVariable.MaxValue; } return getValue(left, right); }
public void Constructor() { var sut = new NumericVariable("MyIdentifier", -42, 1701); Assert.AreEqual("MyIdentifier", sut.Identifier); Assert.AreEqual(-42, sut.MinValue); Assert.AreEqual(1701, sut.MaxValue); }
public void ToStringTest() { var numVariable = new NumericVariable("MyVariable"); var sut = new NumericValue(numVariable, 42); Assert.AreEqual(numVariable.Identifier + " = 42", sut.ToString()); }
public void Constructor() { var numVariable = new NumericVariable("MyVariable"); var sut = new NumericValue(numVariable, 42); Assert.AreEqual(numVariable, sut.Variable); Assert.AreEqual(42, sut.Value); }
public void EqualsTest() { var sut2 = new NumericVariable("MyIdentifier"); var sut1 = new NumericVariable("MyIdentifier"); // ReSharper disable SuspiciousTypeConversion.Global Assert.IsFalse(sut1.Equals("MyIdentifier")); // ReSharper restore SuspiciousTypeConversion.Global Assert.IsTrue(sut1.Equals(sut2)); }
/// <summary> /// Creates a new numeric value. /// </summary> /// <param name="variable">The variable the value gets assigned to.</param> /// <param name="value">The value for the variable.</param> public NumericValue(NumericVariable variable, double value) { if (variable == null) throw new ArgumentNullException("variable"); if (variable.MinValue > value || variable.MaxValue < value) throw new ArgumentOutOfRangeException("value"); Variable = variable; Value = value; }
public FuzzyVariable(string identifier, NumericVariable numericVariable, params FuzzyTerm[] fuzzyTerms) { if (string.IsNullOrEmpty(identifier)) throw new ArgumentException("identifier"); Identifier = identifier; Defuzzify = false; NumericVariable = numericVariable; FuzzyTerms = fuzzyTerms; }
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 ConstructorFails() { var numVariable = new NumericVariable("MyVariable", 42, 1701); Assert.AreEqual("value", Assert.Throws<ArgumentOutOfRangeException>(() => new NumericValue(numVariable, 13)).ParamName); Assert.AreEqual("variable", Assert.Throws<ArgumentNullException>(() => new NumericValue(null, 42)).ParamName); }
public void Apply_4(string method, double expected) { var numVar = new NumericVariable("Num Variable", 0, 3); var msf = new MembershipFunction { { 1, 1 } }; var sut = createSut(method); var result = sut.Apply(numVar, msf); Assert.AreEqual(expected, result); }
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 Apply_On_Symmetric_Msf_4() { var var = new NumericVariable("Var", -5, 5); var msf = new MembershipFunction { { -5, 0 }, { -4, 1 }, { -3, 0 }, { -2, 1 }, { 2, 1 }, { 3, 0 }, { 4, 1 }, { 5, 0 } }; var sut = new CoGDefuzzifyStrategy(); const double expectedResult = 0; var result = sut.Apply(var, msf); Assert.AreEqual(expectedResult, result, 0.00000000001); }
public void Apply_On_Symmetric_Msf_3() { var var = new NumericVariable("Var", 0, 4); var msf = new MembershipFunction { { 0, 0 }, { 1, 1 }, { 2, 0.5 }, { 3, 1 }, { 4, 0 } }; var sut = new CoGDefuzzifyStrategy(); const double expectedResult = 2; var result = sut.Apply(var, msf); Assert.AreEqual(expectedResult, result, 0.00000000001); }
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_On_Empty_Msf() { var var = new NumericVariable("Var", 0, 2); var msf = new MembershipFunction(); var sut = new CoGDefuzzifyStrategy(); const double expectedResult = 1d; var result = sut.Apply(var, msf); Assert.AreEqual(expectedResult, result, 0.00000000001); }
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 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 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 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_4() { var var = new NumericVariable("Var", 0, 3); var msf = new MembershipFunction { { 0, 0 }, { 1, 0.5 }, { 3, 0.5 }, { 4, 1 }, { 6, 0 } }; var sut = new CoGDefuzzifyStrategy(); const double expectedResult = 57.0/18.0; var result = sut.Apply(var, msf); Assert.AreEqual(expectedResult, result, 0.00000000001); }
public bool Equals(NumericVariable obj) { return obj != null && obj.Identifier == Identifier; }
public void Apply_2() { var var = new NumericVariable("Var", 1, 2); var msf = new MembershipFunction { { 1, 0.5 }, { 2, 1 } }; var sut = new CoGDefuzzifyStrategy(); const double expectedResult = 14.0 / 9.0; var result = sut.Apply(var, msf); Assert.AreEqual(expectedResult, result, 0.00000000001); }
public void ToStringTest() { var sut = new NumericVariable("MyIdentifier"); Assert.AreEqual(sut.Identifier, sut.ToString()); }
public void GetHashCodeTest() { var sut = new NumericVariable("MyIdentifier"); Assert.AreEqual(sut.Identifier.GetHashCode(), sut.GetHashCode()); }