/** * 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()); } } } }