Beispiel #1
0
        /// <inheritdoc />
        public IEnumerable <int> NearestNeighborIndexes(TNode target, int k)
        {
            var point = this.PointSelector(target);
            var nearestNeighborList = new BoundablePriorityList <int, double>(k, true);
            var rect = HyperRect <double> .Infinite(this.Dimensions, double.MaxValue, double.MinValue);

            this.SearchForNearestNeighbors(this.rootNode, point, rect, 0, nearestNeighborList, double.MaxValue);
            return(nearestNeighborList);
        }
 public void SetMaxPriorityTest()
 {
     var bp = new BoundablePriorityList <int, double>(3, true)
     {
         { 34, 98744.90383 },
         { 23, 67.39030 },
         { 2, 2 },
         { 89, 3 }
     };
 }
Beispiel #3
0
        /// <inheritdoc />
        public IEnumerable <int> RadialSearchIndexes(TNode center, double radius)
        {
            var nearestNeighbors = new BoundablePriorityList <int, double>();
            var centerPoint      = this.PointSelector(center);

            this.SearchForNearestNeighbors(
                this.rootNode,
                centerPoint,
                HyperRect <double> .Infinite(this.Dimensions, double.MaxValue, double.MinValue),
                0,
                nearestNeighbors,
                radius);

            return(nearestNeighbors);
        }
        public void InsertTest()
        {
            var bp = new BoundablePriorityList <int, double>(3, true)
            {
                { 34, 98744.90383 },
                { 23, 67.39030 },
                { 2, 2 },
                { 89, 3 }
            };


            Assert.That(bp[0], Is.EqualTo(2));
            Assert.That(bp[1], Is.EqualTo(89));
            Assert.That(bp[2], Is.EqualTo(23));
        }
Beispiel #5
0
        private void KnnNodeSearch(
            MNode <int> node,
            T queryObject,
            BoundablePriorityList <MNodeEntry <int>, double> nearestNeighboorList,
            BoundablePriorityList <MNode <int>, double> priorityQueue)
        {
            foreach (var entry in node.Entries)
            {
                var maxPriority = nearestNeighboorList.MaxPriority;
                if (node.IsInternalNode)
                {
                    if ((node.ParentEntry == null) ||
                        (Math.Abs(
                             this.Metric(this.internalArray[node.ParentEntry.Value], queryObject)
                             - entry.DistanceFromParent) <= entry.CoveringRadius + maxPriority))
                    {
                        var distanceFromQueryObject = this.Metric(this.internalArray[entry.Value], queryObject);
                        var dMin = Math.Max(distanceFromQueryObject - entry.CoveringRadius, 0);
                        if (dMin < maxPriority)
                        {
                            priorityQueue.Add(entry.ChildNode, dMin);

                            var dMax = distanceFromQueryObject + entry.CoveringRadius;
                            if (dMax < maxPriority)
                            {
                                nearestNeighboorList.Add(null, dMax);
                                maxPriority = Math.Max(dMax, maxPriority);
                                priorityQueue.RemoveAllPriorities(p => p > maxPriority);
                            }
                        }
                    }
                }
                else if (Math.Abs(this.Metric(this.internalArray[node.ParentEntry.Value], queryObject) - entry.DistanceFromParent)
                         < maxPriority)
                {
                    var distanceFromQueryObject = this.Metric(this.internalArray[entry.Value], queryObject);
                    if (distanceFromQueryObject < maxPriority)
                    {
                        nearestNeighboorList.Add(entry, distanceFromQueryObject);
                        nearestNeighboorList.RemoveAllElements(e => e == null);
                        maxPriority = Math.Max(distanceFromQueryObject, maxPriority);
                        priorityQueue.RemoveAllPriorities(p => p > maxPriority);
                    }
                }
            }
        }
