Help please with alpha-beta

Discussion of chess software programming and technical issues.

Moderator: Ras

Alexandr Novikov
Posts: 22
Joined: Tue Sep 15, 2015 8:21 pm

Help please with alpha-beta

Post by Alexandr Novikov »

Hi friends! I am a beginner chess programmer , this is my hobby, so do not judge strictly. I wrote a simple chess program with graphics interface(pascal abc net). It's five ply or depth (I do not understand the difference between them) search with minimax method. My search procedure without reduction and my code looks like this:
//--------------------------------------------------------------------------------------
procedure search;
var .....
black_move_generator;
for i:=0 to bcnt - 1; //all possible black moves
make_move;
first_ply; //call next iteration
if i = 0 then best_score := best_score1 else
if best_score1 < best_score then best_score := best_score1; //min
unmake_move;
//--------------------------------------------------------------------------------------
procedure two_ply;
var .....
white_move_generator;
for i:=0 to wcnt - 1; //all possible white moves
make_move;
tree_ply; //call next iteration
if i = 0 then best_score1 := best_score2 else
if best_score2 > best_score1 then best_score1 := best_score2; //max
unmake_move;
//-------------------------------------------------------------------------------------- procedure tree_ply;
var .....
black_move_generator;
for i:=0 to bcnt - 1; //all possible black moves
make_move;
four_ply; //call next iteration
if i = 0 then best_score2 := best_score3 else
if best_score3 < best_score2 then best_score2 := best_score3; // min
unmake_move;
//--------------------------------------------------------------------------------------procedure four_ply;
var .....
white_move_generator;
for i:=0 to wcnt - 1; //all possible white moves
make_move;
five_ply; //call next iteration
if i = 0 then best_score3 := best_score4 else
if best_score4 > best_score3 then best_score3 := best_score4; //max
unmake_move;
//--------------------------------------------------------------------------------------procedure five_ply;
var .....
black_move_generator;
for i:=0 to bcnt - 1; //all possible black moves
make_move;
evaluate; //call evaluate function
if i = 0 then best_score4 := eval else
if eval < best_score4 then best_score4 := eval; // min
unmake_move;
//--------------------------------------------------------------------------------------
Minimax works fine, but I do not understand both should work alpha beta( Please, explain me, how on my pseudo code should work alpha beta. I know, that for a-b very important moves ordering and i have simple moves ordering.
thank you in advance.
best
Alexandr Novikov
p.s.I do not want to read other people code, I want to understand everything myself
Henk
Posts: 7251
Joined: Mon May 27, 2013 10:31 am

Re: Help please with alpha-beta

Post by Henk »

First do you understand this ?

upperbound(white to move ) = -lowerbound(black to move)
value( white to move) = - value (black to move )


I use something like this:

Code: Select all

search (lb, ub)
{
   val = -infinite;
    for each possible move
    {
       make move -> changes also side to move
        val = max (val, -search(-ub, -lb));
       undo  move -> changes also side to move
    }
     return val
}
Last edited by Henk on Wed Sep 16, 2015 8:13 pm, edited 1 time in total.
Alexandr Novikov
Posts: 22
Joined: Tue Sep 15, 2015 8:21 pm

Re: Help please with alpha-beta

Post by Alexandr Novikov »

Henk wrote:First do you understand this ?

upperbound(white to move ) = -lowerbound(black to move)
value( white to move) = - value (black to move )
minimax? yes?
Henk
Posts: 7251
Joined: Mon May 27, 2013 10:31 am

Re: Help please with alpha-beta

Post by Henk »

See second part of previous pos. I think you can find the difference yourself.
Alexandr Novikov
Posts: 22
Joined: Tue Sep 15, 2015 8:21 pm

Re: Help please with alpha-beta

Post by Alexandr Novikov »

Henk wrote:See second part of previous pos. I think you can find the difference yourself.
ok, but where alpha beta in your code? sorry, I don't understand
AlvaroBegue
Posts: 932
Joined: Tue Mar 09, 2010 3:46 pm
Location: New York
Full name: Álvaro Begué (RuyDos)

Re: Help please with alpha-beta

Post by AlvaroBegue »

You are using procedures, but the main thing each subroutine is doing is computing a value (e.g., `best_score2'). Why not write functions and return the best score? That's how most of us do it.

You don't need different functions for different levels in the tree: You have a single function that takes an argument that says how much depth is left.

I recommend you use the negamax convention, where scores are positive for the side to move, and then you also don't need separate code for max nodes and min nodes.

So, ignoring alpha-beta pruning for the time being, can you rewrite your code following my advice above? Adding alpha-beta pruning to that code is a small modification (although it's not particularly easy to understand).
Henk
Posts: 7251
Joined: Mon May 27, 2013 10:31 am

Re: Help please with alpha-beta

Post by Henk »

Alexandr Novikov wrote:
Henk wrote:See second part of previous pos. I think you can find the difference yourself.
ok, but where alpha beta in your code? sorry, I don't understand
search(lb, ub) is my interpretation of search(alpha, beta)
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: Help please with alpha-beta

Post by bob »

Henk wrote:First do you understand this ?

upperbound(white to move ) = -lowerbound(black to move)
value( white to move) = - value (black to move )


I use something like this:

Code: Select all

search (lb, ub)
{
   val = -infinite;
    for each possible move
    {
       make move -> changes also side to move
        val = max (val, -search(-ub, -lb));
       undo  move -> changes also side to move
    }
     return val
}
So NO alpha/beta at all? Pure minimax???
Henk
Posts: 7251
Joined: Mon May 27, 2013 10:31 am

Re: Help please with alpha-beta

Post by Henk »

bob wrote:
Henk wrote:First do you understand this ?

upperbound(white to move ) = -lowerbound(black to move)
value( white to move) = - value (black to move )


I use something like this:

Code: Select all

search (lb, ub)
{
   val = -infinite;
    for each possible move
    {
       make move -> changes also side to move
        val = max (val, -search(-ub, -lb));
       undo  move -> changes also side to move
    }
     return val
}
So NO alpha/beta at all? Pure minimax???
Fortunately I said I use something like this. I am a bad tutor. I have no patience.
Last edited by Henk on Wed Sep 16, 2015 9:17 pm, edited 1 time in total.
Alexandr Novikov
Posts: 22
Joined: Tue Sep 15, 2015 8:21 pm

Re: Help please with alpha-beta

Post by Alexandr Novikov »

bob wrote:
Henk wrote:First do you understand this ?

upperbound(white to move ) = -lowerbound(black to move)
value( white to move) = - value (black to move )


I use something like this:

Code: Select all

search (lb, ub)
{
   val = -infinite;
    for each possible move
    {
       make move -> changes also side to move
        val = max (val, -search(-ub, -lb));
       undo  move -> changes also side to move
    }
     return val
}
So NO alpha/beta at all? Pure minimax???
I thought so too