Example #1
0
        public void TestFourSequences()
        {
            List <Object> sequence0 = new List <Object>(new Integer[] { 7, 12, 16 });
            List <Object> sequence1 = new List <Object>(new Integer[] { 3, 4, 5 });
            List <Object> sequence2 = new List <Object>(new Integer[] { 3, 3, 4, 5 });
            List <Object> sequence3 = new List <Object>(new Integer[] { 3, 3, 4, 5 });

            List <Pair <List <Object>, Func <Object, Integer> > > list = new List <Pair <List <object>, Func <object, Integer> > >();

            list.Add(new Pair <List <Object>, Func <Object, Integer> >(sequence0, m_Times1Fnc));
            list.Add(new Pair <List <Object>, Func <Object, Integer> >(sequence1, m_Times3Fnc));
            list.Add(new Pair <List <Object>, Func <Object, Integer> >(sequence2, m_Times4Fnc));
            list.Add(new Pair <List <Object>, Func <Object, Integer> >(sequence3, m_Times5Fnc));

            GroupBy2 <Integer> m = GroupBy2 <Integer> .Of(list.ToArray());

            List <Pair <Object, List <List <Object> > > > expectedValues = new List <Pair <Object, List <List <Object> > > >(
                new Pair <Object, List <List <Object> > >[]
            {
                new Pair <Object, List <List <Object> > >(7, new List <List <Object> >(getListOfList(getList(7), getList(m_None), getList(m_None), getList(m_None)))),
                new Pair <Object, List <List <Object> > >(9, new List <List <Object> >(getListOfList(getList(m_None), getList(3), getList(m_None), getList(m_None)))),
                new Pair <Object, List <List <Object> > >(12, new List <List <Object> >(getListOfList(getList(12), getList(4), getList(3, 3), getList(m_None)))),
                new Pair <Object, List <List <Object> > >(15, new List <List <Object> >(getListOfList(getList(m_None), getList(5), getList(m_None), getList(3, 3)))),
                new Pair <Object, List <List <Object> > >(16, new List <List <Object> >(getListOfList(getList(16), getList(m_None), getList(4), getList(m_None)))),
                new Pair <Object, List <List <Object> > >(20, new List <List <Object> >(getListOfList(getList(m_None), getList(m_None), getList(5), getList(4)))),
                new Pair <Object, List <List <Object> > >(25, new List <List <Object> >(getListOfList(getList(m_None), getList(m_None), getList(m_None), getList(5)))),
            });

            assertResults(m, expectedValues);
        }
Example #2
0
        private static void assertResults(GroupBy2 <Integer> grp, List <Pair <object, List <List <object> > > > expectedValues)
        {
            int i = 0;

            foreach (var t in grp)
            {
                if (t.Key is Integer)
                {
                    Assert.IsTrue(((Integer)t.Key).Value == (int)expectedValues[i].Key);
                }

                int j = 0;
                foreach (var o in t.Value)
                {
                    for (int k = 0; k < o.Count; k++)
                    {
                        if (o[k] is Integer)
                        {
                            Assert.IsTrue((Integer)o[k] == (Integer)expectedValues[i].Value[j][k]);
                        }
                        else
                        {
                            Assert.IsTrue(o[k] == (Object)expectedValues[i].Value[j][k]);
                        }
                    }

                    j++;
                }
                i++;
            }
        }
        /// <summary>
        /// <inheritdoc/>
        /// </summary>
        /// <param name="conn"></param>
        /// <param name="activeColumnIndices"></param>
        /// <param name="learn"></param>
        /// <returns></returns>
        protected override ComputeCycle ActivateCells(Connections conn, int[] activeColumnIndices, bool learn)
        {
            ComputeCycle cycle = new ComputeCycle
            {
                ActivColumnIndicies = activeColumnIndices
            };

            ConcurrentDictionary <int, ComputeCycle> cycles = new ConcurrentDictionary <int, ComputeCycle>();

            ISet <Cell> prevActiveCells = conn.ActiveCells;
            ISet <Cell> prevWinnerCells = conn.WinnerCells;

            // 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) =>
            {
                var colIndx   = ((DistalDendrite)segment).ParentCell.ParentColumnIndex;
                var parentCol = this.connections.HtmConfig.Memory.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.ActiveSegments.ToArray(), item => (object)item).ToList(), segToCol);
            list[2] = new Pair <List <object>, Func <object, Column> >(Array.ConvertAll(conn.MatchingSegments.ToArray(), item => (object)item).ToList(), segToCol);

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

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

            ParallelOptions opts = new ParallelOptions
            {
                MaxDegreeOfParallelism = Environment.ProcessorCount
            };

            //
            // Grouping by columns, which have active and matching segments.
            Parallel.ForEach(grouper, opts, (tuple) =>
            {
                ActivateCellsInColumn(conn, learn, tuple, cycle, cycles, prevActiveCells, prevWinnerCells, permanenceIncrement, permanenceDecrement);
            });

            foreach (var colCycle in cycles.Values)
            {
                cycle.ActiveCells.AddRange(colCycle.ActiveCells);
                cycle.WinnerCells.AddRange(colCycle.WinnerCells);
            }

            return(cycle);
        }
