Exemplo n.º 1
0
        /// <summary>
        /// Finds the best matching unit.
        /// </summary>
        /// <param name="grid">The grid.</param>
        /// <param name="datum">The datum.</param>
        /// <returns>INeuron.</returns>
        public IBestMatchingUnit FindBestMatchingUnit(IGrid2D grid, IDatum datum)
        {
            // get weights from datum
            var referenceWeighs = datum.MapToWeights();

            // prepare search
            var metric           = _metric;
            var smallestDistance = Double.NaN;
            var bmu = null as IGridNeuron;

            // linear search
            foreach (var gridNeuron in grid)
            {
                var weigths  = gridNeuron.Neuron.Weights;
                var distance = metric.CalculateDistance(referenceWeighs, weigths);
                if (!(Double.IsNaN(smallestDistance) || distance < smallestDistance))
                {
                    continue;
                }

                smallestDistance = distance;
                bmu = gridNeuron;
            }

            return(new BestMatchingUnit(bmu, smallestDistance));
        }