Пример #1
0
        /// <summary>
        /// Creates a new Cell belonging to the specified <see cref="Column"/>. The index is an 
        /// integer id to distinguish this Cell from others in the <see cref="Column"/>.
        /// </summary>
        /// <param name="column"></param>
        /// <param name="index"></param>
        public Cell(Column column, int index)
        {
            // Set fields
            this.ActiveState = new List<bool>();
            this.Column = column;
            this.Index = index;
            this.DistalSegments = new List<DistalSegment>();
            this.LearnState = new List<bool>();
            this.PredictiveState = new List<bool>();
            this.SegmentUpdates = new List<SegmentUpdate>();

            // Initialize state vectors with fixed lenght = T
            for (int t = 0; t <= Global.T; t++)
            {
                this.ActiveState.Add(false);
                this.PredictiveState.Add(false);
                this.LearnState.Add(false);
            }

            SelectablelType = SelectableObjectType.Cell;
        }
Пример #2
0
		private float PickProximalSynapseConnections ( Ray ray, Column column, ref ProximalSynapse returnedProximalSynapse )
		{
			float intersectDistance = float.MaxValue;
			float minDistance = float.MaxValue;

			returnedProximalSynapse = null;

			// Draw Connections if existing
			if (column.IsDataGridSelected || (Simulation3D.Form.ShowSpatialLearning && column.ActiveState[Global.T]))
			{
				Vector3 rayP1 = ray.Position;
				Vector3 rayP2 = rayP1 + ray.Direction;

				foreach (ProximalSynapse synapse in column.ProximalSegment.Synapses)
				{
					synapse.mouseOver = false;

					if (column.Statistics.StepCounter > 0)
					{
						//var proximalSynapse = synapse as ProximalSynapse;

						// Get the two vectors to draw line between
						var startPosition = new Vector3 ( column.PositionInRegion.X, 0, column.PositionInRegion.Y + _zHtmRegion );

						// Get input source position
						int x = synapse.InputSource.X;
						int y = (int)_yHtmPlane;
						int z = synapse.InputSource.Y;
						var endPosition = new Vector3 ( x, y, z + _zHtmPlane );
							
						bool intersect;
						Vector3 Line1ClosestPt = new Vector3 ();
						Vector3 Line2ClosestPt = new Vector3 ();
						intersect = Math3D.ClosestPointsLineSegmentToLine ( out Line1ClosestPt, out Line2ClosestPt, startPosition, endPosition, rayP1, rayP2, 0.1f, out intersectDistance );

						if (intersect && intersectDistance < minDistance)
						{
							minDistance = intersectDistance;
							returnedProximalSynapse = synapse;
						}
					}
				}
			}

			return minDistance;
		}
Пример #3
0
		private float PickCell ( Ray ray, Matrix worldTranslation, Column column, Cell cell, ref Cell returnedCell )
		{
			cell.mouseOver = false;

			BoundingBox box = this._cube.GetBoundingBox ( worldTranslation );

			float? intersectDistance = ray.Intersects ( box );

			if (intersectDistance != null)
			{
				returnedCell = cell;
				return (float)intersectDistance;
			}
				
			return float.MaxValue;
		}
Пример #4
0
		/// <summary>
		/// Draw distal synapse connections for chosen cells
		/// </summary>
		/// <param name="worldTranslation"></param>
		/// <param name="worldRotate"></param>
		/// <param name="column"></param>
		/// <param name="cell"></param>
		private void DrawDistalSynapseConnections(ref Matrix worldTranslation, 
			ref Matrix worldRotate, Column column, Cell cell)
		{
			try
			{
				// Draw Connections if existing
				if (cell.IsDataGridSelected || 
					(Simulation3D.Form.ShowTemporalLearning && cell.PredictiveState[Global.T]))
				{
					foreach (var segment in cell.DistalSegments)
					{
						foreach (var synapse in segment.Synapses)
						{
							var distalSynapse = synapse as DistalSynapse;

							// Get the two vectors to draw line between
							var startPosition = new Vector3(column.PositionInRegion.X,
								cell.Index, column.PositionInRegion.Y);

							// Get input source position
							int x = distalSynapse.InputSource.Column.PositionInRegion.X;
							int y = distalSynapse.InputSource.Index;
							int z = distalSynapse.InputSource.Column.PositionInRegion.Y;
							var endPosition = new Vector3(x, y, z);

							//Color color = distalSynapse.IsActive(Global.T) ? Color.Black : Color.White;
							Color color;
							float alphaValue;
							GetColorFromDistalSynapse ( distalSynapse, out color, out alphaValue );
							this._connectionLine.SetUpVertices(startPosition, endPosition, color);

							// Draw line
							this._connectionLine.Draw(worldTranslation * worldRotate,
								this._viewMatrix, this._projectionMatrix);
						}
					}
				}
			}
			catch (Exception)
			{
				// Is sometimes raised because of collections modification by another thread.
			}
		}
