//Start from start, move to within distance of goal within max steps. public static HexPosition[] search(HexPosition start, HexPosition goal, int max, int distance = 0) { max += distance; //Now it's the maximum distance to the goal, instead of just the maximum number of steps. //HashSet<HexPosition> closedSet = new HashSet<HexPosition>(); // The set of nodes already evaluated. //HashSet<HexPosition> openSet = new HashSet<HexPosition>(start); // The set of tentative nodes to be evaluated, initially containing the start node Dictionary <HexPosition, HexPosition> cameFrom = new Dictionary <HexPosition, HexPosition>(); // The map of navigated nodes. Dictionary <HexPosition, int> gScore = new Dictionary <HexPosition, int>(); // Cost from start along best known path. Domain is the open and closed sets. Dictionary <HexPosition, int> fScore = new Dictionary <HexPosition, int>(); // Estimated total cost from start to goal through y. Domain is the open set. gScore.Add(start, 0); fScore.Add(start, start.dist(goal)); while (fScore.Count > 0) { HexPosition current = getMin(fScore); if (current.dist(goal) <= distance) { int length = 0; gScore.TryGetValue(current, out length); return(reconstructPath(cameFrom, current, length + 1)); } fScore.Remove(current); foreach (HexPosition neighbor in current.Neighbors) { if (neighbor.containsKey("Obstacle") || neighbor.containsKey("Unit")) { continue; //Make this more general. } if (gScore.ContainsKey(neighbor) && !fScore.ContainsKey(neighbor)) { continue; } int tentativeGScore = 0; gScore.TryGetValue(current, out tentativeGScore); ++tentativeGScore; if (tentativeGScore > max) { continue; } int neighborGScore = 0; gScore.TryGetValue(current, out neighborGScore); if (!fScore.ContainsKey(neighbor) || tentativeGScore < neighborGScore) { int newFScore = tentativeGScore + neighbor.dist(goal); if (newFScore > max) { continue; } cameFrom.Add(neighbor, current); gScore.Add(neighbor, tentativeGScore); fScore.Add(neighbor, newFScore); } } } return(new HexPosition[0] { }); }
//Start from start, move to within distance of goal within max steps. public static HexPosition[] search(HexPosition start, HexPosition goal, int max, int distance = 0) { max += distance; //Now it's the maximum distance to the goal, instead of just the maximum number of steps. //HashSet<HexPosition> closedSet = new HashSet<HexPosition>(); // The set of nodes already evaluated. //HashSet<HexPosition> openSet = new HashSet<HexPosition>(start); // The set of tentative nodes to be evaluated, initially containing the start node Dictionary<HexPosition, HexPosition> cameFrom = new Dictionary<HexPosition, HexPosition>(); // The map of navigated nodes. Dictionary<HexPosition, int> gScore = new Dictionary<HexPosition, int>(); // Cost from start along best known path. Domain is the open and closed sets. Dictionary<HexPosition, int> fScore = new Dictionary<HexPosition, int>(); // Estimated total cost from start to goal through y. Domain is the open set. gScore.Add(start, 0); fScore.Add(start, start.dist(goal)); while (fScore.Count > 0) { HexPosition current = getMin(fScore); if (current.dist(goal) <= distance) { int length = 0; gScore.TryGetValue(current, out length); return reconstructPath(cameFrom, current, length + 1); } fScore.Remove(current); foreach (HexPosition neighbor in current.Neighbors) { if (neighbor.containsKey("Obstacle") || neighbor.containsKey("Unit")) { continue; //Make this more general. } if (gScore.ContainsKey(neighbor) && !fScore.ContainsKey(neighbor)) { continue; } int tentativeGScore = 0; gScore.TryGetValue(current, out tentativeGScore); ++tentativeGScore; if (tentativeGScore > max) { continue; } int neighborGScore = 0; gScore.TryGetValue(current, out neighborGScore); if (!fScore.ContainsKey(neighbor) || tentativeGScore < neighborGScore) { int newFScore = tentativeGScore + neighbor.dist(goal); if (newFScore > max) { continue; } cameFrom.Add(neighbor, current); gScore.Add(neighbor, tentativeGScore); fScore.Add(neighbor, newFScore); } } } return new HexPosition[0] { }; }
/// <summary> /// Static reward for a given state /// </summary> /// <param name="stateIndex"></param> /// <returns></returns> float TestRewardFunction(int state) { // reward for going to position 2,2 float maxReward = 1.0f; var aircraft = stateSpace.GetAircraftAtState(state); HexPosition target = new HexPosition(targetPosition.x, targetPosition.y); int distanceToTarget = target.dist(aircraft.hexPos); if (aircraft.hitPoints == 0) { return(-1); } if (distanceToTarget == 0 && aircraft.heading == targetHeading) { return(maxReward); } else { return(0); } }
private bool isAttackable(Unit attacker, Unit attacked, HexPosition coordinates) { return(attacked.PLAYER != player && coordinates.dist(attacked.Coordinates) <= attacker.RANGE); }
//TODO: Move to Unit.cs private bool isAttackable(Unit attacker, Unit attacked, HexPosition coordinates) { return attacked.PLAYER != player && coordinates.dist(attacked.Coordinates) <= attacker.RANGE; }