Introduction to Artificial Intelligence——A* Algorithm Experiment

1. Purpose of the experiment:

Familiar with and master the definition, evaluation function and algorithm process of heuristic search, and use the A* algorithm to solve N digital problems, and understand the solution process and search sequence.

2. Experimental principle:

The A* algorithm is a heuristic graph search algorithm, which is characterized by the definition of the evaluation function. For general heuristic graph search, always choose the node with the smallest evaluation function f value as the expansion node. Therefore, f evaluates nodes based on the need to find a minimum cost path. Therefore, the evaluation function value of each node n can be considered to have two components: the actual cost from the starting node to node n and the actual cost from node n to reach The estimated cost of the target node.

In the A * algorithm, if h(x)≤h*(x) exists for all x, then h(x) is called the lower limit of , indicating a conservative estimate. The A algorithm with the lower limit h(x) as the heuristic function is called the A* algorithm, and the restriction: h(x)≤h*(x) is very important, which can ensure that the A* algorithm can find the optimal solution. In this problem, g(x) is relatively easy to obtain, which is the path cost from the initial node to the current node, that is, the depth of the current node in the search tree. The key lies in the selection of the heuristic function h(x), and the search efficiency of the A* algorithm largely depends on the evaluation function h(x). Generally speaking, under the premise of satisfying h(x)≤h*(x), the larger the value of h(x), the better, indicating that the more heuristic information it carries, the fewer nodes will be expanded when the A* algorithm searches. The higher the search efficiency.

The traditional BFS selects the depth of the current node in the search tree as g(x), but does not use the heuristic function h(x), searches blindly before finding the target state, and generates too many nodes, so the search efficiency is relatively low .

In this experiment , the number of elements that are not present and the Manhattan distance are used as the heuristic function h(x). Every time it is selected from the open table, the state with the smallest evaluation function is preferentially selected for expansion.

The evaluation function of the A* algorithm can be expressed as:

    f'(n) = g'(n) + h'(n) 

Here, f'(n) is the evaluation function, g'(n) is the shortest path value (also known as the minimum cost or minimum cost) from the start point to the destination point, and h'(n) is the shortest path value from n to the goal heuristic value. Since this f'(n) cannot be known in advance, the following evaluation function is actually used:

f(n) = g(n) + h(n) 

where g(n) is the actual cost from the initial node to node n, h(n) is the cost from node n to the target node

The estimated cost of the best path for . Here mainly h(n) embodies the heuristic information of the search, because g(n) is known. Use f(n) as an approximation to f'(n), that is, use g(n) instead of g'(n), and h(n) instead of h'(n). Two conditions must be met for this:

(1) g(n)>=g'(n) (satisfied in most cases, you can ignore it), and f must keep increasing monotonically.

(2) h must be less than or equal to the actual minimum cost h(n)<=h'(n) from the current node to the target node ;

Two points are particularly important. It can be shown that applying such a valuation function can find the shortest path.

Specific steps: Starting from the initial state S_0, use different operators to generate a new state x and add it to the open table (corresponding to the state space diagram, the root node generates a new child node n), and then from Select a state x in the open table according to a certain restriction or strategy, so that the operator acts on x to generate a new state and add it to the open table (the corresponding new child node is also generated in the state space graph), and so on until it is generated target state.

For the "certain strategy" mentioned above, in the graph search process, if the strategy is based on sorting and selecting the smallest estimated value, the process is called A algorithm.

3. Experimental content:

1 Refer to the core code of the A* algorithm (the output of the original program is shown in Figure 1 below), and take the 8-digit problem as an example to realize the solution program of the A* algorithm, which requires the design of two different evaluation functions.

Two heuristic functions h(x) :

①Number of elements that are not present

int calw(string s) //Calculate the absent digit h(n) of this state

{

int re=0;

for(int i=0;i<9;i++) if(s[i]!=t[i]) re++;

return re;

}

Manhattan distance

int distance(string s) {

int count=0,begin[3][3],end[3][3]; //count records the number of steps required for all pieces to move to the correct position

for(int i = 0; i < 8; i++){

begin[i/3][i%3]=s[i];

end[i/3][i%3]=t[i];

}

for(int i = 0; i < 3; i++) // check the correctness of the current graphics

for(int j = 0; j < 3; j++)

{

if(begin[i][j] == 0)

continue;

else if(begin[i][j] != end[i][j])

{

for(int k=0; k<3; k++)

for(int w=0; w<3; w++)

if(begin[i][j] == end[k][w])

count = count + fabs(i-k*1.0) + fabs(j-w*1.0);

}

}

return count ;

}

Figure 1  Original program output

 

2 In the A* algorithm program for solving 8-number problems, add the judgment of the reverse logarithmic parity of the initial state and the target state, and then set the same initial state and target state (as shown in Figure 2 below), for different evaluation functions, find Get the solution to the problem, and compare their impact on the performance of the search algorithm, including the number of expanded nodes, the number of generated nodes, etc. The reference output is shown in Figure 3 below.

Compute reversed pairs:

