Here I have a query regarding the A Star Search Algorithm. I'm building what is known as the 8 piece puzzle. This is a game with 9 places (1 is empty) and you have to arrange the tiles in a correct order to meet the goal position.
I just need to verify that I have written the algorithm correctly so I can seek elsewhere in my code for the issue.
I personally believe that the algorithm is correct because it is able to solve the first pre-set puzzle I have created which only requires one move to reach the goal position. However, it is unable to solve the puzzles which require more moves.
I've tried to get this to work in 3 different ways and all bring the same issue.
Attempt 1:
while (openList.Count() > 0)
{
PuzzleNode currentNode;
var orderedOpenList = openList.OrderBy(PuzzleNode => PuzzleNode.getPathCost());
currentNode = orderedOpenList.First();
if (compare(currentNode.getPuzzle(), goalArray) == true)
{
//openList.RemoveAt(0); //POLL
break;
// otherwise push currentNode onto closedList & remove from openList
}
else
{
openList.Remove(currentNode);
closedList.Add(currentNode);
}
//generate all the neighbor nodes
generateSuccessors(currentNode, tempList);
for (int i = 0; i < tempList.Count(); i++)
{
PuzzleNode tempNode = tempList[i];
//skip the node if it's in the closed list
if (closedList.Contains(tempNode))
{
continue;
}//end if
//We need to ensure that the G we have seen here is the shortest one
int gScore = currentNode.getG() + 1;
if (!openList.Contains(tempNode) || gScore < tempNode.getG())
{
tempNode.setParentNode(currentNode);
tempNode.setH(tempNode.calcH(currentHueristic, tempNode.getPuzzle(), goalArray));
tempNode.setG(gScore);
tempNode.updatePathCost();
if (!openList.Contains(tempNode))
{
openList.Add(tempNode);
}//end if
}//end if
}//end for
}//end while
Attempt 2:
while (openList.Count() > 0)
{
PuzzleNode currentNode = GetBestNodeFromOpenList(openList);
openList.Remove(currentNode);
closedList.Add(currentNode);
generateSuccessors(currentNode, tempList);
foreach (PuzzleNode successorNode in tempList)
{
if (compare(successorNode.getPuzzle(), goalArray) == true)
{
//goto thebreak;
return successorNode;
}
successorNode.setG(currentNode.getG() + 1);
successorNode.setH(successorNode.calcH(currentHueristic, successorNode.getPuzzle(), goalArray));
successorNode.updatePathCost();
if (OpenListHasBetterNode(successorNode, openList))
continue;
openList.Add(successorNode);
}
}//end while
private static PuzzleNode GetBestNodeFromOpenList(IEnumerable<PuzzleNode> openList)
{
return openList.OrderBy(n => n.getPathCost()).First();
}
private static bool OpenListHasBetterNode(PuzzleNode successor, IEnumerable<PuzzleNode> list)
{
return list.FirstOrDefault(n => n.getG().Equals(successor.getG())
&& n.getPathCost() < successor.getPathCost()) != null;
}
Attempt 2 is an alteration of an algorithm I found on the internet: Solving the 8-Puzzle Problem
However I done my best to follow the psuedocode on wikipedia:
function A*(start,goal)
closedset := the empty set // The set of nodes already evaluated.
openset := {start} // The set of tentative nodes to be evaluated, initially containing the start node
came_from := the empty map // The map of navigated nodes.
g_score[start] := 0 // Cost from start along best known path.
// Estimated total cost from start to goal through y.
f_score[start] := g_score[start] + heuristic_cost_estimate(start, goal)
while openset is not empty
current := the node in openset having the lowest f_score[] value
if current = goal
return reconstruct_path(came_from, goal)
remove current from openset
add current to closedset
for each neighbor in neighbor_nodes(current)
tentative_g_score := g_score[current] + dist_between(current,neighbor)
if neighbor in closedset
if tentative_g_score >= g_score[neighbor]
continue
if neighbor not in openset or tentative_g_score < g_score[neighbor]
came_from[neighbor] := current
g_score[neighbor] := tentative_g_score
f_score[neighbor] := g_score[neighbor] + heuristic_cost_estimate(neighbor, goal)
if neighbor not in openset
add neighbor to openset
I ask whether you can find an issue because I'm confused as to why it would work for only one of the puzzles. The only this seperate these puzzles is the amount of moves needed to solve to the goal state.
I've debugged for hours and I just can't see it, I also can't see where else in my code could have an issue. So I guess I'm just asking, does this look correct to you?
Any questions be sure to ask and I'll provide more information where possible! Thank you in advance!