Wuziqi AI step by step [4] A Way of Thinking close to human beings-iterative deepening, board tailoring, empty step tailoring, playing chess Extension

Source: Internet
Author: User

Note: The PCGO function is used to add uncorrected methods. Please modify it by yourself.

I. Deepening Iteration

1. What is iteration deepening?

The so-called iteration deepening means to crop alpha-beta to run depth 1, then run depth 1, 2, and then run depth 1, 2, 3.

2. Why does the iteration deepen?

The advantage of this is that after a search, you can obtain the basis for sorting-the historical table. Then, the historical table will give an approximate direction for the next search-the next step is better, that is, truncation is more likely to occur.

3. How much overhead does this increase?

In fact, it may run a little longer simply by increasing iteration, but considering the history table, the time is almost offset. What's more, there is a replacement table (I have never liked this name ).

4. How to Achieve iterative deepening

The idea is very simple. In fact, the code is very simple. You only need to implement a loop. for I = 1 to n, I is the depth to be scanned. In this loop, you can call alpha-beta tailoring. Just a few lines of code:

 

'================================================ Iteration deepening ======== ======================================
'Iteratively deepen the search process
Function SearchMain () As Integer
Dim I, t, vl As Integer
'Initialization
Pos. ClearnHistoryTable () 'clear the historical table
MvsCompare. ms = pos. nHistoryTable
T = My. Computer. Clock. TickCount 'initialize the timer
Pos. nDistance = 0' initial step count
Winplayr = 2' winner
'Iteration deepening process
For I = 1 To LIMIT_DEPTH_SearchFull-1
Debug. Print ("iteration:" & I)
Vl = SearchFull (-MATE_VALUE, MATE_VALUE, I)
'When you find the game kill, the search will be terminated.
If vl> WIN_VALUE then' computer victory
Winplayr = 1
Exit
End If
If vl <-WIN_VALUE then' wins
Winplayr = 0
Exit
End If
'More than one second, the search will be terminated
If My. Computer. Clock. TickCount-t> 1000 Then
Exit
End If
Next
Debug. Print ("iterations:" & I)
Return pos. mvResult
End Function
'================================================ ========================================================

 

In the entire loop, first clear the History Table (but I don't think we should replace the table at the same time, of course this is the case later); then call the recursive function of alpha-beta. Other judgments are easy to understand.

 

 

Ii. Board Cutting

1. Why do we cut the board?

This can cut out a lot of unreasonable moves. In wuziqi, we can easily see that if the child is far away from the Sunday and the white child, it must be a bad game. So what is the degree of separation? It may be 4, because it is 5, so it can only be 4 cells away from the current child, but is it true? Let's make a bold guess: in fact, the fourth level is also a bad game! Why? If there are three cells in the middle, the distance is too large. You need to connect three sub-nodes in this direction. How much chance can we do this? Even if we want to, what are the chances of success? I think it's very short-the entanglement between the two sides of wuziqi (that is, the congestion between the two sides, of course, the congestion of haoqi ......) Is very serious, so our conclusion is 3 rather than 4.

2. How to Implement

In my code, I first traverse the board, find the sunspots or white child, and then record the non-Child points around them. Of course, there is a repeat record problem, so I recorded it in a bitarray, it operates very fast, and then traverse it again to retrieve a reasonable point. Of course, another idea might be better-traverse all vertices and record null points within each three cells. But unfortunately, what I first realized was the first approach.

 

This code is already in use in the previous example. It is not listed here.

 

3. Empty step tailoring

1. What is empty step tailoring?

That is, it's your turn to keep yourself down, but let the other party continue.

Reprinted please indicate the source: http://www.cnblogs.com/zcsor/

2. Why empty step tailoring?

It is based on the idea that if the other party fails, what will the other party do? This is not only a result of predictive tailoring, but more importantly, when the other party is playing chess, it can take the playing point directly as a reasonable method: if you do not block it, you are about to be killed! Of course, in my code, because the evaluation function is not perfect (this mainly refers to the inaccuracy in the score settings), the program will block each other's single punch 3 or live 2, this can be completely solved by re-planning the score.

3. How to restrict empty step tailoring

First, when to perform empty step pruning. If the other party has been playing chess (Chong 4, active 3, etc.), it is wise to let him go again. Therefore, empty step pruning is performed only when the opponent does not play games.

Second, unrestricted empty step tailoring is certainly not a good solution. From the perspective of prediction, it seems that one step is a little small, and two steps are acceptable. If the other step is 2, the other step is 5! Therefore, the conclusion is two steps.

4. How to Implement

In fact, the code is very simple, but it should be built on the premise that the alpha-beta tailoring has been fully understood. First, analyze whether the game is played. If not, empty-step tailoring. If yes, use the playing point as the "reasonable method" for the next iteration. This is the idea. The code is no longer complex. Of course, it should be noted that the impact of empty step pruning on "depth" is as follows:

 

'============================================================ Empty step tailoring ============================================
If pos. nDistance> 0 Then
'1. Reach the horizontal line
If nDepth <= 0 Then Return pos. Evaluate'
'1-1. Return situation evaluation when the limit depth is reached
If pos. nDistance = LIMIT_DEPTH_SearchFull Then Return pos. Evaluate ()
'1-2. Try to crop in the empty step (the Beta value of the root node is "MATE_VALUE", so it is impossible to crop in the empty step)
If pos. Evaluate () =-3000 Then 'is used to generate a chart based on the return value of the chart, instead of generating all the chart types.
'Traverse the chess information and extract all the playing points.
For I = 0 To pos. Vectors. lnkinf. Count-1
For j = 0 To pos. Vectors. lnkinf (I). cqpend
NGenMoves + = 1
Mvs (nGenMoves) = pos. Vectors. lnkinf (I). cqp (j)
Next
Next
Else' empty step tailoring when not playing chess
Pos. NullMove ()
Vl =-SearchFull (-vlBeta, 1-vlBeta, nDepth-NULL_DEPTH-1)
Pos. UnNullMove ()
If (vl> = vlBeta) Then
Return vl
End If
End If
End If
'==================================================== Empty step cropping ends = ========================================

Next we will discuss the extension of Chongqi.

 

Iv. Chongqi Extension

1. What is Chongqi extension?

When the opponent plays a game, we do not have to iterate until the final solution is analyzed.

2. Why do I extend the game?

When playing a game, it is often a relatively risky game, And all mistakes are lost. Therefore, we need to analyze the final situation-it is wise to know exactly which game to take. Of course, this is very time-consuming, but it also provides a lot of valuable information for table replacement.

3. How to extend Chongqi

Based on our ideas, we can infinitely extend iterations to reach the final solution. But in fact, we have made some restrictions and we have not really analyzed the whole situation. The specific restrictions are described in the Code above. "When the limit depth is reached, the situation evaluation is returned." The real expansion of Chongqi is easy to implement: when the game is played, the input parameter of our iteration process is not n-1, but n, so that the depth will not be reduced to 0, the analysis will continue:

 

'========================================== ==================================
'Vl =-SearchFull (-vlBeta,-vlAlpha, nDepth-1)
'======== The above is the replaced code ============
Vl =-SearchFull (-vlBeta,-vlAlpha, IIf (pos. Evaluate =-3000, nDepth, nDepth-1 ))
'=============================================================== ======================================

 

 

So, this set is over.

 

Code of this set:

/Files/zcsor/qingyue lianzhu 0.4.7z

 

 

Next episode notice:

Static search. After static search, the table will be replaced. The release of the replacement table may take longer. Because there have been less time in the past few days, there are still some other things to be done to implement the dad plan.

 

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.