Exemple #1
0
        private void ActivateCellsInColumn(Connections conn, bool learn, Pair <Column, List <List <object> > > tuple, ComputeCycle cycle, ConcurrentDictionary <int, ComputeCycle> cycles, ISet <Cell> prevActiveCells, ISet <Cell> prevWinnerCells, double permanenceIncrement, double permanenceDecrement)
        {
            ColumnData activeColumnData = new ColumnData();

            activeColumnData.Set(tuple);

            if (activeColumnData.IsExistAnyActiveCol(cIndexofACTIVE_COLUMNS))
            {
                // If there are some active segments on the column already...
                if (activeColumnData.ActiveSegments != null && activeColumnData.ActiveSegments.Count > 0)
                {
                    //Debug.Write("A");

                    List <Cell> cellsOwnersOfActSegs = ActivatePredictedColumn(conn, activeColumnData.ActiveSegments,
                                                                               activeColumnData.MatchingSegments, prevActiveCells, prevWinnerCells,
                                                                               permanenceIncrement, permanenceDecrement, learn, cycle.ActiveSynapses);

                    ComputeCycle colCycle = new ComputeCycle();
                    cycles[tuple.Key.Index] = colCycle;

                    foreach (var item in cellsOwnersOfActSegs)
                    {
                        colCycle.ActiveCells.Add(item);
                        colCycle.WinnerCells.Add(item);
                    }
                }
                else
                {
                    Debug.Write("M");

                    //
                    // If no active segments are detected (start of learning) then all cells are activated
                    // and a random single cell is chosen as a winner.
                    BurstingResult burstingResult = BurstColumn(conn, activeColumnData.Column(), activeColumnData.MatchingSegments,
                                                                prevActiveCells, prevWinnerCells, permanenceIncrement, permanenceDecrement, conn.HtmConfig.Random,
                                                                learn);


                    ComputeCycle colCycle = new ComputeCycle();
                    cycles[tuple.Key.Index] = colCycle;

                    //
                    // Here we activate all cells by putting them to list of active cells.
                    foreach (var item in burstingResult.Cells)
                    {
                        colCycle.ActiveCells.Add(item);
                    }

                    colCycle.WinnerCells.Add((Cell)burstingResult.BestCell);
                }
            }
            else
            {
                if (learn)
                {
                    PunishPredictedColumn(conn, activeColumnData.ActiveSegments, activeColumnData.MatchingSegments,
                                          prevActiveCells, prevWinnerCells, conn.HtmConfig.PredictedSegmentDecrement);
                }
            }
        }
        /**
         * Calculate the active cells, using the current active columns and dendrite
         * segments. Grow and reinforce synapses.
         *
         * <pre>
         * Pseudocode:
         *   for each column
         *     if column is active and has active distal dendrite segments
         *       call activatePredictedColumn
         *     if column is active and doesn't have active distal dendrite segments
         *       call burstColumn
         *     if column is inactive and has matching distal dendrite segments
         *       call punishPredictedColumn
         *
         * </pre>
         *
         * @param conn
         * @param activeColumnIndices
         * @param learn
         */

        protected ComputeCycle ActivateCells(Connections conn, int[] activeColumnIndices, bool learn)
        {
            ComputeCycle cycle = new ComputeCycle();

            ColumnData activeColumnData = new ColumnData();

            List <Cell> prevActiveCells = conn.getActiveCells();
            List <Cell> prevWinnerCells = conn.getWinnerCells();

            // The list of active columns.
            List <Column> activeColumns = new List <Column>();

            foreach (var indx in activeColumnIndices.OrderBy(i => i))
            {
                activeColumns.Add(conn.getColumn(indx));
            }

            //Func<Object, Column> segToCol = segment => ((DistalDendrite)segment).getParentCell().getParentColumnIndex();

            Func <Object, Column> segToCol = (segment) =>
            {
                var colIndx   = ((DistalDendrite)segment).GetParentCell().getParentColumnIndex();
                var parentCol = this.connections.getMemory().GetColumn(colIndx);
                return(parentCol);
            };

            Func <object, Column> times1Fnc = x => (Column)x;

            var list = new Pair <List <object>, Func <object, Column> > [3];

            list[0] = new Pair <List <object>, Func <object, Column> >(Array.ConvertAll(activeColumns.ToArray(), item => (object)item).ToList(), times1Fnc);
            list[1] = new Pair <List <object>, Func <object, Column> >(Array.ConvertAll(conn.getActiveSegments().ToArray(), item => (object)item).ToList(), segToCol);
            list[2] = new Pair <List <object>, Func <object, Column> >(Array.ConvertAll(conn.getMatchingSegments().ToArray(), item => (object)item).ToList(), segToCol);

            GroupBy2 <Column> grouper = GroupBy2 <Column> .of(list);

            double permanenceIncrement = conn.HtmConfig.PermanenceIncrement;
            double permanenceDecrement = conn.HtmConfig.PermanenceDecrement;

            //
            // Grouping by columns, which have active and matching segments.
            foreach (var tuple in grouper)
            {
                Console.Write(":");
                activeColumnData = activeColumnData.Set(tuple);

                if (activeColumnData.IsExistAnyActiveCol(cIndexofACTIVE_COLUMNS))
                {
                    // If there are some active segments on the column already...
                    if (activeColumnData.ActiveSegments != null && activeColumnData.ActiveSegments.Count > 0)
                    {
                        Debug.Write(".");

                        List <Cell> cellsOwnersOfActSegs = ActivatePredictedColumn(conn, activeColumnData.ActiveSegments,
                                                                                   activeColumnData.MatchingSegments, prevActiveCells, prevWinnerCells,
                                                                                   permanenceIncrement, permanenceDecrement, learn);

                        foreach (var item in cellsOwnersOfActSegs)
                        {
                            cycle.ActiveCells.Add(item);
                            cycle.WinnerCells.Add(item);
                        }

                        //foreach (var item in cellsOwnersOfActSegs)
                        //{
                        //    cycle.WinnerCells.Add(item);
                        //}
                    }
                    else
                    {
                        Debug.Write("B.");
                        //
                        // If no active segments are detected (start of learning) then all cells are activated
                        // and a random single cell is chosen as a winner.
                        BurstingResult burstingResult = BurstColumn(conn, activeColumnData.Column(), activeColumnData.MatchingSegments,
                                                                    prevActiveCells, prevWinnerCells, permanenceIncrement, permanenceDecrement, conn.getRandom(),
                                                                    learn);

                        //
                        // Here we activate all cells by putting them to list of active cells.
                        foreach (var item in burstingResult.Cells)
                        {
                            cycle.ActiveCells.Add(item);
                        }

                        cycle.WinnerCells.Add((Cell)burstingResult.BestCell);
                    }
                }
                else
                {
                    if (learn)
                    {
                        punishPredictedColumn(conn, activeColumnData.ActiveSegments, activeColumnData.MatchingSegments,
                                              prevActiveCells, prevWinnerCells, conn.getPredictedSegmentDecrement());
                    }
                }
            }


            //int[] arr = new int[cycle.winnerCells.Count];
            //int count = 0;
            //foreach (Cell activeCell in cycle.winnerCells)
            //{
            //    arr[count] = activeCell.Index;
            //    count++;
            //}

            return(cycle);
        }