Ejemplo n.º 1
0
        private void FindNearestNNeighbors(Vector <TField> location, KdTreeNode <TValue> node,
                                           ref TValue maxBestValue, ref TField maxBestDistance, int numNeighbors, C5.IPriorityQueue <TValue> valuesList,
                                           int depth)
        {
            if (node == null)
            {
                return;
            }

            var dimension    = depth % this.dimensionality;
            var nodeLocation = this.locationGetter(node.Value);
            var distance     = (nodeLocation - location).Norm(this.dimensionality);

            // Check if current node is better than maximum best node, and replace maximum node in list with it.
            // Current node cannot be same as search location.
            if (!fieldArithmetic.AlmostEqual(distance, fieldArithmetic.Zero) &&
                fieldComparer.Compare(distance, maxBestDistance) < 0)
            {
                TValue maxValue;
                if (valuesList.Count == numNeighbors)
                {
                    maxValue = valuesList.DeleteMax();
                }
                valuesList.Add(node.Value);

                if (valuesList.Count == numNeighbors)
                {
                    maxBestValue    = valuesList.FindMax();
                    maxBestDistance = (this.locationGetter(maxBestValue) - location).Norm(this.dimensionality);
                }
            }

            // Check for best node in sub-tree of near child.
            var nearChildNode = fieldComparer.Compare(location[dimension], nodeLocation[dimension]) < 0 ?
                                node.LeftChild : node.RightChild;

            if (nearChildNode != null)
            {
                FindNearestNNeighbors(location, nearChildNode, ref maxBestValue, ref maxBestDistance, numNeighbors,
                                      valuesList, depth + 1);
            }

            // Check whether splitting hyperplane given by current node intersects with hypersphere of current smallest
            // distance around given location.
            if (fieldComparer.Compare(maxBestDistance, fieldArithmetic.Abs(fieldArithmetic.Subtract(
                                                                               nodeLocation[dimension], location[dimension]))) > 0)
            {
                // Check for best node in sub-tree of far child.
                var farChildValue = nearChildNode == node.LeftChild ? node.RightChild : node.LeftChild;

                if (farChildValue != null)
                {
                    FindNearestNNeighbors(location, farChildValue, ref maxBestValue, ref maxBestDistance, numNeighbors,
                                          valuesList, depth + 1);
                }
            }
        }
Ejemplo n.º 2
0
            private IPath <V, E> ComputeNext(IPath <V, E> lastPath)
            {
                // Start searching for the next path.
                for (int i = 0; i < lastPath.Edges.Count; ++i)
                {
                    V        spurNode         = lastPath.Edges[i].Src;
                    List <E> rootPathEdgeList = lastPath.Edges.Take(i).ToList();

                    foreach (IPath <V, E> path in resultPaths)
                    {
                        if (path.Edges.Count >= i && rootPathEdgeList.SequenceEqual(path.Edges.Take(i)))
                        {
                            maskingWeigher.Excluded.Add(path.Edges[i]);
                        }
                    }

                    // Effectively remove all root path nodes other than the spur node.
                    foreach (E edge in rootPathEdgeList)
                    {
                        foreach (E e in graph.GetEdgesFrom(edge.Src))
                        {
                            maskingWeigher.Excluded.Add(e);
                        }
                        foreach (E e in graph.GetEdgesTo(edge.Src))
                        {
                            maskingWeigher.Excluded.Add(e);
                        }
                    }

                    IPath <V, E> spurPath = search.Search(graph, spurNode, dst, maskingWeigher, 1).Paths.FirstOrDefault();
                    if (spurPath != null)
                    {
                        ImmutableList <E> .Builder builder = ImmutableList.CreateBuilder <E>();
                        builder.AddRange(rootPathEdgeList);
                        builder.AddRange(spurPath.Edges);
                        potentialPaths.Add(Path(builder.ToImmutable()));
                    }

                    // Restore all removed paths and nodes.
                    maskingWeigher.Excluded.Clear();
                }

                return(potentialPaths.IsEmpty ? null : potentialPaths.DeleteMax());
            }