コード例 #1
0
 public int CalculateDistance(ExampleMetric other)
 {
     return(DistanceMetric.CalculateLeeDistance(
                Data,
                other.Data
                ));
 }
コード例 #2
0
        public void BKTreeShouldQueryBestMatchesBelowGivenThreshold()
        {
            BKTree <ExampleMetric> tree = new BKTree <ExampleMetric>();

            ExampleMetric search = new ExampleMetric(new int[] { 399, 400, 400 });

            ExampleMetric best1 = new ExampleMetric(41, new int[] { 400, 400, 400 });
            ExampleMetric best2 = new ExampleMetric(42, new int[] { 403, 403, 403 });
            ExampleMetric best3 = new ExampleMetric(43, new int[] { 406, 406, 406 });

            tree.Add(new ExampleMetric(1, new int[] { 100, 100, 100 }));
            tree.Add(new ExampleMetric(2, new int[] { 200, 200, 200 }));
            tree.Add(new ExampleMetric(3, new int[] { 300, 300, 300 }));
            tree.Add(best1);
            tree.Add(best2);
            tree.Add(new ExampleMetric(5, new int[] { 500, 500, 500 }));

            // Query for match within distance of 1 (best1 is only expected result)
            IDictionary <ExampleMetric, int> results = tree.Query(search, 1);

            Assert.AreEqual(1, DistanceMetric.CalculateLeeDistance(search.Data, best1.Data));
            Assert.AreEqual(1, results.Values.ElementAt(0));
            Assert.AreEqual(41, results.Keys.ElementAt(0).Id);
            Assert.AreEqual(best1.Data, results.Keys.ElementAt(0).Data);

            // Query for match within distance of 10 (best1 & best2 are expected results)
            tree.Add(best3); // exercise adding another node after already queried
            results = tree.Query(search, 10);

            Assert.AreEqual(2, results.Count);
            Assert.AreEqual(1, DistanceMetric.CalculateLeeDistance(search.Data, best1.Data));
            Assert.AreEqual(10, DistanceMetric.CalculateLeeDistance(search.Data, best2.Data));
            Assert.IsTrue(results.Contains(new KeyValuePair <ExampleMetric, int>(best1, 1)));
            Assert.IsTrue(results.Contains(new KeyValuePair <ExampleMetric, int>(best2, 10)));

            // Query for matches within distance of 20 (best1, best2 & best3 are expected results)
            results = tree.Query(search, 20);

            Assert.AreEqual(3, results.Count);
            Assert.AreEqual(1, DistanceMetric.CalculateLeeDistance(search.Data, best1.Data));
            Assert.AreEqual(10, DistanceMetric.CalculateLeeDistance(search.Data, best2.Data));
            Assert.AreEqual(19, DistanceMetric.CalculateLeeDistance(search.Data, best3.Data));
            Assert.IsTrue(results.Contains(new KeyValuePair <ExampleMetric, int>(best1, 1)));
            Assert.IsTrue(results.Contains(new KeyValuePair <ExampleMetric, int>(best2, 10)));
            Assert.IsTrue(results.Contains(new KeyValuePair <ExampleMetric, int>(best3, 19)));
        }
コード例 #3
0
        public void BKTreeShouldCalculateVarietyOfDistances()
        {
            Assert.AreEqual(10,
                            DistanceMetric.CalculateHammingDistance(
                                new byte[] { 0xEF, 0x35, 0x20 },
                                new byte[] { 0xAD, 0x13, 0x87 }));

            Assert.AreEqual(101,
                            DistanceMetric.CalculateLeeDistance(
                                new int[] { 196, 105, 48 },
                                new int[] { 201, 12, 51 }));

            Assert.AreEqual(3,
                            DistanceMetric.CalculateLevenshteinDistance(
                                "kitten",
                                "sitting"
                                ));
        }
コード例 #4
0
        public void BKTreeShouldFindBestNodeWithDistance()
        {
            BKTree <ExampleMetric> tree = new BKTree <ExampleMetric>();

            ExampleMetric search = new ExampleMetric(new int[] { 365, 422, 399 });
            ExampleMetric best   = new ExampleMetric(4, new int[] { 400, 400, 400 });

            tree.Add(new ExampleMetric(1, new int[] { 100, 100, 100 }));
            tree.Add(new ExampleMetric(2, new int[] { 200, 200, 200 }));
            tree.Add(new ExampleMetric(3, new int[] { 300, 300, 300 }));
            tree.Add(best);
            tree.Add(new ExampleMetric(5, new int[] { 500, 500, 500 }));

            Tuple <ExampleMetric, int> result = tree.FindClosestElement(search);

            Assert.AreEqual(58, DistanceMetric.CalculateLeeDistance(search.Data, best.Data));
            Assert.AreEqual(58, result.Item2);
            Assert.AreEqual(4, result.Item1.Id);
            Assert.AreEqual(best.Data, result.Item1.Data);
        }