Пример #5
0
		/// <summary>
		/// Draw distal synapse connections for chosen cells.
		/// Attention! lateral movement of planes, regions causes Z-Value correction
		/// </summary>
		/// <param name="worldRotate"></param>
		/// <param name="column"></param>
		private void DrawProximalSynapseConnections(ref Matrix worldRotate, Column column)
		{
			try
			{
				// Draw Connections if existing
				if (column.IsDataGridSelected || (Simulation3D.Form.ShowSpatialLearning && column.ActiveState[Global.T]))
				{
					foreach (var synapse in column.ProximalSegment.Synapses)
					{
						if (column.Statistics.StepCounter > 0)
						{
							var proximalSynapse = synapse as ProximalSynapse;

							// Get the two vectors to draw line between
							var startPosition = new Vector3 ( column.PositionInRegion.X, 0, column.PositionInRegion.Y + _zHtmRegion );

							// Get input source position
							int x = proximalSynapse.InputSource.X;
							int y = -5;
							int z = proximalSynapse.InputSource.Y;
							var endPosition = new Vector3 ( x, y, z + _zHtmPlane );

							//// Check for color
							//if (proximalSynapse.IsActive ( Global.T ))
							//{
							//	if (proximalSynapse.IsConnected ())	// Active & connected.
							//		this._connectionLine.SetUpVertices ( startPosition, endPosition, Color.Green );
							//	else	// Active.
							//		this._connectionLine.SetUpVertices ( startPosition, endPosition, Color.Orange );
							//}
							//else // Not active.
							//{
							//	this._connectionLine.SetUpVertices ( startPosition, endPosition, Color.White );
							//}

							////debug js
							//if (proximalSynapse.mouseSelected)
							//{
							//	this._connectionLine.SetUpVertices ( startPosition, endPosition, Color.Red );
							//}
							//if (proximalSynapse.mouseOver)
							//{
							//	this._connectionLine.SetUpVertices ( startPosition, endPosition, mouseOverColor );
							//}

							Color color;
							float alphaValue;
							GetColorFromProximalSynapse ( proximalSynapse, out color, out alphaValue );
							this._connectionLine.SetUpVertices ( startPosition, endPosition, color );

							// Draw line
							this._connectionLine.Draw ( worldRotate, this._viewMatrix, this._projectionMatrix );
						}
					}
				}
			}
			catch (Exception)
			{
				
			}
		}
