Пример #1
0
class Column:
    """
	A class only to group properties related to columns.
	"""

    # region Constructor

    def __init__(self):
        """
		Initializes a new instance of the Column class.
		"""

        # region Instance fields

        self.x = -1
        """Position on X axis"""

        self.y = -1
        """Position on Y axis"""

        self.segment = Segment(SegmentType.proximal)
        """Proximal segment of this column"""

        self.cells = []
        """List of cells that compose this column."""

        # region 3d-tree properties (simulation form)

        self.tree3d_x = 0
        self.tree3d_y = 0
        self.tree3d_z = 0

        # endregion

        # endregion

        # endregion

        # region Methods

    def getCell(self, z):
        """
		Return the cell located at given position
		"""

        for cell in self.cells:
            if cell.z == z:
                return cell

    def nextTimeStep(self):
        """
		Perfoms actions related to time step progression.
		"""

        self.segment.nextTimeStep()
        for cell in self.cells:
            cell.nextTimeStep()
Пример #2
0
class Column:
    """
	A class only to group properties related to columns.
	"""

    #region Constructor

    def __init__(self):
        """
		Initializes a new instance of this class.
		"""

        #region Instance fields

        self.x = -1
        """Position on X axis"""

        self.y = -1
        """Position on Y axis"""

        self.segment = Segment(SegmentType.proximal)
        """Proximal segment of this column"""

        self.cells = []
        """List of cells that compose this column."""

        #region 3d-tree properties (simulation form)

        self.tree3d_x = 0
        self.tree3d_y = 0
        self.tree3d_z = 0

        #endregion

        #endregion

    #endregion

    #region Methods

    def getCell(self, z):
        """
		Return the cell located at given position
		"""

        for cell in self.cells:
            if cell.z == z:
                return cell

    def nextStep(self):
        """
		Perfoms actions related to time step progression.
		"""

        self.segment.nextStep()
        for cell in self.cells:
            cell.nextStep()
Пример #3
0
    def __init__(self):
        """
		Initializes a new instance of this class.
		"""

        #region Instance fields

        self.x = -1
        """Position on X axis"""

        self.y = -1
        """Position on Y axis"""

        self.segment = Segment(SegmentType.proximal)
        """Proximal segment of this column"""

        self.cells = []
        """List of cells that compose this column."""

        #region 3d-tree properties (simulation form)

        self.tree3d_x = 0
        self.tree3d_y = 0
        self.tree3d_z = 0
Пример #4
0
    def __init__(self):
        """
		Initializes a new instance of the Column class.
		"""

        # region Instance fields

        self.x = -1
        """Position on X axis"""

        self.y = -1
        """Position on Y axis"""

        self.segment = Segment(SegmentType.proximal)
        """Proximal segment of this column"""

        self.cells = []
        """List of cells that compose this column."""

        # region 3d-tree properties (simulation form)

        self.tree3d_x = 0
        self.tree3d_y = 0
        self.tree3d_z = 0
