From b01c79ec5431edc8910c09a5c908231de0e99e76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20C=C3=A1ceres?= Date: Mon, 20 Nov 2023 20:37:49 +0100 Subject: [PATCH 1/9] First attempt --- src/Lynx.Cli/appsettings.json | 2 ++ src/Lynx/Configuration.cs | 2 ++ src/Lynx/Search/NegaMax.cs | 20 ++++++++++++++++++++ 3 files changed, 24 insertions(+) diff --git a/src/Lynx.Cli/appsettings.json b/src/Lynx.Cli/appsettings.json index 96416ebc0..a4cd86549 100644 --- a/src/Lynx.Cli/appsettings.json +++ b/src/Lynx.Cli/appsettings.json @@ -38,6 +38,8 @@ "IIR_MinDepth": 4, + "LMP_MaxDepth": 3, + "History_MaxMoveValue": 8192, // Evaluation diff --git a/src/Lynx/Configuration.cs b/src/Lynx/Configuration.cs index d3c58936b..6fc0fceba 100644 --- a/src/Lynx/Configuration.cs +++ b/src/Lynx/Configuration.cs @@ -149,6 +149,8 @@ public sealed class EngineSettings public int IIR_MinDepth { get; set; } = 4; + public int LMP_MaxDepth { get; set; } = 3; + public int History_MaxMoveValue { get; set; } = 8_192; #region Evaluation diff --git a/src/Lynx/Search/NegaMax.cs b/src/Lynx/Search/NegaMax.cs index c5101ae13..7668ea67d 100644 --- a/src/Lynx/Search/NegaMax.cs +++ b/src/Lynx/Search/NegaMax.cs @@ -231,6 +231,26 @@ private int NegaMax(int depth, int ply, int alpha, int beta, bool parentWasNullM } else { + // Late Move Pruning (LMP) - all quiet moves can be pruned + // after searching the first few given by the move ordering algorithm + if (!pvNode + && !isInCheck + && depth <= Configuration.EngineSettings.LMP_MaxDepth + && scores[i] < EvaluationConstants.PromotionMoveScoreValue // Quiet moves + && i >= depth * 10) // Suggested by Antares + //&& i >= base + depth * depth) // SP, base 2 or 3 + { + // After making a move + Game.HalfMovesWithoutCaptureOrPawnMove = oldValue; + if (!isThreeFoldRepetition) + { + Game.PositionHashHistory.Remove(position.UniqueIdentifier); + } + position.UnmakeMove(move, gameState); + + break; + } + int reduction = 0; // 🔍 Late Move Reduction (LMR) - search with reduced depth From eb5ae7f23578c32efc75a5942fe1e14429047640 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20C=C3=A1ceres?= Date: Mon, 20 Nov 2023 20:42:41 +0100 Subject: [PATCH 2/9] Try formula with a min amont fo moves to always consider --- src/Lynx.Cli/appsettings.json | 1 + src/Lynx/Configuration.cs | 2 ++ src/Lynx/Search/NegaMax.cs | 3 +-- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Lynx.Cli/appsettings.json b/src/Lynx.Cli/appsettings.json index a4cd86549..4220df5db 100644 --- a/src/Lynx.Cli/appsettings.json +++ b/src/Lynx.Cli/appsettings.json @@ -39,6 +39,7 @@ "IIR_MinDepth": 4, "LMP_MaxDepth": 3, + "LMP_BaseMovesToTry": 3, "History_MaxMoveValue": 8192, diff --git a/src/Lynx/Configuration.cs b/src/Lynx/Configuration.cs index 6fc0fceba..972d1438a 100644 --- a/src/Lynx/Configuration.cs +++ b/src/Lynx/Configuration.cs @@ -151,6 +151,8 @@ public sealed class EngineSettings public int LMP_MaxDepth { get; set; } = 3; + public int LMP_BaseMovesToTry { get; set; } = 3; + public int History_MaxMoveValue { get; set; } = 8_192; #region Evaluation diff --git a/src/Lynx/Search/NegaMax.cs b/src/Lynx/Search/NegaMax.cs index 7668ea67d..50640ecc3 100644 --- a/src/Lynx/Search/NegaMax.cs +++ b/src/Lynx/Search/NegaMax.cs @@ -237,8 +237,7 @@ private int NegaMax(int depth, int ply, int alpha, int beta, bool parentWasNullM && !isInCheck && depth <= Configuration.EngineSettings.LMP_MaxDepth && scores[i] < EvaluationConstants.PromotionMoveScoreValue // Quiet moves - && i >= depth * 10) // Suggested by Antares - //&& i >= base + depth * depth) // SP, base 2 or 3 + && i >= Configuration.EngineSettings.LMP_BaseMovesToTry + depth * depth) // Based on SP { // After making a move Game.HalfMovesWithoutCaptureOrPawnMove = oldValue; From 4022a6ed07f48e22842abcc9f7fe49b1eeff5adc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20C=C3=A1ceres?= Date: Mon, 20 Nov 2023 22:07:40 +0100 Subject: [PATCH 3/9] Combine two formulas --- src/Lynx/Search/NegaMax.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Lynx/Search/NegaMax.cs b/src/Lynx/Search/NegaMax.cs index 50640ecc3..573d75084 100644 --- a/src/Lynx/Search/NegaMax.cs +++ b/src/Lynx/Search/NegaMax.cs @@ -237,7 +237,7 @@ private int NegaMax(int depth, int ply, int alpha, int beta, bool parentWasNullM && !isInCheck && depth <= Configuration.EngineSettings.LMP_MaxDepth && scores[i] < EvaluationConstants.PromotionMoveScoreValue // Quiet moves - && i >= Configuration.EngineSettings.LMP_BaseMovesToTry + depth * depth) // Based on SP + && i >= Configuration.EngineSettings.LMP_BaseMovesToTry + 10 * depth) // Based on SP and Altair { // After making a move Game.HalfMovesWithoutCaptureOrPawnMove = oldValue; From d40486a6c40b8fede3754b595a6eb203ff157cf6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20C=C3=A1ceres?= Date: Wed, 22 Nov 2023 04:16:39 +0100 Subject: [PATCH 4/9] Count all moves --- src/Lynx.Cli/appsettings.json | 5 +++-- src/Lynx/Configuration.cs | 6 ++++-- src/Lynx/Search/NegaMax.cs | 14 +++++++------- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/Lynx.Cli/appsettings.json b/src/Lynx.Cli/appsettings.json index 4220df5db..d0fa15d5f 100644 --- a/src/Lynx.Cli/appsettings.json +++ b/src/Lynx.Cli/appsettings.json @@ -38,8 +38,9 @@ "IIR_MinDepth": 4, - "LMP_MaxDepth": 3, - "LMP_BaseMovesToTry": 3, + "LMP_MaxDepth": 2, + "LMP_BaseMovesToTry": 0, + "LMP_MovesDepthMultiplier": 10, "History_MaxMoveValue": 8192, diff --git a/src/Lynx/Configuration.cs b/src/Lynx/Configuration.cs index 972d1438a..12903dff0 100644 --- a/src/Lynx/Configuration.cs +++ b/src/Lynx/Configuration.cs @@ -149,9 +149,11 @@ public sealed class EngineSettings public int IIR_MinDepth { get; set; } = 4; - public int LMP_MaxDepth { get; set; } = 3; + public int LMP_MaxDepth { get; set; } = 2; - public int LMP_BaseMovesToTry { get; set; } = 3; + public int LMP_BaseMovesToTry { get; set; } = 0; + + public int LMP_MovesDepthMultiplier { get; set; } = 10; public int History_MaxMoveValue { get; set; } = 8_192; diff --git a/src/Lynx/Search/NegaMax.cs b/src/Lynx/Search/NegaMax.cs index 573d75084..c0d281bef 100644 --- a/src/Lynx/Search/NegaMax.cs +++ b/src/Lynx/Search/NegaMax.cs @@ -182,20 +182,20 @@ private int NegaMax(int depth, int ply, int alpha, int beta, bool parentWasNullM } } - for (int i = 0; i < pseudoLegalMoves.Length; ++i) + for (int moveIndex = 0; moveIndex < pseudoLegalMoves.Length; ++moveIndex) { // Incremental move sorting, inspired by https://github.com/jw1912/Chess-Challenge and suggested by toanth // There's no need to sort all the moves since most of them don't get checked anyway // So just find the first unsearched one with the best score and try it - for (int j = i + 1; j < pseudoLegalMoves.Length; j++) + for (int j = moveIndex + 1; j < pseudoLegalMoves.Length; j++) { - if (scores[j] > scores[i]) + if (scores[j] > scores[moveIndex]) { - (scores[i], scores[j], pseudoLegalMoves[i], pseudoLegalMoves[j]) = (scores[j], scores[i], pseudoLegalMoves[j], pseudoLegalMoves[i]); + (scores[moveIndex], scores[j], pseudoLegalMoves[moveIndex], pseudoLegalMoves[j]) = (scores[j], scores[moveIndex], pseudoLegalMoves[j], pseudoLegalMoves[moveIndex]); } } - var move = pseudoLegalMoves[i]; + var move = pseudoLegalMoves[moveIndex]; var gameState = position.MakeMove(move); @@ -236,8 +236,8 @@ private int NegaMax(int depth, int ply, int alpha, int beta, bool parentWasNullM if (!pvNode && !isInCheck && depth <= Configuration.EngineSettings.LMP_MaxDepth - && scores[i] < EvaluationConstants.PromotionMoveScoreValue // Quiet moves - && i >= Configuration.EngineSettings.LMP_BaseMovesToTry + 10 * depth) // Based on SP and Altair + && scores[moveIndex] < EvaluationConstants.PromotionMoveScoreValue // Quiet moves + && moveIndex >= Configuration.EngineSettings.LMP_BaseMovesToTry + (Configuration.EngineSettings.LMP_MovesDepthMultiplier * depth)) // Based on SP and Altair { // After making a move Game.HalfMovesWithoutCaptureOrPawnMove = oldValue; From 66b0a1628abaddc9fc4272ae59c453210482b09e Mon Sep 17 00:00:00 2001 From: Eduardo Caceres Date: Wed, 22 Nov 2023 15:32:35 +0100 Subject: [PATCH 5/9] Fix amount of moves independent from depth --- src/Lynx/Search/NegaMax.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Lynx/Search/NegaMax.cs b/src/Lynx/Search/NegaMax.cs index c0d281bef..26b105fd1 100644 --- a/src/Lynx/Search/NegaMax.cs +++ b/src/Lynx/Search/NegaMax.cs @@ -237,7 +237,7 @@ private int NegaMax(int depth, int ply, int alpha, int beta, bool parentWasNullM && !isInCheck && depth <= Configuration.EngineSettings.LMP_MaxDepth && scores[moveIndex] < EvaluationConstants.PromotionMoveScoreValue // Quiet moves - && moveIndex >= Configuration.EngineSettings.LMP_BaseMovesToTry + (Configuration.EngineSettings.LMP_MovesDepthMultiplier * depth)) // Based on SP and Altair + && moveIndex >= Configuration.EngineSettings.LMP_BaseMovesToTry + Configuration.EngineSettings.LMP_MovesDepthMultiplier) // Based on SP and Altair { // After making a move Game.HalfMovesWithoutCaptureOrPawnMove = oldValue; From 46515fd68ab37d107f395b26ee061257ded6cfcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20C=C3=A1ceres?= Date: Thu, 23 Nov 2023 01:06:40 +0100 Subject: [PATCH 6/9] Revert "Fix amount of moves independent from depth" This reverts commit 66b0a1628abaddc9fc4272ae59c453210482b09e. --- src/Lynx/Search/NegaMax.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Lynx/Search/NegaMax.cs b/src/Lynx/Search/NegaMax.cs index 26b105fd1..c0d281bef 100644 --- a/src/Lynx/Search/NegaMax.cs +++ b/src/Lynx/Search/NegaMax.cs @@ -237,7 +237,7 @@ private int NegaMax(int depth, int ply, int alpha, int beta, bool parentWasNullM && !isInCheck && depth <= Configuration.EngineSettings.LMP_MaxDepth && scores[moveIndex] < EvaluationConstants.PromotionMoveScoreValue // Quiet moves - && moveIndex >= Configuration.EngineSettings.LMP_BaseMovesToTry + Configuration.EngineSettings.LMP_MovesDepthMultiplier) // Based on SP and Altair + && moveIndex >= Configuration.EngineSettings.LMP_BaseMovesToTry + (Configuration.EngineSettings.LMP_MovesDepthMultiplier * depth)) // Based on SP and Altair { // After making a move Game.HalfMovesWithoutCaptureOrPawnMove = oldValue; From fc9149d9ea9b2283bc478447074591b3e4ed0ae4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20C=C3=A1ceres?= Date: Thu, 23 Nov 2023 23:47:07 +0100 Subject: [PATCH 7/9] Fix attribution --- src/Lynx/Search/NegaMax.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Lynx/Search/NegaMax.cs b/src/Lynx/Search/NegaMax.cs index c0d281bef..27ca44530 100644 --- a/src/Lynx/Search/NegaMax.cs +++ b/src/Lynx/Search/NegaMax.cs @@ -237,7 +237,7 @@ private int NegaMax(int depth, int ply, int alpha, int beta, bool parentWasNullM && !isInCheck && depth <= Configuration.EngineSettings.LMP_MaxDepth && scores[moveIndex] < EvaluationConstants.PromotionMoveScoreValue // Quiet moves - && moveIndex >= Configuration.EngineSettings.LMP_BaseMovesToTry + (Configuration.EngineSettings.LMP_MovesDepthMultiplier * depth)) // Based on SP and Altair + && moveIndex >= Configuration.EngineSettings.LMP_BaseMovesToTry + (Configuration.EngineSettings.LMP_MovesDepthMultiplier * depth)) // Based on formula suggested by Antares { // After making a move Game.HalfMovesWithoutCaptureOrPawnMove = oldValue; From d47f2e67549f117b6f856c0f181f18eebda15dca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20C=C3=A1ceres?= Date: Thu, 23 Nov 2023 23:47:25 +0100 Subject: [PATCH 8/9] Try remove score limitation --- src/Lynx/Search/NegaMax.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Lynx/Search/NegaMax.cs b/src/Lynx/Search/NegaMax.cs index 27ca44530..afec4f3b9 100644 --- a/src/Lynx/Search/NegaMax.cs +++ b/src/Lynx/Search/NegaMax.cs @@ -236,7 +236,6 @@ private int NegaMax(int depth, int ply, int alpha, int beta, bool parentWasNullM if (!pvNode && !isInCheck && depth <= Configuration.EngineSettings.LMP_MaxDepth - && scores[moveIndex] < EvaluationConstants.PromotionMoveScoreValue // Quiet moves && moveIndex >= Configuration.EngineSettings.LMP_BaseMovesToTry + (Configuration.EngineSettings.LMP_MovesDepthMultiplier * depth)) // Based on formula suggested by Antares { // After making a move From a6ac62e7dbbaffc9869ea71ec0972f8e9d9627e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20C=C3=A1ceres?= Date: Fri, 24 Nov 2023 01:54:30 +0100 Subject: [PATCH 9/9] Revert "Try remove score limitation" This reverts commit d47f2e67549f117b6f856c0f181f18eebda15dca. --- src/Lynx/Search/NegaMax.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Lynx/Search/NegaMax.cs b/src/Lynx/Search/NegaMax.cs index afec4f3b9..27ca44530 100644 --- a/src/Lynx/Search/NegaMax.cs +++ b/src/Lynx/Search/NegaMax.cs @@ -236,6 +236,7 @@ private int NegaMax(int depth, int ply, int alpha, int beta, bool parentWasNullM if (!pvNode && !isInCheck && depth <= Configuration.EngineSettings.LMP_MaxDepth + && scores[moveIndex] < EvaluationConstants.PromotionMoveScoreValue // Quiet moves && moveIndex >= Configuration.EngineSettings.LMP_BaseMovesToTry + (Configuration.EngineSettings.LMP_MovesDepthMultiplier * depth)) // Based on formula suggested by Antares { // After making a move