Пример #6
0
		public void ShowInternalCells_RegularMode(Graphics grpOnBitmap, Column column)
		{
			// Display the cells in the column
			foreach (var cell in column.Cells)
			{
				Color cellColor = Color.Tan;

				Color colorAverage = Color.Black;
				/*if (cell.IsActive)
					clrCellColor = Color.Yellow;
				if ((cell.WasPredicted == true) && (cell.IsActive == false))
					clrCellColor = Color.Red;
				if (cell.IsPredicting == true)
					clrCellColor = Color.Green;
				if ((cell.WasPredicted == true) && (cell.IsActive == true))
					clrCellColor = Color.Aqua;*/
				if (cell.ActiveState[Global.T] && this.ViewActiveCells)
				{
					if ((colorAverage.R == 0) && (colorAverage.G == 0) && (colorAverage.B == 0))
					{
						colorAverage = Color.Yellow;
					}
					else
					{
						colorAverage = Color.FromArgb((colorAverage.R + Color.Yellow.R) / 2,
													  (colorAverage.G + Color.Yellow.G) / 2,
													  (colorAverage.B + Color.Yellow.B) / 2);
					}
				}
				if (cell.PredictiveState[Global.T - 1] && (cell.ActiveState[Global.T] == false)
					&& this.ViewNonActiveAndPredictedCells)
				{
					if ((colorAverage.R == 0) && (colorAverage.G == 0) && (colorAverage.B == 0))
					{
						colorAverage = Color.Red;
					}
					else
					{
						colorAverage = Color.FromArgb((colorAverage.R + Color.Red.R) / 2,
													  (colorAverage.G + Color.Red.G) / 2,
													  (colorAverage.B + Color.Red.B) / 2);
					}
				}

				if (cell.PredictiveState[Global.T] && this.ViewPredictingCells)
				{
					if ((colorAverage.R == 0) && (colorAverage.G == 0) && (colorAverage.B == 0))
					{
						colorAverage = Color.Aqua;
					}
					else
					{
						colorAverage = Color.FromArgb ( (colorAverage.R + Color.Aqua.R) / 2,
													  (colorAverage.G + Color.Aqua.G) / 2,
													  (colorAverage.B + Color.Aqua.B) / 2 );
					}
				}
				if (cell.PredictiveState[Global.T - 1] && cell.ActiveState[Global.T]
					&& this.ViewActiveAndPredictedCells)
				{
					if ((colorAverage.R == 0) && (colorAverage.G == 0) && (colorAverage.B == 0))
					{
						colorAverage = Color.LimeGreen;
					}
					else
					{
						colorAverage = Color.FromArgb ( (colorAverage.R + Color.LimeGreen.R) / 2,
													  (colorAverage.G + Color.LimeGreen.G) / 2,
													  (colorAverage.B + Color.LimeGreen.B) / 2 );
					}
				}

				if ((colorAverage.R != 0) || (colorAverage.G != 0) || (colorAverage.B != 0))
				{
					cellColor = colorAverage;
				}

				Point cellPoint;
				Size cellSizeOnDisplay;
				PointF cellPointVirtual;
				SizeF cellSizeVirtual;

				this.GetCellVirtualPointAndSize(cell, out cellPointVirtual, out cellSizeVirtual);
				this.GetCellDisplayPointAndSize(cell, out cellPoint, out cellSizeOnDisplay);
				grpOnBitmap.FillRectangle(new SolidBrush(cellColor),
										  cellPoint.X, cellPoint.Y,
										  cellSizeOnDisplay.Width, cellSizeOnDisplay.Height);
				grpOnBitmap.DrawRectangle(new Pen(Color.WhiteSmoke, 1),
										  cellPoint.X, cellPoint.Y,
										  cellSizeOnDisplay.Width, cellSizeOnDisplay.Height);

				// Find out how much segments this cell has. if it has more than one, then
				// Make a gray circle above it.
				if (cell.DistalSegments.Count > 0)
				{
					grpOnBitmap.FillEllipse(new SolidBrush(Color.FromArgb(127, Color.Gray)),
											cellPoint.X, cellPoint.Y,
											cellSizeOnDisplay.Width, cellSizeOnDisplay.Height);

					// Add the number of segments on top.
					this.DrawVirtualString(cell.DistalSegments.Count.ToString(), grpOnBitmap,
										   cellPointVirtual, cellSizeVirtual, Color.LightGray);
				}

				string textToDrawOnCell = string.Empty;
				// If the cell is a learning cell, draw "L" on it.
				if (cell.LearnState[Global.T])
				{
					textToDrawOnCell += "L";
				}
				// If the cell is a previous learning cell, draw "PL" on it.
				if (cell.LearnState[Global.T - 1])
				{
					textToDrawOnCell += "PL";
				}

				// Draw the text on the cell.
				this.DrawVirtualString(textToDrawOnCell, grpOnBitmap,
									   cellPointVirtual, cellSizeVirtual,
									   Color.Black);

				// Does the mouse hover over the cell? let's check.
				if (this._keyControlIsPressed)
				{
					if ((this._relativeMouseLocationInVirtualWorld.X >= cellPointVirtual.X) &&
						(this._relativeMouseLocationInVirtualWorld.Y >= cellPointVirtual.Y) &&
						(this._relativeMouseLocationInVirtualWorld.X < (cellPointVirtual.X + cellSizeVirtual.Width)) &&
						(this._relativeMouseLocationInVirtualWorld.Y < (cellPointVirtual.Y + cellSizeVirtual.Height)))
					{
						this._mouseHoversEntity = cell;

						// Paint a light over the highlighted cell.
						grpOnBitmap.FillRectangle(new SolidBrush(Color.FromArgb(127, Color.White)),
												  cellPoint.X, cellPoint.Y,
												  cellSizeOnDisplay.Width, cellSizeOnDisplay.Height);
					}
				}
			}
		}