Example #4
0
        /**
         * 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
         */
        public void ActivateCells(Connections conn, ComputeCycle cycle, int[] activeColumnIndices, bool learn)
        {
            ColumnData columnData = new ColumnData();

            HashSet <Cell> prevActiveCells = conn.GetActiveCells();
            HashSet <Cell> prevWinnerCells = conn.GetWinnerCells();

            List <Column> activeColumns = activeColumnIndices
                                          .OrderBy(i => i)
                                          .Select(i => conn.GetColumn(i))
                                          .ToList();

            Func <Column, Column>         identity = c => c;
            Func <DistalDendrite, Column> segToCol = segment => segment.GetParentCell().GetColumn();

            //@SuppressWarnings({ "rawtypes" })
            GroupBy2 <Column> grouper = GroupBy2 <Column> .Of(
                new Tuple <List <object>, Func <object, Column> >(activeColumns.Cast <object>().ToList(), x => identity((Column)x)),
                new Tuple <List <object>, Func <object, Column> >(new List <DistalDendrite>(conn.GetActiveSegments()).Cast <object>().ToList(), x => segToCol((DistalDendrite)x)),
                new Tuple <List <object>, Func <object, Column> >(new List <DistalDendrite>(conn.GetMatchingSegments()).Cast <object>().ToList(), x => segToCol((DistalDendrite)x)));

            double permanenceIncrement = conn.GetPermanenceIncrement();
            double permanenceDecrement = conn.GetPermanenceDecrement();

            foreach (Tuple t in grouper)
            {
                columnData = columnData.Set(t);

                if (columnData.IsNotNone(ACTIVE_COLUMNS))
                {
                    if (columnData.ActiveSegments().Any())
                    {
                        List <Cell> cellsToAdd = ActivatePredictedColumn(conn, columnData.ActiveSegments(),
                                                                         columnData.MatchingSegments(), prevActiveCells, prevWinnerCells,
                                                                         permanenceIncrement, permanenceDecrement, learn);

                        cycle.ActiveCells().UnionWith(cellsToAdd);
                        cycle.WinnerCells().UnionWith(cellsToAdd);
                    }
                    else
                    {
                        Tuple cellsXwinnerCell = BurstColumn(conn, columnData.Column(), columnData.MatchingSegments(),
                                                             prevActiveCells, prevWinnerCells, permanenceIncrement, permanenceDecrement, conn.GetRandom(),
                                                             learn);

                        cycle.ActiveCells().UnionWith((IEnumerable <Cell>)cellsXwinnerCell.Get(0));
                        cycle.WinnerCells().Add((Cell)cellsXwinnerCell.Get(1));
                    }
                }
                else
                {
                    if (learn)
                    {
                        PunishPredictedColumn(conn, columnData.ActiveSegments(), columnData.MatchingSegments(),
                                              prevActiveCells, prevWinnerCells, conn.GetPredictedSegmentDecrement());
                    }
                }
            }
        }
Example #5
0
        public void testFourSequences()
        {
            List <int> sequence0 = new List <int> {
                7, 12, 16
            };
            List <int> sequence1 = new List <int> {
                3, 4, 5
            };
            List <int> sequence2 = new List <int> {
                3, 3, 4, 5
            };
            List <int> sequence3 = new List <int> {
                3, 3, 4, 5
            };

            Func <object, int> identity = ig => (int)ig;
            Func <object, int> times3   = x => (int)x * 3;
            Func <object, int> times4   = x => (int)x * 4;
            Func <object, int> times5   = x => (int)x * 5;

            //@SuppressWarnings({ "unchecked", "rawtypes" })
            GroupBy2 <int> m = GroupBy2 <int> .Of(
                new Tuple <List <object>, Func <object, int> >(sequence0.Cast <object>().ToList(), identity),
                new Tuple <List <object>, Func <object, int> >(sequence1.Cast <object>().ToList(), times3),
                new Tuple <List <object>, Func <object, int> >(sequence2.Cast <object>().ToList(), times4),
                new Tuple <List <object>, Func <object, int> >(sequence3.Cast <object>().ToList(), times5));

            List <Tuple> expectedValues = new List <Tuple>
            {
                new Tuple(7, list(7), none, none, none),
                new Tuple(9, none, list(3), none, none),
                new Tuple(12, list(12), list(4), list(3, 3), none),
                new Tuple(15, none, list(5), none, list(3, 3)),
                new Tuple(16, list(16), none, list(4), none),
                new Tuple(20, none, none, list(5), list(4)),
                new Tuple(25, none, none, none, list(5))
            };

            int i = 0;

            foreach (Tuple t in m)
            {
                int j = 0;
                foreach (Object o in t.All())
                {
                    if (o is IList)
                    {
                        Assert.IsTrue(Arrays.AreEqualList((IList)o, (IList)expectedValues[i].Get(j)), "grouped list has a problem");
                    }
                    else
                    {
                        Assert.AreEqual(o, expectedValues[i].Get(j));
                    }
                    j++;
                }
                i++;
            }
        }
