From 73c0da0e0c74a738724b7eb81c64acd7749f7094 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Gr=C4=85cikowski?= <01180781@pw.edu.pl> Date: Tue, 19 May 2026 16:17:12 +0200 Subject: [PATCH 1/2] refine piece advanceemnt heuristic --- src/compute/src/agents.rs | 4 +- src/compute/src/agents/minimax/heuristic.rs | 58 ++++++++++++------- .../configs/with_human/mcts_vs_human.toml | 4 +- .../configs/with_human/minimax_vs_human.toml | 6 +- .../without_human/minimax_vs_minimax.toml | 4 ++ 5 files changed, 48 insertions(+), 28 deletions(-) diff --git a/src/compute/src/agents.rs b/src/compute/src/agents.rs index 581e4cf..8e86f8f 100644 --- a/src/compute/src/agents.rs +++ b/src/compute/src/agents.rs @@ -17,10 +17,10 @@ fn default_max_depth() -> u8 { } fn default_material() -> i32 { - 100 + 200 } fn default_advancement() -> i32 { - 10 + 5 } fn default_defended() -> i32 { 5 diff --git a/src/compute/src/agents/minimax/heuristic.rs b/src/compute/src/agents/minimax/heuristic.rs index 4e1012a..c4a5c12 100644 --- a/src/compute/src/agents/minimax/heuristic.rs +++ b/src/compute/src/agents/minimax/heuristic.rs @@ -24,7 +24,7 @@ impl Default for HeuristicEvaluator { fn default() -> Self { Self { material_weight: 10, - advancement_weight: 40, + advancement_weight: 10, defended_weight: 5, edge_penalty_weight: -2, } @@ -52,19 +52,29 @@ impl HeuristicEvaluator { white_count - black_count } - fn evaluate_piece_advancement(&self, board: &Board, config: &BoardConfig) -> i32 { - let white_highest_bit = match board.white_board == 0 { - true => 0, - false => 127 - board.white_board.leading_zeros() as i32, - }; - let white_advancement = white_highest_bit / config.width as i32; + fn advancement_score(&self, board: u128, is_white: bool, config: &BoardConfig) -> i32 { + let mut score = 0; + let mut pieces = board; + let height = config.height as i32; + let width = config.width as i32; + + while pieces != 0 { + let square = pieces.trailing_zeros() as i32; + let row = square / width; + let advancement = if is_white { + row + } else { + (height - 1) - row + }; + score += 1 << advancement; + pieces &= pieces - 1; + } + score + } - let black_lowest_bit = match board.black_board == 0 { - true => 127, - false => board.black_board.trailing_zeros() as i32, - }; - let black_advancement = - (config.height as i32 - 1) - (black_lowest_bit / config.width as i32); + fn evaluate_piece_advancement(&self, board: &Board, config: &BoardConfig) -> i32 { + let white_advancement = self.advancement_score(board.white_board, true, config); + let black_advancement = self.advancement_score(board.black_board, false, config); white_advancement - black_advancement } @@ -151,16 +161,16 @@ mod tests { let config = BoardConfig::new(4, 4); let evaluator = HeuristicEvaluator::default(); - // White advancement: 9 / 4 = 2 - // Black advancement: (4 - 1) - (14 / 4) = 3 - 3 = 0 + // White advancement: 1 << (9 / 4) = 1 << 2 = 4 + // Black advancement: 1 << ((4 - 1) - (14 / 4)) = 1 << (3 - 3) = 1 << 0 = 1 let board = Board { white_board: 1 << 9, black_board: 1 << 14, turn: Player::White, }; - // 2 - 0 = 2 - assert_eq!(evaluator.evaluate_piece_advancement(&board, &config), 2); + // 4 - 1 = 3 + assert_eq!(evaluator.evaluate_piece_advancement(&board, &config), 3); } #[test] @@ -239,9 +249,15 @@ mod tests { }; // expected result calculation: - // (10 * 2) + (40 * 1) + (5 * 2) + (-2 * 2) - // = 20 + 40 + 10 - 4 = 66 - - assert_eq!(evaluator.evaluate(&board, &config), 66); + // Material: (3 - 1) * 10 = 20 + // Advancement: + // White: (1<<0) + (1<<1) + (1<<1) = 1 + 2 + 2 = 5 + // Black: 1<<(2-2) = 1<<0 = 1 + // (5 - 1) * 40 = 160 + // Defense: White(2) - Black(0) = 2. 2 * 5 = 10 + // Edges: White(2) - Black(0) = 2. 2 * -2 = -4 + // Total: 20 + 160 + 10 - 4 = 186 + + assert_eq!(evaluator.evaluate(&board, &config), 186); } } diff --git a/src/compute/src/resources/configs/with_human/mcts_vs_human.toml b/src/compute/src/resources/configs/with_human/mcts_vs_human.toml index 3497e6e..d2f2990 100644 --- a/src/compute/src/resources/configs/with_human/mcts_vs_human.toml +++ b/src/compute/src/resources/configs/with_human/mcts_vs_human.toml @@ -1,5 +1,5 @@ -board_width = 11 -board_height = 11 +board_width = 6 +board_height = 7 seed = 42 [black_player] diff --git a/src/compute/src/resources/configs/with_human/minimax_vs_human.toml b/src/compute/src/resources/configs/with_human/minimax_vs_human.toml index 037f537..c1217ba 100644 --- a/src/compute/src/resources/configs/with_human/minimax_vs_human.toml +++ b/src/compute/src/resources/configs/with_human/minimax_vs_human.toml @@ -1,10 +1,10 @@ -board_width = 11 -board_height = 11 +board_width = 8 +board_height = 8 seed = 42 [black_player] type = "Minimax" -max_depth = 4 +max_depth = 5 [white_player] type = "Human" diff --git a/src/compute/src/resources/configs/without_human/minimax_vs_minimax.toml b/src/compute/src/resources/configs/without_human/minimax_vs_minimax.toml index f67474e..77a26e2 100644 --- a/src/compute/src/resources/configs/without_human/minimax_vs_minimax.toml +++ b/src/compute/src/resources/configs/without_human/minimax_vs_minimax.toml @@ -5,7 +5,11 @@ seed = 42 [black_player] type = "Minimax" max_depth = 4 +material_weight = 200 +advancement_weight = 5 [white_player] type = "Minimax" max_depth = 5 +material_weight = 200 +advancement_weight = 5 From 2178bf75a1dc780c19a5fe790e073b0acd9cf74d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Gr=C4=85cikowski?= <01180781@pw.edu.pl> Date: Tue, 19 May 2026 16:28:32 +0200 Subject: [PATCH 2/2] fmt --- src/compute/src/agents/minimax/heuristic.rs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/compute/src/agents/minimax/heuristic.rs b/src/compute/src/agents/minimax/heuristic.rs index c4a5c12..4448bd7 100644 --- a/src/compute/src/agents/minimax/heuristic.rs +++ b/src/compute/src/agents/minimax/heuristic.rs @@ -61,11 +61,7 @@ impl HeuristicEvaluator { while pieces != 0 { let square = pieces.trailing_zeros() as i32; let row = square / width; - let advancement = if is_white { - row - } else { - (height - 1) - row - }; + let advancement = if is_white { row } else { (height - 1) - row }; score += 1 << advancement; pieces &= pieces - 1; } @@ -250,7 +246,7 @@ mod tests { // expected result calculation: // Material: (3 - 1) * 10 = 20 - // Advancement: + // Advancement: // White: (1<<0) + (1<<1) + (1<<1) = 1 + 2 + 2 = 5 // Black: 1<<(2-2) = 1<<0 = 1 // (5 - 1) * 40 = 160