예제 #1
0
        public void NoiseTest3()
        {
            List <double[]> inputSequences = new List <double[]>()
            {
                new double[] { 2.0, 1.0, 3.0, 4.0 },
                new double[] { 3.0, 5.0 },
                new double[] { 4.0, 4.0, 2.0, 1.0 }
            };

            InputParameters inputs = new InputParameters();

            inputs.setWidth(25);
            inputs.setMaxIndex(100.0);
            inputs.setRadius(-1.0);
            inputs.setCompareNumber(1.0);

            ProcessTestCase(inputSequences, inputs);
        }
예제 #2
0
        /// <summary>
        /// Returns the input vectors as array of integers
        /// </summary>
        /// <param name="inputSequence">An array of double, consisting the starting indexes for each input vector</param>
        /// <param name="min">Minimum index in the input vector plane</param>
        /// <param name="max">Maximum index in the input vector plane</param>
        /// <param name="inputs">
        /// </param>
        /// <param name="outFolder">The path where the input vectors are to be generated</param>
        /// <returns></returns>
        static List <int[]> GetEncodedSequence(double[] inputSequence, double min, double max, InputParameters inputs, string outFolder)
        {
            List <int[]> sdrList = new List <int[]>();

            DateTime now = DateTime.Now;

            ScalarEncoder encoder = new ScalarEncoder(new Dictionary <string, object>()
            {
                { "W", inputs.getWidth() },
                { "N", 1024 },
                { "Radius", inputs.getRadius() },
                { "MinVal", min },
                { "MaxVal", max },
                { "Periodic", false },
                { "Name", "scalar" },
                { "ClipInput", false },
            });

            foreach (var i in inputSequence)
            {
                var result = encoder.Encode(i);

                sdrList.Add(result);

                int[,] twoDimenArray = ArrayUtils.Make2DArray <int>(result, (int)Math.Sqrt(result.Length), (int)Math.Sqrt(result.Length));
                var twoDimArray = ArrayUtils.Transpose(twoDimenArray);

                int counter = 0;
                if (File.Exists(outFolder + @"\\" + i + @".png"))
                {
                    counter = 1;
                    while (File.Exists(outFolder + @"\\" + i + @"-" + counter.ToString() + @".png"))
                    {
                        counter++;
                    }
                }

                if (counter == 0)
                {
                    NeoCortexUtils.DrawBitmap(twoDimArray, 1024, 1024, $"{outFolder}\\{i}.png", Color.Yellow, Color.Black, text: i.ToString());
                }
                else
                {
                    NeoCortexUtils.DrawBitmap(twoDimArray, 1024, 1024, $"{outFolder}\\{i}-{counter}.png", Color.Yellow, Color.Black, text: i.ToString());
                }
            }

            return(sdrList);
        }
