/// <summary> /// Score the Garden based on the linearity of the Rocks in it. /// If Rocks are determined to be linear (or close to linear), the number returned will /// be less than 1, allowing the evaluator to reduce the score of the Garden. If no Rocks /// are linear, then this method will return a value /// </summary> /// <param name="garden"></param> /// <returns></returns> private double scoreLinearity(Garden garden) { double score = 1; int triangle = 3; List <double> scores = new List <double>(); List <Resident> allRocksInGarden = getAllResidents(garden, Rock.rockString); List <Resident[]> AllRockTriangles = new List <Resident[]>(); List <int[]> pickThree = new List <int[]>(); Resident[] tempResidents = new Resident[triangle], tempResidentsCopy = new Resident[triangle]; combinations(ref pickThree, 0, 0, triangle, allRocksInGarden.Count, new int[triangle]); foreach (int[] combo in pickThree) { for (int i = 0; i < triangle; i++) { tempResidents[i] = allRocksInGarden[combo[i]]; } tempResidents.CopyTo(tempResidentsCopy, 0); AllRockTriangles.Add(tempResidentsCopy); } foreach (Resident[] triangleArrrangement in AllRockTriangles) { scores.Add(Math.Pow(triangleLinearity(triangleArrrangement), 1 / linearityRootWeight)); } if (scores.Count > 0) { return(scores.Average()); } return(score); }
/// <summary> /// Determine all combinations of k Residents from residentsList. /// </summary> /// <param name="residentsList">A List of Residents to be selected from.</param> /// <param name="k">How many Residents each selection should have.</param> /// <returns>All combinations of k Residents from residents List as a 2d array /// of Residents.</returns> private Resident[][] NChooseK(List <Resident> residentsList, int k) { int n = residentsList.Count; if (k > n) { return(new Resident[0][]); } int selectionsNum = factorial(n) / (factorial(k) * factorial(n - k)); Resident[][] combinatorics = new Resident[selectionsNum][]; for (int i = 0; i < selectionsNum; i++) { combinatorics[i] = new Resident[k]; } List <int[]> combinationIndecies = new List <int[]>(); combinations(ref combinationIndecies, 0, 0, k, n, new int[k]); for (int i = 0; i < combinationIndecies.Count; i++) { for (int q = 0; q < k; q++) { combinatorics[i][q] = residentsList[combinationIndecies[i][q]]; } } return(combinatorics); }
/// <summary> /// Finds the minimal distance from one rock to another. /// Straight line disregarding any obstacles. /// Assumed to be a line from the centers /// </summary> /// <param name="other">The resident to find the distance to.</param> /// <returns>A double representing the distance between this Resident and the passed /// Resident.</returns> public double getDistance(Resident other) { double otherCenterX = (other.origin.X + other.width / 2.0); double otherCenterY = (other.origin.X + other.length / 2.0); double slope = (center.Y - otherCenterY) / (center.X - otherCenterX); double rawDistance = Math.Sqrt(Math.Pow(center.X - otherCenterX, 2) + Math.Pow(center.Y - otherCenterY, 2)); //negative tracking on this, positive on other //remove the space from centers to edges of both residents. double edgeDistance = rawDistance - distanceToEdge(-1 * slope) - other.distanceToEdge(slope); return(edgeDistance); }
/// <summary> /// Removes any object that occupies this point on the grid. /// Will remove all instances of this object. /// </summary> /// <param name="x">Any x coordinate of the object to be removed so long as the y /// coodinate that forms the pair (x, y) contains the Resident to be removed. /// </param> /// <param name="y">Any y coordinate of the object to be removed so long as the x /// coodinate that forms the pair (x, y) contains the Resident to be removed.</param> /// <returns>Boolean indicating whether any object existed on the passed set of /// coordinates.</returns> public bool removeResident(Point spot) { Resident evictee = atomAt(spot).getResident(); residentsList.Remove(evictee); if (evictee == null) { //no Resident occupies this space return(false); } //go through each Atom that contains this object foreach (Point coordinate in getCoordinates(atomAt(spot).getLocation(), evictee.width, evictee.length)) { atomAt(coordinate).removeResident(); } return(true); }
private bool isSearchBetween(Func <Resident, bool> detectionMethod, Resident start, Resident end) { int direction; Resident tempRes; Point floatingPoint = start.center; while (!floatingPoint.Equals(end.center)) { tempRes = atomAt(floatingPoint).getResident(); if (detectionMethod(tempRes)) { return(true); } direction = closestDirection(floatingPoint, end.center); floatingPoint = moveByDirection(floatingPoint, direction); } //no stream found after all points between searched. return(false); }
/// <summary> /// Adds a Resident to the garden. /// </summary> /// <param name="newNeighbor">The Resident to be added to the garden.</param> /// <param name="x">The leftmost x coordinate where the Resident will be placed.</param> /// <param name="y">The bottommost y coordinate where the Resident will be placed.</param> /// <param name="overwrite">Boolean indicating if the object will remove any object that /// may be in its intended footprint.</param> /// <returns>A boolean indicating whether the addition was successful or not.</returns> public bool addResident(Resident newNeighbor, Point spot, bool overwrite) { newNeighbor.origin = spot; newNeighbor.hueristic = atomAt(newNeighbor.center).rockHeuristic; if (!(inGarden(spot) && inGarden(new Point(spot.X + newNeighbor.width, spot.Y + newNeighbor.length)))) { return(false); } residentsList.Add(newNeighbor); List <Point> pointsToAddTo = getCoordinates(spot, newNeighbor.width, newNeighbor.length); //check if we can add the resident to the grid //return false if user does not want to overwrite anything that isn't gravel //remove that object if it is. foreach (Point coordinate in pointsToAddTo) { //is there an item here? is it gravel? if (getResident(coordinate) != null && !getResident(coordinate).isGravel) { if (!overwrite) { //Since there is a Resident here, we must throw false to show that the addition won't happen return(false); } else { //delete the object here at all locations it exists removeResident(spot); } } } //add the resident to the grid at each point it would occupy based on its size foreach (Point coordinate in pointsToAddTo) { atomAt(coordinate).setResident(newNeighbor, spot); } return(true); }
/// <summary> /// Returns a boolean on the existence of a stream on tempRes. /// </summary> /// <param name="tempRes">The Resident to be examined for a stream.</param> /// <returns>A bool on the existence of a stream at tempRes.</returns> private bool detectStream(Resident tempRes) { return(tempRes.hasStream); }
/// <summary> /// Returns a boolean on the existence of a rock on tempRes. /// </summary> /// <param name="tempRes">The Resident to be examined for a rock.</param> /// <returns>A bool on the existence of a rock at tempRes.</returns> private bool detectRock(Resident tempRes) { return(!tempRes.isGravel); }
/// <summary> /// Determines if there is a Rock between the two Residents. /// A line will be drawn from the centers of the residents and a Rock will be searched /// for along that path. Any Rock on that path will cause this method to evaluate to /// true. If no Rocks are found, the method will return false. /// </summary> /// <param name="start">The Resident from which the search will begin.</param> /// <param name="end">The Resident on which the search will end.</param> /// <returns>The existence of a Rock between the two Residents.</returns> public bool isRockBetween(Resident start, Resident end) { return(isSearchBetween(detectRock, start, end)); }
/// <summary> /// Determines if there is a stream between the two Residents. /// A line will be drawn from the centers of the residents and a stream will be searched /// for along that path. Any stream on that path will cause this method to evaluate to /// true. If no streams are found, the method will return false. /// </summary> /// <param name="start">The Resident from which the search will begin.</param> /// <param name="end">The Resident on which the search will end.</param> /// <returns>The existence of a stream between the two Residents.</returns> public bool isStreamBetween(Resident start, Resident end) { return(isSearchBetween(detectStream, start, end)); }
public bool addResident(Resident resident, Point spot, bool overwrite) { return(testGarden.addResident(resident, spot, overwrite)); }
public void removeResident() { occupant = null; heuristic = defaultHeuristic; }
public void setResident(Resident member, Point spot) { occupant = member; setRockHeuristic(); }