Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Evaluator trait #46

Merged
merged 7 commits into from
May 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions src/book.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,12 @@ impl Book {
}
}

fn search(board: Board, think_time_limit: u128, solve_obj: &mut SolveObj, sub_solver: &Arc<SubSolver>) -> Hand {
fn search<Eval: Evaluator>(
board: Board,
think_time_limit: u128,
solve_obj: &mut SolveObj<Eval>,
sub_solver: &Arc<SubSolver>,
) -> Hand {
solve_obj.cache_gen += 1;
if board.empty().count_ones() <= 18 {
let mut solve_obj = solve_obj.clone();
Expand All @@ -187,7 +192,13 @@ fn search(board: Board, think_time_limit: u128, solve_obj: &mut SolveObj, sub_so
node_count: 0,
cache_gen: solve_obj.cache_gen,
};
let (_score, hand, _depth) = think_parallel(&searcher, board, EVAL_SCORE_MIN, EVAL_SCORE_MAX, false);
let (_score, hand, _depth, _node_count) = think_parallel(
&searcher,
board,
solve_obj.evaluator.score_min(),
solve_obj.evaluator.score_max(),
false,
);
hand
}
}
Expand All @@ -205,10 +216,10 @@ fn gen_opening(rng: &mut SmallRng) -> (Board, Vec<Hand>) {
(board, hands)
}

fn play_with_book(
fn play_with_book<Eval: Evaluator>(
book: Arc<Mutex<Book>>,
think_time_limit: u128,
solve_obj: &mut SolveObj,
solve_obj: &mut SolveObj<Eval>,
rng: &mut SmallRng,
sub_solver: &Arc<SubSolver>,
) {
Expand Down
1 change: 1 addition & 0 deletions src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub mod eval;
pub mod hand;
pub mod last_cache;
pub mod midgame;
pub mod pattern_eval;
pub mod search;
pub mod table;
pub mod think;
39 changes: 30 additions & 9 deletions src/engine/endgame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
mod test;
use crate::engine::bits::*;
use crate::engine::board::*;
use crate::engine::eval::*;
use crate::engine::hand::*;
use crate::engine::search::*;
use crate::engine::table::*;
Expand All @@ -10,7 +11,7 @@ use crc64::Crc64;
use std::cmp::max;
use std::io::Write;

fn near_leaf(solve_obj: &mut SolveObj, board: Board) -> (i8, SolveStat) {
fn near_leaf<Eval: Evaluator>(solve_obj: &mut SolveObj<Eval>, board: Board) -> (i8, SolveStat) {
let (score, node_count) = solve_obj.last_cache.solve_last(board);
(
score,
Expand All @@ -21,7 +22,12 @@ fn near_leaf(solve_obj: &mut SolveObj, board: Board) -> (i8, SolveStat) {
)
}

fn naive(solve_obj: &mut SolveObj, board: Board, (mut alpha, beta): (i8, i8), passed: bool) -> (i8, SolveStat) {
fn naive<Eval: Evaluator>(
solve_obj: &mut SolveObj<Eval>,
board: Board,
(mut alpha, beta): (i8, i8),
passed: bool,
) -> (i8, SolveStat) {
let mut pass = true;
let mut res = -(BOARD_SIZE as i8);
let mut stat = SolveStat::one();
Expand All @@ -47,7 +53,12 @@ fn naive(solve_obj: &mut SolveObj, board: Board, (mut alpha, beta): (i8, i8), pa
(res, stat)
}

fn static_order(solve_obj: &mut SolveObj, board: Board, (mut alpha, beta): (i8, i8), passed: bool) -> (i8, SolveStat) {
fn static_order<Eval: Evaluator>(
solve_obj: &mut SolveObj<Eval>,
board: Board,
(mut alpha, beta): (i8, i8),
passed: bool,
) -> (i8, SolveStat) {
let mut pass = true;
let mut res = -(BOARD_SIZE as i8);
let mut stat = SolveStat::one();
Expand Down Expand Up @@ -86,7 +97,12 @@ fn static_order(solve_obj: &mut SolveObj, board: Board, (mut alpha, beta): (i8,
(res, stat)
}

fn negascout_impl(solve_obj: &mut SolveObj, next: Board, (alpha, beta): (i8, i8), is_first: bool) -> (i8, SolveStat) {
fn negascout_impl<Eval: Evaluator>(
solve_obj: &mut SolveObj<Eval>,
next: Board,
(alpha, beta): (i8, i8),
is_first: bool,
) -> (i8, SolveStat) {
if is_first {
solve_inner(solve_obj, next, (-beta, -alpha), false)
} else {
Expand All @@ -104,7 +120,12 @@ fn negascout_impl(solve_obj: &mut SolveObj, next: Board, (alpha, beta): (i8, i8)
}
}

fn fastest_first(solve_obj: &mut SolveObj, board: Board, (mut alpha, beta): (i8, i8), passed: bool) -> (i8, SolveStat) {
fn fastest_first<Eval: Evaluator>(
solve_obj: &mut SolveObj<Eval>,
board: Board,
(mut alpha, beta): (i8, i8),
passed: bool,
) -> (i8, SolveStat) {
const MAX_FFS_NEXT: usize = 20;
let mut nexts = ArrayVec::<_, MAX_FFS_NEXT>::new();
for (next, _pos) in board.next_iter() {
Expand Down Expand Up @@ -134,8 +155,8 @@ fn fastest_first(solve_obj: &mut SolveObj, board: Board, (mut alpha, beta): (i8,
(res, stat)
}

fn move_ordering_by_eval(
solve_obj: &mut SolveObj,
fn move_ordering_by_eval<Eval: Evaluator>(
solve_obj: &mut SolveObj<Eval>,
board: Board,
(mut alpha, beta): (i8, i8),
passed: bool,
Expand Down Expand Up @@ -169,8 +190,8 @@ fn move_ordering_by_eval(
(res, best, stat)
}

pub fn solve_inner(
solve_obj: &mut SolveObj,
pub fn solve_inner<Eval: Evaluator>(
solve_obj: &mut SolveObj<Eval>,
board: Board,
(mut alpha, mut beta): (i8, i8),
passed: bool,
Expand Down
19 changes: 19 additions & 0 deletions src/engine/endgame/test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
extern crate test;
use super::*;
use crate::setup::*;
use rayon::prelude::*;
use std::fs::File;
use std::io::{BufRead, BufReader};
use test::Bencher;
Expand Down Expand Up @@ -59,3 +60,21 @@ fn bench_solve_inner(b: &mut Bencher) {
}
});
}

#[bench]
fn bench_solve_inner_parallel(b: &mut Bencher) {
let solve_obj = setup_default();
let dataset = load_stress_test_set();

b.iter(|| {
dataset.par_iter().for_each(|&(board, _desired)| {
let mut obj = solve_obj.clone();
let (_res, _stat) = solve_inner(
&mut obj,
board,
(-(BOARD_SIZE as i8), BOARD_SIZE as i8),
false,
);
});
});
}
Loading
Loading