A * search for the road

Source: Internet
Author: User
Tags square root

Translator Preface

Long ago knew A * algorithm, but never read the relevant articles, and did not read the code, but the brain has a vague concept. This decision starts from the beginning and studies the simple method of being highly regarded as the beginning of learning artificial intelligence.

This article is very well-known, there should be a lot of people in China have translated it, I did not find, I think the translation itself is also the level of their own English exercise. After efforts, finally completed the document, but also understand the principle of a * algorithm. There is no doubt that the author of the image of the description, concise and humorous language to tell the magic of this magical algorithm, I believe that every person who has read this will have knowledge (if not, that is my translation is too bad--b).

The following is the text of the translation. (As I use UltraEdit editor, I do not have to deal with various links in the original text (except the chart), but also in order to avoid the suspicion of unauthorized links, interested readers can refer to the original.

is not difficult, a * (read as A Star) algorithm for beginners is indeed a bit difficult.

This article does not attempt to make an authoritative statement on the subject. Instead, it simply describes the principle of the algorithm, allowing you to understand other relevant information in a further reading.

Finally, this article has no procedural details. You can do it in any computer program language. As you wish, I included a link to the example program at the end of the article. Compression packs include C + + and blitz basic two languages, and if you just want to see how it works, it also contains executable files.

We are improving ourselves. Let's start from the beginning ...

Order: Search area

Suppose someone wants to move from point A to separated B, as shown below, the green is the start a, the red is the end B, the blue box is the middle wall.

[Figure 1]

You first notice that the search area is divided into square grids. Like this, simplifying the search area is the first step in finding a way. This method simplifies the search area into a two-dimensional array. Each element of an array is a square of the grid, and the squares are marked as both accessible and not passed. The path is described as a collection of squares we pass from A to B. Once the path is found, our people move from the center of one square to the other until they reach their destination.

These midpoint are called "nodes". When you read other search information, you will often see people talking about nodes. Why don't you just describe them as squares? Because it is possible that your path is divided into other structures that are not squares. They can be rectangular, hexagonal, or any other shape. Nodes can be placed anywhere in the shape-either in the center, or along the border, or somewhere else. We use this system anyway, because it is the simplest.

Start Search

As we deal with the above graph grid, once the search area is transformed into a manageable node, the next step is to lead a search that finds the shortest path. In a * seek algorithm, we check the adjacent squares by starting from point A, extending outward until we find the target.

We do the following to start the search:

    1. Start with point A and deposit it as a "open list" as a pending point. Opening a list is like a shopping list. Although there is only one element in the list, there will be more later. Your path may pass through the squares it contains, or it may not. Basically, this is a list of checked squares.
    2. Look for all reachable or accessible squares around the starting point, skipping walls, water, or other squares that can't pass through the terrain. Also add them to the open list. Save point A as "parent square" for all these squares. When we want to describe the path, the data of the parent square is very important. It will explain its specific uses later on.
    3. Remove point A from the open list and add it to a "close list" that holds all squares that do not need to be checked again.

At this point, you should form a structure like the diagram. In the diagram, the dark green squares are the center of your starting squares. It is stroked in light blue to indicate that it has been added to the closed list. All adjacent squares are now on the open list, and they are stroked with light green. Each square has a gray pointer that refers to their parent square, which is the starting square.

[Figure 2]

Next, we choose to open the adjacent square in the list, roughly repeat the previous process, as follows. But which squares are we going to choose? That's the lowest F value.

Path score

The key to choosing which squares to go through in a path is the following equation:

F = G + H

Over here:

    • G = from Start a, along the resulting path, move to the grid to specify the movement cost of the squares.
    • H = The estimated move cost of moving from that square on the grid to end B. This is often called heuristic, and may be a bit confusing to you. The reason for this is that it's just a guess. We have no way of knowing the length of the path beforehand, as there may be obstacles (walls, water, etc.) on the road. Although this article only provides a way to compute H, you can find many other ways on the web.

Our path is generated by iterating through the open list and selecting a square with the lowest F value. The article will describe the process in more detail. First, let's take a closer look at how to calculate this equation.

As mentioned above, G represents the cost of moving along the path from the start point to the current point. In this case, we make the horizontal or vertical move 10, and the diagonal direction cost 14. We take these values because the distance along the diagonal is the square root 2 (don't be afraid), or about 1.414 times times as long as the horizontal or vertical movement is spent. To simplify, we use 10 and 14 approximations. The proportions are basically correct, and we avoid the root operation and decimals. It's not just because we're afraid of trouble or we don't like math. Using such integers is also faster for computers. You will find that if you do not use these simplified methods, it will become very slow to find your way.

Since we are calculating G values leading to a square along a particular path, the method of evaluation is to take the G value of its parent node, and then increase by 14 and 10, respectively, according to its relative parent, diagonally or at right angles (not diagonally). The demand for this method in the example becomes more, because we get more than one square out of the start grid.

The H value can be estimated in different ways. The method we use here is called the Manhattan method, which calculates the sum of both horizontal and vertical squares from the current grid to the target, ignoring the diagonal direction. Then multiply the result by 10. This is the Manhattan approach because it looks like counting the number of blocks from one place to another in the city, where you can't cross the block diagonally. It is important that we overlook all the obstacles. This is an estimate of the remaining distance, not the actual value, which is why this method is called heuristic. Want to know more? You can find the equation and the extra annotations here.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.