Exemplo n.º 1
0
        public void CalculateHammingDistanceCorrect()
        {
            byte[] first  = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            byte[] second = new byte[] { 1, 2, 3, 8, 5, 9, 7, 8, 11, 13 };

            var result = similarityUtility.CalculateHammingDistance(first, second);

            Assert.AreEqual(4, result);
        }
Exemplo n.º 2
0
        public void ShouldBeAbleToControlReturnedCandidatesWithThresholdParameter()
        {
            int l = 25, k = 4, width = 128, height = 72;

            var hashingConfig = new DefaultHashingConfig
            {
                Width = width, Height = height, NumberOfLSHTables = l, NumberOfMinHashesPerTable = k
            };

            var lsh = LocalitySensitiveHashingAlgorithm.Instance;

            double[] howSimilarly       = { 0.3, 0.5, 0.6, 0.7, 0.75, 0.8, 0.85, 0.9 };
            int[]    expectedThresholds = { 0, 0, 0, 2, 3, 5, 7, 11 };

            const int simulations = 10000;

            Parallel.For(0, howSimilarly.Length, r =>
            {
                var random           = new Random((r + 1) * 100);
                double howSimilar    = howSimilarly[r];
                int topWavelets      = (int)(0.035 * width * height);
                var agreeOn          = new List <int>();
                var hammingDistances = new List <int>();
                for (int i = 0; i < simulations; ++i)
                {
                    var fingerprints    = TestUtilities.GenerateSimilarFingerprints(random, howSimilar, topWavelets, width * height * 2);
                    int hammingDistance = similarity.CalculateHammingDistance(fingerprints.Item1.ToBools(), fingerprints.Item2.ToBools());
                    hammingDistances.Add(hammingDistance);
                    var hashed1    = lsh.HashImage(new Fingerprint(fingerprints.Item1, 0, 0, Array.Empty <byte>()), hashingConfig);
                    var hashed2    = lsh.HashImage(new Fingerprint(fingerprints.Item2, 0, 0, Array.Empty <byte>()), hashingConfig);
                    int agreeCount = AgreeOn(hashed1.HashBins, hashed2.HashBins);
                    agreeOn.Add(agreeCount);
                }

                int requested = (int)((1 - howSimilar) * topWavelets * 2);
                Assert.AreEqual(requested, hammingDistances.Average(), 1);
                Assert.AreEqual(expectedThresholds[r], Math.Floor(agreeOn.Average()));
                Console.WriteLine($"Similarity: {howSimilar: 0.00}, Avg. Table Matches {agreeOn.Average(): 0.000}");
            });
        }