Ejemplo n.º 1
0
        // Update map from network weights
        private void UpdateMap()
        {
            // lock
            Monitor.Enter(this);

            // lock bitmap
            BitmapData mapData = mapBitmap.LockBits(new Rectangle(0, 0, 200, 200),
                                                    ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

            int   stride = mapData.Stride;
            int   offset = stride - 200 * 3;
            Layer layer  = network.Layers[0];

            unsafe
            {
                byte *ptr = (byte *)mapData.Scan0;

                // for all rows
                for (int y = 0, i = 0; y < 100; y++)
                {
                    // for all pixels
                    for (int x = 0; x < 100; x++, i++, ptr += 6)
                    {
                        Neuron neuron = layer.Neurons[i];

                        // red
                        ptr[2] = ptr[2 + 3] = ptr[2 + stride] = ptr[2 + 3 + stride] =
                            (byte)Math.Max(0, Math.Min(255, neuron.Weights[0]));

                        // green
                        ptr[1] = ptr[1 + 3] = ptr[1 + stride] = ptr[1 + 3 + stride] =
                            (byte)Math.Max(0, Math.Min(255, neuron.Weights[1]));

                        // blue
                        ptr[0] = ptr[0 + 3] = ptr[0 + stride] = ptr[0 + 3 + stride] =
                            (byte)Math.Max(0, Math.Min(255, neuron.Weights[2]));
                    }

                    ptr += offset;
                    ptr += stride;
                }
            }

            // unlock image
            mapBitmap.UnlockBits(mapData);

            // unlock
            Monitor.Exit(this);

            // invalidate maps panel
            mapPanel.Invalidate();
        }
Ejemplo n.º 2
0
        // Generate point
        private void GeneratePoints()
        {
            int width    = pointsPanel.ClientRectangle.Width;
            int height   = pointsPanel.ClientRectangle.Height;
            int diameter = groupRadius * 2;

            // generate groups of ten points
            for (int i = 0; i < pointsCount;)
            {
                int cx = rand.Next(width);
                int cy = rand.Next(height);

                // generate group
                for (int j = 0; (i < pointsCount) && (j < 10);)
                {
                    int x = cx + rand.Next(diameter) - groupRadius;
                    int y = cy + rand.Next(diameter) - groupRadius;

                    // check if wee are not out
                    if ((x < 0) || (y < 0) || (x >= width) || (y >= height))
                    {
                        continue;
                    }

                    // add point
                    points[i, 0] = x;
                    points[i, 1] = y;

                    j++;
                    i++;
                }
            }

            map = null;
            pointsPanel.Invalidate();
            mapPanel.Invalidate();
        }