From 66c4a5b49f69704422cd3a58fbcb8ee5df2cd3f1 Mon Sep 17 00:00:00 2001 From: Cesar Souza Date: Sun, 12 Jan 2014 21:01:02 +0100 Subject: [PATCH] Adjusting more unit tests. --- .../Learning/SequentialMinimalOptimization.cs | 17 +++-- .../BagOfVisualWordsTest.cs | 26 +++++-- .../FastRetinaKeypointDetectorTest.cs | 4 +- .../SequentialMinimalOptimizationTest.cs | 1 - .../Accord.Tests.Math/CombinatoricsTest.cs | 75 +++++++++++++++++-- 5 files changed, 99 insertions(+), 24 deletions(-) diff --git a/Sources/Accord.MachineLearning/VectorMachines/Learning/SequentialMinimalOptimization.cs b/Sources/Accord.MachineLearning/VectorMachines/Learning/SequentialMinimalOptimization.cs index a13151500..a6a0c098c 100644 --- a/Sources/Accord.MachineLearning/VectorMachines/Learning/SequentialMinimalOptimization.cs +++ b/Sources/Accord.MachineLearning/VectorMachines/Learning/SequentialMinimalOptimization.cs @@ -633,7 +633,8 @@ public double Run(bool computeError, CancellationToken token) { // loop I over all training examples for (int i = 0; i < samples; i++) - if (examineExample(i)) numChanged++; + if (examineExample(i)) + numChanged++; wholeSetChecks++; } @@ -648,7 +649,8 @@ public double Run(bool computeError, CancellationToken token) { if (alpha[i] != 0 && alpha[i] != c) { - if (examineExample(i)) numChanged++; + if (examineExample(i)) + numChanged++; if (b_upper > b_lower - 2.0 * tolerance) { @@ -673,7 +675,8 @@ public double Run(bool computeError, CancellationToken token) if (alpha[i] == negativeCost) continue; } - if (examineExample(i)) numChanged++; + if (examineExample(i)) + numChanged++; if (b_upper > b_lower - 2.0 * tolerance) { @@ -920,7 +923,7 @@ private int I(int i) else if (y == -1 && a == 0) return 4; // I4 - return 0; // I0 + return 0; // I0 (not at bounds) } /// @@ -929,7 +932,8 @@ private int I(int i) /// private bool takeStep(int i1, int i2) { - if (i1 == i2) return false; + if (i1 == i2) + return false; double[] p1 = inputs[i1]; // Input point at index i1 double alph1 = alpha[i1]; // Lagrange multiplier for p1 @@ -969,7 +973,8 @@ private bool takeStep(int i1, int i2) H = Math.Min(c2, alph2 + alph1); } - if (L == H) return false; + if (L == H) + return false; double k11 = kernelCache.GetOrCompute(i1); diff --git a/Sources/Accord.Tests/Accord.Tests.Imaging/BagOfVisualWordsTest.cs b/Sources/Accord.Tests/Accord.Tests.Imaging/BagOfVisualWordsTest.cs index 93dcfec94..de3309923 100644 --- a/Sources/Accord.Tests/Accord.Tests.Imaging/BagOfVisualWordsTest.cs +++ b/Sources/Accord.Tests/Accord.Tests.Imaging/BagOfVisualWordsTest.cs @@ -81,17 +81,27 @@ public void BagOfVisualWordsConstructorTest() Assert.AreEqual(723, points[4].Count); Assert.AreEqual(1263, points[5].Count); - Assert.AreEqual(388.043776954555, points[0][0].X); - Assert.AreEqual(105.99327164889745, points[0][0].Y); + double tol = 1e-7; + Assert.AreEqual(388.043776954555, points[0][0].X, tol); + Assert.AreEqual(105.99327164889745, points[0][0].Y, tol); - Assert.AreEqual(335.64548481033881, points[3][7].X); - Assert.AreEqual(152.14505651866821, points[2][3].Y); + Assert.AreEqual(335.64548481033881, points[3][7].X, tol); + Assert.AreEqual(152.14505651866821, points[2][3].Y, tol); - Assert.AreEqual(573.691355494602, points[2][52].X); - Assert.AreEqual(153.6650841848263, points[1][11].Y); + Assert.AreEqual(573.691355494602, points[2][52].X, tol); + Assert.AreEqual(153.6650841848263, points[1][11].Y, tol); - Assert.AreEqual(573.03087205188058, points[0][42].X); - Assert.AreEqual(374.27580307739436, points[4][125].Y); + Assert.AreEqual(573.03087205188058, points[0][42].X, tol); + Assert.AreEqual(374.27580307739436, points[4][125].Y, tol); + + foreach (var point in points) + { + foreach (var p in point) + { + Assert.IsFalse(double.IsNaN(p.X)); + Assert.IsFalse(double.IsNaN(p.Y)); + } + } } [TestMethod()] diff --git a/Sources/Accord.Tests/Accord.Tests.Imaging/FastRetinaKeypointDetectorTest.cs b/Sources/Accord.Tests/Accord.Tests.Imaging/FastRetinaKeypointDetectorTest.cs index d84a70439..4eff9c257 100644 --- a/Sources/Accord.Tests/Accord.Tests.Imaging/FastRetinaKeypointDetectorTest.cs +++ b/Sources/Accord.Tests/Accord.Tests.Imaging/FastRetinaKeypointDetectorTest.cs @@ -95,7 +95,7 @@ public void ProcessImageTest() i = 1042; Assert.AreEqual(116, actual[i].X); Assert.AreEqual(410, actual[i].Y); - Assert.AreEqual(-86.11209043916692, actual[i].Orientation); + Assert.AreEqual(-86.11209043916692, actual[i].Orientation, 1e-10); code = actual[i].ToBinary(); Assert.AreEqual("11110111010011000010101001011011010101100111010011011101001111010110001010110000111001010111101011001011001001101101100011011011110111110000010111111000000000010001001000010110101010010001100011011110101000100101100010101100000100111011101100010110001000111000100001110100001000110001110011111011000001100000111001001000011111111011010101001101110000010101001110111101011100001100110110000100111010100010000001000000011110000110111010011000111100111011111110110101110011100000001001110010100100001000101001110011", code); code = actual[i].ToHex(); @@ -162,7 +162,7 @@ public void ProcessImageTest2() i = 1042; Assert.AreEqual(116, actual[i].X); Assert.AreEqual(410, actual[i].Y); - Assert.AreEqual(-86.11209043916692, actual[i].Orientation); + Assert.AreEqual(-86.11209043916692, actual[i].Orientation, 1e-10); code = actual[i].ToBinary(); Assert.AreEqual("0000000001001111111110011100000000000001100010011100111000110001000001100010000011110111111000110001000000000000000000000001100010000011001110011101101110011110111111011110000000000000001000000100001000000100010000000000000000000000001100010001011100111000110001000001100011100011100111011011100111110011110111111011110111111000000000000000100000100000000000000000000000000000000001100010011011100111110011001111011111101111011111111111111111111111111111111111111110001100010011011100111110011000000000000000000100010100001100000000000000000001000001000001000000011110111111111111111111111110111001111011111101111111111111111011101111111111111111111111111111110111111111111111111111111111111111110111111000110001000001100011101001100001100000000000000000001000001000001000000000000011100111011011100111111011100111000011111111011111111111111111111111011111001111111110111111111111111111111110111110011100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", code); code = actual[i].ToHex(); diff --git a/Sources/Accord.Tests/Accord.Tests.MachineLearning/VectorMachines/SequentialMinimalOptimizationTest.cs b/Sources/Accord.Tests/Accord.Tests.MachineLearning/VectorMachines/SequentialMinimalOptimizationTest.cs index bd6ea7549..4d01fd4e1 100644 --- a/Sources/Accord.Tests/Accord.Tests.MachineLearning/VectorMachines/SequentialMinimalOptimizationTest.cs +++ b/Sources/Accord.Tests/Accord.Tests.MachineLearning/VectorMachines/SequentialMinimalOptimizationTest.cs @@ -50,7 +50,6 @@ public TestContext TestContext } - [TestMethod()] public void LearnTest() { diff --git a/Sources/Accord.Tests/Accord.Tests.Math/CombinatoricsTest.cs b/Sources/Accord.Tests/Accord.Tests.Math/CombinatoricsTest.cs index 4dc198e12..857202187 100644 --- a/Sources/Accord.Tests/Accord.Tests.Math/CombinatoricsTest.cs +++ b/Sources/Accord.Tests/Accord.Tests.Math/CombinatoricsTest.cs @@ -54,8 +54,18 @@ public TestContext TestContext public void TruthTableTest() { { - int symbols = 2; - int length = 3; + // Suppose we would like to generate a truth table for a binary + // problem. In this case, we are only interested in two symbols: + // 0 and 1. Let's then generate the table for three binary values + + int symbols = 2; // Binary variables: either 0 or 1 + int length = 3; // The number of variables; or number + // of columns in the generated table. + + // Generate the table using Combinatorics.TruthTable(2,3) + int[][] table = Combinatorics.TruthTable(symbols, length); + + // The generated table will be int[][] expected = { @@ -69,9 +79,7 @@ public void TruthTableTest() new int[] { 1, 1, 1 }, }; - int[][] actual = Combinatorics.TruthTable(symbols, length); - - Assert.IsTrue(expected.IsEqual(actual)); + Assert.IsTrue(expected.IsEqual(table)); } { @@ -118,8 +126,17 @@ public void TruthTableTest() [TestMethod()] public void TruthTableTest2() { + // Suppose we would like to generate a truth table (i.e. all possible + // combinations of a set of discrete symbols) for variables that contain + // different numbers symbols. Let's say, for example, that the first + // variable may contain symbols 0 and 1, the second could contain either + // 0, 1, or 2, and the last one again could contain only 0 and 1. Thus + // we can generate the truth table in the following way: + int[] symbols = { 2, 3, 2 }; + int[][] actual = Combinatorics.TruthTable(symbols); + int[][] expected = { new int[] { 0, 0, 0 }, @@ -136,8 +153,6 @@ public void TruthTableTest2() new int[] { 1, 2, 1 }, }; - int[][] actual = Combinatorics.TruthTable(symbols); - Assert.IsTrue(expected.IsEqual(actual)); } @@ -167,9 +182,35 @@ public void SequencesTest() Assert.IsTrue(expected.IsEqual(actual)); } + [TestMethod()] + public void SequencesTest2() + { + int[][] expected = + { + new int[] { 0, 0, 0 }, + new int[] { 0, 0, 1 }, + new int[] { 0, 1, 0 }, + new int[] { 0, 1, 1 }, + new int[] { 1, 0, 0 }, + new int[] { 1, 0, 1 }, + new int[] { 1, 1, 0 }, + new int[] { 1, 1, 1 }, + }; + + int i = 0; + foreach (int[] row in Combinatorics.Sequences(2, 3)) + { + Assert.IsTrue(row.IsEqual(expected[i++])); + } + } + [TestMethod()] public void PermutationsTest() { + // Let's say we would like to generate all possible permutations + // of the elements (1, 2, 3). In order to enumerate all those + // permutations, we can use: + int[] values = { 1, 2, 3 }; List permutations = new List(); @@ -183,5 +224,25 @@ public void PermutationsTest() Assert.IsTrue(permutations[3].IsEqual(new[] { 3, 1, 2 })); Assert.IsTrue(permutations[4].IsEqual(new[] { 3, 2, 1 })); } + + [TestMethod()] + public void CombinationsTest() + { + // Let's say we would like to generate all possible combinations + // of length 2 containing the elements (1, 2, 3). To enumerate all + // those combinations, we can use: + + int[] values = { 1, 2, 3 }; + int length = 2; + + List combinations = new List(); + foreach (var p in Combinatorics.Combinations(values, length)) + combinations.Add(p); + + Assert.AreEqual(3, combinations.Count); + Assert.IsTrue(combinations[0].IsEqual(new[] { 1, 2 })); + Assert.IsTrue(combinations[1].IsEqual(new[] { 1, 3 })); + Assert.IsTrue(combinations[2].IsEqual(new[] { 2, 3 })); + } } }