コード例 #1
0
        /// <summary>
        /// Calculates then n-nearest neighbours in a forward-direction only.
        /// </summary>
        /// <returns></returns>
        public static NearestNeighbours Forward(float[][] weights, int n, int customer)
        {
            var neighbours = new Collections.SortedDictionary <float, List <int> >();

            for (var current = 0; current < weights.Length; current++)
            {
                if (current != customer)
                {
                    var        weight    = weights[customer][current];
                    List <int> customers = null;
                    if (!neighbours.TryGetValue(weight, out customers))
                    {
                        customers = new List <int>();
                        neighbours.Add(weight, customers);
                    }
                    customers.Add(current);
                }
            }

            var result = new NearestNeighbours(n);

            foreach (var pair in neighbours)
            {
                foreach (var current in pair.Value)
                {
                    if (result.Count < n)
                    {
                        if (result.Max < pair.Key)
                        {
                            result.Max = pair.Key;
                        }
                        result.Add(current);
                    }
                    else
                    {
                        break;
                    }
                }
            }
            return(result);
        }
コード例 #2
0
        /// <summary>
        /// Calculates then nearest neighbours using a weight smaller than or equal to a maximum in a forward-direction only.
        /// </summary>
        /// <returns></returns>
        public static SortedNearestNeighbours Forward(float[][] weights, float max, int customer)
        {
            var neighbours = new Collections.SortedDictionary <float, List <int> >();
            var maxFound   = 0f;

            for (var current = 0; current < weights.Length; current++)
            {
                if (current != customer)
                {
                    var weight = weights[customer][current];
                    if (weight <= max)
                    {
                        List <int> customers = null;
                        if (!neighbours.TryGetValue(weight, out customers))
                        {
                            customers = new List <int>();
                            neighbours.Add(weight, customers);
                        }
                        customers.Add(current);

                        if (maxFound < weight)
                        {
                            maxFound = weight;
                        }
                    }
                }
            }

            var result = new SortedNearestNeighbours(maxFound);

            foreach (var pair in neighbours)
            {
                foreach (var current in pair.Value)
                {
                    result.Add(current);
                }
            }
            return(result);
        }