Пример #7
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Segment"/> class.
 /// </summary>
 internal ProximalSegment(Column column)
     : base()
 {
     this.parentColumn = column;
 }
Пример #8
0
        /// <summary>
        /// Returns a list of all the columns that are within inhibitionRadius of a column.
        /// </summary>
        /// <returns></returns>
        public List<Column> GetNeighbors(Column column)
        {
            var neighborColumns = new List<Column>();

            // First find bounds of neighbors within inhibition radius of the column
            int positionX = column.PositionInRegion.X;
            int positionY = column.PositionInRegion.Y;
            var inhibitionRadius = (int) Math.Round(this.InhibitionRadius);
            int initialX = Math.Max(0, Math.Min(positionX - 1, positionX - inhibitionRadius));
            int initialY = Math.Max(0, Math.Min(positionY - 1, positionY - inhibitionRadius));
            int finalX = Math.Min(this.Size.Width, Math.Max(positionX + 1, positionX + inhibitionRadius));
            int finalY = Math.Min(this.Size.Height, Math.Max(positionY + 1, positionY + inhibitionRadius));

            // Extra 1's for correct looping
            finalX = Math.Min(this.Size.Width, finalX + 1);
            finalY = Math.Min(this.Size.Height, finalY + 1);

            // Loop over all columns that are within inhibitionRadius of given column
            for (int x = initialX; x < finalX; ++x)
            {
                for (int y = initialY; y < finalY; ++y)
                {
                    Column neighborColumn = this.GetColumn(x, y);
                    neighborColumns.Add(neighborColumn);
                }
            }

            return neighborColumns;
        }
Пример #9
0
		public void ShowColumns_RegularMode(Graphics grpOnBitmap, Column column)
		{
			Point columnPointOnDisplay;
			Size columnSizeOnDisplay;
			PointF columnPointVirtual;
			SizeF columnSizeVirtual;
			this.GetColumnDisplayPointAndSize(column, out columnPointOnDisplay, out columnSizeOnDisplay);
			this.GetColumnVirtualPointAndSize(column, out columnPointVirtual, out columnSizeVirtual);

			Color columnColor = Color.Tan;

			bool columnWasPredicted = false;
			foreach (var cell in column.Cells)
			{
				if (cell.PredictiveState[Global.T - 1])
				{
					columnWasPredicted = true;
					break;
				}
			}

			bool columnIsPredicting = false;
			foreach (var cell in column.Cells)
			{
				if (cell.PredictiveState[Global.T])
				{
					columnIsPredicting = true;
					break;
				}
			}

			if (column.ActiveState[Global.T] && this.ViewActiveCells)
			{
				columnColor = Color.Yellow;
			}
			if (column.ActiveState[Global.T] && columnWasPredicted &&
				this.ViewActiveAndPredictedCells)
			{
				columnColor = Color.Aqua;
			}
			if ((column.ActiveState[Global.T] == false) && columnWasPredicted &&
				this.ViewNonActiveAndPredictedCells)
			{
				columnColor = Color.Red;
			}
			if (columnIsPredicting && this.ViewPredictingCells)
			{
				columnColor = Color.LimeGreen;
			}

			grpOnBitmap.FillRectangle(new SolidBrush(columnColor),
									  columnPointOnDisplay.X, columnPointOnDisplay.Y,
									  columnSizeOnDisplay.Width, columnSizeOnDisplay.Height);

			// Does the mouse hovers over the column? let's check.
			if (this._keyControlIsPressed)
			{
				if ((this._relativeMouseLocationInVirtualWorld.X >= columnPointVirtual.X) &&
					(this._relativeMouseLocationInVirtualWorld.Y >= columnPointVirtual.Y) &&
					(this._relativeMouseLocationInVirtualWorld.X < (columnPointVirtual.X + columnSizeVirtual.Width)) &&
					(this._relativeMouseLocationInVirtualWorld.Y < (columnPointVirtual.Y + columnSizeVirtual.Height)))
				{
					this._mouseHoversEntity = column;

					// Paint a light over the highlighted cell.
					grpOnBitmap.FillRectangle(new SolidBrush(Color.FromArgb(127, Color.White)),
											  columnPointOnDisplay.X, columnPointOnDisplay.Y,
											  columnSizeOnDisplay.Width, columnSizeOnDisplay.Height);
				}
			}
		}
