//---------------------------------------------------------------------
 private void AssertAreEqual(Location[] expected,
     Location[] actual)
 {
     Assert.AreEqual(expected.Length, actual.Length);
     for (int i = 0; i < expected.Length; i++)
         Assert.AreEqual(expected[i], actual[i]);
 }
		public void ActiveSiteIndexer_Location()
		{
			int index = 0;
			Location? nextActiveSite;
			if (index < activeSites.Count)
				nextActiveSite = activeSites[index];
			else
				nextActiveSite = null;
			for (uint row = 1; row <= grid.Rows; ++row)
				for (uint col = 1; col <= grid.Columns; ++col) {
					Location location = new Location(row, col);
					ActiveSite site = landscape[location];
					if (nextActiveSite != null && nextActiveSite == location) {
						Assert.AreEqual(location, site.Location);
						Assert.AreEqual(index, site.DataIndex);
						Assert.AreEqual(landscape, site.Landscape);
						Assert.AreEqual(true, site.IsActive);
						index++;
						if (index < activeSites.Count)
							nextActiveSite = activeSites[index];
						else
							nextActiveSite = null;
					}
					else
						Assert.IsNull(site);
				}
		}
		public void Init()
		{
			string[] rows = new string[] {	"....X..",
											"...XX.X",
											".......",
											"...XXX.",
											"X.X.X.X",
											"XXXXXXX" };
			bool [,] activeSites = Bool.Make2DimArray(rows, "X");
			mixedGrid = new DataGrid<bool>(activeSites);
			mixedMap = new ActiveSiteMap(mixedGrid);

			bool found_1stActive = false;
			bool found_1stInactive = false;
			for (uint row = 1; row <= mixedGrid.Rows; ++row) {
				for (uint column = 1; column <= mixedGrid.Columns; ++column) {
					if (mixedGrid[row, column]) {
						if (! found_1stActive) {
							mixed_1stActive = new Location(row, column);
							found_1stActive = true;
						}		
					}
					else {
						if (! found_1stInactive) {
							mixed_1stInactive = new Location(row, column);
							found_1stInactive = true;
						}		
					}
				}
			}
		}
示例#4
0
		//---------------------------------------------------------------------

		/// <summary>
		/// Initializes a new instance for a site on a landscape.
		/// </summary>
		/// <param name="landscape">
		///  The landscape where the site is located.
		/// </param>
		/// <param name="location">
		///  The location of the site.
		/// </param>
		/// <param name="dataIndex">
		///  The index of the site's data for site variables.
		/// </param>
		internal protected Site(ILandscape landscape,
				                Location   location,
				                uint	   dataIndex)
		{
           	Debug.Assert( landscape.IsValid(location) );
			this.landscape = landscape;
			this.locationAndIndex = new LocationAndIndex(location, dataIndex);
		}
		//---------------------------------------------------------------------

		internal MutableSite(ILandscape landscape,
		               	     Location   location,
		               	     bool       isActive,
		               	     uint       dataIndex)
			: base(landscape, location, dataIndex)
		{
			this.isActive = isActive;
		}
示例#6
0
		//---------------------------------------------------------------------

		/// <summary>
		/// Initializes a new instance for a site on a landscape.
		/// </summary>
		/// <param name="landscape">
		///  The landscape where the site is located.
		/// </param>
		/// <param name="location">
		///  The location of the site.
		/// </param>
		/// <param name="dataIndex">
		///  The index of the site's data for site variables.
		/// </param>
		internal protected Site(ILandscape landscape,
				                Location   location,
				                uint	   dataIndex)
		{
           	Debug.Assert( landscape.IsValid(location) );
			this.landscape = landscape;
			this.location  = location;
			this.index	   = dataIndex;
		}
		//---------------------------------------------------------------------

		private void CheckNeighbor(Site     neighbor,
		                           Location expectedLocation)
		{
			Assert.IsNotNull(neighbor);
			Assert.AreEqual(expectedLocation, neighbor.Location);
			Assert.AreEqual(activeSites[expectedLocation.Row - 1,
			                            expectedLocation.Column - 1],
			                neighbor.IsActive);
		}
		//---------------------------------------------------------------------

		internal void SetAll(Landscape landscape,
		                     Location  location,
		                     bool      isActive,
		                     uint      dataIndex)
		{
			this.Landscape = landscape;
			this.LocationAndIndex.Location = location;
			this.LocationAndIndex.Index    = dataIndex;
			this.isActive = isActive;
		}
