Chess ai algorithm (i.)

Source: Internet
Author: User

I want to play a chess game recently, but the AI got me stumped. This is the result of the past few days:

The chess program uses the "search" function to find the moves. The search function gets the game information and then looks for the best moves for the program's side.

one, Min-Max search Minimax


First
: The minimum and maximum are relative, and only for one side, AI is in favor of AI
The smallest and largest search in chess ai: Simply speaking, the AI is gone,in this process, the best (max) way for AI is the worst (minimum) way for me.
and this is the best way to go about the AI we are looking for.


This process is the same as when you play chess with others guess the other way and then chess, just, the computer can think a few steps, the number of steps here is the following depth of search


As An example ,: Suppose the search depth is 4. Then the AI takes a step (he thinks the best, recorded as the number of steps 1, search depth 4), will first consider if he goes this step 1, then I'm sure to go relative to this step
The worst Step 2 (search depth 3), then the AI then assumes that the number of steps according to step number of the best step 3 (search depth of 2), continue to consider I walk according to the number of steps 3 the worst step 4 (search depth 1)
Then, the search depth is 0, and the evaluation function of the situation at this time is given.


they call each other recursively, so that's the opposite of the search idea


This is just plain to talk about the minimum maximum search principle, but also do not realize the specific AI function






Here is the implementation code


int Max (int depth) {
int best =-infinity;
if (depth <= 0) {
return Evaluate ();
}
Generatelegalmoves (); To produce all reasonable methods.
while (Movesleft ()) {
Makenextmove (); When you take this step
val = Min (depth-1); Accept a relative minimum value
Unmakemove ();
if (val > Best) {
Best = val;
}
}
return best; Returns a relative maximum rating (AI thinks the best moves)
}
 
int Min (int depth) {
int best = INFINITY; Note that this is different from the "Max" algorithm
if (depth <= 0) {
return Evaluate ();
}
Generatelegalmoves ();
while (Movesleft ()) {
Makenextmove ();
val = Max (depth-1); Accept a relative maximum value
Unmakemove ();
if (Val < best) {//Note this is different from the "Max" algorithm
Best = val;
}
}
return best; Return a relatively minimal evaluation (the other person, the worst way people think)
}
 
The above code can be called this way:
 
val = Minmax (5);
 
This returns the evaluation of the current situation, which is the result of 5 steps forward. Just look at the notes for the explanations.




The above algorithm code is long, and is only beneficial to one side to speculate (that is, the best and worst way for the party), the following will introduce an optimized algorithm


--------------------------------------------------------------------------Gorgeous Split Line
two, negative value maximum function Negamax Search


int Negamax (int depth) {
int best =-infinity;
if (depth <= 0) {
return Evaluate ();
}
Generatelegalmoves ();
while (Movesleft ()) {
Makenextmove ();
val =-negamax (depth-1); //notice there's a minus sign here.
Unmakemove ();
if (val > Best) {//Always optimal value
Best = val;
}
}
return best;
}


As can be seen from this function, this function is always the optimal value of the current node (that is, always find the best way to the current node), only when the transformation node (that is, from the AI to the person), the result of the function is negative, to become the best law for the AI evaluation, thus eliminating the Min function step, reduce the code



——————————————————————————————————————— Gorgeous Split Line


three, Alpha-beta search


Minimum maximum run time toCheck the entire game tree .And then choose the best route possible, but becausetoo large a branching factor leads to very low efficiency, unable to do a very deep search.
andThe advantage of Alpha-beta search is that it cuts out unnecessary branching factors


As an example
,
(Example of pocket):
For example, you have a lot of pockets in front of your mortal enemy, he bet you lose, so he has to give you something, and the choice of rules is very strange:
There are several items in each pocket, you can take one of them, you pick the pocket of the item, and he picks the item in the pocket. You have to pick out your pockets and leave, because you don't want to keep flipping your pockets over there and let your mortal enemies stare at you.
Suppose you can only find one pocket at a time, and you can only touch something from it at a time when looking for a pocket.


Analysis: It's easy to apply the minimum and maximum principle to the problem-you pick out the best pockets and your sworn enemies pick the worst items from them.so your goal is to--
Pick out the pockets of the best items in many of the worst items