Пример #10
0
		public void DrawColumnRectangle(Column column, Graphics grpOnBitmap, Color color,
			bool drawFill, bool drawOutline)
		{
			Size columnSizeOnDisplay;
			Point columnPoint;
			this.GetColumnDisplayPointAndSize(column, out columnPoint, out columnSizeOnDisplay);

			if (drawFill)
			{
				grpOnBitmap.FillRectangle(new SolidBrush(color),
										  columnPoint.X, columnPoint.Y,
										  columnSizeOnDisplay.Width, columnSizeOnDisplay.Height);
			}

			if (drawOutline)
			{
				grpOnBitmap.DrawRectangle(new Pen(Color.Black, 3),
										  columnPoint.X, columnPoint.Y,
										  columnSizeOnDisplay.Width, columnSizeOnDisplay.Height);
			}
		}
Пример #11
0
		public void GetColumnDisplayPointAndSize(Column column, out Point displayPoint, out Size displaySize)
		{
			PointF columnLocation;
			SizeF columnSize;
			this.GetColumnVirtualPointAndSize(column, out columnLocation, out columnSize);

			displayPoint = this.ConvertViewPointToDisplayPoint(columnLocation);
			displaySize = this.GetSizeOnDisplay(columnSize);
		}
Пример #12
0
		public void GetColumnVirtualPointAndSize(Column column, out PointF columnPoint, out SizeF size)
		{
			columnPoint = new PointF(column.PositionInRegion.X, column.PositionInRegion.Y);
			size = _sizeColumnInVirtual;

			// If the viewer mode is sideview of the columns,
			// Calculate the position and size differently.
			if (this._viewerMode == Mode.ColumnsSideView)
			{
				columnPoint = new PointF(
					column.PositionInRegion.X * this._region.Size.Height + column.PositionInRegion.Y, 0);
				size = new SizeF(_sizeColumnInVirtual.Width,
								 _sizeColumnInVirtual.Height * this._region.CellsPerColumn);
			}
		}
Пример #13
0
		private void DrawConnectionBetweenColumns(Graphics grpOnBitmap, Column outputColumn,
			DistalSynapse synapse, Color color)
		{
			// Note that the start and end points are from the center of the
			// Column rectangle, that's why we add COLUMN_SIZE_VIRTUAL / 2
			// To the start points of the column rectangles.
			SizeF columnSizeVirtual;
			PointF columnStartPointVirtual, columnEndPointVirtual;
			this.GetColumnVirtualPointAndSize(synapse.InputSource.Column,
											  out columnStartPointVirtual, out columnSizeVirtual);
			this.GetColumnVirtualPointAndSize(outputColumn,
											  out columnEndPointVirtual, out columnSizeVirtual);

			Point pntConnectionStart = this.ConvertViewPointToDisplayPoint(new PointF(
																			   columnStartPointVirtual.X + columnSizeVirtual.Width / 2,
																			   columnStartPointVirtual.Y + columnSizeVirtual.Height / 2));
			Point pntConnectionEnd = this.ConvertViewPointToDisplayPoint(new PointF(
																			 columnEndPointVirtual.X + columnSizeVirtual.Width / 2,
																			 columnEndPointVirtual.Y + columnSizeVirtual.Height / 2));

			grpOnBitmap.DrawLine(new Pen(color, 5.0f),
								 pntConnectionStart, pntConnectionEnd);
		}
Пример #14
0
        private void dataGridColumns_SelectionChanged( object sender, EventArgs e )
        {
            if (this.dataGridColumns.SelectedRows.Count > 0)
            {
                // Get old selection
                if (this._selectedColumn != null)
                {
                    this._selectedColumn.IsDataGridSelected = false;
                }

                // Retrieve selected Column
                this._selectedColumn = this.dataGridColumns.SelectedRows[0].DataBoundItem as Column;
                this._selectedColumn.IsDataGridSelected = true;

                // Bind the cells of the selected column
                this.dataGridCells.DataSource = this._selectedColumn.Cells;
            }
        }
