static void Main(string[] args) { bool Kmeans = true; bool GasSwitch = false; bool KohonenSwitch = false; Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US"); NeuronGenerator NeuralGenerator = new NeuronGenerator(); Configuration Configuration = new Configuration(); Pointparser Parser = new Pointparser(); Parser.Filepath = "zestaw1.txt"; double InitialXRange = 2.0; double InitialYRange = 2.0; //describes initial square (centered at 0,0) from which the neurons are generated List <Neuron> Neurons = NeuralGenerator.GetNeurons(Configuration.GetAmountOfNeurons(), InitialXRange, InitialYRange); List <Point> TrainingPointsList = Parser.Parse(); KohonenLearning NeuralNetworkKohonenStyle = new KohonenLearning(Neurons, TrainingPointsList, Configuration); NeuralGas NeuralNetworkGasStyle = new NeuralGas(Neurons, TrainingPointsList, Configuration, NeuralGenerator); VoronoiDiagramContext DiagramContext = new VoronoiDiagramContext { Width = 1366, Height = 768, MinX = TrainingPointsList.Min(point => point.XCoordinate) - 0.5, MaxX = TrainingPointsList.Max(point => point.XCoordinate) + 0.5, MinY = TrainingPointsList.Min(point => point.YCoordinate) - 0.5, MaxY = TrainingPointsList.Max(point => point.YCoordinate) + 0.5, }; if (KohonenSwitch) { for (int i = 0; i < 100; i++) { if (i % 10 == 0) { Console.WriteLine("Progress o 10 %"); } NeuralNetworkKohonenStyle.Train(i); IEnumerable <Point> Points = NeuralNetworkKohonenStyle.ReturnNeuronsAsPoints(); VoronoiDiagram.CreateImage(DiagramContext, Points, TrainingPointsList).Save("Kohonen" + i.ToString("D3") + ".png"); } } else if (GasSwitch) { for (int i = 0; i < 500; i++) { if (i % 50 == 0) { Console.WriteLine("Progress o 10 %"); } NeuralNetworkGasStyle.Train(i); IEnumerable <Point> Points = NeuralNetworkGasStyle.ReturnNeuronsAsPoints(); VoronoiDiagram.CreateImage(DiagramContext, Points, TrainingPointsList).Save("NeuralGas" + i.ToString("D3") + ".png"); } } else if (Kmeans) { KSrednie KMeans = new KSrednie(10, 10, 10, TrainingPointsList, 150); Parser.ParseOut(KMeans.Clusterize(DiagramContext)); } }
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); }
public static Image CreateImage(VoronoiDiagramContext context, params IEnumerable <Point>[] inputs) { IEnumerable <IEnumerable <Vector> > dataPoints = inputs.Select( input => input.Select( point => new Vector(point.XCoordinate, point.YCoordinate) ) ); double factorX = context.Width / (context.MaxX - context.MinX); double factorY = context.Height / (context.MaxY - context.MinY); double factor = Math.Min(factorX, factorY); double minX = context.MinX - context.Width * (0.5 / factor - 0.5 / factorX); double minY = context.MinY - context.Height * (0.5 / factor - 0.5 / factorY); dataPoints = dataPoints.Select(points => points.Select(vector => new Vector( (vector[0] - minX) * factor, (vector[1] - minY) * factor ))); PointF toPoint(Vector vector) => new PointF( (float)vector[0], (float)vector[1] ); RectangleF toRectangle(Vector vector, float radius) => new RectangleF( (float)(vector[0]) - radius, (float)(vector[1]) - radius, radius * 2f, radius * 2f ); var image = new Bitmap(context.Width, context.Height); var linePen = new Pen(context.LineColor); var dataPointBrushes = context.DataPointsColors.Select(color => new SolidBrush(color)); var surface = Graphics.FromImage(image); surface.Clear(context.BackgroundColor); surface.SmoothingMode = SmoothingMode.HighQuality; try { VoronoiGraph graph = Fortune.ComputeVoronoiGraph(dataPoints.First().Distinct()); double infiniteLength = context.Width + context.Height; foreach (var edge in graph.Edges) { Vector left = edge.VVertexA; Vector right = edge.VVertexB; if (edge.IsPartlyInfinite) { Vector extension = edge.DirectionVector * infiniteLength; if (left == Fortune.VVInfinite) { left = edge.FixedPoint - extension; } if (right == Fortune.VVInfinite) { right = edge.FixedPoint + extension; } } surface.DrawLine(linePen, toPoint(left), toPoint(right)); } } catch (Exception) {} var target = dataPoints.Zip( dataPointBrushes.Zip( context.DataPointsRadii, (brush, radius) => new { brush, radius }), (points, style) => new { points, style } ).Reverse(); foreach (var data in target) { foreach (var point in data.points) { surface.FillEllipse(data.style.brush, toRectangle(point, data.style.radius)); } } return(image); }