mirror of
https://github.com/dancojocaru2000/ai-project-idastar.git
synced 2025-02-23 00:49:34 +02:00
Fixed threshold computation
Also made the implementation less generic and less of a headache; int everything! Show the threshold when printing intermediary steps.
This commit is contained in:
parent
8bbfbc837f
commit
dbe2cfd500
2 changed files with 25 additions and 14 deletions
32
IDAstar.cs
32
IDAstar.cs
|
@ -124,7 +124,7 @@ namespace IdaStar
|
||||||
get => _board.Select((row) => row.AsReadOnly()).ToList().AsReadOnly();
|
get => _board.Select((row) => row.AsReadOnly()).ToList().AsReadOnly();
|
||||||
}
|
}
|
||||||
|
|
||||||
public event Action<WorkingBoard>? AlgorithmStep;
|
public event Action<WorkingBoard, int>? AlgorithmStep;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Clean board, making it ready for a new run.
|
/// 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() => RunIdaStar((p1, p2) => p1.ManhattanDistance(p2));
|
||||||
public void RunIdaStar<Num>(Num zero, Num increment, Func<Point, Point, Num> heuristic) where Num: INumber<Num> {
|
public void RunIdaStar(Func<Point, Point, int> heuristic) {
|
||||||
// Don't run algorithm on a "dirty" board
|
// Don't run algorithm on a "dirty" board
|
||||||
// "dirty" = the algorithm was already ran before
|
// "dirty" = the algorithm was already ran before
|
||||||
if (_board.Select((row) => row.Where((state) => state == CellState.PATH).Count()).Any((cnt) => cnt > 0)) {
|
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);
|
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);
|
var h = heuristic(current, destinationPoint);
|
||||||
if (h == zero) {
|
if (h == 0) {
|
||||||
return h;
|
return h;
|
||||||
}
|
}
|
||||||
var f = cost + h;
|
var f = cost + h;
|
||||||
|
@ -179,34 +179,44 @@ namespace IdaStar
|
||||||
if (!neighbour.IsInsideBox(_board.Count, _board[0].Count)) {
|
if (!neighbour.IsInsideBox(_board.Count, _board[0].Count)) {
|
||||||
continue;
|
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;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_board[neighbour.Row][neighbour.Column] == CellState.EMPTY) {
|
if (_board[neighbour.Row][neighbour.Column] == CellState.EMPTY) {
|
||||||
_board[neighbour.Row][neighbour.Column] = CellState.PATH;
|
_board[neighbour.Row][neighbour.Column] = CellState.PATH;
|
||||||
}
|
}
|
||||||
AlgorithmStep?.Invoke(this);
|
AlgorithmStep?.Invoke(this, threshold);
|
||||||
var neighbourF = search(neighbour, cost + increment, threshold);
|
var neighbourF = search(neighbour, cost + 1, threshold);
|
||||||
|
|
||||||
if (neighbourF < min) {
|
if (neighbourF < min) {
|
||||||
min = neighbourF;
|
min = neighbourF;
|
||||||
}
|
}
|
||||||
if (min == zero) {
|
if (min == 0) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_board[neighbour.Row][neighbour.Column] == CellState.PATH) {
|
if (_board[neighbour.Row][neighbour.Column] == CellState.PATH) {
|
||||||
_board[neighbour.Row][neighbour.Column] = CellState.EMPTY;
|
_board[neighbour.Row][neighbour.Column] = CellState.EMPTY;
|
||||||
}
|
}
|
||||||
|
AlgorithmStep?.Invoke(this, threshold);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return min;
|
return min;
|
||||||
}
|
}
|
||||||
|
|
||||||
var threshold = heuristic(startPoint, destinationPoint);
|
var threshold = heuristic(startPoint, destinationPoint);
|
||||||
while (threshold != zero) {
|
while (threshold != 0) {
|
||||||
threshold = search(startPoint, zero, threshold);
|
var newThreshold = search(startPoint, 0, threshold);
|
||||||
|
AlgorithmStep?.Invoke(this, threshold);
|
||||||
|
if (newThreshold == 0) {
|
||||||
|
threshold = 0;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
threshold++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,15 +14,15 @@ var algoBoard = new IdaStar.WorkingBoard(labyrinthIN.Select((row) => row.ToList(
|
||||||
int step = 0;
|
int step = 0;
|
||||||
bool done = false;
|
bool done = false;
|
||||||
ConsoleColor border = ConsoleColor.Magenta;
|
ConsoleColor border = ConsoleColor.Magenta;
|
||||||
algoBoard.AlgorithmStep += (_) => {
|
algoBoard.AlgorithmStep += (_, threshold) => {
|
||||||
Console.Clear();
|
Console.Clear();
|
||||||
step++;
|
step++;
|
||||||
if(done){
|
if(done){
|
||||||
System.Console.WriteLine("The solved labyrinth is:");
|
System.Console.WriteLine("The solved labyrinth is:");
|
||||||
}else if(step%2 == 0) {
|
}else if(step%2 == 0) {
|
||||||
System.Console.WriteLine("Computing [· ]");
|
System.Console.WriteLine($"Computing (threshold: {threshold}) [· ]");
|
||||||
}else {
|
}else {
|
||||||
System.Console.WriteLine("Computing [ ·]");
|
System.Console.WriteLine($"Computing (threshold: {threshold}) [ ·]");
|
||||||
}
|
}
|
||||||
|
|
||||||
//top border
|
//top border
|
||||||
|
@ -62,6 +62,7 @@ algoBoard.AlgorithmStep += (_) => {
|
||||||
Console.ResetColor();
|
Console.ResetColor();
|
||||||
Console.WriteLine();
|
Console.WriteLine();
|
||||||
Thread.Sleep(200);
|
Thread.Sleep(200);
|
||||||
|
// Console.ReadLine();
|
||||||
};
|
};
|
||||||
|
|
||||||
algoBoard.RunIdaStar();
|
algoBoard.RunIdaStar();
|
||||||
|
|
Loading…
Add table
Reference in a new issue