Ejemplo n.º 1
0
        /// <summary>
        /// It traverses all connected synapses of the column and calculates the span, which synapses
        /// spans between all input bits. Then it calculates average of spans accross all dimensions.
        /// </summary>
        /// <param name="column"></param>
        /// <param name="htmConfig">Topology</param>
        /// <returns></returns>
        public static double CalcAvgSpanOfConnectedSynapses(Column column, HtmConfig htmConfig)
        {
            // Gets synapses connected to input bits.(from pool of the column)
            int[] connected = column.ProximalDendrite.GetConnectedSynapsesSparse();

            if (connected == null || connected.Length == 0)
            {
                return(0);
            }

            int[] maxCoord = new int[htmConfig.InputModuleTopology.Dimensions.Length];
            int[] minCoord = new int[maxCoord.Length];
            ArrayUtils.FillArray(maxCoord, -1);
            ArrayUtils.FillArray(minCoord, ArrayUtils.Max(htmConfig.InputModuleTopology.Dimensions));

            //
            // It takes all connected synapses
            for (int i = 0; i < connected.Length; i++)
            {
                maxCoord = ArrayUtils.MaxBetween(maxCoord, AbstractFlatMatrix.ComputeCoordinates(htmConfig.InputModuleTopology.Dimensions.Length,
                                                                                                 htmConfig.InputModuleTopology.DimensionMultiplies, htmConfig.InputModuleTopology.IsMajorOrdering, connected[i]));

                minCoord = ArrayUtils.MinBetween(minCoord, AbstractFlatMatrix.ComputeCoordinates(htmConfig.InputModuleTopology.Dimensions.Length,
                                                                                                 htmConfig.InputModuleTopology.DimensionMultiplies, htmConfig.InputModuleTopology.IsMajorOrdering, connected[i]));
            }

            return(ArrayUtils.Average(ArrayUtils.Add(ArrayUtils.Subtract(maxCoord, minCoord), 1)));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Uniform Column Mapping <br></br>
        /// Maps a column to its respective input index, keeping to the topology of the region. It takes the index of the column as an argument and determines
        /// what is the index of the flattened input vector that is to be the center of the column's potential pool. It distributes the columns over the inputs
        /// uniformly. The return value is an integer representing the index of the input bit. Examples of the expected output of this method:
        /// <list type="bullet">
        ///     <item>
        ///     If the topology is one dimensional, and the column index is 0, this method will return the input index 0. If the column index is 1, and there are
        ///     3 columns over 7 inputs, this method will return the input index 3.
        ///     </item>
        ///     <item>If the topology is two dimensional, with column dimensions [3, 5] and input dimensions [7, 11], and the column index is 3, the method returns
        ///     input index 8.
        ///     </item>
        /// </list>
        /// </summary>
        /// <param name="columnIndex">The index identifying a column in the permanence, potential and connectivity matrices.</param>
        /// <param name="colTop"></param>
        /// <param name="inpTop"></param>
        /// <returns>Flat index of mapped column.</returns>
        public static int MapColumn(int columnIndex, HtmModuleTopology colTop, HtmModuleTopology inpTop)
        {
            int[] columnCoords = AbstractFlatMatrix.ComputeCoordinates(colTop.NumDimensions,
                                                                       colTop.DimensionMultiplies, colTop.IsMajorOrdering, columnIndex);

            double[] colCoords = ArrayUtils.ToDoubleArray(columnCoords);

            double[] columnRatios = ArrayUtils.Divide(
                colCoords, ArrayUtils.ToDoubleArray(colTop.Dimensions), 0, 0);

            double[] inputCoords = ArrayUtils.Multiply(
                ArrayUtils.ToDoubleArray(inpTop.Dimensions), columnRatios, 0, 0);

            var colSpanOverInputs = ArrayUtils.Divide(
                ArrayUtils.ToDoubleArray(inpTop.Dimensions),
                ArrayUtils.ToDoubleArray(colTop.Dimensions), 0, 0);

            inputCoords = ArrayUtils.AddOffset(inputCoords, ArrayUtils.Multiply(colSpanOverInputs, 0.5));

            // Makes sure that inputCoords are in range [0, inpDims]
            int[] inputCoordInts = ArrayUtils.Clip(ArrayUtils.ToIntArray(inputCoords), inpTop.Dimensions, -1);

            return(AbstractFlatMatrix.ComputeIndex(inputCoordInts, inpTop.Dimensions, inpTop.NumDimensions,
                                                   inpTop.DimensionMultiplies, inpTop.IsMajorOrdering, true));
        }