/// <summary>
        /// Takes in a Hopfield pattern and returns the pattern it most closely matches.
        /// </summary>
        /// <param name="inputPattern"></param>
        /// <returns></returns>
        public HopfieldPattern Recognize(HopfieldPattern inputPattern)
        {
            var recognizedPattern = HopfieldPattern.CreateEmptyPattern(inputPattern.Length);

            // Get value from each column and place it in an array.
            for(var colIndex=0; colIndex < inputPattern.Length; colIndex++)
            {
                // We need to sum the totals for each column
                var columnResult = 0.0;

                for(var rowIndex=0; rowIndex < inputPattern.Length; rowIndex++)
                {
                    columnResult += _WeightMatrix.GetValue(rowIndex, colIndex) * inputPattern[rowIndex];
                }

                // Then place each column's scalar result in the result vector.
                recognizedPattern[colIndex] = columnResult;
            }

            // switch from bipolar form
            for (var i = 0; i < recognizedPattern.Length; i++)
            {
                recognizedPattern[i] = recognizedPattern[i] < 0 ? 0 : 1;
            }

            // We have the result vector so return it as a pattern.
            return recognizedPattern;
        }
        /// <summary>
        /// Takes in a Hopfield pattern and returns the pattern it most closely matches.
        /// </summary>
        /// <param name="inputPattern"></param>
        /// <returns></returns>
        public HopfieldPattern Recognize(HopfieldPattern inputPattern)
        {
            var recognizedPattern = HopfieldPattern.CreateEmptyPattern(inputPattern.Length);

            // Get value from each column and place it in an array.
            for (var colIndex = 0; colIndex < inputPattern.Length; colIndex++)
            {
                // We need to sum the totals for each column
                var columnResult = 0.0;

                for (var rowIndex = 0; rowIndex < inputPattern.Length; rowIndex++)
                {
                    columnResult += _WeightMatrix.GetValue(rowIndex, colIndex) * inputPattern[rowIndex];
                }

                // Then place each column's scalar result in the result vector.
                recognizedPattern[colIndex] = columnResult;
            }

            // switch from bipolar form
            for (var i = 0; i < recognizedPattern.Length; i++)
            {
                recognizedPattern[i] = recognizedPattern[i] < 0 ? 0 : 1;
            }

            // We have the result vector so return it as a pattern.
            return(recognizedPattern);
        }
        public void Setup()
        {
            Pattern1 = new HopfieldPattern(0.0, 1.0, 0.0, 1.0);
            Pattern2 = new HopfieldPattern(1.0, 0.0, 0.0, 1.0);

            Network = new HopfieldNetwork(4);

            Network.Train(Pattern1);
            Network.Train(Pattern2);
        }
 public void Train(HopfieldPattern pattern)
 {
     _WeightMatrix = _WeightMatrix.Add(new Matrix(pattern.GetWeightMatrix()));
 }
        private void RecognizeButton_Click(object sender, RoutedEventArgs e)
        {
            var colCount = 5;
            var rowCount = 5;

            var pattern = new double[colCount * rowCount];

            for (var row = 0; row < rowCount; row++)
            {
                for (var col = 0; col < colCount; col++)
                {
                    pattern[row * colCount + col] = ((ToggleButton)_GetControlIn(DrawingGrid, new GridCoordinates(row, col))).IsChecked.Value
                        ? 1.0
                        : 0.0;
                }
            }

            var hopfieldPattern = new HopfieldPattern(pattern);
            var recognizedPattern = hopfieldNetwork.Recognize(hopfieldPattern);

            for (var row = 0; row < rowCount; row++)
            {
                for (var col = 0; col < colCount; col++)
                {
                    ((ToggleButton) _GetControlIn(DrawingGrid, new GridCoordinates(row, col))).IsChecked =
                        recognizedPattern[row*colCount + col] <= 0
                            ? false
                            : true;
                }
            }
        }
        private void TrainButton_Click(object sender, RoutedEventArgs e)
        {
            var colCount = 5;
            var rowCount = 5;

            var pattern = new double[colCount * rowCount];

            for(var row=0; row<rowCount; row++)
            {
                for(var col=0; col<colCount; col++)
                {
                    pattern[row*colCount + col] = ((ToggleButton)_GetControlIn(DrawingGrid, new GridCoordinates(row, col))).IsChecked.Value
                        ? 1.0
                        : 0.0;
                }
            }

            var hopfieldPattern = new HopfieldPattern(pattern);
            hopfieldNetwork = hopfieldNetwork ?? new HopfieldNetwork(25);
            hopfieldNetwork.Train(hopfieldPattern);
        }
 public void Train(HopfieldPattern pattern)
 {
     _WeightMatrix = _WeightMatrix.Add(new Matrix(pattern.GetWeightMatrix()));
 }