[Pathfinding] Beginning looking at and implementing A Star [C++]

Started by
10 comments, last by SCB 7 years, 3 months ago

deque<pNode*> openList;

I'm having is with adding to the openlist and compares their costs so that the smallest goes onto the Priority Queue and then compare its neighbors

A "deque" is a list, it's purpose is to make it fast to add or remove elements at the head and the tail quickly. For example you can make a temporary buffer with it, add new elements at one side, and the elements come out at the other side, in the same order as you added them.

In particular, it doesn't do sorting, your compare function isn't actually used. http://en.cppreference.com/w/cpp/container/deque

A second problem is that you store pointers to nodes in the list, while your comparator is about comparing nodes. Even if "deque" would do sorting, it would still not use your compare routine, as comparing pointers and comparing node objects are two different things.

To solve both, use std::multiset<pNode> instead for your open list. At least I think that should work for total costs. It will however fail for finding duplicate nodes (as in, at the same position). I am not sure how standard A-star solves that, but you can either keep a set around, that you sort on position (std::set, with a custom comparator), or rely on the closed list by checking that when you pull a node from the open list.

Advertisement

deque<pNode*> openList;

I'm having is with adding to the openlist and compares their costs so that the smallest goes onto the Priority Queue and then compare its neighbors

A "deque" is a list, it's purpose is to make it fast to add or remove elements at the head and the tail quickly. For example you can make a temporary buffer with it, add new elements at one side, and the elements come out at the other side, in the same order as you added them.

In particular, it doesn't do sorting, your compare function isn't actually used. http://en.cppreference.com/w/cpp/container/deque

A second problem is that you store pointers to nodes in the list, while your comparator is about comparing nodes. Even if "deque" would do sorting, it would still not use your compare routine, as comparing pointers and comparing node objects are two different things.

To solve both, use std::multiset<pNode> instead for your open list. At least I think that should work for total costs. It will however fail for finding duplicate nodes (as in, at the same position). I am not sure how standard A-star solves that, but you can either keep a set around, that you sort on position (std::set, with a custom comparator), or rely on the closed list by checking that when you pull a node from the open list.

Yea, I actually used:


list<pNode*>openList;

and


#include  <list>;

this was a step I missed when I went through it again. Completely realise what you meant with Deque though

This topic is closed to new replies.

Advertisement