示例#9
0
		//---------------------------------------------------------------------

		public static List<Location> ReadLocations(string path)
		{
			List<Location> sites = new List<Location>();
			Util.FileLineReader reader = new Util.FileLineReader(path);
			string line;
			while ((line = reader.ReadLine()) != null) {
				string[] rowAndCol = line.Split(null);
				Assert.AreEqual(2, rowAndCol.Length);
				uint row = uint.Parse(rowAndCol[0]);
				uint col = uint.Parse(rowAndCol[1]);
				Location loc = new Location(row, col);
				sites.Add(loc);
			}
			reader.Close();
			return sites;
		}
		public void MixedLandscape()
		{
									// columns:    123456789
			ILandscape landscape = MakeLandscape( "---------",	// row 1
			                                      "---XXXX--",	// row 2
			                                      "--XXXXX--",	// row 3
			                                      "--XXXXXX-",	// row 4
			                                      "-XXX--XX-",	// row 5
			                                      "-XX---XXX",	// row 6
			                                      "--XX--X--");	// row 7

			ISiteVar<bool> disturbed = landscape.NewSiteVar<bool>();
			Location[] disturbedLocs = new Location[] {
				new Location(2,6),
				new Location(3,4),
				new Location(3,5),
				new Location(5,7),
				new Location(5,8),
				new Location(6,2),
				new Location(7,7)
			};
			foreach (Location loc in disturbedLocs) {
				ActiveSite site = landscape[loc];
				Assert.IsNotNull(site);
				disturbed[site] = true;
			}

			DisturbedSiteEnumerator disturbedSites;
			disturbedSites = new DisturbedSiteEnumerator(landscape, disturbed);
			int count = 0;
			foreach (ActiveSite site in disturbedSites) {
				count++;
				Assert.IsTrue(count <= disturbedLocs.Length);
				Assert.AreEqual(disturbedLocs[count-1], site.Location);
			}
			Assert.AreEqual(count, disturbedLocs.Length);
		}
		//---------------------------------------------------------------------

		public uint this[Location location]
		{
			get {
				uint? rowOffset = Interval.Search(rowIntervals, location.Row);
				if (rowOffset == null)
					return this.count;
				ActiveRow activeRow = activeRows[(int)rowOffset.Value];
				List<Interval> colIntervalsInRow =
					columnIntervals.GetRange((int)
					                         activeRow.FirstIntervalOffset,
					                         (int) activeRow.IntervalCount);
				uint? colOffset = Interval.Search(colIntervalsInRow,
				                                  location.Column);
				if (colOffset == null)
					return this.count;
				else
					return colOffset.Value;
			}
		}
		public void Enumerator()
		{
			int cellCount = 0;
			for (uint row = 1; row <= grid.Rows; ++row)
				for (uint col = 1; col <= grid.Columns; ++col) {
					cellCount++;
					Location loc = new Location(row, col);
					grid[loc] = cellCount;
				}

			cellCount = 0;
			foreach (int i in grid) {
				cellCount++;
				Assert.AreEqual(cellCount, i);
			}
		}
		public void LocationIndexerColumnTooBig()
		{
			Location badLoc = new Location(1, dimensions.Columns + 1);
			grid[badLoc] = 0;
		}
		public void LocationIndexerRowTooBig()
		{
			Location badLoc = new Location(dimensions.Rows + 1, 1);
			grid[badLoc] = 0;
		}
		public void LocationIndexerColumn0()
		{
			Location badLoc = new Location(1, 0);
			grid[badLoc] = 0;
		}
		//---------------------------------------------------------------------

		internal ImmutableInactiveSite(ILandscape landscape,
		               	               Location   location,
		               	               uint       dataIndex)
			: base(landscape, location, dataIndex)
		{
		}
		public void Grid5x3True()
		{
			bool[,] array = new bool[,] { {true, true, true},
										  {true, true, true},
										  {true, true, true},
										  {true, true, true},
										  {true, true, true} };
			DataGrid<bool> grid = new DataGrid<bool>(array);
			ActiveSiteMap map = new ActiveSiteMap(grid);

			Assert.AreEqual(grid.Count, map.Count);

			int index = 0;
			Location location = new Location(1,1);
			foreach (LocationAndIndex entry in map) {
				index++;
				Assert.IsTrue(index <= map.Count);
				Assert.AreEqual(location, entry.Location);
				Assert.AreEqual((uint) index, entry.Index);
				location = RowMajor.Next(location, grid.Columns);
			}
			Assert.AreEqual(map.Count, index);

			Assert.AreEqual(new Location(1,1), map.FirstActive.Location);
			Assert.AreEqual(1, map.FirstActive.Index);
			Assert.IsNull(map.FirstInactive);
		}
		public void Enumerator()
		{
			bool cellValue = false;
			for (uint row = 1; row <= grid.Rows; ++row)
				for (uint col = 1; col <= grid.Columns; ++col) {
					cellValue = !cellValue;
					Location loc = new Location(row, col);
					grid[loc] = cellValue;
				}

			cellValue = false;
			foreach (bool b in grid) {
				cellValue = !cellValue;
				Assert.AreEqual(cellValue, b);
			}
		}
		public void Test15_SetRow()
		{
			Location myLoc = new Location(98, 321);
			myLoc.Row = 44;
			Assert.AreEqual(44, myLoc.Row);
			Assert.AreEqual(new Location(44, 321), myLoc);
		}
		public void IndexerLocation()
		{
			int index = 0;
			Location? nextActiveSite;
			if (index < activeSites.Count)
				nextActiveSite = activeSites[index];
			else
				nextActiveSite = null;
			for (uint row = 1; row <= grid.Rows; ++row)
				for (uint col = 1; col <= grid.Columns; ++col) {
					Location location = new Location(row, col);
					if (nextActiveSite != null && nextActiveSite == location) {
						Assert.AreEqual(index+1, map[location]);
						index++;
						if (index < activeSites.Count)
							nextActiveSite = activeSites[index];
						else
							nextActiveSite = null;
					}
					else
						Assert.AreEqual(ActiveSiteMap.InactiveSiteDataIndex,
						                map[location]);
				}
		}
		public void Test16_SetColumn()
		{
			Location myLoc = new Location(98, 321);
			myLoc.Column = 7;
			Assert.AreEqual(7, myLoc.Column);
			Assert.AreEqual(new Location(98, 7), myLoc);
		}
		public void Init()
		{
			loc_1234_987 = new Location(1234, 987);
		}