int  num(string s){

int a = 0;

for (int i = 0; i < 9; i++){

if (s[i] == '0')

continue;

for (int j = i+1; j < 9; j++)

    {

if (s[j] == '0')continue;

if (s[i]>s[j]) a++;}

}

return a;

}

If the parity of the reversed pair is different, an error will be returned (written in the solve function):

int k1=num(t);

int k2=num(p.s);

if(k1%2 != k2%2)

    {cout<<"Error"<<endl;

    return -1;

 }

Goal Status Setting:

const string t="123456780";

Initial state:

“486703215”

         

                                                     initial state target state

                                                                  figure 2

The performance comparison of different heuristic functions is shown in Table 2

Table 2 Comparison of different heuristic functions

heuristic function h(n)

absent digit

Manhattan distance

breadth first (0)

initial state

486703215

486703215

486703215

target state

123456780

123456780

123456780

Optimal solution

 

 

 

 

 

 

 

 

Number of expansion nodes

10374

2183

102659

Number of generated nodes

15715

3506

123849

operation hours

0.297s

0.159s

1.742s

3 For the 8-digit problem, set the same initial state and target state as in the above 2, use the breadth-first search algorithm (that is, the A* algorithm with estimated cost h ( n ) = 0) to find the solution of the problem, and the extension in the search process Number of nodes, number of generated nodes.

That is: h(n)=0

Program setting: pw=0;

The results of searching with breadth first are shown in the table above.

  1. Referring to the core code of the A* algorithm, try to modify it into a code that can solve the 15-digit problem. The initial state (initial chess game) and target state (target chess game) of the 15-digit problem are as follows. The solution to the problem and the expansion during the search process are required. Number of nodes, number of generated nodes.

Because the two-digit number output is not convenient, change [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0] to [

A,B,C,D,E,F,G,H,I,J,K,L,M,N,O], the input is still digital.

Example: 11 9 4 15 1 3 0 12 7 5 8 6 13 2 10 14

5 1 3 4 2 6 7 8 9 10 12 0 13 14 11 15

After running it for a long time, I found that the heuristic function of absent digits cannot be used.

Using Manhattan distance as a heuristic function is relatively fast.

The test results are shown in Figure 4   

                     

                     

               

Figure 4 15 digital search results

It can be seen that the number of generated nodes is: 244266

      The number of extended nodes is: 126723

      The search time is: 2.388s

5 Partial codes of digital modification:

 

6 Submit the experiment report and source program.

4. Experimental results : 

1 In the A* algorithm program for solving the 8-digit problem, add the reverse logarithmic parity of the initial state and the target state to judge whether there is a solution, and then set the same initial state and target state to obtain the solution of the problem for different evaluation functions , and compare their impact on the performance of the search algorithm, including the number of expanded nodes, the number of generated nodes, etc.

In the case of the same input and target, the results are compared as shown in Table 3 (source Table 2)

Table 3 Comparison of the results of different valuation functions

Valuation function

absent digit

Manhattan distance

breadth first

Number of expansion nodes

10374

2183

102659

Number of generated nodes

15715

3506

123849

operation hours

0.297s

0.159s

1.742s

The expansion node is count1, and the generation node is count2. The experimental results and the required number of nodes are in the experimental content 2.

When the input and the target are the same, it can be obtained that Manhattan distance is used as an evaluation function better than non-existing digits. Manhattan distance is used as the generation node and expansion node of the evaluation function. The number of nodes exceeds 10,000, so it can be seen that the search space is much smaller, and the efficiency of Manhattan as a valuation function is much higher.

2 According to breadth-first search algorithm and A* algorithm, analyze the characteristics of heuristic search.

The breadth-first search method can always guarantee to find the shortest path in the case of a solution, that is, the path with the least number of steps. But the biggest problem with the breadth-first search method is that there are too many nodes to search, because in the breadth-first search method, every possible expanded node is the object of search. As the depth of nodes in the search tree increases, the number of search nodes will increase rapidly and expand exponentially, so the required storage space and search time will also increase exponentially.

We can find that the time to solve the eight-digit problem using the A* algorithm and the number of searched nodes are much shorter than the breadth-first search algorithm, which shows that for the eight-digit problem, the selected heuristic information is conducive to improving the search efficiency.

3 Comparing the 15-digit and 8-digit problems, try to analyze the performance of the A* algorithm for solving different problem scales.

When using non-existing digits as the evaluation function to improve the 8-digit code, the program runs for a long time and there is no result. Therefore, if the scale of the problem is too large, it is necessary to design the evaluation function when using the A-star algorithm. If the design is not appropriate, the search space will be too large and there will be no results. In this experiment, the Manhattan distance is used as the evaluation function, and the search is still very fast. There are more than 200,000 generation nodes and more than 100,000 expansion nodes. Using Manhattan as the evaluation function is very efficient, and the time is only more than 2s.

Five experiment summary:

Through this experiment, I have a better understanding of the heuristic search algorithm, especially the application and design of the evaluation function. A suitable and efficient evaluation function is very important for the heuristic search algorithm. important.

Guess you like

Origin blog.csdn.net/cangzhexingxing/article/details/124001898