我实施了A *。但是,当它运行时,当向量中的所有节点具有相等的f分数时,它将作为BFS运行。
有许多简单的优化或实现细节可能会显着影响A *实现的性能。需要注意的第一个细节是优先级队列处理关系的方式在某些情况下会对性能产生重大影响。如果关系被破坏以使队列以LIFO方式运行, A *将表现为类似成本路径中的深度优先搜索(避免探索多个同样最佳的解决方案)
[维基百科 - A *]
我想知道是否有办法修改我现有的程序(提供的代码段),不仅要检索最低元素,还要检索第一个最低元素。
void Search::aStar()
{
searchMethod = "ASTAR";
generateMap();
// Draw Window and wall
guiOpen("VisX", 800, 600);
guiShowWall();
std::vector<Node> nodeHistory;
std::vector<std::reference_wrapper<Node>> openSet;
// Get starting point, add it to the queue and set as visited
Coord start = getStartPos();
Node &root = mapMaze[start.x][start.y];
openSet.push_back(root);
root.setVisitFlag(true);
root.setGScore(0);
while (!openSet.empty())
{
// Put the minimium fscore element to the front
auto result = std::min_element(openSet.begin(), openSet.end(), lowestFScore());
int minElementPos = std::distance(std::begin(openSet), result);
std::swap(openSet[minElementPos], openSet.front());
Node ¤t = openSet.front();
// Re-assign pending flag to visited
current.setPendingVisit(false);
current.setVisitFlag(true);
// Update the GUI display
guiRefresh(current);
openSet.erase(openSet.begin());
// Add to list of visited nodes
nodeHistory.push_back(current);
if (current.isFinish())
{
std::cout << "[Informed] A*: Found Finish"
<< "\nNote: Speed of search has been slowed down by GUI display."
<< std::endl;
// Construct path & update GUI with path
constructPath(nodeHistory);
guiShowConstructedPath();
guiClose();
break;
}
// Add each valid edges node to the queue
for (int i = 0; i < EDGE_AMOUNT; i++)
{
if (current.isValidEdge(i))
{
Node &neighbor = mapMaze[current.getEdge(i).x][current.getEdge(i).y];
// If not a wall and has been visited, ignore
if (neighbor.isNotWall() && !(neighbor.isNotVisited())) continue;
// If not in openset, add it and set flag
if (neighbor.isNotWall() && neighbor.isNotVisited() && neighbor.isNotPendingVisit())
{
// Add to queue and set flag
openSet.push_back(neighbor);
neighbor.setPendingVisit(true);
// Update the GUI display
guiRefresh(neighbor);
}
// Calculate gScore, and see if it is better than neigbours current score.
#define MOVEMENT_COST (1)
int tentativeGScore = current.getGScore() + MOVEMENT_COST;
if (tentativeGScore >= neighbor.getGScore()) continue;
// This path is the best until now. Record it!
neighbor.setParent(current);
neighbor.setGScore(tentativeGScore);
int fScore = neighbor.getGScore() + neighbor.getHScore();
neighbor.setFScore(fScore);
}
}
}
}
struct lowestFScore
{
bool operator()(const Node& lhs, const Node& rhs) const
{
return lhs.getFScore() < rhs.getFScore();
}
};
答案 0 :(得分:2)
标准中有一个priority_queue。
以下是参考:http://en.cppreference.com/w/cpp/container/priority_queue
我不确定你是否需要它:
AdvancedDatastore
答案 1 :(得分:1)
将Node
包裹到这样的结构中:
struct OpenNode
{
const Node &node;
const unsigned int order;
};
并定义您的openSet
,如:
std::vector<OpenNode> openSet;
在unsigned int counter
循环之前将while
初始化为0并进行以下更改:
// Add before while loop
unsigned int counter = 0;
// ...
Node ¤t = openSet.front().node;
// ...
openSet.push_back({neighbor, counter++});
最后调整lowestScore
:
struct lowestFScore
{
bool operator()(const OpenNode& lhs, const OpenNode& rhs) const
{
auto lScore = lhs.node.getFScore();
auto rScore = rhs.node.getFScore();
if (lScore == rScore)
{
// Bigger order goes first
return lhs.order > rhs.order;
}
return lScore < rScore;
}
};
根据建议,您可能希望将openSet
切换为std::priority_queue
,这样可以更快地检索最少的元素。您应该能够使用相同的比较逻辑。