Library tutorials & articles
AI 1 - Problem Solving (Artificial intelligence)
- Introduction
- Methods Theory
- Coding
- Linked List
- Main Loop
- Final bit of code
- Conclusion
Methods Theory
The 3 main methods we would use for solving a single player problem like these are:
- Breadth First Search.
- Depth First Search.
- Heuristic Search.
- A* search.
The code to all these searches is almost identical, however the way they work very differently.
All the methods use a list. Initially the list has only 1 item in it, the playing grid in its initial or problem-state. This item is then expanded into all of its possible child states (every possible move is played) and these child states are then added to the list. This is done in a loop until 1 of the child states is a goal state (in other words, until the game is solved).
The pseudo code for most simple single player programs is as follows.
Do
Current_Item = Remove_Item_From_List
Child_State_Array = Expand (Current_State)
Add_to_List(Child_State_Array)
If child_State_Array Holds the solution then IsGameOver
= true
Loop while (IsGameOver = false)
That code can be found in all the above AI methods. The only difference is how they are ordered in the list.
- In a breadth first search, Items are always added to 1 side of the list, and removed from the opposite side.
- In a depth first search, Items are added, and removed from the same side of the list.
- In a heuristic search, Items are added to any side of the list. However, before an item is removed from the list, each item is given a score by a “heuristic” function.. ( a higher score for states that are closer to the goal state) the item with the highest score is the 1 that is removed from the list.
- An A* search uses two heuristic functions, and adds the core together. Using 2 heuristics greatly improves the programs ability to spot the best state to expand next, but slows down the execution of the program.
An example of a heuristic function for the game we are playing could be a function that counts the number of squares that are in the correct grid position, or maybe 100 – the depth of the state.
Using a heuristic in a search has advantages and disadvantages. In this example, using a heuristic causes the programs main loop to run about 10 times slower. However, it reduces the amount of loops needed to complete the search from 6 million to about 4 thousand !!!! Saving LOADS of memory, and time in this example. (which reminds me, the initial state I am using takes 25 moves to solve…. This may not seem like much, but without a heuristic, the search uses about 300 megabytes of ram in about 1 second ! Unless you are using a powerful machine ( I am using a 1.3 Ghz, 256 meg ram Pc) you may need to reduce the number of moves needed to complete the puzzle down to about 10)
OKAY, so, we know of a few different methods, we know their advantages and disadvantages. On the next page, we will talk about how to code this program.
Related articles
Related discussion
-
VS2005 app's won't run on another machine
by ted4444 (0 replies)
-
VB.NET: Hide and show table using radio buttons
by converter2009 (1 replies)
-
Convert C++ code to VB6
by mawcot (4 replies)
-
How to create a games like FIFA08
by mawcot (0 replies)
-
Binary Studio | software development outsourcing Ukraine
by shane124 (4 replies)
i need help with an error im recieving.
i have a template linked list class
template <class LT>
class LList
{
private:
class LNode
{
public:
LNode ();
LT data;
LNode * next;
};
public:
LList();
LList( const LList & other);
~LList ();
LList & operator = (const LList & other);
bool operator == (const LList & other);
int Size() const;
friend ostream & operator << <> (ostream & outs, const LList<LT> & L);
bool InsertFirst (const LT & value);
bool InsertLast (const LT & value);
bool DeleteFirst ();
bool DeleteLast ();
private:
LNode * first;
int size;
};
and the friend function is giving me an error:
template <class LT>
ostream & operator << (ostream & outs, const LList<LT> & L)
{
if (L.first == NULL)
return outs;
outs << L.first -> data;
for (LList<LT>::LNode * n = L.first -> next; n != NULL; n = n -> next)
{
outs << ' ' << n -> data;
}
return outs;
}
i get an error at the for loop... the error is :
LLIST.tmp: In function
std:<img src="images/smilies/redface.gif" width=15>stream& operator<<(std:<img src="images/smilies/redface.gif" width=15>stream&, const LList<LT>&)': <br> LLIST.tmp:111: error:n' undeclared (first use this function)LLIST.tmp:111: error: (Each undeclared identifier is reported only once for each function it appears in.)
LLIST.tmp:33: error:
LList<int>::LNode*LList<int>::first' is private <br> LLIST.tmp:106: error: within this context <br> LLIST.tmp:33: error:LList<int>::LNode*LList<int>::first' is privateLLIST.tmp:109: error: within this context
application.cpp:19: instantiated from here
LLIST.tmp:33: error:
LList<int>::LNode*LList<int>::first' is private <br> LLIST.tmp:111: error: within this context <br> LLIST.tmp:111: error: dependent-nameLList<LT>::LNode' is parsed as a non-type, but instantiation yields a typeLLIST.tmp:111: note: say `typename LList<LT>::LNode' if a type is meant
i know these all have to do with friend and being private, what do i do?!?!
This thread is for discussions of AI 1 - Problem Solving (Artificial intelligence).