public NeighborInfo(int rowOffset, int columnOffset, double probability) { this.RelativeLocation = new RelativeLocation(rowOffset, columnOffset); this.DistanceProbability = probability; }
//--------------------------------------------------------------------- /// <summary> /// Gets a neighboring site. /// </summary> /// <param name="location"> /// The location of the neighboring site relative to the site. /// </param> /// <returns> /// null if the location refers to an absolute location that is not /// valid for the site's landscape. /// </returns> public Site GetNeighbor(RelativeLocation location) { Location?neighborLocation = ComputeNeighborLocation(this.Location, location); if (neighborLocation.HasValue) { return(landscape.GetSite(neighborLocation.Value)); } return(null); }
//--------------------------------------------------------------------- /// <summary> /// Gets a neighboring site. /// </summary> /// <param name="location"> /// The location of the neighboring site relative to the site. /// </param> /// <param name="neighbor"> /// The object which will be assigned the requested site. If this /// parameter is null and the location is valid, then a new instance /// will be created and assigned to the parameter. /// </param> /// <returns> /// true if the location is on the landscape, and the information /// about the requested site was assigned to the neighbor parameter. /// false if the location is not valid (in which case, the neighbor /// parameter is unchanged). /// </returns> public bool GetNeighbor(RelativeLocation location, ref MutableSite neighbor) { Location?neighborLocation = ComputeNeighborLocation(this.Location, location); if (neighborLocation.HasValue) { return(landscape.GetSite(neighborLocation.Value, ref neighbor)); } return(false); }
//--------------------------------------------------------------------- /// <summary> /// Gets a neighboring site. /// </summary> /// <param name="location"> /// The location of the neighboring site relative to the site. /// </param> /// <returns> /// null if the location refers to an absolute location that is not /// valid for the site's landscape. /// </returns> public Site GetNeighbor(RelativeLocation location) { long neighborRow = this.Location.Row + location.Row; long neighborColumn = this.Location.Column + location.Column; if (neighborRow < 0 || neighborRow > uint.MaxValue || neighborColumn < 0 || neighborColumn > uint.MaxValue) { return(null); } return(landscape.GetSite((uint)neighborRow, (uint)neighborColumn)); }
//--------------------------------------------------------------------- /// <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)); } }
//--------------------------------------------------------------------- /// <summary> /// Gets a neighboring site. /// </summary> /// <param name="location"> /// The location of the neighboring site relative to the site. /// </param> /// <returns> /// null if the location refers to an absolute location that is not /// valid for the site's landscape. /// </returns> public Site GetNeighbor(RelativeLocation location) { long neighborRow = this.Location.Row + location.Row; long neighborColumn = this.Location.Column + location.Column; if (neighborRow < 0 || neighborRow > uint.MaxValue || neighborColumn < 0 || neighborColumn > uint.MaxValue) return null; return landscape.GetSite((uint) neighborRow, (uint) neighborColumn); }
//--------------------------------------------------------------------- /// <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); }
//--------------------------------------------------------------------- /// <summary> /// Gets a neighboring site. /// </summary> /// <param name="location"> /// The location of the neighboring site relative to the site. /// </param> /// <param name="neighbor"> /// The object which will be assigned the requested site. If this /// parameter is null and the location is valid, then a new instance /// will be created and assigned to the parameter. /// </param> /// <returns> /// true if the location is on the landscape, and the information /// about the requested site was assigned to the neighbor parameter. /// false if the location is not valid (in which case, the neighbor /// parameter is unchanged). /// </returns> public bool GetNeighbor(RelativeLocation location, ref MutableSite neighbor) { Location? neighborLocation = ComputeNeighborLocation(this.Location, location); if (neighborLocation.HasValue) return landscape.GetSite(neighborLocation.Value, ref neighbor); return false; }
public RelativeLocationWeighted (RelativeLocation location, double weight) { this.location = location; this.weight = weight; }
//--------------------------------------------------------------------- /// <summary> /// Gets a neighboring site. /// </summary> /// <param name="location"> /// The location of the neighboring site relative to the site. /// </param> /// <returns> /// null if the location refers to an absolute location that is not /// valid for the site's landscape. /// </returns> public Site GetNeighbor(RelativeLocation location) { Location? neighborLocation = ComputeNeighborLocation(this.Location, location); if (neighborLocation.HasValue) return landscape.GetSite(neighborLocation.Value); return null; }
public void Equals_DiffObjSameValue() { RelativeLocation loc_0_0 = new RelativeLocation(); Assert.IsTrue(loc.Equals(loc_0_0)); Assert.IsTrue(loc_1234_987.Equals(new RelativeLocation(1234, 987))); }
//--------------------------------------------------------------------- private List<Site> GetNeighbors(Site site, int windDirection) { if(windDirection > 7) windDirection = 7; double[] windProbs = { (((4.0 - this.intensity)/8.0) * (1+this.intensity)), //Primary direction (((4.0 - this.intensity)/8.0) * (1+this.intensity)), (((4.0 - this.intensity)/8.0)), (((4.0 - this.intensity)/8.0) * (1-this.intensity)), (((4.0 - this.intensity)/8.0) * (1-this.intensity)), //Opposite of primary direction (((4.0 - this.intensity)/8.0) * (1-this.intensity)), (((4.0 - this.intensity)/8.0)), (((4.0 - this.intensity)/8.0) * (1+this.intensity)), }; double windProb = 0.0; int index = 0; List<Site> neighbors = new List<Site>(9); foreach (RelativeLocation relativeLoc in neighborhood) { Site neighbor = site.GetNeighbor(relativeLoc); if(index + windDirection > 7) windProb = windProbs[index + windDirection - 8]; else windProb = windProbs[index + windDirection]; if (neighbor != null && Random.GenerateUniform() < windProb) neighbors.Add(neighbor); index++; } //Next, add the 9th neighbor, a neighbor one cell beyond the //8 nearest neighbors. //array index 0 = north; 1 = northeast, 2 = east,...,8 = northwest int[] vertical ={2,2,0,-2,-2,-2,0,2}; int[] horizontal={0,2,2,2,0,-2,-2,-2}; RelativeLocation relativeLoc9 = new RelativeLocation(vertical[windDirection], horizontal[windDirection]); Site neighbor9 = site.GetNeighbor(relativeLoc9); if (neighbor9 != null && Random.GenerateUniform() < this.intensity) neighbors.Add(neighbor9); return neighbors; }
public void NegativeValues() { RelativeLocation loc = new RelativeLocation(-22, -6543); Assert.AreEqual(-22, loc.Row); Assert.AreEqual(-6543, loc.Column); }
//--------------------------------------------------------------------- private List<Site> GetNeighbors(Site site, int windDirection, double windSpeed) { if(windDirection > 7) windDirection = 7; double[] windProbs = { (((4.0 - windSpeed)/8.0) * (1+windSpeed)), //Primary direction (((4.0 - windSpeed)/8.0) * (1+windSpeed)), (((4.0 - windSpeed)/8.0)), (((4.0 - windSpeed)/8.0) * (1-windSpeed)), (((4.0 - windSpeed)/8.0) * (1-windSpeed)), //Opposite of primary direction (((4.0 - windSpeed)/8.0) * (1-windSpeed)), (((4.0 - windSpeed)/8.0)), (((4.0 - windSpeed)/8.0) * (1+windSpeed)), }; double windProb = 0.0; int index = 0; int success = 0; List<Site> neighbors = new List<Site>(9); foreach (RelativeLocation relativeLoc in neighborhood) { Site neighbor = site.GetNeighbor(relativeLoc); if(index + windDirection > 7) windProb = windProbs[index + windDirection - 8]; else windProb = windProbs[index + windDirection]; //System.Console.WriteLine("WindProb={0:0.00}, windSpeed={1:0.00}, neighbor={2}.", windProb, windSpeed, index+1); if (neighbor != null && Random.GenerateUniform() < windProb) { neighbors.Add(neighbor); success++; } index++; } logger.Debug(string.Format("Successfully added {0} neighbors.", success)); //Next, add the 9th neighbor, a neighbor one cell beyond the //8 nearest neighbors. //array index 0 = north; 1 = northeast, 2 = east,...,8 = northwest int[] vertical ={2,2,0,-2,-2,-2,0,2}; int[] horizontal={0,2,2,2,0,-2,-2,-2}; RelativeLocation relativeLoc9 = new RelativeLocation(vertical[windDirection], horizontal[windDirection]); Site neighbor9 = site.GetNeighbor(relativeLoc9); if (neighbor9 != null && Random.GenerateUniform() < windSpeed) neighbors.Add(neighbor9); return neighbors; }
//--------------------------------------------------------------------- //Generate List of 4,8,12, or 24 nearest neighbors. //--------------------------------------------------------------------- private static List<RelativeLocation> GetNeighbors(int numNeighbors) { RelativeLocation[] neighborhood4 = new RelativeLocation[] { new RelativeLocation( 0, 1), // east new RelativeLocation( 1, 0), // south new RelativeLocation( 0, -1), // west new RelativeLocation(-1, 0), // north }; RelativeLocation[] neighborhood8 = new RelativeLocation[] { new RelativeLocation(-1, 1), // northeast new RelativeLocation( 1, 1), // southeast new RelativeLocation( 1, -1), // southwest new RelativeLocation( -1, -1), // northwest }; RelativeLocation[] neighborhood12 = new RelativeLocation[] { new RelativeLocation(-2, 0), // north north new RelativeLocation( 0, 2), // east east new RelativeLocation( 2, 0), // south south new RelativeLocation( 0, -2), // west west }; RelativeLocation[] neighborhood24 = new RelativeLocation[] { new RelativeLocation(-2, -2), // northwest northwest new RelativeLocation( -2, -1), // northwest south new RelativeLocation( -1, -2), // northwest east new RelativeLocation( -2, 2), // northeast northeast new RelativeLocation( -2, 1), // northeast west new RelativeLocation( -1, 2), // northeast south new RelativeLocation( 2, 2), // southeast southeast new RelativeLocation(1, 2), // southeast north new RelativeLocation(2, 1), //southeast west new RelativeLocation( 2, -2), // southwest southwest new RelativeLocation( 2, -1), // southwest east new RelativeLocation( 1, -2), // southwest north }; //Progressively add neighbors as necessary: List<RelativeLocation> neighbors = new List<RelativeLocation>(); foreach (RelativeLocation relativeLoc in neighborhood4) neighbors.Add(relativeLoc); if(numNeighbors <= 4) return neighbors; foreach (RelativeLocation relativeLoc in neighborhood8) neighbors.Add(relativeLoc); if(numNeighbors <= 8) return neighbors; foreach (RelativeLocation relativeLoc in neighborhood12) neighbors.Add(relativeLoc); if(numNeighbors <= 12) return neighbors; foreach (RelativeLocation relativeLoc in neighborhood24) neighbors.Add(relativeLoc); return neighbors; }
//--------------------------------------------------------------------- //Generate a Relative Location array (with WEIGHTS) of neighbors. //Check each cell within a block surrounding the center point. This will //create a set of POTENTIAL neighbors. These potential neighbors //will need to be later checked to ensure that they are within the landscape // and active. private static IEnumerable<RelativeLocationWeighted> GetResourceNeighborhood(IAgent agent) { float CellLength = Model.Core.CellLength; UI.WriteLine("Creating Neighborhood List."); int neighborRadius = agent.NeighborRadius; int numCellRadius = (int) ((double) neighborRadius / CellLength) ; UI.WriteLine("NeighborRadius={0}, CellLength={1}, numCellRadius={2}", neighborRadius, CellLength, numCellRadius); double centroidDistance = 0; double cellLength = CellLength; double neighborWeight = 0; List<RelativeLocationWeighted> neighborhood = new List<RelativeLocationWeighted>(); for(int row=(numCellRadius * -1); row<=numCellRadius; row++) { for(int col=(numCellRadius * -1); col<=numCellRadius; col++) { neighborWeight = 0; centroidDistance = DistanceFromCenter(row ,col); //UI.WriteLine("Centroid Distance = {0}.", centroidDistance); if(centroidDistance <= neighborRadius && centroidDistance > 0) { if(agent.ShapeOfNeighbor == NeighborShape.uniform) neighborWeight = 1.0; if(agent.ShapeOfNeighbor == NeighborShape.linear) { //neighborWeight = (neighborRadius - centroidDistance + (cellLength/2)) / (double) neighborRadius; neighborWeight = 1.0 - (centroidDistance / (double) neighborRadius); } if(agent.ShapeOfNeighbor == NeighborShape.gaussian) { double halfRadius = neighborRadius / 2; neighborWeight = (float) System.Math.Exp(-1 * System.Math.Pow(centroidDistance, 2) / System.Math.Pow(halfRadius, 2)); } RelativeLocation reloc = new RelativeLocation(row, col); neighborhood.Add(new RelativeLocationWeighted(reloc, neighborWeight)); } } } return neighborhood; }
public void Init() { loc_1234_987 = new RelativeLocation(1234, 987); }
//--------------------------------------------------------------------- private List<Site> GetNeighbors(Site site, int windDirection) { List<Site> neighbors = new List<Site>(5); foreach (RelativeLocation relativeLoc in neighborhood) { Site neighbor = site.GetNeighbor(relativeLoc); if (neighbor != null) neighbors.Add(neighbor); } int vertical=0; int horizontal=0; if(windDirection==1) { //wind is from south vertical = -2; horizontal = 0; } if(windDirection==2) { //wind is from north vertical = 2; horizontal = 0; } if(windDirection==3) { //wind is from east vertical = 0; horizontal = -2; } if(windDirection==4) { //wind is from west vertical = 0; horizontal = 2; } RelativeLocation relativeLoc5 = new RelativeLocation(vertical, horizontal); Site neighbor5 = site.GetNeighbor(relativeLoc5); if (neighbor5 != null) neighbors.Add(neighbor5); return neighbors; }
private static List<Site> Get8Neighbors(Site site) { List<Site> neighbors = new List<Site>(); RelativeLocation[] neighborhood = new RelativeLocation[] { new RelativeLocation(-1, 0), // north new RelativeLocation(-1, 1), // northeast new RelativeLocation( 0, 1), // east new RelativeLocation( 1, 1), // southeast new RelativeLocation( 1, 0), // south new RelativeLocation( 1, -1), // southwest new RelativeLocation( 0, -1), // west new RelativeLocation(-1, -1), // northwest }; foreach (RelativeLocation relativeLoc in neighborhood) { Site neighbor = site.GetNeighbor(relativeLoc); if(neighbor != null && neighbor.IsActive) neighbors.Add(neighbor); } Landis.Util.Random.Shuffle(neighbors); return neighbors; }