Пример #15
0
        private void dataGridColumns_PreviewKeyDown( object sender, PreviewKeyDownEventArgs e )
        {
            int currRow = this.dataGridColumns.SelectedRows[0].Index;

            if (this.dataGridColumns.SelectedRows.Count > 0)
            {
                switch (e.KeyCode)
                {
                    case Keys.Right:
                        if (this._selectedColumn.PositionInRegion.X < this._selectedRegion.Size.Width - 1)
                            currRow = Math.Min ( currRow + this._selectedRegion.Size.Height, this.dataGridColumns.Rows.Count );
                        break;
                    case Keys.Left:
                        if (this._selectedColumn.PositionInRegion.X > 0)
                            currRow = Math.Max ( currRow - this._selectedRegion.Size.Height, 0 );
                        break;
                }

                // Get old selection
                if (this._selectedColumn != null)
                {
                    this._selectedColumn.IsDataGridSelected = false;
                }

                // change selected row
                this.dataGridColumns.Rows[currRow].Selected = true;
                //scroll to it
                this.dataGridColumns.FirstDisplayedScrollingRowIndex = this.dataGridColumns.SelectedRows[0].Index;
                //set active cell (moves caret)
                this.dataGridColumns.CurrentCell = this.dataGridColumns.Rows[currRow].Cells[0];

                // Retrieve selected Column
                this._selectedColumn = this.dataGridColumns.SelectedRows[0].DataBoundItem as Column;
                this._selectedColumn.IsDataGridSelected = true;

                // Bind the cells of the selected column
                this.dataGridCells.DataSource = this._selectedColumn.Cells;
            }
        }
Пример #16
0
		private float PickDistalSynapseConnections ( Ray ray, Column column, Cell cell, ref DistalSynapse returnedDistalSynapse )
		{
			float intersectDistance;
			float minDistance = float.MaxValue;

			returnedDistalSynapse = null;

			// Draw Connections if existing
			if (cell.IsDataGridSelected ||
				(Simulation3D.Form.ShowTemporalLearning && cell.PredictiveState[Global.T]))
			{
				Vector3 rayP1 = ray.Position;
				Vector3 rayP2 = rayP1 + ray.Direction;

				foreach (DistalSegment segment in cell.DistalSegments)
				{
					foreach (DistalSynapse synapse in segment.Synapses)
					{
						synapse.mouseOver = false;

						var distalSynapse = synapse as DistalSynapse;

						// Get the two vectors to draw line between
						var startPosition = new Vector3 ( column.PositionInRegion.X, cell.Index, column.PositionInRegion.Y + _zHtmRegion );

						// Get input source position
						int x = distalSynapse.InputSource.Column.PositionInRegion.X;
						int y = distalSynapse.InputSource.Index;
						int z = distalSynapse.InputSource.Column.PositionInRegion.Y;
						var endPosition = new Vector3 ( x, y, z + _zHtmPlane );

						bool intersect;
						Vector3 Line1ClosestPt = new Vector3 ();
						Vector3 Line2ClosestPt = new Vector3 ();
						intersect = Math3D.ClosestPointsLineSegmentToLine ( out Line1ClosestPt, out Line2ClosestPt, startPosition, endPosition, rayP1, rayP2, 0.1f, out intersectDistance );

						if (intersect && intersectDistance < minDistance)
						{
							minDistance = intersectDistance;
							returnedDistalSynapse = synapse;
						}
					}
				}
			}

			return minDistance;
		}
Пример #17
0
        /// <summary>
        /// Return true if the given Column has an overlap value that is at least the
        /// k'th largest amongst all neighboring columns within inhibitionRadius.
        /// </summary>
        /// <remarks>
        /// This function is effectively determining which columns are to be inhibited 
        /// during the spatial pooling procedure of the region.
        /// </remarks>
        internal bool IsWithinKthScore(Column column, int k)
        {
            // Loop over all columns that are within inhibitionRadius of given column
            // Count how many neighbor columns have strictly greater overlap than our
            // given column. If this count is < k then we are within the kth score
            int numberColumns = 0;
            foreach (var neighborColumn in this.GetNeighbors(column))
            {
                if (neighborColumn.Overlap > column.Overlap)
                {
                    numberColumns += 1;
                }
            }

            // If count is < k, we are within the kth score of all neighbors
            return numberColumns < k;
        }
