示例#1
0
        public void SerializeSegmentActivityTest()
        {
            SegmentActivity segment = new SegmentActivity();

            segment.ActiveSynapses = new Dictionary <int, int>();
            segment.ActiveSynapses.Add(23, 1);
            segment.ActiveSynapses.Add(24, 2);
            segment.ActiveSynapses.Add(35, 3);
            segment.PotentialSynapses = new Dictionary <int, int>();
            segment.PotentialSynapses.Add(2, 56);
            segment.PotentialSynapses.Add(22, 6);
            segment.PotentialSynapses.Add(24, 26);

            using (StreamWriter sw = new StreamWriter($"ser_{nameof(SerializeSegmentActivityTest)}.txt"))
            {
                segment.Serialize(sw);
            }
            using (StreamReader sr = new StreamReader($"ser_{nameof(SerializeSegmentActivityTest)}.txt"))

            {
                SegmentActivity segment1 = SegmentActivity.Deserialize(sr);

                Assert.IsTrue(segment1.Equals(segment));
            }
        }
示例#2
0
        public void SerializeSegmentActivityTest()
        {
            SegmentActivity segment = new SegmentActivity();

            using (StreamWriter sw = new StreamWriter($"ser_{nameof(SerializeSegmentActivityTest)}.txt"))
            {
                segment.Serialize(sw);
            }
            using (StreamReader sr = new StreamReader($"ser_{nameof(SerializeSegmentActivityTest)}.txt"))

            {
                SegmentActivity segment1 = SegmentActivity.Deserialize(sr);

                Assert.IsTrue(segment1.Equals(segment));
            }
        }
        /**
         * Calculate dendrite segment activity, using the current active cells.
         *
         * <pre>
         * Pseudocode:
         *   for each distal dendrite segment with activity >= activationThreshold
         *     mark the segment as active
         *   for each distal dendrite segment with unconnected activity >= minThreshold
         *     mark the segment as matching
         * </pre>
         *
         * @param conn     the Connectivity
         * @param cycle    Stores current compute cycle results
         * @param learn    If true, segment activations will be recorded. This information is used
         *                 during segment cleanup.
         */
        protected void ActivateDendrites(Connections conn, ComputeCycle cycle, bool learn)
        {
            SegmentActivity activity = conn.ComputeActivity(cycle.ActiveCells, conn.getConnectedPermanence());

            int i = 0;
            var activeSegments = new List <DistalDendrite>();

            foreach (var item in activity.ActiveSynapses)
            {
                if (item.Value >= conn.getActivationThreshold())
                {
                    activeSegments.Add(conn.GetSegmentForFlatIdx(item.Key));
                }
            }

            //
            // Step through all synapses on active cells and find involved segments.
            var matchingSegments = new List <DistalDendrite>();

            foreach (var item in activity.PotentialSynapses)
            {
                if (item.Value >= conn.getMinThreshold())
                {
                    matchingSegments.Add(conn.GetSegmentForFlatIdx(item.Key));
                }
            }

            //
            // Step through all synapses on active cells with permanence over threshold (conencted synapses)
            // and find involved segments.
            activeSegments.Sort(GetComparer(conn.getNextSegmentOrdinal()));

            matchingSegments.Sort(GetComparer(conn.getNextSegmentOrdinal()));

            cycle.ActiveSegments   = activeSegments;
            cycle.MatchingSegments = matchingSegments;

            conn.lastActivity = activity;
            conn.setActiveCells(new List <Cell>(cycle.ActiveCells));
            conn.setWinnerCells(new List <Cell>(cycle.WinnerCells));
            conn.setActiveSegments(activeSegments);
            conn.setMatchingSegments(matchingSegments);

            // Forces generation of the predictive cells from the above active segments
            conn.clearPredictiveCells();

            //ISet<Cell> predictiveCells = conn.getPredictiveCells();
            //string[] arr = new string[predictiveCells.Count];
            //foreach (Cell c in predictiveCells)
            //{
            //    arr[i] = c.Index + "-" + c.ParentColumnIndex;
            //    i++;
            //}

            //Debug.WriteLine($"ACT: {activity.Active.Count}, POT: {activity.Potential.Count}");
            //string output = string.Join("", predictiveCells);
            //Debug.WriteLine($"Active Segs: {activeSegments.Count} Matching segs: {matchingSegments.Count}, Predicted cells: {Helpers.StringifyVector(arr)}");
            //Debug.WriteLine("-----------------------------------------------------\n-----------------------------------------------------");

            if (learn)
            {
                foreach (var segment in activeSegments)
                {
                    conn.recordSegmentActivity(segment);
                }

                conn.startNewIteration();
            }
        }