public List <Point> ReturnNeuronsAsPoints() { List <Point> ReturnPoints = new List <Point>(); foreach (Neuron Neuron in Neurons) { ReturnPoints.Add(Neuron.GetAsPoint()); } return(ReturnPoints); }
private double CalculateMetric(Neuron FirstNeuron, Neuron OtherNeuron) { if (MetricChoice == Metric.Taxi) { return(Math.Abs(FirstNeuron.GetAsPoint().XCoordinate - OtherNeuron.GetAsPoint().XCoordinate) + Math.Abs(FirstNeuron.GetAsPoint().YCoordinate - OtherNeuron.GetAsPoint().YCoordinate)); } if (MetricChoice == Metric.Euclidean) { return(Math.Sqrt(Math.Pow((FirstNeuron.GetAsPoint().XCoordinate - OtherNeuron.GetAsPoint().XCoordinate), 2) + Math.Pow((FirstNeuron.GetAsPoint().YCoordinate - OtherNeuron.GetAsPoint().YCoordinate), 2))); } if (MetricChoice == Metric.Maximum) { return(Math.Max(Math.Abs(FirstNeuron.GetAsPoint().XCoordinate - OtherNeuron.GetAsPoint().XCoordinate), Math.Abs(FirstNeuron.GetAsPoint().YCoordinate - OtherNeuron.GetAsPoint().YCoordinate))); } else { return(0.0); } }
public void Output(Pointparser Parser, String BasePathfile, int iteration) { //neurons as points List <Point> NeuronsAsPoints = new List <Point>(); foreach (Neuron Neuron in Neurons) { NeuronsAsPoints.Add(Neuron.GetAsPoint()); } Parser.OutputFilePath = BasePathfile + "\\Iteration" + iteration.ToString() + "\\NeuronsAfterIteration.txt"; Parser.ParseOut(NeuronsAsPoints); }
private List <Neuron> GetNeuronsToChange() { List <Neuron> NeuronsToUpdate = new List <Neuron>(); foreach (Neuron Neuron in Neurons) { Point NeuronAsPoint = Neuron.GetAsPoint(); if (Winner.CalculateDistanceFrom(NeuronAsPoint) < LambdaParameter) { NeuronsToUpdate.Add(Neuron); } } NeuronsToUpdate.Add(Winner); return(NeuronsToUpdate); }
public List <Point> Clusterize(VoronoiDiagramContext Context) { Neurons = NeurGen.GetNeurons(ClusterCount, Xrange, Yrange); foreach (Point Point in Points) { double ClosestDistance = double.MaxValue; int BestSoFar = -1; for (int i = 0; i < ClusterCount; i++) { if (Neurons[i].CalculateDistanceFrom(Point) < ClosestDistance) { // Console.WriteLine("Distance from Neuron" + i.ToString() + Neurons[i].CalculateDistanceFrom(Point) ); BestSoFar = i; ClosestDistance = Neurons[i].CalculateDistanceFrom(Point); } } Clusters[BestSoFar].Add(Point); //Console.WriteLine("Assigned to: " + BestSoFar.ToString()); } ; for (int Iteration = 0; Iteration < MaxIters; Iteration++) { Point Err = new Point(Iteration, 0.0); for (int i = 0; i < ClusterCount; i++) { { Double ErrorComponent = 0.0; Point Mean = CalculateMean(Clusters[i]); ErrorComponent = Neurons[i].CalculateDistanceFrom(Mean); Err.YCoordinate += ErrorComponent; //if(Iteration == 0 || Iteration == 1) Console.WriteLine(i.ToString() + Neurons[i].GetAsPoint().XCoordinate + " " + Neurons[i].GetAsPoint().YCoordinate + " MEAN " + Mean.XCoordinate + " " + Mean.YCoordinate); Neurons[i].UpdateWeight(Mean.XCoordinate, Mean.YCoordinate); } Error.Add(Err); } ; //reassign clusters; for (int i = 0; i < ClusterCount; i++) { Clusters[i] = new List <Point>(); } foreach (Point Point in Points) { double ClosestDistance = double.MaxValue; int BestSoFar = -1; for (int i = 0; i < ClusterCount; i++) { if (Neurons[i].CalculateDistanceFrom(Point) < ClosestDistance) { BestSoFar = i; ClosestDistance = Neurons[i].CalculateDistanceFrom(Point); } } Clusters[BestSoFar].Add(Point); } ; List <Point> Voro = new List <Point>(); foreach (Neuron Neuron in Neurons) { Voro.Add(Neuron.GetAsPoint()); } VoronoiDiagram.CreateImage(Context, Voro).Save("KMEANS" + Iteration.ToString() + "Means" + ClusterCount.ToString() + ".png"); } return(Error); }