Пример #18
0
		public void ShowColumns_InputMode(Graphics grpOnBitmap, Column column,
			float maxOverlap)
		{
			// Display the column overlap and if it's inhibited or not.
			Color columnColor = Color.Black;
			var overlapColor = (byte)((column.Overlap / maxOverlap) * byte.MaxValue);
			string text = string.Empty;

			// Only draw the column if it has some overlap.
			if (column.Overlap > 0)
			{
				if (column.InhibitedState[Global.T])
				{
					columnColor = Color.FromArgb(overlapColor, 0, 0);
					text += "Inhibited : ";
				}
				else
				{
					columnColor = Color.FromArgb(0, overlapColor, 0);
					text += "Active : ";
				}

				text += column.Overlap + " Overlap";

				// Draw the column and add column text above it.
				this.DrawColumnRectangle(column, grpOnBitmap, columnColor, true, true);
				this.DrawVirtualString(text, grpOnBitmap, column.PositionInRegion,
									   _sizeColumnInVirtual, Color.Black);
			}
		}
Пример #19
0
        /// <summary>
        /// Prior to receiving any inputs, the region is initialized by computing a list of initial potential synapses for each column.
        /// This consists of a random set of inputs selected from the input space. Each input is represented by a synapse and assigned a random permanence value.
        /// The random permanence values are chosen with two criteria.
        /// First, the values are chosen to be in a small range around connectedPerm (the minimum permanence value at which a synapse is considered "connected").
        /// This enables potential synapses to become connected (or disconnected) after a small number of training iterations.
        /// Second, each column has a natural center over the input region, and the permanence values have a bias towards this center (they have higher values near the center).
        /// </summary>
        /// <remarks> 
        /// In addition to this Uwe added a concept of Locality Radius, which is an 
        /// additional parameter to control how far away synapse connections can be made 
        /// instead of allowing connections anywhere.  The reason for this is that in the
        /// case of video images I wanted to experiment with forcing each Column to only
        /// learn on a small section of the total input to more effectively learn lines or 
        /// corners in a small section without being 'distracted' by learning larger patterns
        /// in the overall input space (which hopefully higher hierarchical Regions would 
        /// handle more successfully).  Passing in 0 for locality radius will mean no 
        /// restriction which will more closely follow the Numenta doc if desired.
        /// </remarks>
        public void Initialize()
        {
            Segment.ActivationThreshold = SegmentActiveThreshold;

            // Loop over all lower regions and sensors to calculate the total size of the input
            // All regions/sensors should the same dimensions
            var inputSize = new Size();
            foreach (var child in this.Children)
            {
                inputSize.Width += child.Size.Width;
                inputSize.Height += child.Size.Height;
            }
            this.InputSize = inputSize;

            // Hard-coded means that input bits are mapped directly to columns.  In other words the
            // normal spatial pooler is disabled and we instead assume the input sparsification
            // has already been decided by some preprocessing code outside the Region.
            // It is then assumed (though not checked) that the input array will have
            // only a sparse number of "1" values that represent the active columns
            // for each time step.
            if (this.HardcodedSpatial)
            {
                this.Size = this.InputSize;
            }

            // Calculate the conversion factor to get from region's column grid back to original input grid
            // original
            //this.InputProportionX = (this.InputSize.Width - 1) / ((float) this.Size.Width - 1);
            //this.InputProportionY = (this.InputSize.Height - 1) / ((float) this.Size.Height - 1);
            // JS
            this.InputProportionX = (float)this.InputSize.Width / (float)this.Size.Width;
            this.InputProportionY = (float)this.InputSize.Height / (float)this.Size.Height;

            // Create the columns based on the size of the region and input data to connect to
            for (int x = 0; x < this.Size.Width; x++)
            {
                for (int y = 0; y < this.Size.Height; y++)
                {
                    // Position of this column in the region grid
                    var positionInRegion = new Point(x, y);

                    // Calculate the conversion factor to get from region's column grid back to original input grid
                    var inputPositionX = (int) Math.Round(positionInRegion.X * this.InputProportionX);
                    var inputPositionY = (int) Math.Round(positionInRegion.Y * this.InputProportionY);

                    // JS clamp values to imput range
                    inputPositionX = FloatExtensions.Clamp ( inputPositionX, 0, this.InputSize.Width - 1 );
                    inputPositionY = FloatExtensions.Clamp ( inputPositionY, 0, this.InputSize.Height - 1);

                    // Set 'center' position of columns in the original input grid
                    var centralPositionInInput = new Point(inputPositionX, inputPositionY);

                    // Create new column and add to region's list
                    var column = new Column(this, centralPositionInInput, positionInRegion);

                    this.Columns.Add(column);

                    //if (diag)
                    //{
                    //	file.WriteLine ( positionInRegion.X + "," + positionInRegion.Y + ",," + centralPositionInInput.X + "," + centralPositionInInput.Y );
                    //}
                }
            }

            // With hardcoded the Region will create a matching number of Columns to
            // mirror the size of the input array. Locality radius may still be
            // defined as it is still used by the temporal pooler.  If non-zero it will
            // restrict temporal segments from connecting further than r number of
            // columns away.
            if (this.HardcodedSpatial)
            {
                this.PercentageInputPerColumn = 1.0f / this.Columns.Count;
                this.PercentageMinOverlap = 1.0f;
                this.PercentageLocalActivity = 1.0f;
                this.DesiredLocalActivity = 1;
            }
            else
            {
                // Create Segments with potential synapses for columns
                foreach (var column in this.Columns)
                {
                    column.CreateProximalSegments();
                }

                // Inhibition radius is recomputed
                /// AS The radius of the average connected receptive field size of all the columns.
                /// The connected receptive field size of a column includes only the connected
                /// synapses (those with permanence values >= connectedPerm). This is used to
                /// determine the extent of lateral inhibition between columns.
                // JS
                // this.InhibitionRadius = this.AverageReceptiveFieldSize();
                this.InhibitionRadius = Math.Max(1, this.AverageReceptiveFieldSize ());

                // Set the desired local activity, ie the number of columns that will be
                // activated within a given spatial pooling inhibition radius
                if (this.LocalityRadius == 0)
                {
                    this.DesiredLocalActivity = (int) Math.Round(this.InhibitionRadius * this.PercentageLocalActivity);
                }
                else
                {
                    this.DesiredLocalActivity = (int) (Math.Pow(this.LocalityRadius, 2) * this.PercentageLocalActivity);
                }
                this.DesiredLocalActivity = Math.Max(2, this.DesiredLocalActivity);

            }

            Write (0);
        }
