public IEnumerable <RobotAction> GetNextActions(Robot robot) { robot.Targets = null; robot.Target = null; // gimme all the empty cells within x moves var empties = DijkstraPathfinder.FindUnwrappedCellsWithin(robot.Position, Problem.Map, int.MaxValue, false); if (empties.Count == 0) { return new[] { RobotAction.Done, } } ; var islands = new List <HashSet <Point> >(); foreach (var t in empties) { // point is already in an island bool found = false; foreach (var island in islands) { if (island.Contains(t)) { found = true; break; } } if (found) { continue; } var np = DijkstraPathfinder.FindUnwrappedCellsWithin(t, Problem.Map, int.MaxValue, true); islands.Add(new HashSet <Point>(np)); } // no islands, do something else if (islands.Count == 0) { return(NextController.GetNextActions(robot)); } // find smallest island var smallestIsland = islands.OrderBy(x => x.Count).First(); robot.Targets = smallestIsland; // dij a path to it var route = DijkstraPathfinder.RouteToClosestCell(robot.Position, smallestIsland, Problem.Map); robot.Target = route.Item1; // go to there return(route.Item2); } }
public void TestNonContinuous() { List <PathNode> testPathNodes = Get5x5PathNodeList(); testPathNodes.RemoveAll(node => node.pos.xPos == -4); List <PathNode> accessible = DijkstraPathfinder.CalculatePathDistance(testPathNodes, 0, 0); Assert.AreEqual(57, accessible.Count, "Some pathnodes were trimmed inappropriately"); }
public void Setup() { GameField1 = string.Join("", GameField1.Split(default(string[]), StringSplitOptions.RemoveEmptyEntries)); mapParser = new MapParser(codes); map = mapParser.ParseMapFromString(GameField1, new VectorInt2(8, 7)); graph = map.ToFullGraph(); mind = new Mastermind(""); pathfinder = new DijkstraPathfinder(); playerTile = map.Values.First(x => x.IsPlayer); pathfinder.FindAllPaths(graph, playerTile); }
public IEnumerable <RobotAction> GetNextActions(Robot robot) { var result = DijkstraPathfinder.ClosestUnwrappedCell(robot.Position, Problem.Map); if (result == null) { return new[] { RobotAction.Done } } ; return(result.Item2); } }
public void TestUniformCostUnbounded() { List <PathNode> testPathNodes = Get5x5PathNodeList(); List <PathNode> accessible = DijkstraPathfinder.CalculatePathDistance(testPathNodes, 0, 0); Assert.AreEqual(61, accessible.Count, "Some pathnodes were trimmed inappropriately"); int[] distArray = new int[] { 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 }; int distIndex = 0; foreach (PathNode pnode in accessible) { Assert.AreEqual(distArray[distIndex++], pnode.distance, "A pathnode was not the expected distance"); } }
public void TestCostPocket() { List <PathNode> testPathNodes = Get5x5PathNodeList(); testPathNodes.First(node => node.pos.xPos == 1 && node.pos.yPos == 1).cost = 100; testPathNodes.First(node => node.pos.xPos == 1 && node.pos.yPos == 2).cost = 100; testPathNodes.First(node => node.pos.xPos == 2 && node.pos.yPos == 1).cost = 100; testPathNodes.First(node => node.pos.xPos == 3 && node.pos.yPos == 1).cost = 100; testPathNodes.First(node => node.pos.xPos == 3 && node.pos.yPos == 2).cost = 100; List <PathNode> accessible = DijkstraPathfinder.CalculatePathDistance(testPathNodes, 0, 0); PathNode pocketNode = accessible.First(node => node.pos.xPos == 2 && node.pos.yPos == 2); Assert.AreEqual(6, pocketNode.distance, "Pocket cost not accounted for"); PathNode edgeNode1 = accessible.First(node => node.pos.xPos == 1 && node.pos.yPos == 1); Assert.AreEqual(101, edgeNode1.distance, "edge node cost not accounted for"); PathNode edgeNode2 = accessible.First(node => node.pos.xPos == 3 && node.pos.yPos == 2); Assert.AreEqual(106, edgeNode2.distance, "edge node cost not accounted for"); }
public IEnumerable <RobotAction> GetNextActions(Robot robot) { robot.Targets = null; robot.Target = null; // gimme all the empty cells within x moves var empties = DijkstraPathfinder.FindUnwrappedCellsWithin(robot.Position, Problem.Map, 10, false); if (empties.Count == 0) { return(NextController.GetNextActions(robot)); } var islands = new Dictionary <Point, HashSet <Point> >(); var notIslands = new HashSet <Point>(); foreach (var t in empties) { // point is not in an island cluster if (notIslands.Contains(t)) { continue; } // point is already in an island bool found = false; foreach (var island in islands.Values) { if (island.Contains(t)) { found = true; break; } } if (found) { continue; } var np = DijkstraPathfinder.FindUnwrappedCellsWithin(t, Problem.Map, 100, true); if (np.Count > 50) { np.ToList().ForEach(x => notIslands.Add(x)); // not an island, mark 'em } else { // found a new island islands.Add(t, new HashSet <Point>(np)); } } // no islands, do something else if (islands.Count == 0) { return(NextController.GetNextActions(robot)); } // paint islands robot.Targets = new HashSet <Point>(islands.Values.SelectMany(x => x)); // find the closest point in the smallest island var closestScore = int.MaxValue; IEnumerable <RobotAction> closestRoute = null; foreach (var target in islands.OrderBy(x => x.Value.Count).First().Value) { var result = AStarPathFinder.GetRouteTo(robot.Position, target, Problem.Map, closestScore); if (result != null && result.Count < closestScore) { closestScore = result.Count; closestRoute = result; robot.Target = target; } } // go to there return(closestRoute.Take(1)); }
override protected Vector2Int FetchMove() { DijkstraPathfinder pf = new DijkstraPathfinder(this.board, this.position, this.Target(), this.moves); return(pf.BestMoveList()[0]); }