Implementing 8-chip game based on A* algorithm, the application of a* algorithm in this game is described in detail, and DFS and BFS search methods are also compared. It helps a lot to understand the related algorithms in the comparative understanding of path finding problems.
4. Task introduction
The puzzle "The Game of Eight Chips" is a heuristic search task. Chips will be
moved in order (right, left, up, down) to adjacent spaces. The goal is to reach the final
state with ordered chips.
Feasible heuristic function:
1.The heuristic function H1 characterizes number of chips shifted from optimal final state. For the state
(a) heuristic function H1=8.
2.The heuristic function H2 characterizes sum of distances between current (a) and target (b) chip
positions. For the state (a) heuristic function H2=3+1+2+2+2+3+3+2=18.
6. Heuristic function analysis
The requirement of the task is to implement a path shortest path search using the A* algorithm.The
A* algorithm is a heuristic graph search algorithm characterized by the definition of the valuation
function.
f(n) = g(n) + h(n)
The heuristic function f(n) is the key function used to evaluate nodes in the A* algorithm and
consists of two parts: g(n) and h(n).
g(n): this is the distance from the starting node to node n at actual cost. It reflects the cost of
reaching the current node n. For example, in an octet problem, it could be the number of steps
required to reach the current state from the initial state.
h(n): this is the estimated cost of traveling from node n to the goal node. This is a heuristic estimate
of the cost required to reach the goal node from the current node n to the goal node. In the octet
problem, this can be the estimated number of steps from the current state to the goal state, computed
by the heuristics H1, H2.
8. Judgment of solutions
Conclusion of the eight-digit problem with and without solutions:
A state is represented in one-dimensional form by finding the sum of the inverse order of all the digits
except 0. That is, If a larger number precedes a smaller number, the two numbers are said to form an
inverse order, and the total number of inverses in an arrangement is called the number of inverses in that
arrangement.
Two states are mutually reachable if their inverse order parity is the same, otherwise they are not
mutually reachable.
Examples from the task,such as:
For the initial state, the one-dimensional array becomes: 724596831, Sum of inverses = 1 + 1 + 1 +
2 + 1 + 6 + 8 = 20(even).
For the final state, the one-dimensional array becomes: 912345678, Sum of inverses = 1 + 1 + 1 +
1+ 1 + 1+ 1 + 1 = 8(even).
So these two states are interchangeable.
9. Heuristic function H1
Heuristic is the number of misplaced digits.
f(n) = g(n) + h(n)
g(n) represents the depth
h(n) represents the total number of absent arrays
The following figure shows the specific operation of the heuristic strategy H1: the numbers circled
in red indicate the order of expansion.
11. Heuristic function H2
Heuristic is the Manhattan distance.
f(n) = g(n) + h(n)
h(n) denotes the Manhattan distance. Using the Manhattan distance as heuristic information, it is the
sum of the distances required for misplaced digits to reach the target state compared to the current
state and the target state. For example the initial state in the example: h(n) = 3+1+2+2+2+3+3+2 =
18.The specific operation is the same, except that the value calculated for each h(n) is different.
12. Data Structure
In fact, in the BFS search algorithm, if the nodes in the Open table (queue) can be sorted
using the valuation function f(n) = g(n) + h(n) at each step of the search, then the search
algorithm is Algorithm A*.
The A* algorithm needs to maintain two data structures: the OPEN set and the CLOSED
set.The OPEN set and the COLED set of this implementation are realized using Map.The OPEN
set contains all the nodes to be detected that have been searched. In the initial state, the OPEN set
contains only one element: the start node.The CLOSED set contains the detected nodes. The
initial state CLOSED set is empty. Each node also contains a pointer to the parent node to
determine the tracking relationship.
13. Main loop
1.Each time an optimal node n (i.e., the node
with the smallest F-value) is taken from the
OPEN set to be tested.
2.If the OPEN table is empty, the search fails
and the program exits.
3.Remove node n from the OPEN set and add it
to the CLOSED set.
4.If n is the target node, the search succeeds and
the program exits.
14. Main loop
5. Otherwise try to add all neighbor nodes n of node n:
Neighbor node in CLOSED set means it has already
been detected, then no need to add it again.
Neighboring nodes are again in the OPEN set:
If the recalculated G-value is smaller than the G-
value saved by the neighboring node, then the G-
value amount F-value of this neighboring node needs
to be updated, as well as the parent node.
Otherwise, no operation is done.
Otherwise, add this neighbor node to the OPEN set,
set its parent node to n, and set its G value and F
value.
16. UML1: Diagram of precedents
Users can choose the calculation algorithm, and after using the corresponding algorithm
to calculate, they can view the results of the algorithm and get the path to the final state.
The user can control the movement of the queen. Before controlling the movement of the
queen, the user needs to input or generate the starting state or ending state of the queen.
17. UML2: Diagram of activity
Enter the starting state and ending state,
and the misunderstanding ends directly.
If you have a solution, select the
corresponding algorithm and click the
corresponding button to calculate.
View the path to the final state and
control the movement of the queen.
18. UML3: Class diagram
Algorithm.java--algorithm file, is
a tool class that implements all
algorithms. Of course, I also added
the DFS and BFS algorithms for
performance comparison.
State.java---node class, stores
the nine-square grid sequence,
real cost, real cost + heuristic
cost of each state.
20. UML4: Diagram of interactions
The user needs to input or randomly
generate an initial state or state, then
submit the initial state and final state to
the corresponding algorithm and then
call the corresponding algorithm. After
the Algorithm calculation is completed,
the corresponding calculation result is
returned.
21. UML5: Diagram of states
At the beginning, the
program waits for input or
generates the initial state and
final state. If there is no
solution to the two, it ends
directly.
If there is a solution, select
the corresponding algorithm
and perform the calculation.
According to the calculation
results, control the movement
of the queen.
22. UML6: Diagram of components
The LimitedField component and the
CalThread component are inserted into the
GUI. The Algorithm component is called in
the CalThread component.
The smallest unit of calculation in the
Algorithm component is the State
component.
30. Conclusion
DFS is not necessarily faster than BFS, and vice versa.
DFS may not necessarily find the shortest path.
BFS can definitely find the shortest path if the search depth is not limited.
The choice of heuristic function h(n) in the A* algorithm is crucial. h*(n) represents the
actual minimum cost from node n to the target node.
h(n) < h*(n), in this case, there are many points to search, the search range is large,
and the efficiency is low. But the optimal solution can be obtained.
h(n)=h*(n), the search efficiency is the highest at this time.
h(n)>h*(n), the number of search points is small, the search range is small, and the
efficiency is high, but the optimal solution cannot be guaranteed.