Data structure practice 2: project preparation and design

1. Practice content

Implicit graph search problem

(1) Practical tasks
  • For the Jiugong rearrangement problem, establish a heuristic search solution method for graphs;
  • Use the A* algorithm to solve the Jiugong rearrangement problem.
(2) Practical requirements
  • 3х3 Jiugong chessboard, place 8 chess pieces with numbers 1~8, there is a space in the chessboard, and the chess pieces around the space can be moved into the space to change the layout of the chessboard. According to the given initial layout and target layout, move the pieces from the initial layout to the target layout, solve the moving steps and output. Please design an algorithm and use a suitable search strategy to find the shortest path with less space and time costs.
    Insert picture description here

2. Development environment

  • Implementation language: Java
  • Development platform: IntelliJ IDEA

3. Project design

(1) A* algorithm

This is an effective algorithm for solving the shortest path in a static road network. In layman's terms, it is not a fool-like search like the depth-first search algorithm and the breadth-first search algorithm. It analyzes the current situation first. Get the most probable branch, then expand on that branch, then compare the results of the expansion in the previous environment, and then select the most probable branch to expand until the final state is found.
The core of the A* algorithm is the selection of the evaluation function (in layman's terms, it is the selection of the evaluation method of the current situation, and the branch selected by which method is the branch most likely to be closest to the final state).
The formula is expressed as: f(n)=g(n)+h(n),
where f(n) is the evaluation function from the initial point through node n to the target point, and
g(n) is from the initial node in the state space The actual cost to n nodes,
h(n) is the estimated cost of the best path from n to the target node.

Steps to find the best path:
1. Start from the initial state A and store it as a point to be processed in an "open list". The open list is like a shopping list. Although there is only one element in the list now, there will be more in the future. Your path may pass through other states it contains, or it may not. Basically, this is a list of squares to be checked.
2. Find all reachable states around the starting point by moving up, down, left, and right. Also add them to the open list. Save state A as the "parent square" for all these states. When we want to describe the path, the information of the parent grid is very important. The specific purpose of it will be explained later.
3. Remove state A from the open list and add it to a "close list".
4. Take out the lowest state of f(n)
in the open list , and judge the nature of the state (1) If it appears in the open list, then judge the state of g(n) through the current node (take out from the open list, and expand the The node of the node) is the g(n) brought as the parent node is less than the original parent node, if it is less, you need to change its parent node and g(n).
(2) If it appears in the close list, then judge the state The g(n) of the current node (taken out from the open list, expand the node of the node) as the parent node to bring g(n) is less than the original parent node, if it is less than the need to delete the node from the close list , And add it to the open list, and change its parent node to the current node.
(3) Neither in the open list nor in the close list. This is very simple. Just add it directly to the open list (keep the open list in ascending order)
(4) If the retrieved node is the target node, exit successfully

Pseudo code of A* algorithm

创建两个表,open表保存所有已生成而未考察的节点,close表中记录已访问过的节点。 
算起点的估价值;
将起点放入open表;
   while(open!=NULL)
 {
    
    
    从open表中取估价值f最小的节点n;
         if(n节点==目标节点){
    
    
         break;
          }
         for(当前节点n 的每个子节点X)
       {
    
    
          算X的估价值;
              if(X in open)
            {
    
    
                if( X的估价值小于open表的X估价值 ){
    
    
                把n设置为X的父亲;
               更新open表中的估价值; //取最小路径的估价值 
                     }
          }
        if(X in close) {
    
    
                if( X的估价值小于close表的X估价值 )
                把n设置为X的父亲;  
                 将该节点从close表中除去
             把X节点放入open //取最小路径的估价值 
                       }
              }
         if(X not in both){
    
    
           把n设置为X的父亲;
           求X的估价值;
           并将X插入open表中; //升序排列open
         }
    }//end for
  将n节点插入close表中;
  按照估价值将open表中的节点排序; //实际上是比较open表内节点f的大小,从最小路径的节点向下进行。
  } //end while(open!=NULL)
 保存路径,即从终点开始,每个节点沿着父节点移动直至起点,这就是你的路径;
(2) A* algorithm solves the problem of rearrangement of Jiugongge

Algorithm calculation formula f(n) = g(n)+h(n),
where g(n) is the number of steps consumed from the initial state to the current state, and h(n) is the estimation required from the current state to the target state The number of steps, generally h(n) is less than or equal to the actual number of steps required, so that the optimal solution will not be ignored, because h(n) has some relationship with the solution space, if h(n) is set to be more than the actual number of steps required If the number is large, the optimal solution may be ignored in the solution space. For example, the width-first search is the effect brought by h(n)=0, and the depth-first search is the effect brought by g(n)=0, but h(n) is the distance h*(n)[the actual step required The degree of the number] cannot be too large, otherwise h(x) will not have strong distinguishing ability, and the algorithm efficiency will not be very high. The evaluation of a good h(n) is: h(n) is below the lower bound of h*(n) and is as close as possible to h*(n).
Then the eight-digit problem g(n) is the number of steps needed to get the new state by moving the numbers near the space up, down, left, and right, h(n) is the distance between the current state and the target state, that is, the sum of all the numbers that are not in the target position, which must be less than h*(n).

For the eight-digit problem, each node has 8 numbers and a space. The space can be regarded as 0, so there are 9 numbers in total, and an integer can be used to represent the information corresponding to a node.
We let g(n) be the actual number of steps (ie depth) taken from the start node to the current node, and h(n) is the estimate of the number of steps that must be at least from the node to the final node (in the estimation of h(n) When, we ignore the influence of other numbers on the number to its final position, so h(n) is only the lower limit of its minimum number of steps), f(n)=h(n)+g(n).

Guess you like

Origin blog.csdn.net/u014174973/article/details/114264315