Пример #20
0
		public void ShowColumns_BoostingMode(Graphics grpOnBitmap, Column column,
			float minBoosting, float maxBoosting)
		{
			// Only draw the column if it has some boosting.
			if (column.Boost > 0)
			{
				float precentageFromBoostingRange =
					(column.Boost - minBoosting) / (maxBoosting - minBoosting);
				var colorByte = (byte)(precentageFromBoostingRange * byte.MaxValue);
				Color columnColor = Color.FromArgb(colorByte, 255 - colorByte, 255 - colorByte);

				// Draw the column and add column text above it.
				this.DrawColumnRectangle(column, grpOnBitmap, columnColor, true, true);

				// Invert the color of the column in order to show textual information :
				Color invertedColor = Color.FromArgb(byte.MaxValue - columnColor.R,
													 byte.MaxValue - columnColor.G, byte.MaxValue - columnColor.B);

				float proximalSegmentPermanenceSum = 0;
				float averageProximalSegmentPermanence = 0;
				foreach (ProximalSynapse synapse in column.ProximalSegment.Synapses)
				{
					proximalSegmentPermanenceSum += synapse.Permanence;
				}
				averageProximalSegmentPermanence =
					proximalSegmentPermanenceSum / column.ProximalSegment.Synapses.Count;

				string text = "Boosting : " + column.Boost +
							  "\nActiveDutyCycle : " + column.ActiveDutyCycle +
							  "\nOverlapDutyCycle : " + column.OverlapDutyCycle +
							  "\nAverage proximal segment permanence : " + averageProximalSegmentPermanence;

				this.DrawVirtualString(text, grpOnBitmap, column.PositionInRegion,
									   _sizeColumnInVirtual, Color.Black);
			}
		}