From dbe2cfd50014b8d945108befe4bc84fe62c25f97 Mon Sep 17 00:00:00 2001 From: Dan Cojocaru Date: Thu, 2 Dec 2021 00:44:28 +0200 Subject: [PATCH] Fixed threshold computation Also made the implementation less generic and less of a headache; int everything! Show the threshold when printing intermediary steps. --- IDAstar.cs | 32 +++++++++++++++++++++----------- Program.cs | 7 ++++--- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/IDAstar.cs b/IDAstar.cs index f912589..ed66129 100644 --- a/IDAstar.cs +++ b/IDAstar.cs @@ -124,7 +124,7 @@ namespace IdaStar get => _board.Select((row) => row.AsReadOnly()).ToList().AsReadOnly(); } - public event Action? AlgorithmStep; + public event Action? AlgorithmStep; /// /// Clean board, making it ready for a new run. @@ -141,8 +141,8 @@ namespace IdaStar } } - public void RunIdaStar() => RunIdaStar(0, 1, (p1, p2) => p1.ManhattanDistance(p2)); - public void RunIdaStar(Num zero, Num increment, Func heuristic) where Num: INumber { + public void RunIdaStar() => RunIdaStar((p1, p2) => p1.ManhattanDistance(p2)); + public void RunIdaStar(Func heuristic) { // Don't run algorithm on a "dirty" board // "dirty" = the algorithm was already ran before if (_board.Select((row) => row.Where((state) => state == CellState.PATH).Count()).Any((cnt) => cnt > 0)) { @@ -164,9 +164,9 @@ namespace IdaStar Point destinationPoint = findPoint(CellState.DESTINATION); - Num search(Point current, Num cost, Num threshold) { + int search(Point current, int cost, int threshold) { var h = heuristic(current, destinationPoint); - if (h == zero) { + if (h == 0) { return h; } var f = cost + h; @@ -179,34 +179,44 @@ namespace IdaStar if (!neighbour.IsInsideBox(_board.Count, _board[0].Count)) { continue; } - if (_board[neighbour.Row][neighbour.Column] == CellState.OBSTACLE) { + if (_board[neighbour.Row][neighbour.Column] == CellState.OBSTACLE || + _board[neighbour.Row][neighbour.Column] == CellState.PATH) { continue; } if (_board[neighbour.Row][neighbour.Column] == CellState.EMPTY) { _board[neighbour.Row][neighbour.Column] = CellState.PATH; } - AlgorithmStep?.Invoke(this); - var neighbourF = search(neighbour, cost + increment, threshold); + AlgorithmStep?.Invoke(this, threshold); + var neighbourF = search(neighbour, cost + 1, threshold); if (neighbourF < min) { min = neighbourF; } - if (min == zero) { + if (min == 0) { break; } if (_board[neighbour.Row][neighbour.Column] == CellState.PATH) { _board[neighbour.Row][neighbour.Column] = CellState.EMPTY; } + AlgorithmStep?.Invoke(this, threshold); + } return min; } var threshold = heuristic(startPoint, destinationPoint); - while (threshold != zero) { - threshold = search(startPoint, zero, threshold); + while (threshold != 0) { + var newThreshold = search(startPoint, 0, threshold); + AlgorithmStep?.Invoke(this, threshold); + if (newThreshold == 0) { + threshold = 0; + } + else { + threshold++; + } } } } diff --git a/Program.cs b/Program.cs index babba88..dc3e50e 100644 --- a/Program.cs +++ b/Program.cs @@ -14,15 +14,15 @@ var algoBoard = new IdaStar.WorkingBoard(labyrinthIN.Select((row) => row.ToList( int step = 0; bool done = false; ConsoleColor border = ConsoleColor.Magenta; -algoBoard.AlgorithmStep += (_) => { +algoBoard.AlgorithmStep += (_, threshold) => { Console.Clear(); step++; if(done){ System.Console.WriteLine("The solved labyrinth is:"); }else if(step%2 == 0) { - System.Console.WriteLine("Computing [· ]"); + System.Console.WriteLine($"Computing (threshold: {threshold}) [· ]"); }else { - System.Console.WriteLine("Computing [ ·]"); + System.Console.WriteLine($"Computing (threshold: {threshold}) [ ·]"); } //top border @@ -62,6 +62,7 @@ algoBoard.AlgorithmStep += (_) => { Console.ResetColor(); Console.WriteLine(); Thread.Sleep(200); + // Console.ReadLine(); }; algoBoard.RunIdaStar();