Ejemplo n.º 1
0
        /// <summary>
        /// Draws the ANN on the appropriate Canvas.
        /// </summary>
        private void DrawANN()
        {
            // Clear Canvas
            AnnCanvas.Children.Clear();

            // Save positions of neurons in this array
            Point[] neuronPos = new Point[_ANN.GetTotalNumberOfNeurons()];

            double neuronDiameter = 20.0;
            double layerDist      = (double)AnnCanvas.ActualWidth / (double)(_ANN.NumLayers - 1) - neuronDiameter;

            RadialGradientBrush neuronBrush = new RadialGradientBrush();

            neuronBrush.RadiusX        = 1.0;
            neuronBrush.RadiusY        = 1.0;
            neuronBrush.GradientOrigin = new Point(0.7, 0.3);
            neuronBrush.GradientStops.Add(new GradientStop(Colors.White, 0.0));
            neuronBrush.GradientStops.Add(new GradientStop(Colors.Black, 1.0));

            #region Iterate over every layer
            for (int l = 0; l < _ANN.NumLayers; l++)
            {
                double neuronDist = 0;

                if (_ANN.GetNumNeurons(l) > 1)
                {
                    neuronDist = (double)AnnBorder.ActualHeight / ((double)_ANN.GetNumNeurons(l));
                }
                else
                {
                    neuronDist = (double)AnnBorder.ActualHeight / ((double)_ANN.GetNumNeurons(l));
                }

                #region Iterare over every neuron on the current layer
                for (int n = 0; n < _ANN.GetNumNeurons(l); n++)
                {
                    Ellipse e = new Ellipse();
                    e.Stroke          = Brushes.White;
                    e.Width           = neuronDiameter;
                    e.Height          = neuronDiameter;
                    e.StrokeThickness = 2.0;
                    e.Fill            = Brushes.White;

                    Canvas.SetLeft(e, layerDist * l);
                    Canvas.SetTop(e, neuronDist * n);

                    AnnCanvas.Children.Add(e);

                    // Safe the point of the current neuron for line drawing
                    neuronPos[_ANN.GetNumberOfNeuronsBefore(l) + n] = new Point(layerDist * l, neuronDist * n);

                    #region Draw all connections from previous layer to current neuron.
                    for (int i = 0; l > 0 && i < _ANN.GetNumNeurons(l - 1); i++)
                    {
                        Line line = new Line();
                        line.X1              = neuronPos[_ANN.GetNumberOfNeuronsBefore(l - 1) + i].X + neuronDiameter / 2.0;
                        line.Y1              = neuronPos[_ANN.GetNumberOfNeuronsBefore(l - 1) + i].Y + neuronDiameter / 2.0;
                        line.X2              = neuronPos[_ANN.GetNumberOfNeuronsBefore(l) + n].X + neuronDiameter / 2.0;
                        line.Y2              = neuronPos[_ANN.GetNumberOfNeuronsBefore(l) + n].Y + neuronDiameter / 2.0;
                        line.Stroke          = Brushes.White;
                        line.StrokeThickness = 1;
                        line.Opacity         = 0.5;

                        AnnCanvas.Children.Add(line);
                    }
                    #endregion
                }
                #endregion
            }
            #endregion
        }