Пример #5
0
	def performTemporalProcess(self):
		"""
		Perfoms spatial process.
		"""

		# First we should convert from float array to integer set
		activeColumnsSet = set()
		for colIdx in range(len(self.columns)):
			column = self.columns[colIdx]
			if column.segment.isActive[maxTimeSteps - 1]:
				activeColumnsSet.add(colIdx)

		# Send active columns to Temporal Pooler and get processed output (i.e. the predicting cells)
		self.temporalPooler.compute(activeColumnsSet, self.enableTemporalPooling)

		# Update cells, distal segments and synapses according to active columns
		for colIdx in range(len(self.columns)):
			column = self.columns[colIdx]

			for cell in column.cells:
				cellIdx = (colIdx * self.numCellsPerColumn) + column.cells.index(cell)
				if cell.index == -1:
					cell.index = cellIdx

				# Update cell's state following the priority
				if cellIdx in self.temporalPooler.winnerCells:
					cell.isLearning[maxTimeSteps - 1] = True
				if cellIdx in self.temporalPooler.activeCells:
					cell.isActive[maxTimeSteps - 1] = True
				if cellIdx in self.temporalPooler.predictiveCells:
					cell.isPredicted[maxTimeSteps - 1] = True

					# Mark proximal segment and its connected synapses as predicted
					column.segment.isPredicted[maxTimeSteps - 1] = True
					for synapse in column.segment.synapses:
						if synapse.isConnected[maxTimeSteps - 1]:
							synapse.isPredicted[maxTimeSteps - 1] = True
							synapse.inputElem.isPredicted[maxTimeSteps - 1] = True

				# Get the indexes of the distal segments of this cell
				segmentsForCell = self.temporalPooler.connections.segmentsForCell(cellIdx)

				# Add the segments that appeared after last iteration
				for segIdx in segmentsForCell:
					# Check if segment already exists in the cell
					segFound = False
					for segment in cell.segments:
						if segment.index == segIdx:
							segFound = True
							break

					# If segment is new, add it to cell
					if not segFound:
						segment = Segment(SegmentType.distal)
						segment.index = segIdx
						cell.segments.append(segment)

				# Update distal segments
				for segment in cell.segments:
					segIdx = segment.index

					# If segment not found in segments indexes returned in last iteration mark it as removed
					if segIdx in segmentsForCell:

						# Update segment's state
						if segIdx in self.temporalPooler.activeSegments:
							segment.isActive[maxTimeSteps - 1] = True
						else:
							segment.isActive[maxTimeSteps - 1] = False

						# Get the indexes of the synapses of this segment
						synapsesForSegment = self.temporalPooler.connections.synapsesForSegment(segIdx)

						# Add the synapses that appeared after last iteration
						for synIdx in synapsesForSegment:
							# Check if synapse already exists in the segment
							synFound = False
							for synapse in segment.synapses:
								if synapse.index == synIdx:
									synFound = True
									break

							# If synapse is new, add it to segment
							if not synFound:
								synapse = Synapse()
								synapse.index = synIdx
								segment.synapses.append(synapse)

						# Update synapses
						for synapse in segment.synapses:
							synIdx = synapse.index

							# If synapse not found in synapses indexes returned in last iteration mark it as removed
							if synIdx in synapsesForSegment:

								# Update synapse's state
								(_, sourceCellIdx, permanence) = self.temporalPooler.connections.dataForSynapse(synIdx)
								synapse.permanence[maxTimeSteps - 1] = permanence
								if permanence >= self.distalSynConnectedPerm:
									synapse.isConnected[maxTimeSteps - 1] = True
								else:
									synapse.isConnected[maxTimeSteps - 1] = False

								# Get cell given cell's index
								for sourceColumn in self.columns:
									for sourceCell in sourceColumn.cells:
										if sourceCell.index == sourceCellIdx:
											synapse.inputElem = sourceCell
							else:
								synapse.isRemoved[maxTimeSteps - 1] = True
					else:
						segment.isRemoved[maxTimeSteps - 1] = True
