alloc test

This commit is contained in:
2025-03-13 10:02:54 -04:00
parent ed5ad738e3
commit efd956d614
11 changed files with 113 additions and 12 deletions

44
src/allocs.rs Normal file
View File

@@ -0,0 +1,44 @@
use crate::{
logic::{ChildrenEvalMethod, FutureMoveConfig, FutureMoves},
repr::{Board, Piece, Winner},
};
use allocative::FlameGraphBuilder;
use rand::seq::IteratorRandom;
pub fn run() {
let mut flamegraph = FlameGraphBuilder::default();
let mut fut = FutureMoves::new(
Piece::Black,
FutureMoveConfig {
max_depth: 20,
min_arena_depth: 14,
top_k_children: 2,
up_to_minus: 10,
max_arena_size: 100_000_000,
do_prune: true,
print: true,
children_eval_method: ChildrenEvalMethod::AverageDivDepth,
},
);
let mut board = Board::new().starting_pos();
let mut rng = rand::rng();
let mut i = 0;
while board.game_winner() == Winner::None && i < 2 {
fut.update_from_board(&board);
flamegraph.visit_root(&fut);
fut.generate();
flamegraph.visit_root(&fut);
if let Some(m) = fut.best_move().expect("FutureMoves has no move?") {
board.place(m, Piece::Black).expect("failed move");
}
if let Some(m) = board.possible_moves(Piece::White).choose(&mut rng) {
board.place(m, Piece::White).expect("enemy failed move");
}
i += 1;
}
println!("{}", flamegraph.finish_and_write_flame_graph());
}

View File

@@ -1,4 +1,5 @@
mod agent;
mod allocs;
mod complexagent;
mod elo;
mod game;

View File

@@ -2,9 +2,11 @@ use crate::{
logic::r#move::Move,
repr::{Board, CoordPair, Piece, Winner},
};
use allocative::Allocative;
use indicatif::{ProgressIterator, ProgressStyle};
use std::{collections::HashMap, hash::BuildHasherDefault, ops::ControlFlow};
#[derive(Allocative)]
pub struct FutureMoves {
/// Arena containing all [`Move`]
arena: Vec<Move>,
@@ -23,7 +25,8 @@ pub struct FutureMoves {
board: Board,
}
#[derive(Copy, Clone)]
#[derive(Copy, Clone, Allocative)]
pub struct FutureMoveConfig {
/// Max depth of that we should try and traverse
pub max_depth: usize,
@@ -73,7 +76,7 @@ impl std::fmt::Display for FutureMoveConfig {
}
}
#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, Allocative)]
#[allow(dead_code)]
pub enum ChildrenEvalMethod {
/// Best (by far) strat compared to Max or Min

View File

@@ -1,8 +1,9 @@
use super::board_value::BoardValueMap;
use crate::repr::{Board, CoordPair, Piece, Winner};
use allocative::Allocative;
use std::sync::LazyLock;
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Allocative)]
pub struct Move {
/// Coordinates (i, j) of the move (if it exists)
pub coord: Option<CoordPair>,

View File

@@ -3,6 +3,7 @@ use logic::{ChildrenEvalMethod, FutureMoveConfig};
use repr::Piece;
mod agent;
mod allocs;
mod complexagent;
mod elo;
mod game;
@@ -12,6 +13,8 @@ pub mod repr;
// TODO! make this agent configuration a config option via `clap-rs`
fn main() {
allocs::run();
return;
// elo::run();
// return;
let player1 = complexagent::ComplexAgent::new(

View File

@@ -3,11 +3,12 @@ use super::{
coords::{CoordPair, CoordPairInner},
CoordAxis,
};
use allocative::Allocative;
use static_assertions::const_assert;
pub type BitBoardInner = u64;
#[derive(Copy, Clone, PartialEq, Eq)]
#[derive(Copy, Clone, PartialEq, Eq, Allocative)]
pub struct BitBoard(BitBoardInner);
// BitBoard should be big enough to fit all points on the board

View File

@@ -1,4 +1,5 @@
use super::{bitboard::BitBoard, piece::Piece, CoordAxis, CoordPair};
use allocative::Allocative;
use arrayvec::ArrayVec;
use rand::seq::IteratorRandom;
use std::{cmp::Ordering, fmt};
@@ -39,7 +40,7 @@ impl<T: Default + Copy> From<PosMapOrig<T>> for PosMap<T> {
}
}
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
#[derive(PartialEq, Eq, Copy, Clone, Debug, Allocative)]
pub enum Winner {
Player(Piece),
Tie,
@@ -81,7 +82,7 @@ macro_rules! get_board {
}
/// Repersents a Othello game board at a certain space
#[derive(Copy, Clone, PartialEq, Eq)]
#[derive(Copy, Clone, PartialEq, Eq, Allocative)]
pub struct Board {
/// [`BitBoard`] containing all white pieces
white_board: BitBoard,

View File

@@ -1,4 +1,5 @@
use super::Board;
use allocative::Allocative;
use num::Integer;
use static_assertions::const_assert;
@@ -11,7 +12,7 @@ pub type CoordPairInner = u8;
const_assert!(CoordPairInner::MAX as usize >= Board::AREA.0 as usize);
#[derive(PartialEq, Eq, Copy, Clone, Hash)]
#[derive(PartialEq, Eq, Copy, Clone, Hash, Allocative)]
pub struct CoordPair(pub CoordPairInner);
impl CoordPair {

View File

@@ -1,4 +1,6 @@
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
use allocative::Allocative;
#[derive(Copy, Clone, PartialEq, Eq, Debug, Allocative)]
pub enum Piece {
Black,
White,