I am trying to write a program to perform the A star search algorithm.
I have the map and A Star Node defined as below, and I have declared some variables to work with.
class MapCell {
public:
short x_co;
short y_co;
bool obstacle;
string description;
};
class AStarNode {
public:
AStarNode *Parent;
short x_co;
short y_co;
short f;
short g;
short h;
};
Now I want to write a function with return an AStarNode in a List which is the smallest f, either from OpenList or CloseList, so the function will take the arguments of the List and the Node.
However I am just a newbie and I have no idea how to access the data f and compare it. Could anyone give me some help please?
Either way you should write a predicate to use with either the algorithm or the sort member function of list. By the way what is the *Parent attribute for? It'd be nice to see a bit more of what you are trying to do with the program. I'm glad to see that you are trying to use the STL. Let us know if you can't figure out what a predicate is. Look at the link and there is one overloaded version of the algorithm that requires a function pointer or functor which will compare two values and you'll use that program to access the f attribute for each. I wanted to say that your list could just be of objects but since you have that pointer attribute the copy construction and assignment would be an issue.
Since I am writing the A Star Path Search algorithm, the *parent gives me the information of how the path is constructed by the Node I found, so at the end I will be at the Goal, and by looking back the parent, I can see the Path from Goal to Start.
To be honest, I have no idea what predicate is. Also, I still don't know how to access my data in AStarNode, as I was trying to do the follow, and have no idea how to write. In this code, I am comparing x_co and y_co to search a Node in a List.
bool SearchInList(const list<AStarNodePtr>& List, AStarNodePtr Node) // don't copy - pass by reference
{
bool found = false;
// where did iter come from? You have to actually give it a type and form a complete
// declaration. Also don't keep calling end() within the loop. It is pointless. This is why
// you need to learn to use functors and algorithms so that you don't retype the same
// errors when writing your own loops. Google the terms and start reading some articles.
list<AStarNodePtr>::iterator iter(List.begin()), end(List.end());
for(; iter !=end; ++iter)
{
// you are missing the comparison here
if((*iter).x_co == Node->x_co && (*iter).y_co == Node->y_co){
found = true;
break; // don't keep looping if you found it already
}
}
return found;
}