public void RBFSWithoutOrderByTest() { var queryable = HeuristicSearch.RecursiveBestFirstSearch(start, goal, (step, lv) => step.GetFourDirections(unit)); var obstacles = new[] { new Point(5, 5), new Point(6, 6), new Point(7, 7), new Point(8, 8), new Point(9, 9) }; var solution = from step in queryable from obstacle in obstacles where step != obstacle select step; var exception = Assert.Throws <InvalidOperationException>(() => solution.ToList()); Assert.StartsWith("Unable to evaluate steps.", exception.Message); }
public IEnumerator <object[]> GetEnumerator() { foreach (var setting in MapData.GetStartGoalCombinations()) { var start = setting.Start; var goal = setting.Goal; var queryable = HeuristicSearch.RecursiveBestFirstSearch(start, goal, (step, lv) => step.GetFourDirections(MapData.Unit)); var solution = from step in queryable.Except(MapData.Obstacles) where step.X >= 0 && step.Y >= 0 && step.X <= 40 && step.Y <= 40 orderby step.GetManhattanDistance(goal) select step; yield return(new object[] { start, goal, solution.ToArray() }); } }
public RecursiveBestFirstSearchValidityTest() { var mapData = TestHelper.LoadMapData(); _start = mapData.Start; _goal = mapData.Goal; _obstacles = mapData.Obstacles; _unit = 1f; var queryable = HeuristicSearch.RecursiveBestFirstSearch(_start, _goal, (step, lv) => step.GetFourDirections(_unit)); var solution = from step in queryable.Except(mapData.Obstacles) where step.X >= 0 && step.Y >= 0 && step.X <= 40 && step.Y <= 40 orderby step.GetManhattanDistance(_goal) select step; _path = solution.ToList(); }
static void Main(string[] args) { while (true) { // http://www.8puzzle.com/images/8_puzzle_start_state_a.png var initial = new BoardState(new[] { new Point(1, 2), // empty square new Point(0, 1), // square 1 new Point(0, 0), // square 2 new Point(2, 0), // square 3 new Point(2, 1), // square 4 new Point(2, 2), // square 5 new Point(1, 1), // square 6 new Point(0, 2), // square 7 new Point(1, 0), // square 8 }); // http://www.8puzzle.com/images/8_puzzle_goal_state_a.png var goal = new BoardState(new[] { new Point(1, 1), // empty square new Point(0, 0), // square 1 new Point(1, 0), // square 2 new Point(2, 0), // square 3 new Point(2, 1), // square 4 new Point(2, 2), // square 5 new Point(1, 2), // square 6 new Point(0, 2), // square 7 new Point(0, 1), // square 8 }); Console.WriteLine("A)* Search"); Console.WriteLine("B)est-first Search"); Console.WriteLine("I)terative Deepening AStar Search"); Console.WriteLine("R)ecursive Best-first Search"); Console.Write("Select an algorithm: "); var queryable = default(HeuristicSearchBase <BoardState, BoardState>); // Initialize the algorithm with the callback that gets all valid moves. switch (Console.ReadKey().Key) { case ConsoleKey.A: queryable = HeuristicSearch.AStar(initial, goal, (board, lv) => board.GetNextSteps()); break; case ConsoleKey.B: queryable = HeuristicSearch.BestFirstSearch(initial, goal, (board, lv) => board.GetNextSteps()); break; case ConsoleKey.I: queryable = HeuristicSearch.IterativeDeepeningAStar(initial, goal, (board, lv) => board.GetNextSteps()); break; case ConsoleKey.R: queryable = HeuristicSearch.RecursiveBestFirstSearch(initial, goal, (board, lv) => board.GetNextSteps()); break; default: continue; } Console.WriteLine(); // GetSumOfDistances() calculates the sum of Manhattan distance // between each of square and its goal. // ------------------------------------------------- var solution = from board in queryable orderby board.GetSumOfDistances(goal) select board; // ------------------------------------------------- // Print out the solution. foreach (var board in solution) { Console.WriteLine(board); } } }
static void Main(string[] args) { var boundary = new Rectangle(0, 0, MapWidth, MapWidth); var presentation = new char[MapHeight][]; while (true) { var queryable = default(HeuristicSearchBase <Point, Point>); var solution = default(HeuristicSearchBase <Point, Point>); var counter = 0; var mapData = LoadMapData(); for (var y = 0; y < MapHeight; y++) { if (presentation[y] == null) { presentation[y] = new char[MapWidth]; } for (var x = 0; x < MapWidth; x++) { var point = new Point(x, y); if (mapData.Obstacles.Contains(point)) { presentation[y][x] = ObstacleSymbol; } else if (point == mapData.Start) { presentation[y][x] = StartSymbol; } else if (point.Equals(mapData.Goal)) { presentation[y][x] = GoalSymbol; } else { presentation[y][x] = SpaceSymbol; } } } Console.WriteLine("A)* Search"); Console.WriteLine("B)est-first Search"); Console.WriteLine("R)ecursive Best-first Search"); Console.WriteLine("I)terative Deepening AStar Search"); Console.Write("Select an algorithm: "); // Initial the algorithm. switch (Console.ReadKey().Key) { case ConsoleKey.A: queryable = HeuristicSearch.AStar(mapData.Start, mapData.Goal, GetNextSteps); break; case ConsoleKey.B: queryable = HeuristicSearch.BestFirstSearch(mapData.Start, mapData.Goal, GetNextSteps); break; case ConsoleKey.I: queryable = HeuristicSearch.IterativeDeepeningAStar(mapData.Start, mapData.Goal, GetNextSteps); break; case ConsoleKey.R: queryable = HeuristicSearch.RecursiveBestFirstSearch(mapData.Start, mapData.Goal, GetNextSteps); break; default: continue; } Console.WriteLine(); Console.WriteLine("C)hebyshev Distance Comparer"); Console.WriteLine("E)uclidean Distance Comparer"); Console.WriteLine("M)anhattan Distance Comparer"); Console.Write("Select comparer: "); // Compare two positions and the goal position with selected distance. switch (Console.ReadKey().Key) { case ConsoleKey.C: solution = from step in queryable.Except(mapData.Obstacles) where boundary.Contains(step) orderby step.GetChebyshevDistance(mapData.Goal) select step; break; case ConsoleKey.E: solution = from step in queryable.Except(mapData.Obstacles) where boundary.Contains(step) orderby step.GetEuclideanDistance(mapData.Goal) select step; break; case ConsoleKey.M: solution = from step in queryable.Except(mapData.Obstacles) where boundary.Contains(step) orderby step.GetManhattanDistance(mapData.Goal) select step; break; default: continue; } Console.WriteLine(); foreach (var step in solution) { if (step != mapData.Start && step != mapData.Goal) { presentation[(int)step.Y][(int)step.X] = PathSymbol; } counter++; } Array.ForEach(presentation, row => Console.WriteLine(string.Join(" ", row))); Console.WriteLine("Total steps: {0}", counter); } }