/// <inheritdoc/> public int Classify(double[] input) { if (input.Length > InputCount) { throw new AIFHError( "Can't classify SOM with input size of " + InputCount + " with input data of count " + input.Length); } double minDist = double.PositiveInfinity; int result = -1; var rowWeights = _weights.ToRowArrays(); for (int i = 0; i < OutputCount; i++) { double dist = calcDist.Calculate(input, rowWeights[i]); if (dist < minDist) { minDist = dist; result = i; } } return(result); }
private void btnRecognize_Click(object sender, EventArgs e) { var ds = new DownSample(_entryImage); _downsampled = ds.PerformDownSample(DownsampleWidth, DownsampleHeight); sample.Invalidate(); int sampleSize = DownsampleHeight * DownsampleWidth; char bestChar = '?'; double bestDistance = double.MaxValue; foreach (char ch in _letterData.Keys) { double[] data = _letterData[ch]; double dist = _distCalc.Calculate(data, _downsampled); if (dist < bestDistance) { bestDistance = dist; bestChar = ch; } } MessageBox.Show(@"That might be " + bestChar, @"Recognize"); ClearEntry(); }
/// <summary> /// Find the nearest neighbor particle. /// </summary> /// <param name="target">The particle to look for neighbors to.</param> /// <param name="particles">All particles.</param> /// <param name="k">The number of particles to find.</param> /// <param name="maxDist">The max distance to check.</param> /// <returns>The nearest neighbors.</returns> private IList <Particle> FindNearest(Particle target, IEnumerable <Particle> particles, int k, double maxDist) { IList <Particle> result = new List <Particle>(); var tempDist = new double[k]; int worstIndex = -1; foreach (Particle particle in particles) { if (particle == target) { continue; } double d = _distanceCalc.Calculate(particle.Location, target.Location); if (d > maxDist) { continue; } if (result.Count < k) { tempDist[result.Count] = d; result.Add(particle); worstIndex = MaxIndex(tempDist); } else if (d < tempDist[worstIndex]) { tempDist[worstIndex] = d; result[worstIndex] = particle; worstIndex = MaxIndex(tempDist); } } return(result); }
/// <inheritdoc/> public override double Evaluate() { double result = 0; for (int i = 0; i < (_cities.Length - 1); i++) { // find current and next city double[] city1 = _cities[_currentPath[i]]; double[] city2 = _cities[_currentPath[i + 1]]; result += _distance.Calculate(city1, city2); } return(result); }
/// <summary> /// Find the nearest cluster for an observation. /// </summary> /// <param name="observation">The observation.</param> /// <returns>The nearest cluster.</returns> public Cluster FindNearestCluster(double[] observation) { Cluster result = null; double resultDist = double.PositiveInfinity; foreach (Cluster cluster in _clusters) { double dist = _distanceMetric.Calculate(observation, cluster.Center); if (dist < resultDist) { resultDist = dist; result = cluster; } } return(result); }
public override double Evaluate() { return(_distance.Calculate(Ideal, _currentHolder)); }