Example #6
0
        //Entry point of application
        public static void Main(string[] args)
        {
            //Invoke the linq grouping examples within the project
            GroupBy1.QuerySyntaxExample();
            GroupBy1.MethodSyntaxExample();

            GroupBy2.QuerySyntaxExample();
            GroupBy2.MethodSyntaxExample();

            GroupBy3.QuerySyntaxExample();
            GroupBy3.MethodSyntaxExample();

            GroupByNested1.QuerySyntaxExample();

            GroupByComparer1.MethodSyntaxExample();

            GroupByComparer2.MethodSyntaxExample();
        }
Example #7
0
        public void TestOneSequence()
        {
            List <Object> sequence0 = new List <Object>(new Integer[] { 7, 12, 12, 16 });

            List <Pair <List <Object>, Func <Object, Integer> > > list = new List <Pair <List <object>, Func <object, Integer> > >();

            list.Add(new Pair <List <Object>, Func <Object, Integer> >(sequence0, m_Times1Fnc));
            GroupBy2 <Integer> m = GroupBy2 <Integer> .Of(list.ToArray());

            List <Pair <Object, List <List <Object> > > > expectedValues = new List <Pair <Object, List <List <Object> > > >(
                new Pair <Object, List <List <Object> > >[]
            {
                new Pair <Object, List <List <Object> > >(7, getListOfList(getList(7))),
                new Pair <Object, List <List <Object> > >(12, getListOfList(getList(12, 12))),
                new Pair <Object, List <List <Object> > >(16, getListOfList(getList(16))),
            });

            assertResults(m, expectedValues);
        }
Example #8
0
        public void TestTwoSequences2()
        {
            List <Object> sequence0 = new List <Object>(new Object[] { new Integer(9) });
            List <Object> sequence1 = new List <Object>(new Object[] { new Integer(3), new Integer(4) });

            List <Pair <List <Object>, Func <Object, Integer> > > list = new List <Pair <List <object>, Func <object, Integer> > >();

            list.Add(new Pair <List <Object>, Func <Object, Integer> >(sequence0, m_Times1Fnc));
            list.Add(new Pair <List <Object>, Func <Object, Integer> >(sequence1, m_Times3Fnc));
            GroupBy2 <Integer> group = GroupBy2 <Integer> .Of(list.ToArray());

            List <Pair <Object, List <List <Object> > > > expectedValues = new List <Pair <Object, List <List <Object> > > >(
                new Pair <Object, List <List <Object> > >[]
            {
                new Pair <Object, List <List <Object> > >(9, getListOfList(getList(new Integer(9)), getList(3))),
                new Pair <Object, List <List <Object> > >(12, getListOfList(getList(m_None), getList(4))),
            });

            assertResults(group, expectedValues);
        }
Example #9
0
        public void TestOneSequence()
        {
            List <int> sequence0 = new List <int> {
                7, 12, 12, 16
            };

            Func <int, int> identity = ig => (int)ig;

            //@SuppressWarnings({ "unchecked", "rawtypes" })
            GroupBy2 <int> m = GroupBy2 <int> .Of(
                new Tuple <List <object>, Func <object, int> >(sequence0.Cast <object>().ToList(), x => identity((int)x)));

            List <Tuple> expectedValues = new List <Tuple>
            {
                new Tuple(7, list(7)),
                new Tuple(12, list(12, 12)),
                new Tuple(16, list(16))
            };

            int i = 0;

            foreach (Tuple t in m)
            {
                int j = 0;
                foreach (object o in t.All())
                {
                    if (o is IList)
                    {
                        Assert.IsTrue(Arrays.AreEqualList((IList)o, (IList)expectedValues[i].Get(j)), "grouped list has a problem");
                    }
                    else
                    {
                        Assert.AreEqual(o, expectedValues[i].Get(j));
                    }
                    j++;
                }
                i++;
            }
        }
        /**
         * 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);
        }