"The fun of Arithmetic"--game tree and chess game

Source: Internet
Author: User

From the beginning of this article, the author began to "The fun of the algorithm" a book study. Compared with the previous game-oriented algorithms and classical textbooks, this book is based on algorithms that are used in real life or in the production practice, such as the Tree of games introduced in this article, which is the basis of a very fire man and Ai's go war in the previous period.

One thing that needs to be explained in advance is that because the algorithm in this book has very good application and practicality, but is limited by the author's ability and experience, may not be able to give out and analyze the source code of the algorithm very much, so the author introduces these algorithms in the article, also mainly with the algorithm idea and pseudo-code mainly, If the reader is interested in the source code of an algorithm, you can leave a message that the author will write the original source code to send to you.

Then we begin the text.

First of all, let's throw a question, after IBM's Deep Blue defeated chess master Casparo, and just recently, Google's Alpha Dog has also captured the last fortress of human intelligence--Weiqi. Then we must be curious, how can the life-free computer be so powerful that it could defeat human beings in these intellectually acclaimed games? Then we will be one by one to uncover the mystery of chess AI.

Game:

First of all, we should have a good understanding of the issues we want to explore, whether it is chess or go, they can be called "game" in essence, then what is the game? Game can be understood as a limited selection of participants in the competitive activities of the limited strategy, such as chess, poker, athletics, war and so on. And here we will further simplify the game, we explore some simple "zero-sum, full information, non-accidental" game. The so-called 0 and, is to lose must win, most of the draw, there will be no win game, and "full information" refers to the game process both sides of the information is open and transparent, that is, an information symmetry, and the so-called "non-accidental", that is, the rules of the game participants are rational and intelligent.

We have a preliminary understanding of the definition of game, we start with a simple game.

The maximal minimum search algorithm for the example of Tic-Tac-Chess game:

First of all, the Tic-tac-chess game, very similar to the Gobang, in a 3x3 square, the two sides to fill the "X" and "O", the first to make 3 "x" or "O" connected to the winning party.

We set the player Max to fill "x", Player min fill "o", Max Initiator. So here's a very important thought that we should be aware of: all the seemingly "smart" strategies that a computer can make are essentially simulating all the game states in a relatively short period of time and then filtering out the most advantageous state for AI.

Let's look at such a diagram.

In this figure, Player1 is actually the max we defined above, and it's easy to see that this is a tree structure that lists all the states in the first two steps, a tree structure that records the state of a game called a tree.

So easy to see, we can follow this game tree, get all the chess state, then the question is, suppose we have got a complete game tree, how can we optimize the AI decision?

This is the Minimax search algorithm we are going to introduce, from the point of view of Max, we set a weight of W to each node in the game tree to characterize the benefit of this node (a certain state) to Max, which means that this state is more advantageous to max.

So we are in the process of simulating the game, for the first layer of the tree is also the max fill "X", by the above definition, it is obvious that Max tends to choose nodes with large node weights, that is, w[1] = max (w[2],w[2],w[3]), so we need to find the second layer of the tree three node weights, This will need to continue to expand the third layer of the game tree.

For the player min, it is obviously inclined to choose a more favorable game state for themselves, that is, w the smallest node, thus we know w[2] = min (w[5],w[6],w[7],w[8],w[9]), while W[3], W[4] is a similar solution.

Have you found a pattern of regularity? It is easy to see that this is a recursive algorithm for layer I lazi strategy, regardless of Max or Min, it is based on all states of the i+1 layer, except that, for player Max, he chooses the maximum value for all child nodes of the node, and Min chooses the minimum value.

In the end, we need to extend the game tree to the leaf nodes, and then backtrack back to get the most advantageous strategies for dealing with all kinds of situations.

Based on the understanding of this idea, it is not difficult to write the pseudo code of the following Minimax algorithm.

intMiniMax (node,depth,ismaxplayer) {if(Depth = =0)          returnEvaluate (node); intScore = Ismaxplayer? -Inf:inf; For_each (node's child child_node) {intValue = MiniMax (child_node,depth-1,!Ismaxplayer); if(Ismaxplayer) score=Max (score,value); Elsescore=min (score,value); }}

In fact, we can see that this game tree is based on search or poor or DP thinking can be, this is not difficult to understand, this is a very basic programming thinking, although it can be said that this is the design of chess ai very central part, but this can defeat the human? Of course not, one of the biggest problems with this algorithm design pattern is that the game tree is too large to be overwhelming, so in real algorithmic design we need to limit search depth and a variety of pruning such as alpha beta pruning, a * Pruning algorithms to reduce the size of the game tree, this is what we want to introduce below.

"The fun of Arithmetic"--game tree and chess game

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.