I'm implementing an A* algorithm (mostly for fun and conceptual learning), and the open_list needs to do two things. 1) act as a priority queue for the purpose of searching closest nodes first, and 2) I need to determine if a node is already in the open_list before adding it. (2) is the case when I ask for neighbors of the current node and don't want to inadvertantly add a node twice to the list.
(1) is easily done (in C++) with std::priority_queue, but I cannot efficiently search a PQ for a pre-existing node.
(2) is easily done with a std::map, but I cannot use it as a priority queue
The only answer I see for this right now is to have two parallel structures that I make sure I keep consistent. Any other option that I'm overlooking??
Implementing A* - open_list
I'm implementing an A* algorithm (mostly for fun and conceptual learning), and the open_list needs to do two things. 1) act as a priority queue for the purpose of searching closest nodes first, and 2) I need to determine if a node is already in the open_list before adding it. (2) is the case when I ask for neighbors of the current node and don't want to inadvertantly add a node twice to the list.
(1) is easily done (in C++) with std::priority_queue, but I cannot efficiently search a PQ for a pre-existing node.
(2) is easily done with a std::map, but I cannot use it as a priority queue
The only answer I see for this right now is to have two parallel structures that I make sure I keep consistent. Any other option that I'm overlooking??
for #2, i generally add an ID field to my node, i compare this ID to my generator, if it's != then the node has not been added to the open or close list, as such, i add it, and I then update it's ID.
this of course means that theoretically it's possible for the generator to eventually loop a 32 bit ID, but the odds of that are increadibly small/unlikely, and a worse case is that a single path is constructed incorrectly.
Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.
I'm implementing an A* algorithm (mostly for fun and conceptual learning), and the open_list needs to do two things. 1) act as a priority queue for the purpose of searching closest nodes first, and 2) I need to determine if a node is already in the open_list before adding it.
A common way for handling #2 is just re-adding the node to the priority queue. Sure, it might be in the priority queue twice, but when you (eventually) pull out the node (and there's still a duplicate reference in the priority queue) you mark the node as closed/processed, so when you continue your iterating and pull out the duplicated reference, you just check if it's already been processed/if it's closed, and if so skip it and pull out the next node in the priority queue.
It does create some duplicates, but generally there aren't too many nodes that get re-added/duplicated in the priority queue. Usually there's just a few, and it's cheaper to add duplicates to the priority queue than to maintain multiple data structures or check for and maintain uniqueness in the priority queue. The only time a node gets duplicated in the priority queue is when you haven't processed it but find a shorter path to it, which won't be the case for many of your searched nodes.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
Great ideas! Thank you!
This does assume, however, that my search space has been fully expanded prior to search, correct? Or, put another way, that all the nodes of the search space have been instantiated. What about the situation when there is a huge search space and I don't pre-expand all the nodes but rather generate them as needed (as pointers). In this case I still can't conceptuatlize getting away from a dual structure - pq and map - to serve distint purposes.
This does assume, however, that my search space has been fully expanded prior to search, correct? Or, put another way, that all the nodes of the search space have been instantiated. What about the situation when there is a huge search space and I don't pre-expand all the nodes but rather generate them as needed (as pointers). In this case I still can't conceptuatlize getting away from a dual structure - pq and map - to serve distint purposes.
You could try the std::make_heap(), std::push_heap() and std::pop_heap() functions. This allows you to have a priority queue that you can still see into.
You could try the std::make_heap(), std::push_heap() and std::pop_heap() functions. This allows you to have a priority queue that you can still see into.
Oh, I like that one.
The trick I see is that I want a quick way to check if something is already in the PQ. If I use a std::vector as the container and apply these heap functions, I'll still need to scan the entire list to see if an entry is there. A std::map would be a good option - reduce the search for a pre-existing node to log(n) - but will these functions work with a std::map? From documentation the order of items in the map are the order of the keys used to store them. How would these affect the keys of the map?
I realize also that I'm really being nit-picky here and looking for the be-all end-all solution. I'm a perfectionsist. 8^)
I'm not sure what you're asking here. What are using as a key for your map?
I guess a more thorough explanation is necessary at this point.
I have a very generic AStar class that I've been able to apply to general map searches with the suggestions presented thus far. Now, I'm trying to use that same class to develop an optimal Rubik's Cube solver. Since the search space is so huge, I cannot generate all the nodes up front, but I have come up with a way to generate a unique ID for any given state of the cube. I've been using that UID to store nodes in a map to be able to tell whether I've explored that state before or not. The same node gets stored in a priority queue with the number of turn/manipulations from the start being the sorted value.
Since I'm generating the nodes as needed, it is very possible I'll end up with a cube state that I've seen before. The closed_list is a map and I can check if the UID for a node is present, but for the open_list, I can't get to an easy solution because of the dual roles expected of it. I need the priority queue behavior, obviously, but I need an efficient way to tell if something is already there.
I hope that clarifies things a bit.
I have a very generic AStar class that I've been able to apply to general map searches with the suggestions presented thus far. Now, I'm trying to use that same class to develop an optimal Rubik's Cube solver. Since the search space is so huge, I cannot generate all the nodes up front, but I have come up with a way to generate a unique ID for any given state of the cube. I've been using that UID to store nodes in a map to be able to tell whether I've explored that state before or not. The same node gets stored in a priority queue with the number of turn/manipulations from the start being the sorted value.
Since I'm generating the nodes as needed, it is very possible I'll end up with a cube state that I've seen before. The closed_list is a map and I can check if the UID for a node is present, but for the open_list, I can't get to an easy solution because of the dual roles expected of it. I need the priority queue behavior, obviously, but I need an efficient way to tell if something is already there.
I hope that clarifies things a bit.
[quote name='SiCrane' timestamp='1349453475' post='4987166']
You could try the std::make_heap(), std::push_heap() and std::pop_heap() functions. This allows you to have a priority queue that you can still see into.
Oh, I like that one.
The trick I see is that I want a quick way to check if something is already in the PQ. If I use a std::vector as the container and apply these heap functions, I'll still need to scan the entire list to see if an entry is there. A std::map would be a good option - reduce the search for a pre-existing node to log(n) - but will these functions work with a std::map? From documentation the order of items in the map are the order of the keys used to store them. How would these affect the keys of the map?
[/quote]
It would ruin the std::map, if it were possible. But the make/push/pop heap functions require random access iterators. std::map iterators are bidirectional iterators (they're not random access iterators), so you can't use a std::map for a binary heap.
There are various ways to do what you want. Here's one idea:
// Note that this code is *NOT* tested at all
typedef int UID; // or whatever is the right type for UID
struct Node
{
// ...constructor and whatever else you want to stuff in here...
int f, g, h; // A* values
bool open;
};
std::unordered_map<UID, Node> nodes;
Node& getNode(UID uid)
{
// Note that this will create the node if it doesn't exist
return nodes.emplace(uid, Node())->second;
}
bool isOnOpenList(UID uid)
{
auto i = nodes.find(uid);
return i != nodes.end() && i->second.open;
}
bool compareNodesByUID(UID left, UID right)
{
return getNode(left).f < getNode(right).f;
}
// This is the open list. To determine if something is already on the open list, just check isOnOpenList(id)
// But realistically, I don't think you need to even check if it's on the open list. You just need to make sure it's not closed.
// Just re-add the uid to the openList if you find a better path to a node
std::priority_queue<UID, std::vector<UID>, compareNodesByUID> openList;
// No need to maintain a closed list, as you can just check if node.open is false to determine it's on the closed list
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement