/** * 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()); } } } }
public void testZeroActiveColumns() { TemporalMemory tm = new TemporalMemory(); Connections cn = new Connections(); Parameters p = GetDefaultParameters(); p.Apply(cn); TemporalMemory.Init(cn); int[] previousActiveColumns = { 0 }; Cell cell4 = cn.GetCell(4); DistalDendrite activeSegment = cn.CreateSegment(cell4); cn.CreateSynapse(activeSegment, cn.GetCell(0), 0.5); cn.CreateSynapse(activeSegment, cn.GetCell(1), 0.5); cn.CreateSynapse(activeSegment, cn.GetCell(2), 0.5); cn.CreateSynapse(activeSegment, cn.GetCell(3), 0.5); ComputeCycle cc = tm.Compute(cn, previousActiveColumns, true); Assert.IsFalse(cc.ActiveCells().Count == 0); Assert.IsFalse(cc.WinnerCells().Count == 0); Assert.IsFalse(cc.PredictiveCells().Count == 0); int[] zeroColumns = new int[0]; ComputeCycle cc2 = tm.Compute(cn, zeroColumns, true); Assert.IsTrue(cc2.ActiveCells().Count == 0); Assert.IsTrue(cc2.WinnerCells().Count == 0); Assert.IsTrue(cc2.PredictiveCells().Count == 0); }
public void testNewSegmentAddSynapsesToAllWinnerCells() { TemporalMemory tm = new TemporalMemory(); Connections cn = new Connections(); Parameters p = GetDefaultParameters(null, Parameters.KEY.MAX_NEW_SYNAPSE_COUNT, 4); p.Apply(cn); TemporalMemory.Init(cn); int[] previousActiveColumns = { 0, 1, 2 }; int[] activeColumns = { 4 }; ComputeCycle cc = tm.Compute(cn, previousActiveColumns, true); List <Cell> prevWinnerCells = new List <Cell>(cc.WinnerCells()); Assert.AreEqual(3, prevWinnerCells.Count); cc = tm.Compute(cn, activeColumns, true); List <Cell> winnerCells = new List <Cell>(cc.WinnerCells()); Assert.AreEqual(1, winnerCells.Count); List <DistalDendrite> segments = winnerCells[0].GetSegments(cn); Assert.AreEqual(1, segments.Count); List <Synapse> synapses = segments[0].GetAllSynapses(cn); List <Cell> presynapticCells = new List <Cell>(); foreach (Synapse synapse in synapses) { Assert.AreEqual(0.21, synapse.GetPermanence(), 0.01); presynapticCells.Add(synapse.GetPresynapticCell()); } presynapticCells.Sort(); Assert.IsTrue(prevWinnerCells.SequenceEqual(presynapticCells)); }
public void testActiveSegmentGrowSynapsesAccordingToPotentialOverlap() { TemporalMemory tm = new TemporalMemory(); Connections cn = new Connections(); Parameters p = GetDefaultParameters(null, Parameters.KEY.CELLS_PER_COLUMN, 1); p = GetDefaultParameters(p, Parameters.KEY.MIN_THRESHOLD, 1); p = GetDefaultParameters(p, Parameters.KEY.ACTIVATION_THRESHOLD, 2); p = GetDefaultParameters(p, Parameters.KEY.MAX_NEW_SYNAPSE_COUNT, 4); p.Apply(cn); TemporalMemory.Init(cn); // Use 1 cell per column so that we have easy control over the winner cells. int[] previousActiveColumns = { 0, 1, 2, 3, 4 }; HashSet <Cell> prevWinnerCells = new HashSet <Cell>(Arrays.AsList(0, 1, 2, 3, 4) .Select(i => cn.GetCell(i)) .ToList()); int[] activeColumns = { 5 }; DistalDendrite activeSegment = cn.CreateSegment(cn.GetCell(5)); cn.CreateSynapse(activeSegment, cn.GetCell(0), 0.5); cn.CreateSynapse(activeSegment, cn.GetCell(1), 0.5); cn.CreateSynapse(activeSegment, cn.GetCell(2), 0.2); ComputeCycle cc = tm.Compute(cn, previousActiveColumns, true); Assert.IsTrue(prevWinnerCells.SetEquals(cc.WinnerCells())); cc = tm.Compute(cn, activeColumns, true); HashSet <Cell> presynapticCells = new HashSet <Cell>(cn.GetSynapses(activeSegment) .Select(s => s.GetPresynapticCell()) .ToList()); Assert.IsTrue( presynapticCells.Count == 4 && ( !presynapticCells.Except(new List <Cell> { cn.GetCell(0), cn.GetCell(1), cn.GetCell(2), cn.GetCell(3) }).Any() || !presynapticCells.Except(new List <Cell> { cn.GetCell(0), cn.GetCell(1), cn.GetCell(2), cn.GetCell(4) }).Any())); }
public void testMatchingSegmentAddSynapsesToSubsetOfWinnerCells() { TemporalMemory tm = new TemporalMemory(); Connections cn = new Connections(); Parameters p = GetDefaultParameters(null, Parameters.KEY.CELLS_PER_COLUMN, 1); p = GetDefaultParameters(p, Parameters.KEY.MIN_THRESHOLD, 1); p.Apply(cn); TemporalMemory.Init(cn); int[] previousActiveColumns = { 0, 1, 2, 3 }; HashSet <Cell> prevWinnerCells = cn.GetCellSet(new int[] { 0, 1, 2, 3 }); int[] activeColumns = { 4 }; DistalDendrite matchingSegment = cn.CreateSegment(cn.GetCell(4)); cn.CreateSynapse(matchingSegment, cn.GetCell(0), 0.5); ComputeCycle cc = tm.Compute(cn, previousActiveColumns, true); Assert.IsTrue(cc.WinnerCells().SetEquals(prevWinnerCells)); cc = tm.Compute(cn, activeColumns, true); List <Synapse> synapses = cn.GetSynapses(matchingSegment); Assert.AreEqual(3, synapses.Count); synapses.Sort(); foreach (Synapse synapse in synapses) { if (synapse.GetPresynapticCell().GetIndex() == 0) { continue; } Assert.AreEqual(0.21, synapse.GetPermanence(), 0.01); Assert.IsTrue(synapse.GetPresynapticCell().GetIndex() == 1 || synapse.GetPresynapticCell().GetIndex() == 2 || synapse.GetPresynapticCell().GetIndex() == 3); } }