Example #1
0
 private void AddPatternBut_Click(object sender, EventArgs e)
 {
     Image imgPattern;
     if (ofd.ShowDialog() == DialogResult.OK)
     {
         imgPattern = Image.FromFile(ofd.FileName);
         if (imgPattern.Width != imageDim || imgPattern.Height != imageDim)
         {
             MessageBox.Show("Image size must be " + imageDim.ToString() + "x" + imageDim.ToString() + " pixels", "Wrong image size", MessageBoxButtons.OK, MessageBoxIcon.Information);
         }
         else
         {
             int[,] patternPixels;
             int p = 0;
             int midColor = Math.Abs((int)(Color.Black.ToArgb() / 2));
             Bitmap b = new Bitmap(imgPattern);
             patternPixels = new int[imageDim, imageDim];
             List<Neuron> pattern = new List<Neuron>(imageDim * imageDim);
             for (int i = 0; i < imageDim; i++)
                 for (int j = 0; j < imageDim; j++)
                 {
                     Neuron n = new Neuron();
                     p = Math.Abs(b.GetPixel(i, j).ToArgb());
                     if (p < midColor)
                     {
                         b.SetPixel(i, j, Color.White);
                         n.State = NeuronStates.AlongField;
                     }
                     else
                     {
                         b.SetPixel(i, j, Color.Black);
                         n.State = NeuronStates.AgainstField;
                     }
                     pattern.Add(n);
                 }
             NN.AddPattern(pattern);
             ImageMagnifier.ImageMagnifier im = new ImageMagnifier.ImageMagnifier();
             im.ImageToMagnify = b;
             im.MagnificationCoefficient = 8;
             im.Location = new System.Drawing.Point(1, ((NN.M - 1) * (imageDim + 2) * im.MagnificationCoefficient));
             im.Size = new System.Drawing.Size(imageDim * im.MagnificationCoefficient, imageDim * im.MagnificationCoefficient);
             im.TabIndex = 0;
             im.Click += new EventHandler(im_Click);
             panelStoredImages.Controls.Add(im);
             butRunDynamics.Enabled = true;
             UpdatePropertiesPB();
         }
     }
 }
Example #2
0
        /// <summary>
        /// Builder.
        /// Initializes a new instance of the <seealso cref="NeuralNetwork"/> class
        /// </summary>
        /// <param name="n">Number of neurons</param>
        public NeuralNetwork(int n)
        {
            this.NeuronsCount = n;
            Neurons = new List<Neuron>(n);
            for (int i = 0; i< n; i++)
            {
                Neuron neuron = new Neuron();
                neuron.State = 0;
                Neurons.Add(neuron);
            }

            Matrix = new int[n, n];
            PatternsCount = 0;

            for (int i = 0; i < n; i++)
                for (int j = 0; j < n; j++)
                {
                    Matrix[i, j] = 0;
                }
        }
Example #3
0
        /// <summary>
        /// Initializes a new instance of the <seealso cref="NeuralNetwork"/> class
        /// </summary>
        /// <param name="n">Number of neurons</param>
        public NeuralNetwork(int n)
        {
            this.n = n;
            neurons = new List<Neuron>(n);
            for (int i = 0; i< n; i++)
            {
                Neuron neuron = new Neuron();
                neuron.State = 0;
                neurons.Add(neuron);
            }

            T = new int[n, n];
            m = 0;

            for (int i = 0; i < n; i++)
                for (int j = 0; j < n; j++)
                {
                    T[i, j] = 0;
                }
        }
Example #4
0
 private void RunDynamicsBut_Click(object sender, EventArgs e)
 {
     if (!patternSelected)
     {
         MessageBox.Show("You must select initial pattern.", "Neural Network Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
     else
     {
         List<HopfieldNeuralNetwork.Neuron> initialState = new List<HopfieldNeuralNetwork.Neuron>(NN.N);
         for (int i = 0; i < imageDim; i++)
             for (int j=0;j<imageDim;j++)
             {
                 Neuron neuron = new Neuron();
                 if (imNNState.pixels[i, j] == Color.Black.ToArgb()) neuron.State = NeuronStates.AgainstField;
                 else if (imNNState.pixels[i, j] == Color.White.ToArgb()) neuron.State = NeuronStates.AlongField;
                 initialState.Add(neuron);
             }
         NN.Run(initialState);
         lblEnergy.Text = NN.Energy.ToString();
         patternSelected = false;
     }
 }
Example #5
0
        /// <summary>
        /// Adds a random pattern to interconnection matrix
        /// </summary>
        public void AddRandomPattern()
        {
            List<Neuron> randomPattern = new List<HopfieldNeuralNetwork.Neuron>(NeuronsCount);
            Random rnd = new Random();
            for (int i = 0; i < NeuronsCount; i++)
            {
                Neuron neuron = new Neuron();
                randomPattern.Add(neuron);

                int bit;
                bit = rnd.Next(2);
                if (bit == 0) randomPattern[i].State = NeuronStates.AlongField;
                else
                if (bit == 1) randomPattern[i].State = NeuronStates.AgainstField;
            }
            for (int i = 0; i < NeuronsCount; i++)
                for (int j = 0; j < NeuronsCount; j++)
                {
                    if (i == j) Matrix[i, j] = 0;
                    else Matrix[i, j] += randomPattern[i].State * randomPattern[j].State;
                }
            PatternsCount++;
        }
Example #6
0
        /// <summary>
        /// Realiza el pasaje del patron grafico a su interpretacion valida para la NNA de Hopfield
        /// </summary>
        /// <param name="pattern">Patron grafico</param>
        /// <returns>Patron neuronal valido para hopfield</returns>
        private List<Neuron> patternToNeurons(Pattern pattern)
        {
            var result = new List<Neuron>();
            for(int i = 0; i < pattern.Count; i++)
            {
                var neuron = new Neuron();
                neuron.State = pattern[i] == 1 ? NeuronStates.AgainstField : NeuronStates.AlongField;
                result.Add(neuron);
            }

            return result;
        }