Пример #6
0
    def updateTemporalElements(self):
        """
		Update elements regarding temporal pooler
		"""

        # Update cells, distal segments and synapses according to active columns
        for colIdx in range(len(self.columns)):
            column = self.columns[colIdx]

            # Mark proximal segment and its connected synapses as predicted
            if column.segment.isPredicted[maxStoredSteps - 1]:
                for synapse in column.segment.synapses:
                    if synapse.isConnected[maxStoredSteps - 1]:
                        synapse.isPredicted[maxStoredSteps - 1] = True
                        synapse.inputElem.isPredicted[maxStoredSteps -
                                                      1] = True

            for cell in column.cells:
                cellIdx = cell.index

                # Update cell's states
                if cellIdx in self.temporalPooler.winnerCells:
                    cell.isLearning[maxStoredSteps - 1] = True
                if cellIdx in self.temporalPooler.activeCells:
                    cell.isActive[maxStoredSteps - 1] = True
                if cellIdx in self.temporalPooler.predictiveCells:
                    cell.isPredicted[maxStoredSteps - 1] = True

                # Get the indexes of the distal segments of this cell
                segmentsForCell = self.temporalPooler.connections.segmentsForCell(
                    cellIdx)

                # Add the segments that appeared after last iteration
                for segIdx in segmentsForCell:
                    # Check if segment already exists in the cell
                    segFound = False
                    for segment in cell.segments:
                        if segment.indexTP == segIdx:
                            segFound = True
                            break

                    # If segment is new, add it to cell
                    if not segFound:
                        segment = Segment(SegmentType.distal)
                        segment.indexTP = segIdx
                        cell.segments.append(segment)

                # Update distal segments
                for segment in cell.segments:
                    segIdx = segment.indexTP

                    # If segment not found in segments indexes returned in last iteration mark it as removed
                    if segIdx in segmentsForCell:

                        # Update segment's state
                        if segIdx in self.temporalPooler.activeSegments:
                            segment.isActive[maxStoredSteps - 1] = True
                        else:
                            segment.isActive[maxStoredSteps - 1] = False

                        # Get the indexes of the synapses of this segment
                        synapsesForSegment = self.temporalPooler.connections.synapsesForSegment(
                            segIdx)

                        # Add the synapses that appeared after last iteration
                        for synIdx in synapsesForSegment:
                            # Check if synapse already exists in the segment
                            synFound = False
                            for synapse in segment.synapses:
                                if synapse.indexTP == synIdx:
                                    synFound = True
                                    break

                            # If synapse is new, add it to segment
                            if not synFound:
                                synapse = Synapse()
                                synapse.indexTP = synIdx
                                segment.synapses.append(synapse)

                        # Update synapses
                        for synapse in segment.synapses:
                            synIdx = synapse.indexTP

                            # If synapse not found in synapses indexes returned in last iteration mark it as removed
                            if synIdx in synapsesForSegment:

                                # Update synapse's state
                                (
                                    _, sourceCellIdx, permanence
                                ) = self.temporalPooler.connections.dataForSynapse(
                                    synIdx)
                                synapse.permanence[maxStoredSteps -
                                                   1] = permanence
                                if permanence >= self.distalSynConnectedPerm:
                                    synapse.isConnected[maxStoredSteps -
                                                        1] = True
                                else:
                                    synapse.isConnected[maxStoredSteps -
                                                        1] = False

                                # Get cell given cell's index
                                for sourceColumn in self.columns:
                                    for sourceCell in sourceColumn.cells:
                                        if sourceCell.index == sourceCellIdx:
                                            synapse.inputElem = sourceCell
                            else:
                                synapse.isRemoved[maxStoredSteps - 1] = True
                    else:
                        segment.isRemoved[maxStoredSteps - 1] = True
