A summary of doing artificial intelligence-Gobang

Source: Internet
Author: User

Preface :

Just learn to play C one months, the courtyard has a program design game, so he started writing this thing.


First of all, for each game has a lot of chess, when the black side of the first piece, white side has 254 kinds of dismount, white in these 254 kinds of dismount in the choice of one, black side and 253 in the middle and lower law. In this way, all the dismount is considered, all listed to constitute a huge game tree, also known as the number of searches. The root node of this game tree is the first piece of black, followed by the next layer of white chess, all the situation, in turn, until the end. And the computer to do is from the following sub nodes to find the most beneficial to the computer side of the node, that is, the best way to go. Who think far-reaching, the more the number of steps to predict, the chess game to understand the more powerful. If all the future conditions are considered in place, it must be in an invincible position.

In choosing the best way to go, we need to use the minimum maximum search algorithm. Let's assume that there's an evaluation function for the chess game, that can be on any one situation on the computer side, the higher the more computer-friendly, the lower is good for the player, that is, this evaluation function is to the computer side, the computer WINS is infinite, the loss is infinitely small. All the other things are in between.

As the above picture is called the game tree, if: a corresponding computer, b corresponding players. The computer is currently playing chess, that is, a 0 node, the next level of the child node that player. Both sides hope that in the future situation, in the most conducive to their own direction. Computer of course want to own high, so will pick the node score the biggest 1, score is 2, so a 0 score is 2. and B 1 want to score low, so he will choose a 3-4 in the smallest score of a 3, score 2, B 1 value is 2. Therefore, in the Minimax search algorithm, the node that calls for the maximum value is the maximal point, and the node with the minimum value of B is the minimum point. The search process for this maximum minimum is a minimax search algorithm.

As mentioned above, all are theoretically feasible, because it is impossible to search through such a large game tree in the process of exponential growth of complexity. We can only predict the next few steps, we use the static valuation function to calculate the score, and then pushed backwards, and then to the current best way. At this point, we need an evaluation function that evaluates any situation. If you grade the evaluation function. I was to divide the chess surface into 72 Road, horizontal 15 + vertical 15 + Left oblique 21 + Right Oblique 21 = 72 (because the diagonal of a total of 16 road formation can not be five children). For 72 The road is to collect characteristics that have the possibility of forming 5 children. A chess game with the computer side of these features more, the chess score will be higher, with the player side features more, the score will be lower, so the player's characteristics are recorded negative points. Of course, different feature scores are also different. I abstract the computer side pieces to 1, the space abstract is 0. For example: When you encounter 11111, assign a value of 9999999,011110 assignment 300000,011100 or 001110, assign 3000, and so on. If it is a player's chess, assign the corresponding negative points. Finally put the score of 72 road, black and white both sides of the total score to get the whole chess score. In fact, I put each of the situation into a length of 6 string, one with the original scheduled 16 characteristics to do, leaving each road score the biggest feature. However, the number of cases to be predicted is huge when the next step is not judged, and there is no case to call the static valuation function, which matches about 7,000 times. In order to reduce the number of matches, I used the Huffman Tree coding idea, all the features into a Huffman tree (the following figure, the draft at the time), each node is composed of 0, 1, which is also my chess piece abstract to 1, the space abstract for 0 reasons. Specifically not in detail, Huffman tree Coding thought I was referring to the "algorithm and data Structure" 151 pages. So, each length of 6 characters does not have to be compared with 16 features one by one linear, which greatly reduces the amount of computation.

The above is still far from enough, I experimented, only predict three steps, the computer next, need to wait nearly half an hour. Therefore, I also add the Alpha-beta pruning pruning method in the Minimax search algorithm, which can be called as Alpha-beta search algorithm. The following figure:

In the Minimax search process, the whole tree is traversed, each node is visited once, so the search algorithm is coarse, inefficient and the search volume is very large. If the evaluation of the leaf node, the calculation of the inverted value and the production of the tree at the same time, it may be a significant reduction of the number of nodes required to search, and keep the search effect unchanged. The specific ideas are as follows:

Alpha Pruning:

Figure: Maximum point 3 and all nodes below

1. Since b 7 is 2, a third is greater than or equal to 2;

