cleaned up, added random AI for testing
This commit is contained in:
parent
92a11f0898
commit
b788bd2e4d
5 changed files with 245 additions and 21 deletions
163
src/ai.rs
163
src/ai.rs
|
|
@ -142,13 +142,123 @@ pub fn alphabeta(mut game: Game, depth: u8, mut alpha: i8, mut beta: i8) -> (Boa
|
|||
}
|
||||
}
|
||||
|
||||
/// Using alpha-beta pruning with printing of intermediate data for debugging
|
||||
/// and monitoring purposes.
|
||||
pub fn alphabeta_with_printing(game: Game, depth: u8) -> (Board, i8) {
|
||||
let (b, moves, eval) = alphabeta_with_printing_inner(game, depth, i8::MIN + 1, i8::MAX - 1);
|
||||
println!("beep. boop. we assessed {} moves.", moves);
|
||||
(b, eval)
|
||||
}
|
||||
|
||||
/// Using alpha-beta pruning and the minimax algorithm, determine the best move
|
||||
/// for a game with a recursion depth of `depth`.
|
||||
///
|
||||
/// We use a very simple evaluation heuristic: (Black squares - White squares).
|
||||
fn alphabeta_with_printing_inner(
|
||||
mut game: Game,
|
||||
depth: u8,
|
||||
mut alpha: i8,
|
||||
mut beta: i8,
|
||||
) -> (Board, usize, i8) {
|
||||
// if we reach our maximum recursion depth, return evaluation
|
||||
if depth == 0 {
|
||||
return (0, 0, game.score().diff());
|
||||
}
|
||||
let moves = game.available();
|
||||
if moves == 0 {
|
||||
// if no move, skip and continue recursion
|
||||
// this seems to technically introduce a bias against move-chains
|
||||
// that include skips. I haven't found it to be a big deal in play.
|
||||
game.skip();
|
||||
return (
|
||||
0,
|
||||
0,
|
||||
alphabeta_with_printing_inner(game, depth - 1, alpha, beta).2,
|
||||
);
|
||||
}
|
||||
|
||||
// just initially assume that the best move is no move at all. This will
|
||||
// inevitably be corrected.
|
||||
let mut best_move: Board = 0;
|
||||
// we initially rank moves based on a couple basic heuristics:
|
||||
// - corner pieces are best
|
||||
// - edge pieces are great
|
||||
// - others considered last
|
||||
// This just allows us to prune the tree a bit more aggressively
|
||||
// since we're considering the "best" moves first.
|
||||
// We do this by mapping moves to ranked moves and then sorting.
|
||||
let mut moves = explode_board(moves).map(MoveRank::from).collect::<Vec<_>>();
|
||||
moves.sort();
|
||||
let moves = moves
|
||||
.into_iter()
|
||||
.map(MoveRank::into_inner)
|
||||
.collect::<Vec<_>>();
|
||||
let mut num_moves = moves.len();
|
||||
// I just establish a convention of maximizing for black and minimizing for white.
|
||||
// I'm not sure if that's conventional or not, but it's what I chose.
|
||||
match game.current_team {
|
||||
Team::Black => {
|
||||
for mv in moves {
|
||||
let mut g = game.clone();
|
||||
g.play(mv);
|
||||
// maximize for the evaluation of subsequent moves
|
||||
let (_, num_moves_prime, evaluation) =
|
||||
alphabeta_with_printing_inner(g, depth - 1, alpha, beta);
|
||||
num_moves += num_moves_prime;
|
||||
// if our evaluated move is superior to the alpha, update
|
||||
// it.
|
||||
if evaluation > alpha {
|
||||
alpha = evaluation;
|
||||
best_move = mv;
|
||||
};
|
||||
// if our beta is less than alpha, prune the node.
|
||||
if beta <= alpha {
|
||||
break;
|
||||
}
|
||||
}
|
||||
(best_move, num_moves, alpha)
|
||||
}
|
||||
Team::White => {
|
||||
for mv in moves {
|
||||
let mut g = game.clone();
|
||||
g.play(mv);
|
||||
// minimize for the evaluation of subsequent moves
|
||||
let (_, num_moves_prime, evaluation) =
|
||||
alphabeta_with_printing_inner(g, depth - 1, alpha, beta);
|
||||
num_moves += num_moves_prime;
|
||||
// if our evaluated move produces lower eval than the beta,
|
||||
// update beta.
|
||||
if evaluation < beta {
|
||||
beta = evaluation;
|
||||
best_move = mv;
|
||||
};
|
||||
// if our beta is less than alpha, prune the node.
|
||||
if beta <= alpha {
|
||||
break;
|
||||
}
|
||||
}
|
||||
(best_move, num_moves, beta)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::board::BitBoard;
|
||||
use crate::board::view::View;
|
||||
use crate::board::{BitBoard, Board, Score};
|
||||
use crate::game::Game;
|
||||
|
||||
use rand::prelude::IndexedRandom;
|
||||
use rand::rngs::StdRng;
|
||||
use rand::{Rng, SeedableRng};
|
||||
|
||||
/// An AI player that only makes random moves
|
||||
fn random_move(game: &Game, rng: &mut impl Rng) -> Board {
|
||||
let moves = explode_board(game.available()).collect::<Vec<_>>();
|
||||
*moves.choose(rng).unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
// just a sanity check to ensure that my AI performs up to snuff with another popular engine
|
||||
fn opening() {
|
||||
|
|
@ -171,21 +281,48 @@ mod tests {
|
|||
// I found that, despite the AI clobbering me, the AI could not
|
||||
// compete with itself very well. I'm honestly not quite sure why that is.
|
||||
#[test]
|
||||
#[should_panic] // disabled until I fix whatever causes the AI not to tie
|
||||
fn ai_ties_ai() {
|
||||
// just play through a game letting AI make all the moves.
|
||||
let mut game = Game::default();
|
||||
while !game.is_complete() {
|
||||
if game.available() == 0 {
|
||||
game.skip();
|
||||
} else {
|
||||
let (mv, _) = alphabeta(game.clone(), 8, i8::MIN + 1, i8::MAX - 1);
|
||||
fn ai_beats_random() {
|
||||
// just contains pairings of starting_team and seed value
|
||||
let cases = vec![
|
||||
(Team::Black, 1231293),
|
||||
(Team::White, 491823),
|
||||
(Team::White, 12931),
|
||||
(Team::Black, 982983713),
|
||||
(Team::Black, 123),
|
||||
(Team::White, 87132895),
|
||||
];
|
||||
|
||||
for (team, seed) in cases {
|
||||
let mut rng = StdRng::seed_from_u64(seed);
|
||||
let mut game = Game::default();
|
||||
if team != Team::Black {
|
||||
let mv = random_move(&game, &mut rng);
|
||||
game.play(mv);
|
||||
}
|
||||
}
|
||||
|
||||
// one would assume the AI would compete rather closely against itself.
|
||||
assert!(dbg!(game.score()).diff().abs() < 3);
|
||||
while !game.is_complete() {
|
||||
if game.available() == 0 {
|
||||
game.skip();
|
||||
continue;
|
||||
}
|
||||
let mv = if game.current_team == team {
|
||||
alphabeta(game.clone(), 8, i8::MIN + 1, i8::MAX - 1).0
|
||||
} else {
|
||||
random_move(&game, &mut rng)
|
||||
};
|
||||
game.play(mv);
|
||||
}
|
||||
|
||||
assert!(
|
||||
match (team, game.score()) {
|
||||
(Team::Black, Score(b, w)) => b - w,
|
||||
(Team::White, Score(b, w)) => w - b,
|
||||
} > 4,
|
||||
"game with seed {} and team {:?} failed to win by 4 points or more.",
|
||||
seed,
|
||||
team
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue