Exemple #1
0
        public static int[] InMemoryGet(float[][] data, int count, int Hdims, int seed, out int[] ids)
        {
            LSHTree instance = new LSHTree(data, count, Hdims, seed);

            instance.InMemoryGet(0, data.Length);
            ids = instance.ids;
            return(instance.indicies);
        }
Exemple #2
0
        public static List <int>[] Candidates(float[][] data, int k, LSHFConfiguration LSHFConfig, bool verbose = false)
        {
            Random SeedGen = LSHFConfig.LSHSeed == -1 ? new Random() : new Random(LSHFConfig.LSHSeed);
            int    N       = data.Length;
            int    trees   = LSHFConfig.LSHForestTrees;

            List <int>[] candidates = new List <int> [N];
            if (N < 2 * k)
            {
                for (int i = 0; i < N; i++)
                {
                    candidates[i] = Enumerable.Range(0, N).ToList();
                    candidates[i].RemoveAt(i);
                }
                return(candidates);
            }

            for (int i = 0; i < N; i++)
            {
                candidates[i] = new List <int>(2 * k);
            }
            int[] seeds = new int[trees];
            for (int i = 0; i < trees; i++)
            {
                seeds[i] = SeedGen.Next();
            }

            if (verbose)
            {
                Console.WriteLine("LSH Forest building {0} trees", trees);
            }

            Parallel.For(0, trees, i =>
            {
                int[] indicies = LSHTree.InMemoryGet(data, LSHFConfig.LSHTreeC * k / trees, LSHFConfig.LSHHashDims, seeds[i], out int[] cand);

                for (int j = 0; j < N; j++)
                {
                    lock (candidates[j])
                        for (int ii = indicies[2 * j]; ii < indicies[2 * j + 1]; ii++)
                        {
                            candidates[j].Add(cand[ii]);
                        }
                }
            });

            Parallel.For(0, N, i =>
            {
                candidates[i].Sort();
                int a = 0;
                int b = 1;
                while (b < candidates[i].Count)
                {
                    if (candidates[i][b] == candidates[i][a])
                    {
                        b++;
                    }
                    else
                    {
                        candidates[i][++a] = candidates[i][b++];
                    }
                }
                a++;
                candidates[i].RemoveRange(a, b - a);
                candidates[i].Remove(i);
            });

            int fill = RandomFill(candidates, k, SeedGen.Next());

            if (fill != 0)
            {
                Console.WriteLine("WARNING! Added {0} random candidates total in a LSH Forest.", fill);
            }
            return(candidates);
        }