Beispiel #6
0
        /// <inheritdoc />
        public IEnumerable <int> NearestNeighborIndexes(T target, int k)
        {
            var priorityQueue = new BoundablePriorityList <MNode <int>, double>(-1, false)
            {
                { this.Root, 0 }
            };
            var nearestNeighboorList = new BoundablePriorityList <MNodeEntry <int>, double>(k)
            {
                { default(MNodeEntry <int>), double.PositiveInfinity }
            };

            while (priorityQueue.Any())
            {
                var nextNode = priorityQueue.MinElement;
                priorityQueue.RemoveAt(priorityQueue.Count - 1);
                this.KnnNodeSearch(nextNode, target, nearestNeighboorList, priorityQueue);
            }

            return(nearestNeighboorList.Select(m => m.Value));
        }
Beispiel #7
0
        /// <summary>
        /// A top-down recursive method to find the nearest neighbors of a given point.
        /// </summary>
        /// <param name="node">The index of the node for the current recursion branch.</param>
        /// <param name="target">The point whose neighbors we are trying to find.</param>
        /// <param name="rect">The <see cref="HyperRect{T}"/> containing the possible nearest neighbors.</param>
        /// <param name="dimension">The current splitting dimension for this recursion branch.</param>
        /// <param name="nearestNeighbors">The <see cref="BoundedPriorityList{TElement,TPriority}"/> containing the nearest neighbors already discovered.</param>
        /// <param name="maxSearchRadius">The radius of the current largest distance to search from the <paramref name="target"/></param>
        private void SearchForNearestNeighbors(
            KDNode node,
            double[] target,
            HyperRect <double> rect,
            int dimension,
            BoundablePriorityList <int, double> nearestNeighbors,
            double maxSearchRadius)
        {
            if (node == null)
            {
                return;
            }

            // Work out the current dimension
            var dim = dimension % this.Dimensions;

            // Get the coordinate of the current node.
            var coordinate = this.PointSelector(this.InternalList[node.ElementIndex]);

            // Split our hyper-rectangle into 2 sub rectangles along the current
            // node's point on the current dimension
            var leftRect = rect.Clone();

            leftRect.MaxPoint[dim] = coordinate[dim];

            var rightRect = rect.Clone();

            rightRect.MinPoint[dim] = coordinate[dim];

            // Determine which side the target resides in
            var comparison = target[dim] <= coordinate[dim];

            var nearerRect  = comparison ? leftRect : rightRect;
            var furtherRect = comparison ? rightRect : leftRect;

            var nearerNode  = comparison ? node.Left : node.Right;
            var furtherNode = comparison ? node.Right : node.Left;

            // Move down into the nearer branch
            this.SearchForNearestNeighbors(
                nearerNode,
                target,
                nearerRect,
                dimension + 1,
                nearestNeighbors,
                maxSearchRadius);

            // Walk down into the further branch but only if our capacity hasn't been reached
            // OR if there's a region in the further rectangle that's closer to the target than our
            // current furtherest nearest neighbor
            var closestPointInFurtherRect = furtherRect.GetClosestPoint(target);
            var distanceToTarget          = this.Metric(closestPointInFurtherRect, target);

            if (distanceToTarget.CompareTo(maxSearchRadius) <= 0)
            {
                if (nearestNeighbors.IsFull)
                {
                    if (distanceToTarget.CompareTo(nearestNeighbors.MaxPriority) < 0)
                    {
                        this.SearchForNearestNeighbors(
                            furtherNode,
                            target,
                            furtherRect,
                            dimension + 1,
                            nearestNeighbors,
                            maxSearchRadius);
                    }
                }
                else
                {
                    this.SearchForNearestNeighbors(
                        furtherNode,
                        target,
                        furtherRect,
                        dimension + 1,
                        nearestNeighbors,
                        maxSearchRadius);
                }
            }

            // Try to add the current node to our nearest neighbors list
            distanceToTarget = this.Metric(coordinate, target);
            if (distanceToTarget.CompareTo(maxSearchRadius) <= 0)
            {
                nearestNeighbors.Add(node.ElementIndex, distanceToTarget);
            }
        }