2. A 19 is 1, then B 8 is less than or equal to 1;

3. Since a 2 is the largest, for the moment equal to 2, you do not have to consider less than or equal to 1 of the B 8.

Therefore, a 20 does not need to be considered, cut it off.

Beta pruning:

Similarly, figure: the minimum point B 1 and all of the following nodes, the same analysis.

This reduces the number of branches, cut off the division, neither the creation of legal approach, nor in the evaluation of a large number of calculations, to a certain extent, optimized a lot. However, the Alpha-beta pruning pruning method has a great relationship with the order of the nodes in the process of reducing the search efficiency. Therefore, it can be sorted to achieve further optimization when the method is generated. Logically speaking, the smallest nodes are sorted in order from small to large, and it is ideal to sort the maximal nodes in order from large to small, but it is impossible to get the evaluation value before the final node is produced. Therefore, the ranking can be based on the threat of large points around the priority search method to achieve the effect of optimization.

Finally, I figured out in the last two days of the optimization method, in the beginning of the dozens of steps, its optimization effect than Alpha-beta pruning pruning method is dozens of times times better. Here I used to narrow the search range algorithm, in fact, I was a blind name. The previous Alpha-beta pruning pruning method is to produce a certain child nodes after cutting off some of the side to achieve the optimization effect, and narrowing the search range algorithm is directly from the root node cut off the branch, cut off from the top, completely abandoned its huge branch. The specific ideas are as follows: We play chess, the basic is in the existing pieces around, so there are many vacancies are completely do not need to search. That is why we can determine the scope of the search, first you judge the maximum diagonal of a piece that has been dropped, draw a rectangle on the base of the diagonal, and then expand the one or two square to the perimeter, reducing the branching on the root node, not to mention the next few steps beyond its scope. However, in the process of forecasting, we have to assume that a child, these predictions will quickly extend the scope to the boundary, and when all the circumstances of a parent node and the following child nodes are predicted, the node with the same depth as the previous parent node does not need a large search scope, so the ideal search range should be the same as before the forecast. Here, we can use the structure of the stack, with the stack operation can be a side of the pieces into the stack, get a new search scope, when the search process of backtracking occurs with the stack operation to exit a child, you can revert to the previous search scope. This can be done in a step-by-step, give-and, minimizing unnecessary calculations.

looking to the future:

The goal of the improved search algorithm mentioned earlier is to remove the unnecessary (redundant) branches from the search process as far as possible, so as to minimize the number of branching bundles to reduce the computation. But it is still not enough to predict a few steps in a limited time, and to make the computer reach world-class levels. At the beginning, I mentioned the "Gene algorithm" in the Central Plains University's master's degree thesis "Gobang Chess Slightly evolution learning method", also called genetic algorithm, is to let the program has learning function, in a large number of practice, the program will not change the evaluation function characteristics of the weight, this will overcome the one-sided subjective factors. In the heuristic search process, in fact, there are many situations are repeated, add a lot of unnecessary duplication, in order to solve this problem, you can use a hash table search algorithm, the search will be stored as data, do not need to clear, encounter the same situation just call the previous calculated data, To avoid the heavy counting. In addition, today's hyper-Threading technology and multi-core technology can also achieve further optimization.


This is to do artificial intelligence Gobang, how much also a little harvest, relatively before more know how in a limited time to complete their seemingly impossible or difficult to complete the task, such a task the best way is to testify than easy. There must be no knowledge of their own, when in today's era, we need this knowledge is very easy to find. However, the most important thing is to digest it in the quickest way, so that the valuable achievements of predecessors or masters have quickly become their own skills. As a programmer, this experience gives me the feeling is, completes a program, the most important absolute is the algorithm, this does Gobang, the artificial intelligence module's algorithm I also took a few days to make clear, perhaps only then a certain point has the doubt, it may also let you one afternoon also not understand. When the algorithm is complete, you can start organizing the code immediately, starting with the data structure, clearing the program flow, and then breaking each of the sub modules one by one. The algorithm takes a few days, and the coding can be completed in a day, and our focus in the future is to conceive of it. The common denominator of code farmers in software engineers is programming, and software engineers are different because they are programmed to solve the problem itself, and coding is a matter for the farmers.

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.