• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
kirkd

Implementing A* - open_list

10 posts in this topic

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??
0

Share this post


Link to post
Share on other sites
[quote name='kirkd' timestamp='1349388325' post='4986924']
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??
[/quote]

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 [b]theoretically[/b] 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. Edited by slicer4ever
1

Share this post


Link to post
Share on other sites
[quote name='kirkd' timestamp='1349388325' post='4986924']
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.
[/quote]
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 [i]too[/i] 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.
2

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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.
1

Share this post


Link to post
Share on other sites
[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.
[/quote]

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^)
0

Share this post


Link to post
Share on other sites
I'm not sure what you're asking here. What are using as a key for your map?
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
[quote name='kirkd' timestamp='1349457274' post='4987183']
[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.
[/quote]

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:
[code]
// 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
[/code]
0

Share this post


Link to post
Share on other sites
Cornstalks,

I wondered if map would work, but for the wrong reason. Thanks for the explanation.

Also thanks for the example. That is in concept what I'm doing now. It is nice to have someone else come to the same conclusion and validate my approach.
0

Share this post


Link to post
Share on other sites
Hi. The way I did this was to have a bool inlist[n][m] that match your grid size then set it when you first come across the node. Then when done rest this list. I did it with vectors.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0