예제 #3
0
        /// <summary>
        /// Processes the test cases for Noise Test taking the necessary parameters.
        /// It's the parent method which calls other utility methods to create the input vectors and the outputs.
        /// </summary>
        /// <param name="inputSequence">An array of double, consisting the starting indexes for each input vector</param>
        /// <param name="inputs">
        /// A parameter of the class "InputParameters", which contains all the input parameters needed as properties which can be set in a test case
        /// </param>
        /// <returns>Returns nothing</returns>
        public void ProcessTestCase(List <double[]> inputSequences, InputParameters inputs)
        {
            string path      = System.IO.Directory.GetCurrentDirectory();
            string parentDir = path.Substring(0, path.IndexOf("bin") - 1);
            string resultDir = parentDir.Substring(0, parentDir.LastIndexOf(@"\"));

            string timeStamp = DateTime.Now.ToString("yyyyMMddHHmmss");
            string outFolder = resultDir + @"\SpatialPooler_Results\" + timeStamp + @"\Output\";
            string inFolder  = resultDir + @"\SpatialPooler_Results\" + timeStamp + @"\InputVectors";

            if (!Directory.Exists(outFolder))
            {
                Directory.CreateDirectory(outFolder);
            }

            if (!Directory.Exists(inFolder + @"\"))
            {
                Directory.CreateDirectory(inFolder);
            }

            //int radius = 0;

            var parameters = GetDefaultParams();

            parameters.Set(KEY.POTENTIAL_RADIUS, 64 * 64);
            parameters.Set(KEY.POTENTIAL_PCT, 1.0);
            parameters.Set(KEY.GLOBAL_INHIBITION, false);
            parameters.Set(KEY.STIMULUS_THRESHOLD, 0.5);
            parameters.Set(KEY.INHIBITION_RADIUS, (int)0.25 * 64 * 64);
            parameters.Set(KEY.LOCAL_AREA_DENSITY, -1);
            parameters.Set(KEY.NUM_ACTIVE_COLUMNS_PER_INH_AREA, 0.1 * 64 * 64);
            parameters.Set(KEY.DUTY_CYCLE_PERIOD, 1000000);
            parameters.Set(KEY.MAX_BOOST, 5);
            Console.WriteLine("Fetched all default parameters\n");

            parameters.setInputDimensions(new int[] { 32, 32 });
            parameters.setColumnDimensions(new int[] { 64, 64 });
            parameters.setNumActiveColumnsPerInhArea(0.02 * 64 * 64);
            var sp  = new SpatialPoolerMT();
            var mem = new Connections();

            parameters.apply(mem);
            Console.WriteLine("\nConfiguring the Inputs...\n");
            sp.Init(mem, GetInMemoryDictionary());
            int outFolderCount = 0;

            int compareIndex = Convert.ToInt32(inputs.getCompareNumber());

            double[][] recordOutput    = null;
            double[]   hammingDistance = null;

            foreach (double[] inputSequence in inputSequences)
            {
                outFolderCount++;
                double minVal = 0.0;
                for (int i = 0; i < inputSequence.Length; i++)
                {
                    if (i == 0)
                    {
                        minVal = inputSequence[i];
                    }
                    else if (inputSequence[i] < minVal)
                    {
                        minVal = inputSequence[i];
                    }
                }
                minVal -= 1.0;

                Console.WriteLine("\nGetting the Input Vectors...\n");
                var inputVectors = GetEncodedSequence(inputSequence, minVal, inputs.getMaxIndex(), inputs, inFolder);

                int count = 1;
                //string output = String.Empty;
                int max = 0;
                for (int i = 0; i < inputVectors.Count; i++)
                {
                    hammingDistance = null;
                    //output = String.Empty;
                    Console.WriteLine("Computing the Output for the vector no: " + count.ToString() + "...\n");
                    var activeArray = sp.Compute(inputVectors[i], true) as int[];

                    for (int j = 0; j < activeArray.Length; j++)
                    {
                        if (activeArray[j] > max)
                        {
                            max = activeArray[j];
                        }
                    }

                    //var str = Helpers.StringifyVector(activeArray);

                    int rows    = Convert.ToInt32(Math.Ceiling(Math.Sqrt(Convert.ToDouble(max))));
                    int counter = 0;
                    int index   = 0;
                    int[,] outTwoDArray = new int[rows, rows];

                    for (int j = 0; j < rows; j++)
                    {
                        for (int k = 0; k < rows; k++)
                        {
                            outTwoDArray[j, k] = 0;
                        }
                    }

                    for (int j = 0; j < rows; j++)
                    {
                        for (int k = 0; k < rows; k++)
                        {
                            counter++;
                            if (index < activeArray.Length && activeArray[index] == counter)
                            {
                                index++;
                                outTwoDArray[j, k] = 1;
                            }
                        }
                    }

                    double[][] comparingArray = new double[rows][];
                    for (int j = 0; j < rows; j++)
                    {
                        comparingArray[j] = new double[rows];
                        for (int k = 0; k < rows; k++)
                        {
                            comparingArray[j][k] = Convert.ToDouble(outTwoDArray[j, k]);
                        }
                    }

                    int[,] record2Darray = null;
                    if (inputSequence[i] == compareIndex)
                    {
                        if (recordOutput != null)
                        {
                            hammingDistance = MathHelpers.GetHammingDistance(recordOutput, comparingArray, false);
                            record2Darray   = new int[recordOutput.Length, recordOutput.Length];
                            for (int j = 0; j < recordOutput.Length; j++)
                            {
                                for (int k = 0; k < recordOutput.Length; k++)
                                {
                                    record2Darray[j, k] = Convert.ToInt32(recordOutput[j][k]);
                                }
                            }
                        }

                        recordOutput = new double[rows][];

                        for (int j = 0; j < rows; j++)
                        {
                            recordOutput[j] = new double[rows];
                            for (int k = 0; k < rows; k++)
                            {
                                recordOutput[j][k] = comparingArray[j][k];
                            }
                        }
                    }

                    if (hammingDistance != null)
                    {
                        int rowHam = Convert.ToInt32(Math.Ceiling(Math.Sqrt(hammingDistance.Length)));
                        int[,] hammingArray = new int[rowHam, rowHam];
                        int limit = 0;

                        for (int j = 0; j < rowHam; j++)
                        {
                            for (int k = 0; k < rowHam; k++)
                            {
                                if (limit < hammingDistance.Length)
                                {
                                    //hj
                                    hammingArray[j, k] = Convert.ToInt32(hammingDistance[limit]);
                                    limit++;
                                }
                            }
                        }

                        int compare_no = 1;
                        if (!File.Exists($"{outFolder}\\Compare_{compareIndex}.png"))
                        {
                            DrawBitmapHamming(hammingArray, 1024, 1024, $"{outFolder}\\Compare_{compareIndex}.png", $"Compare_{compareIndex}");
                            DrawBitmapOverlap(record2Darray, outTwoDArray, 1024, 1024, $"{outFolder}\\Overlap_{compareIndex}.png", $"Overlap_{compareIndex}");
                        }
                        else
                        {
                            while (File.Exists($"{outFolder}\\Compare_{compareIndex}_{compare_no}.png"))
                            {
                                compare_no++;
                            }
                            DrawBitmapHamming(hammingArray, 1024, 1024, $"{outFolder}\\Compare_{compareIndex}_{compare_no}.png", $"Compare_{compareIndex}");
                            compare_no = 1;
                            while (File.Exists($"{outFolder}\\Overlap_{compareIndex}_{compare_no}.png"))
                            {
                                compare_no++;
                            }
                            DrawBitmapOverlap(record2Darray, outTwoDArray, 1024, 1024, $"{outFolder}\\Overlap_{compareIndex}_{compare_no}.png", $"Overlap_{compareIndex}");
                        }
                    }

                    if (!Directory.Exists(outFolder + @"\\" + outFolderCount.ToString()))
                    {
                        Directory.CreateDirectory(outFolder + @"\\" + outFolderCount.ToString());
                    }

                    int[,] out2dimArray = ArrayUtils.Transpose(outTwoDArray);
                    NeoCortexUtils.DrawBitmap(out2dimArray, 1024, 1024, $"{outFolder}\\{outFolderCount}\\{count}.png", Color.Black, Color.Green, text: inputSequence[i].ToString());

                    //File.WriteAllLines(outFolder + count.ToString() + ".txt", new string[] { str });
                    Console.WriteLine("Output is recorded in the path: " + outFolder + count.ToString() + ".txt\n");
                    count++;
                }
            }
        }