help with LMR implementation

Code, algorithms, languages, construction...
Post Reply
gigiosky71
Posts: 1
Joined: Tue Mar 21, 2023 4:18 pm

help with LMR implementation

Post by gigiosky71 » Sun Dec 17, 2023 7:49 am

Hello Guys,
I really need your help. I am trying to add LMR to my engine but looking at the implementations I found around, I came up with something which does not seem too wrong to me but for some reasons, it surely speeds up, but it also spits crazy moves..for example in the Damiano mate

1r1q2k1/6p1/1r1pppP1/8/8/8/PPP5/1KQ4R w - - 0 1

which before this LMR implementation the engine could easily solve in few seconds after this LMR implementation it proposes the crazy Rh2?? ..which really does not make any sense..

here's my code for the negamax (the language is B4j but even if you are not familiar with it, it is fairly self explanatory). Does anyone spot anything really bad in the code? thank you very much for your help.
thanks
pierluigi

Code: Select all




Sub NegaMax(depth As Int, alpha As Int,beta As Int) As Int
	
	Dim InCheck,MovesFound As Boolean 
	Dim score As Int
	Dim FromSq, ToSq As Int
	Dim BestMove As MOVE
	Dim BestScore As Int, HashScore As Int, hashf As Int
	Dim AttackingPieces As List , MovesSearched As Int, TempMove As MOVE
	


	BestScore = -INFINITY 
		
	If Main.Ply > 0 Then
           If IsRepetition = 1 Then	   	 
	      Return 0			
           End If		
	End If
	
       'check the Hash table for the current position. if it is there just return the score and exit.
	
        If Main.Ply > 0 Then Then
	   HashScore = Hash.ProbeHash(depth,alpha,beta)
	   If  HashScore <> -1  Then 
	      Return HashScore		
	   End If	
	End If
    
	AttackingPieces = Main.IsSideUnderCheck(False,False)
	InCheck = IIf(AttackingPieces.Size>0,True,False)
				
	If depth = 0  And InCheck=False Then    
	   Return Quiescence(alpha,beta)
	End If
				
	If Main.Ply >=MAX_PLY-1 Then
	   Return Eval.StaticEvalPosition
	End If
	
	nodes = nodes + 1	
	
	If Bit.AndLong(nodes,SearchLogCounter)=0 Then       'equivalent to mod but way faster     
           Log(nodes)
	end If
	
	
        PV_lenght(Main.Ply)=Main.Ply
	
	If InCheck Then 
	  depth= depth+1
	End If

	Main.GenMoves(AttackingPieces,False) 'generate all moves and sort them
	
	MovesFound = False		 	
        MovesSearched = 0 'for LMR 
	
        For ii = MovesPointer(Main.Ply) To MovesPointer(Main.Ply+1)-1	  	   
       
	   If Main.MakeMove(Main.MOVES_STACK(ii)) = False Then 'ply ++ done in make
	      Continue		  	      
	   End If
	   MovesFound = True	   
	   
	   If MovesSearched = 0 Then
              score = -NegaMax(depth-1,-beta,-alpha) 
	   Else
	      
              'Try LMR...Dec 23
              
              TempMove = Main.MOVES_STACK(ii)
	      If MovesSearched >3 And depth >2 And InCheck = False And TempMove.Capture = False And _
	         TempMove.Capture = False And TempMove.CheckMove = False Then
		      
		 'apply LMR for the moves NOT included in the above type of moves
	         score = -NegaMax(depth-2,-alpha-1,-alpha) 		   
	      Else
	         score = alpha + 1
 	      End If 
		   
	      If score > alpha Then
			    
                 score = -NegaMax(depth-1,-alpha-1,-alpha) 
			   
		 If score > alpha And score < beta Then			     
                    score = -NegaMax(depth-1,-beta,-alpha)				 
		 End If
			   	  
	      End If
           End If
	   
	   
	   Main.UnmakeMove  'ply--done in unmake
       
	   MovesSearched = MovesSearched + 1
	   
	   If score > BestScore Then		
	      BestScore = score
		  
	      BestMove = Main.MOVES_STACK(ii)	  
	      BestMove.Score = score 
		  
	      If score > alpha Then

	         alpha = score		  	        
			 
		 PV(Main.Ply,Main.Ply) = BestMove 'Main.MOVES_STACK(ii)
	         For jj = Main.Ply+1 To (PV_lenght(Main.Ply+1)-1)
		    PV(Main.Ply,jj)=PV(Main.Ply+1,jj)	
		 Next	
		 PV_lenght(Main.Ply)=PV_lenght(Main.Ply+1)
			 
		 If score >= beta Then
		    if BestMove.Capture = False Then
	   	       Dim side As Int
		       side = Main.Currentpos.side
				  
		       FromSq = BestMove.From 
		       ToSq = BestMove.To 
	   	       MOVES_HISTORY(side,FromSq,ToSq) = MOVES_HISTORY(side,FromSq,ToSq) + depth 				 
	   	    End If  
			  
	            Hash.RecordHash(depth,score,BestMove,Hash.hashfBETA)
		    Return beta			 
	         End If			 
			  	 
	      End If
	   End If 	   	   		  
       Next
	
	
       If MovesFound = False Then
          If InCheck Then	    	  
             Return MINF_VALUE + Main.Ply 
	  Else
	    'stale	
	     Return 0
	  End If
       End If
    

       Hash.RecordHash(depth,alpha,BestMove,Hash.hashfEXACT)
       Return alpha
	
End Sub


Post Reply