Пример #7
0
	def updateTemporalElements(self):
		"""
		Update elements regarding temporal pooler
		"""

		# Update cells, distal segments and synapses according to active columns
		for colIdx in range(len(self.columns)):
			column = self.columns[colIdx]

			# Mark proximal segment and its connected synapses as predicted
			if column.segment.isPredicted[maxPreviousSteps - 1]:
				for synapse in column.segment.synapses:
					if synapse.isConnected[maxPreviousSteps - 1]:
						synapse.isPredicted[maxPreviousSteps - 1] = True
						synapse.inputElem.isPredicted[maxPreviousSteps - 1] = True

			# Mark proximal segment and its connected synapses that were predicted but are not active now
			if column.segment.isPredicted[maxPreviousSteps - 2]:
				if not column.segment.isActive[maxPreviousSteps - 1]:
					column.segment.isFalselyPredicted[maxPreviousSteps - 1] = True
				for synapse in column.segment.synapses:
					if (synapse.isPredicted[maxPreviousSteps - 2] and not synapse.isConnected[maxPreviousSteps - 1]) or (synapse.isConnected[maxPreviousSteps - 1] and synapse.inputElem.isFalselyPredicted[maxPreviousSteps - 1]):
						synapse.isFalselyPredicted[maxPreviousSteps - 1] = True

			for cell in column.cells:
				cellIdx = cell.index

				# Update cell's states
				if cellIdx in self.temporalPooler.winnerCells:
					cell.isLearning[maxPreviousSteps - 1] = True
				if cellIdx in self.temporalPooler.activeCells:
					cell.isActive[maxPreviousSteps - 1] = True
				if cellIdx in self.temporalPooler.predictiveCells:
					cell.isPredicted[maxPreviousSteps - 1] = True
				if cell.isPredicted[maxPreviousSteps - 2] and not cell.isActive[maxPreviousSteps - 1]:
					cell.isFalselyPredicted[maxPreviousSteps - 1] = True

				# Get the indexes of the distal segments of this cell
				segmentsForCell = self.temporalPooler.connections.segmentsForCell(cellIdx)

				# Add the segments that appeared after last iteration
				for segIdx in segmentsForCell:
					# Check if segment already exists in the cell
					segFound = False
					for segment in cell.segments:
						if segment.indexTP == segIdx:
							segFound = True
							break

					# If segment is new, add it to cell
					if not segFound:
						segment = Segment(SegmentType.distal)
						segment.indexTP = segIdx
						cell.segments.append(segment)

				# Update distal segments
				for segment in cell.segments:
					segIdx = segment.indexTP

					# If segment not found in segments indexes returned in last iteration mark it as removed
					if segIdx in segmentsForCell:

						# Update segment's state
						if segIdx in self.temporalPooler.activeSegments:
							segment.isActive[maxPreviousSteps - 1] = True
						else:
							segment.isActive[maxPreviousSteps - 1] = False

						# Get the indexes of the synapses of this segment
						synapsesForSegment = self.temporalPooler.connections.synapsesForSegment(segIdx)

						# Add the synapses that appeared after last iteration
						for synIdx in synapsesForSegment:
							# Check if synapse already exists in the segment
							synFound = False
							for synapse in segment.synapses:
								if synapse.indexTP == synIdx:
									synFound = True
									break

							# If synapse is new, add it to segment
							if not synFound:
								synapse = Synapse()
								synapse.indexTP = synIdx
								segment.synapses.append(synapse)

						# Update synapses
						for synapse in segment.synapses:
							synIdx = synapse.indexTP

							# If synapse not found in synapses indexes returned in last iteration mark it as removed
							if synIdx in synapsesForSegment:

								# Update synapse's state
								(_, sourceCellAbsIdx, permanence) = self.temporalPooler.connections.dataForSynapse(synIdx)
								synapse.permanence[maxPreviousSteps - 1] = permanence
								if permanence >= self.distalSynConnectedPerm:
									synapse.isConnected[maxPreviousSteps - 1] = True
								else:
									synapse.isConnected[maxPreviousSteps - 1] = False

								# Get cell given cell's index
								sourceColIdx = sourceCellAbsIdx / self.numCellsPerColumn
								sourceCellRelIdx = sourceCellAbsIdx % self.numCellsPerColumn
								sourceCell = self.columns[sourceColIdx].cells[sourceCellRelIdx]
								synapse.inputElem = sourceCell
							else:
								synapse.isRemoved[maxPreviousSteps - 1] = True
					else:
						segment.isRemoved[maxPreviousSteps - 1] = True