Let's say the items in your pocket

We start with the first pocket, look at each item and make a comment on the pocket. Let's say you have a peanut butter sandwich in your pocket and a key to a new car. You know sandwiches are worse, so if you pick this pocket you'll get a sandwich.
In fact, if we assume that our opponents will evaluate things as well as we do, the car keys in your pocket are irrelevant.
Now that you're going through the second pocket, the plan you've taken this time is different from the minimum-maximum scheme. Every time you look at an item, compare it with the best item (sandwich) you can get. As long as the item is better than the sandwich, you can do it according to the minimum-maximum scheme-
To find the worst, perhaps the worst is better than a sandwich, then you can pick the pocket, which is better than the pocket with the sandwich.
For example, the first item in this pocket is a $20 bill, which is better than a sandwich. If nothing else in the bag is worse than this, then if you choose this pocket, it is the object that the opponent must give you, and this pocket becomes your choice.
The next item in this pocket is a popular record in Liuhe. You think it's better than a sandwich, but worse than $20, then the pocket is still available. The next item is a rotten fish, which is worse than a sandwich. So you say "no thanks", put your pocket back and don't think about it anymore.
No matter what's in your pocket, maybe there's another car's key, and it's useless, because you'll get that rotten fish. Maybe there's something worse than a rotten fish (then you can look at it). Anyway rotten fish is bad enough, and you know it would be better to pick that sandwich pocket.


Item Condition


after sorting

Node 2 has a minimum value of 200 and 150<200 in Node 3.and Node 1, the first child node is only 170, less than 200, and the second child node is smaller than 170, so you don't have to compare him with 200, cut, node 4 is similar, the first child node 50, the back will not have to look again.There's only alpha pruning. That

For a min node (second layer), if you can estimate the upper bound beta (170 and 50) of its inverted value, and this beta value is not less than the estimated backward value of the parent node (max node) of min (200), that is, Alpha≥beta, You do not have to extend the remaining child nodes of the Min node (drawing the child nodes of X) because the values of these nodes have no effect on the backward value of the min parent node, a process known as alpha pruning.




And, of course, Beta pruning:

For a max node, if you can estimate the lower bound alpha of its inverted value, and this alpha value is not less than the estimated backward value of Max's parent node (min node) of the Alpha≥beta Beta, that is, it is not necessary to extend the remaining child nodes of the max node, Because the valuations of these nodes have no effect on the backward value of the max parent node. This process is called beta pruning.




The alpha value of a max node equals the current maximum final backward value of its successor, and the beta value of a min node equals the current smallest final backward value of its successor node

algorithm
two values are passed in the search, the first value is alpha, that is, the best value to search, reflected in if (Val > Alpha) {alpha = val;}
The second value is beta, which is the worst value for the opponent, and if the result of a certain order is greater than or equal to beta, then the entire node is invalidated.

reflected in:if (val >= beta) {return beta;}
   
  

Code:
int Alphabeta (int depth,int alpha,int beta) {
if (depth = = 0) {
return Evaluate ();
}
Generatelegalmoves ();
while (Movesleft ()) {
Makenextmove ();
val =-alphabeta (Depth-1,-beta,-alpha);//alpha and beta are constantly interchangeable. When the function is recursive, alpha and beta not only take negative//number and position Exchange
Unmakemove ();
if (Val >= beta) {
return beta;
}
if (val > Alpha) {
Alpha = val;
}
}
return alpha;
}
Remove the eye-catching part, and the rest is the minimum-maximum function.
The parameters that this function needs to pass are: the depth to be searched, the negative infinity that is alpha, and the positive infinity that is beta:

Possible weaknesses:

This algorithm relies heavily on the order of search. If you always search for the worst, then the beta truncation will not occur, so the algorithm is as small as the maximum and very inefficient. The algorithm will eventually look through the entire game tree, just like the minimum-maximum algorithm. 

So, after all the moves are generated, sorting is important ~ ~ ~

Conclusion:

The principle of the algorithm so far, relatively simple, big God do not spray ~

Chess ai algorithm (i.)

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.