示例#23
0
		//---------------------------------------------------------------------

		/// <summary>
		/// Computes the absolute location of a site's neighbor.
		/// </summary>
		/// <param name="siteLoc">
		/// The site's location.
		/// </param>
		/// <param name="neighborRelLoc">
		/// The location of the neighbor relative to the site.
		/// </param>
		/// <param name="neighborLoc">
		/// The neighbor's location on the landscape.
		/// </param>
		/// <returns>
		/// true if the neighbor is on the landscape; false otherwise (in
		/// which case the
		/// </returns>
		public Location? ComputeNeighborLocation(Location         siteLoc,
		                                         RelativeLocation neighborRelLoc)
		{
			long neighborRow = siteLoc.Row + neighborRelLoc.Row;
			long neighborColumn = siteLoc.Column + neighborRelLoc.Column;
			if (neighborRow < 0 || neighborRow > uint.MaxValue ||
			    neighborColumn < 0 || neighborColumn > uint.MaxValue)
				return null;
			else
				return new Location((uint) neighborRow,
				                    (uint) neighborColumn);
		}
			//-----------------------------------------------------------------

			public Entry(Location location,
			             uint     index)
			{
				this.location = location;
				this.index    = index;
			}
		//---------------------------------------------------------------------

		public uint this[Location location]
		{
			get {
				uint? rowOffset = Interval.Search(location.Row,
				                                  rowIntervals,
				                                  0, rowIntervals.Count);
				if (rowOffset == null)
					return this.count;
				ActiveRow activeRow = activeRows[(int)rowOffset.Value];
				uint? colOffset = Interval.Search(location.Column,
				                                  columnIntervals,
				                                  (int) activeRow.FirstIntervalOffset,
					                              (int) activeRow.IntervalCount);
				if (colOffset == null)
					return this.count;
				else
					return colOffset.Value;
			}
		}
		public void LocationIndexer()
		{
			int cellCount = 0;
			for (uint row = 1; row <= grid.Rows; ++row)
				for (uint col = 1; col <= grid.Columns; ++col) {
					cellCount++;
					Location loc = new Location(row, col);
					grid[loc] = cellCount;
					Assert.AreEqual(cellCount, grid[loc]);
				}
		}
		public void LocationIndexerRow0()
		{
			Location badLoc = new Location(0, 1);
			grid[badLoc] = 0;
		}
		//---------------------------------------------------------------------

		public static bool Algorithm(ISpecies   species,
		                             ActiveSite site)
		{
			if (species.EffectiveSeedDist == EffectiveSeedDist.Universal)
				return UniversalDispersal.Algorithm(species, site);

			if (! Reproduction.SufficientLight(species, site) ||
			    ! Reproduction.Establish(species, site))
				return false;

			if (Reproduction.MaturePresent(species, site))
				return true;

			int row = (int) site.Location.Row;
			int col = (int) site.Location.Column;

			int cellDiam = (int) Model.CellLength;
			int EffD = species.EffectiveSeedDist;
			int MaxD = species.MaxSeedDist;

			double ratio = 0.95;//the portion of the probability in the effective distance
			//lambda1 parameterized for effective distance
			double lambda1 = Math.Log((1-ratio)/EffD);
			//lambda2 parameterized for maximum distance
			double lambda2 = Math.Log(0.01)/MaxD;

			double lowBound=0, upBound=0;
			//bool suitableDist=false;//flag to trigger if seed (plural) can get to a site based on distance probability
			double distanceProb=0.0;
			int pixRange = Math.Max((int) ((float) MaxD / (float) cellDiam), 1);
			int maxrow = (int) Math.Min(row+pixRange, Model.Landscape.Rows);
			int minrow = Math.Max(row-pixRange, 1);
			for (int i=minrow; i<=maxrow; i++) {
				int x1, x2;
				//float b = 5.0;
				findX1X2(out x1, out x2, col, row, i, pixRange);
				for (int j=x1; j<=x2;j++) {
					Location loc = new Location((uint) i, (uint) j);
					if (Model.Landscape.GetSite(loc, ref neighbor) && neighbor.IsActive) {
						if (Reproduction.MaturePresent(species, neighbor)) {
							float distance=(float)Math.Sqrt((float)((row-i)*(row-i)+(col-j)*(col-j))) * cellDiam;  //Pythag

							//set lower boundary to the theoretical (straight-line) edge of parent cell
							lowBound = distance - cellDiam;
							if(lowBound < 0) lowBound=0;

							//set upper boundary to the outer theoretical boundary of the cell
							upBound = distance;
					
							if(cellDiam<=EffD)
							{//Draw probabilities from either EffD or MaxD curves
								if(distance<=(float)EffD)
								{//BCW May 04
									distanceProb = Math.Exp(lambda1*lowBound) - Math.Exp(lambda1*upBound);
								}
								else
								{//BCW May 04
									distanceProb = (1-ratio)*Math.Exp(lambda2*(lowBound-EffD)) - (1-ratio)*Math.Exp(lambda2*(upBound-EffD));
								}
							}
							else
							{
								if(distance<=cellDiam)
								{//Draw probabilities from both EffD and MaxD curves
									distanceProb = Math.Exp(lambda1*lowBound)-(1-ratio)*Math.Exp(lambda2*(upBound-EffD));
								}
								else
								{
									distanceProb = (1-ratio)*Math.Exp(lambda2*(lowBound-EffD)) - (1-ratio)*Math.Exp(lambda2*(upBound-EffD));
								}
							}
							if (distanceProb > Landis.Util.Random.GenerateUniform())// && frand() < l->probRepro(speciesNum))  Modified BCW May '04
							{
								//  success = sites(row,col)->addNewCohort(s, sa, 10);
								return true;
							}
						}
					}  // if neighor is active
				} // for each column, j, of current row in neighborhood
			} // for each row, i, in the neighborhood

			//  Search failed.
			return false;
		}
		//---------------------------------------------------------------------

		/// <summary>
		/// The data index for a particular site.
		/// </summary>
		/// <exception cref="System.IndexOutOfRangeException">
		/// The location's row is 0 or > the # of rows in the map, or the
		/// location's column is 0 or > the # of columns in the map.
		/// </exception>
		public uint this[Location location]
		{
			get {
				return indexes[location.Row-1, location.Column-1];
			}
		}
		public void LocationIndexer()
		{
			bool cellValue = false;
			for (uint row = 1; row <= grid.Rows; ++row)
				for (uint col = 1; col <= grid.Columns; ++col) {
					cellValue = !cellValue;
					Location loc = new Location(row, col);
					grid[loc] = cellValue;
					Assert.AreEqual(cellValue, grid[loc]);
				}
		}