Пример #8
0
    def updateTemporalElements(self):
        """
		Update elements regarding temporal pooler
		"""

        # Update cells, distal segments and synapses according to active columns
        for colIdx in range(len(self.columns)):
            column = self.columns[colIdx]

            # Mark proximal segment and its connected synapses as predicted
            if column.segment.isPredicted.atCurrStep():
                for synapse in column.segment.synapses:
                    if synapse.isConnected.atCurrStep():
                        synapse.isPredicted.setForCurrStep(True)
                        synapse.inputElem.isPredicted.setForCurrStep(True)

            # Mark proximal segment and its connected synapses that were predicted but are not active now
            if column.segment.isPredicted.atPreviousStep():
                if not column.segment.isActive.atCurrStep():
                    column.segment.isFalselyPredicted.setForCurrStep(True)
                for synapse in column.segment.synapses:
                    if (synapse.isPredicted.atPreviousStep()
                            and not synapse.isConnected.atCurrStep()
                        ) or (
                            synapse.isConnected.atCurrStep() and
                            synapse.inputElem.isFalselyPredicted.atCurrStep()):
                        synapse.isFalselyPredicted.setForCurrStep(True)

            for cell in column.cells:
                cellIdx = cell.index

                # Update cell's states
                if cellIdx in self.temporalPooler.winnerCells:
                    cell.isLearning.setForCurrStep(True)
                if cellIdx in self.temporalPooler.activeCells:
                    cell.isActive.setForCurrStep(True)
                if cellIdx in self.temporalPooler.predictiveCells:
                    cell.isPredicted.setForCurrStep(True)
                if cell.isPredicted.atPreviousStep(
                ) and not cell.isActive.atCurrStep():
                    cell.isFalselyPredicted.setForCurrStep(True)

                # Get the indexes of the distal segments of this cell
                segmentsForCell = self.temporalPooler.connections.segmentsForCell(
                    cellIdx)

                # Add the segments that appeared after last iteration
                for segIdx in segmentsForCell:
                    # Check if segment already exists in the cell
                    segFound = False
                    for segment in cell.segments:
                        if segment.indexTP == segIdx:
                            segFound = True
                            break

                    # If segment is new, add it to cell
                    if not segFound:
                        segment = Segment(SegmentType.distal)
                        segment.indexTP = segIdx
                        cell.segments.append(segment)

                # Update distal segments
                for segment in cell.segments:
                    segIdx = segment.indexTP

                    # If segment not found in segments indexes returned in last iteration mark it as removed
                    if segIdx in segmentsForCell:

                        # Update segment's state
                        if segIdx in self.temporalPooler.activeSegments:
                            segment.isActive.setForCurrStep(True)
                        else:
                            segment.isActive.setForCurrStep(False)

                        # Get the indexes of the synapses of this segment
                        synapsesForSegment = self.temporalPooler.connections.synapsesForSegment(
                            segIdx)

                        # Add the synapses that appeared after last iteration
                        for synIdx in synapsesForSegment:
                            # Check if synapse already exists in the segment
                            synFound = False
                            for synapse in segment.synapses:
                                if synapse.indexTP == synIdx:
                                    synFound = True
                                    break

                            # If synapse is new, add it to segment
                            if not synFound:
                                synapse = Synapse()
                                synapse.indexTP = synIdx
                                segment.synapses.append(synapse)

                        # Update synapses
                        for synapse in segment.synapses:
                            synIdx = synapse.indexTP

                            # If synapse not found in synapses indexes returned in last iteration mark it as removed
                            if synIdx in synapsesForSegment:

                                # Update synapse's state
                                (
                                    _, sourceCellAbsIdx, permanence
                                ) = self.temporalPooler.connections.dataForSynapse(
                                    synIdx)
                                synapse.permanence.setForCurrStep(permanence)
                                if permanence >= self.distalSynConnectedPerm:
                                    synapse.isConnected.setForCurrStep(True)
                                else:
                                    synapse.isConnected.setForCurrStep(False)

                                # Get cell given cell's index
                                sourceColIdx = sourceCellAbsIdx / self.numCellsPerColumn
                                sourceCellRelIdx = sourceCellAbsIdx % self.numCellsPerColumn
                                sourceCell = self.columns[sourceColIdx].cells[
                                    sourceCellRelIdx]
                                synapse.inputElem = sourceCell
                            else:
                                synapse.isRemoved.setForCurrStep(True)
                    else:
                        segment.isRemoved.setForCurrStep(True)