Moderator: Andres Valverde
Public Function AlphaBeta(ByVal depth As Integer, ByVal alpha As Integer, ByVal beta As Integer, ByVal whiteToMove As Boolean) As Integer
Me.nodeCount = Me.nodeCount + 1
If (depth <= 0) Then
Return Me.Game.evaluate(whiteToMove)
End If
Dim s As New Stack
Me.Game.GenerateMoves(s, whiteToMove)
If (s.Count <> 0) Then
Do While (s.Count > 0)
Dim currentMove As Move
currentMove = s.Pop
Me.Game.makeMove(currentMove)
' The recursive call is here. Notice that we have decremented depth!
Me.val = -Me.AlphaBeta((depth - 1), -beta, -alpha, Not whiteToMove)
Me.Game.UnmakeMove
If (Me.val >= beta) Then
Return beta
End If
If (Me.val > alpha) Then
alpha = Me.val
If (depth = Me.defaultply) Then
Me.bestmove = currentMove
End If
End If
Loop
Return alpha
End If
Return 0
End Function
Dann Corbit wrote:Look at how LarsenVB did it:
http://cap.connx.com/chess-engines/new- ... RSENVB.ZIP
The basic idea is something like this (it probably would not work without some effort, but the skeleton should be OK):
- Code: Select all
Public Function AlphaBeta(ByVal depth As Integer, ByVal alpha As Integer, ByVal beta As Integer, ByVal whiteToMove As Boolean) As Integer
Me.nodeCount = Me.nodeCount + 1
If (depth <= 0) Then
Return Me.Game.evaluate(whiteToMove)
End If
Dim s As New Stack
Me.Game.GenerateMoves(s, whiteToMove)
If (s.Count <> 0) Then
Do While (s.Count > 0)
Dim currentMove As Move
currentMove = s.Pop
Me.Game.makeMove(currentMove)
' The recursive call is here. Notice that we have decremented depth!
Me.val = -Me.AlphaBeta((depth - 1), -beta, -alpha, Not whiteToMove)
Me.Game.UnmakeMove
If (Me.val >= beta) Then
Return beta
End If
If (Me.val > alpha) Then
alpha = Me.val
If (depth = Me.defaultply) Then
Me.bestmove = currentMove
End If
End If
Loop
Return alpha
End If
Return 0
End Function
pimidrez wrote:Gracias Daniel! se entendio a la perfeccion... dejo unas preguntas más para quien me pueda seguir ayudando:
ENGLISH
1) When I call for the first time this function:
Public Function AlphaBeta(ByVal depth As Integer, ByVal alpha As Integer, ByVal beta As Integer, ByVal whiteToMove As Boolean) As Integer
What value should I give to the variables depth,alpha,beta?? or how do I get it??
2)The next portion of code, what does it exactly? Just take part in the final of the search or before in the beginning?
If (depth <= 0) Then
Return Me.Game.evaluate(whiteToMove)
End If
**************************************************
**************************************************
ESPAÑOL
1) Cuando se llama por primera vez a la funcion:
Public Function AlphaBeta(ByVal depth As Integer, ByVal alpha As Integer, ByVal beta As Integer, ByVal whiteToMove As Boolean) As Integer
Con que valor entra la variable DEPTH??...es decir yo llamo a la funcion, que valores le tengo que dar a cada una de las variables: DEPTH,ALPHA,BETA??? o como los obtengo??
2) La siguiente porción del codigo que hace exactamente? solo tomar parte en el principio o al final de cada busqueda?
If (depth <= 0) Then
Return Me.Game.evaluate(whiteToMove)
End If
Gracias de nuevo!
pimidrez wrote:So when I have searched all the plies and "depth <= 0" then I call to evaluate the last ply in every move??
for example the engine is searching at 3 plies:
e2e4 e7e5 g1f3
the engine will evaluate only the final position with the knight in f3?? and return then the value of the position??
pimidrez wrote:Thanks Dann, now I get the idea! I read the article and is very clear.
but now let me ask you about one detail of the alpha-beta algorithim...
if the engine find a bad movement, for ex. g1f3, and should cut off because is not necesary to keep searching in that way...now..do I have to eliminate that movement (g1f3) from the list? Or is this not necessary?
pimidrez
Dann Corbit wrote:pimidrez wrote:Thanks Dann, now I get the idea! I read the article and is very clear.
but now let me ask you about one detail of the alpha-beta algorithim...
if the engine find a bad movement, for ex. g1f3, and should cut off because is not necesary to keep searching in that way...now..do I have to eliminate that movement (g1f3) from the list? Or is this not necessary?
pimidrez
The alpha-beta routine I posted does the cutoff.
*PLY1* *PLY2* *PLY1* *PLY2* *PLY1* *PLY2*
1.... e7e5 (2) 2.g8f6 (1) 1.... d7d5 (1) 2.b1c3 (3) 1.... b8c6 (2.5) 2.b1c3 (1)
2.d2d4 (1.5) 2.d2d4 (1) 2.d2d4 (2)
2.b1c3 (2) 2.d2d3 (1.5) 2.g8f6 (1.5)
pimidrez wrote:yes I already write those routines, now let me give easy an example and maybe you can say me how goes the cutoff of the alpha-beta:
*black side to move after the first movement of the white pieces ---> 1. e2e4 ....
*Now suppose that the black have the following possible responses, with the value of the position in parentheses:
1.... e7e5 (2)
1.... d7d5 (1)
1.... b8c6 (2.5)
*now suppose that the WHITES have the following possible responses for each one of the previusly responses of the blacks.
- Code: Select all
*PLY1* *PLY2* *PLY1* *PLY2* *PLY1* *PLY2*
1.... e7e5 (2) 2.g8f6 (1) 1.... d7d5 (1) 2.b1c3 (3) 1.... b8c6 (2.5) 2.b1c3 (1)
2.d2d4 (1.5) 2.d2d4 (1) 2.d2d4 (2)
2.b1c3 (2) 2.d2d3 (1.5) 2.g8f6 (1.5)
Dann Corbit wrote:pimidrez wrote:yes I already write those routines, now let me give easy an example and maybe you can say me how goes the cutoff of the alpha-beta:
*black side to move after the first movement of the white pieces ---> 1. e2e4 ....
*Now suppose that the black have the following possible responses, with the value of the position in parentheses:
1.... e7e5 (2)
1.... d7d5 (1)
1.... b8c6 (2.5)
*now suppose that the WHITES have the following possible responses for each one of the previusly responses of the blacks.
- Code: Select all
*PLY1* *PLY2* *PLY1* *PLY2* *PLY1* *PLY2*
1.... e7e5 (2) 2.g8f6 (1) 1.... d7d5 (1) 2.b1c3 (3) 1.... b8c6 (2.5) 2.b1c3 (1)
2.d2d4 (1.5) 2.d2d4 (1) 2.d2d4 (2)
2.b1c3 (2) 2.d2d3 (1.5) 2.g8f6 (1.5)
See:
http://www.cs.mcgill.ca/~cs251/OldCourses/1997/topic11/
http://en.wikipedia.org/wiki/Alpha-beta_pruning
https://chessprogramming.wikispaces.com/Alpha-Beta
If (Me.val >= beta) Then
Return beta
End If
If (Me.val > alpha) Then
alpha = Me.val
If (depth = Me.defaultply) Then
Me.bestmove = currentMove
End If
End If
pimidrez wrote:Thanks Dann, then I think I am going in the right direction..
Now Im apling a small evaluation function with only material values, and let's supoose that the side to move is black...
at ply 1 the evaluation result will be:
Value = BlackMaterial - WhiteMaterial (?) and ALPHA will be the max value
and at ply 2 the evaluation result will be:
Value = BlackMaterial - WhiteMaterial (?) and BETA will be the min value
is this right??
Return to Programming and Technical Discussions
Users browsing this forum: No registered users and 2 guests