Exemplo n.º 1
0
        /// <summary>
        ///   Radius search.
        /// </summary>
        ///
        private void nearest(TNode current, double[] position,
                             double radius, ICollection <NodeDistance <TNode> > list)
        {
            // Check if the distance of the point from this
            // node is within the desired radius, and if it
            // is, add to the list of nearest nodes.

            double d = distance.Distance(position, current.Position);

            if (d <= radius)
            {
                list.Add(new NodeDistance <TNode>(current, d));
            }

            // Prepare for recursion. The following null checks
            // will be used to avoid function calls if possible

            double value  = position[current.Axis];
            double median = current.Position[current.Axis];
            double u      = value - median;

            if (u <= 0)
            {
                if (current.Left != null)
                {
                    nearest(current.Left, position, radius, list);
                }

                if (current.Right != null && Math.Abs(u) <= radius)
                {
                    nearest(current.Right, position, radius, list);
                }
            }
            else
            {
                if (current.Right != null)
                {
                    nearest(current.Right, position, radius, list);
                }

                if (current.Left != null && Math.Abs(u) <= radius)
                {
                    nearest(current.Left, position, radius, list);
                }
            }
        }
Exemplo n.º 2
0
        public void DistanceUnitTest()
        {
            Assert.Inconclusive("TODO");

            IMetric target   = CreateIMetric(); // TODO: Initialize to an appropriate value
            IMBP    myPoint1 = null;            // TODO: Initialize to an appropriate value
            IMBP    myPoint2 = null;            // TODO: Initialize to an appropriate value
            float   expected = 0F;              // TODO: Initialize to an appropriate value
            float   actual;

            actual = target.Distance(myPoint1, myPoint2);
            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 3
0
        private void ComputeCentroid(IEnumerable <IEntitySpecification> items, IMetricItem mean)
        {
            var minDist = double.MaxValue;

            foreach (var item in items)
            {
                var currentDistance = _metric.Distance(item, mean);
                if (!(currentDistance < minDist))
                {
                    continue;
                }
                minDist = currentDistance;
                Height  = item.Height;
                Weight  = item.Weight;
            }
        }
Exemplo n.º 4
0
Arquivo: Knn.cs Projeto: TagruP/KSR
        public static string Classify(ReutersMetricObject reuter, IEnumerable <ReutersMetricObject> trainingData, IMetric metric)
        {
            var distance   = new SortedList <double, string>(new DuplicateComparer <double>());
            var categories = new Dictionary <string, int>();

            foreach (var o in trainingData)
            {
                distance.Add(metric.Distance(reuter.Vector, o.Vector), o.Property);
            }

            for (int i = 0; i < Settings.Default.K; i++)
            {
                categories.TryGetValue(distance.ElementAt(i).Value, out var value);
                categories[distance.ElementAt(i).Value] = value + 1;
            }

            return(categories.First(c => c.Value == categories.Max(x => x.Value)).Key);
        }
Exemplo n.º 5
0
        private bool Assign()
        {
            var numClusters = _clusters.Count;
            var isChanged   = false;
            var distances   = new double[numClusters];

            foreach (var t in _items)
            {
                for (var k = 0; k < numClusters; k++)
                {
                    distances[k] = _metric.Distance(t, _clusters[k].Centroid);
                }

                var newClusterId = _metric.MinIndex(distances);
                if (newClusterId == t.ClusterId)
                {
                    continue;
                }

                isChanged = true;
                t.SetCluster(newClusterId);
            }
            return(isChanged);
        }