这里我有一个关于A星搜索算法的查询。我正在建造所谓的8件拼图。 这是一个有9个位置的游戏(1个是空的),你必须以正确的顺序排列瓷砖以满足目标位置。
我只需要验证我是否正确编写了算法,以便我可以在我的代码中寻找其他问题。
我个人认为算法是正确的,因为它能够解决我创建的第一个预设拼图,只需要一次移动即可到达目标位置。然而,它无法解决需要更多动作的谜题。
我试图以3种不同的方式让它发挥作用,并且都带来同样的问题。
尝试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
尝试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;
}
尝试2是我在互联网上找到的算法的更改:Solving the 8-Puzzle Problem
但是我尽力遵循维基百科上的伪代码:
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
我问你是否可以找到一个问题,因为我很困惑为什么它只适用于其中一个谜题。这个难题的唯一分离是解决目标状态所需的移动量。
我已经调试了好几个小时而且我看不到它,我也看不到我的代码中的其他地方可能有问题。所以我想我只是在问,这对你来说是否正确?
任何问题一定要问,我会尽可能提供更多信息! 提前谢谢!
答案 0 :(得分:0)
注意:我正在使用您可以找到的here
的CustomPriorityQueue类(由 itowlson 编写)所以在这里我基本上使用PriorityQueue,它将通过启发式值
排列问题状态这是C#中的一个简单的A *模板,用于说明A *搜索的工作原理:
void enqueueAll(CustomPriorityQueue<PuzzleNode> a, ArrayList<PuzzleNode> b)
{
foreach(PuzzleNode c in b) a.enqueue(c, h(c));
}
// A* heuritic: h = f + g ; h <= h*
int h(PuzzleNode n);
// returns TRUE if n is a solution
bool isSolution(PuzzleNode n);
// expand n into list of neighbors
ArrayList<PuzzleNode> expand(PuzzleNode n);
PuzzleNode Astar(PuzzleNode startPoint)
{
CustomPriorityQueue list = new CustomPriorityQueue<PuzzleNode>();
PuzzleNode current = null;
list.enqueue(startPoint, h(startPoint));
while (list.size() > 0)
{
current = list.dequeue();
if (isSolution(current))
{
return current;
}
enqueueAll(list, expand(current));
}
}
希望这会有所帮助