Skip to content

Commit 41bbae5

Browse files
committed
Cleanup code and make it nicer. Implement spreadsheet printer, so that a nice table can be made.
1 parent 8f28113 commit 41bbae5

11 files changed

+117
-97
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/target
2-
.idea
2+
.idea
3+
output.txt

src/algorithms/draw_checker.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ pub fn uncount_board(board_played_times_prediction: &mut HashMap<Board, u32>, ne
66
board_played_times_prediction.insert(
77
// TODO Hash it to avoid copying, we need a good hash function for Board
88
*new_board,
9-
*board_played_times_prediction.get(&new_board).unwrap_or(&0) - 1,
9+
*board_played_times_prediction.get(new_board).unwrap_or(&0) - 1,
1010
);
1111
}
1212

1313
pub fn count_board(board_played_times_prediction: &mut HashMap<Board, u32>, new_board: &Board) {
1414
board_played_times_prediction.insert(
1515
// TODO Hash it to avoid copying, we need a good hash function for Board
1616
*new_board,
17-
*board_played_times_prediction.get(&new_board).unwrap_or(&0) + 1,
17+
*board_played_times_prediction.get(new_board).unwrap_or(&0) + 1,
1818
);
1919
}

src/algorithms/eval.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ pub fn eval_no_legal_moves(board: &Board) -> f32 {
1010

1111
// If we arrive at here while it is checkmate, then we know that the side playing
1212
// has been checkmated.
13-
return if board.side_to_move() == Color::White {
13+
if board.side_to_move() == Color::White {
1414
f32::MIN
1515
} else {
1616
f32::MAX
17-
};
17+
}
1818
}
1919
pub(crate) fn new_eval_is_better(maximise: bool, old: &Evaluation, new: &Evaluation) -> bool {
2020
new.eval.is_some()

src/common/constants.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub(crate) mod modules {
1111
pub(crate) const TAPERED_INCREMENTAL_PESTO_PSQT: u32 = 1 << 9;
1212
}
1313

14-
pub(crate) const NUMBER_OF_MODULES: u32 = 9;
14+
pub(crate) const NUMBER_OF_MODULES: usize = 10;
1515

1616
//NAIVE_PSQT TABLES
1717
pub(crate) mod naive_psqt_tables {

src/io.rs

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
1-
use std::{fs::OpenOptions, io::{self, prelude::*}};
1+
use std::{fs::OpenOptions, io::prelude::*};
22

33
use crate::common::constants::NUMBER_OF_MODULES;
44

55
pub(crate) fn write_result(buf: &[u8], file: &str) -> std::io::Result<()> {
6-
let mut file = OpenOptions::new().write(true).create(true).append(true).open(file).unwrap();
7-
file.write(buf)?;
6+
let mut file = OpenOptions::new()
7+
.create(true)
8+
.append(true)
9+
.open(file)
10+
.unwrap();
11+
file.write_all(buf)?;
812
Ok(())
913
}
1014

11-
pub(crate)fn modules_to_string(modules: u32) -> String {
15+
pub(crate) fn modules_to_string(modules: u32) -> String {
1216
let mut start: bool = true;
1317
let mut output: String = "".to_string();
1418
//For loop ranges is all available modules index. +1 because we need inclusive.
15-
for i in 0..NUMBER_OF_MODULES+1 {
19+
for i in 0..NUMBER_OF_MODULES + 1 {
1620
if (modules & (1 << i)).count_ones() == 1 {
1721
let module_string = match i {
1822
0 => "ANALYZE",
@@ -25,15 +29,15 @@ pub(crate)fn modules_to_string(modules: u32) -> String {
2529
7 => "PAWN_STRUCTURE",
2630
8 => "TAPERED_EVERY_PESTO_PSQT",
2731
9 => "TAPERED_INCREMENTAL_PESTO_PSQT",
28-
_ => "INVALID MODULE DETECTED"
32+
_ => "INVALID MODULE DETECTED",
2933
};
3034
if !start {
31-
output = output + &", " + module_string;
35+
output = output + ", " + module_string;
3236
} else {
3337
start = false;
3438
output = module_string.to_string();
3539
}
3640
}
3741
}
3842
output
39-
}
43+
}

src/main.rs

+77-40
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,119 @@
1+
use std::fs::remove_file;
2+
use std::mem;
13
use std::time::Duration;
24

3-
45
use crate::algorithms::the_algorithm::Algorithm;
56
#[allow(unused_imports)]
6-
use crate::common::constants::{modules::{
7-
ALPHA_BETA, ANALYZE, NAIVE_PSQT, PAWN_STRUCTURE, SEARCH_EXTENSIONS, SKIP_BAD_MOVES,
8-
SQUARE_CONTROL_METRIC, TAPERED_EVERY_PESTO_PSQT, TAPERED_INCREMENTAL_PESTO_PSQT,
9-
TRANSPOSITION_TABLE,
10-
}, NUMBER_OF_MODULES};
11-
use crate::io::{write_result, modules_to_string};
7+
use crate::common::constants::{
8+
modules::{
9+
ALPHA_BETA, ANALYZE, NAIVE_PSQT, PAWN_STRUCTURE, SEARCH_EXTENSIONS, SKIP_BAD_MOVES,
10+
SQUARE_CONTROL_METRIC, TAPERED_EVERY_PESTO_PSQT, TAPERED_INCREMENTAL_PESTO_PSQT,
11+
TRANSPOSITION_TABLE,
12+
},
13+
NUMBER_OF_MODULES,
14+
};
15+
use crate::io::write_result;
1216

1317
use self::pitter::logic::{Competition, CompetitionResults};
1418

1519
mod algorithms;
1620
mod common;
21+
mod io;
1722
mod modules;
1823
mod pitter;
19-
mod io;
2024

2125
//If we should print the moves played and results of each game.
2226
pub(crate) const PRINT_GAME: bool = true;
2327
//If we should test all possible pairs of combinations.
2428
const TEST_ALL_PAIRS: bool = true;
2529

26-
2730
#[tokio::main]
2831
async fn main() {
32+
remove_file("./output.txt").unwrap_or_default();
2933
if !TEST_ALL_PAIRS {
3034
//ALPHA_BETA | ANALYZE | SEARCH_EXTENSIONS | SKIP_BAD_MOVES | SQUARE_CONTROL_METRIC | TRANSPOSITION_TABLE | NAIVE_PSQT | PAWN_STRUCTURE | TAPERED_EVERY_PESTO_PSQT | TAPERED_INCREMENTAL_PESTO_PSQT
3135
//Put 0 for no modules.
3236
//Setup modules
33-
let modules1 = ALPHA_BETA | TAPERED_INCREMENTAL_PESTO_PSQT;
34-
let modules2 = ALPHA_BETA | TAPERED_EVERY_PESTO_PSQT;
37+
let modules1 = TRANSPOSITION_TABLE;
38+
let modules2 = 0;
3539
let time_per_move1 = Duration::from_micros(2000);
3640
let time_per_move2 = Duration::from_micros(2000);
37-
let game_pairs = 500;
41+
let game_pairs = 50;
3842

3943
//Run competition
40-
let result = do_competition(modules1, modules2, time_per_move1, time_per_move2, game_pairs).await;
41-
42-
//Save results to file
43-
let mut output: String = "\nAlgo 1 modules: ".to_owned() + &modules_to_string(modules1) + "\nAlgo 2 modules: " + &modules_to_string(modules2)
44-
+ &"\nGame pairs: " + &game_pairs.to_string() + &"\nTime per move, algo1: " + &format!("{:?}", time_per_move1) + "\nTime per move, algo2: " + &format!("{:?}", time_per_move2);
45-
let result = format!("\nCompetition results: {:#?}", &result);
46-
output = output + &result + &"\n\n\n";
47-
let buf = output.as_bytes();
48-
let _ = write_result(buf, "./output.txt");
49-
44+
let result = do_competition(
45+
modules1,
46+
modules2,
47+
time_per_move1,
48+
time_per_move2,
49+
game_pairs,
50+
)
51+
.await;
52+
dbg!(result);
5053
} else {
51-
println!("Running {} possibilites", (NUMBER_OF_MODULES+1)*NUMBER_OF_MODULES/2);
54+
println!(
55+
"Running {} possibilites",
56+
(NUMBER_OF_MODULES + 1) * NUMBER_OF_MODULES / 2
57+
);
5258
let time_per_move1 = Duration::from_micros(2000);
5359
let time_per_move2 = Duration::from_micros(2000);
54-
let game_pairs = 1000;
55-
60+
let game_pairs = 200;
61+
5662
let mut competitions_run: u32 = 0;
57-
for i in 0..NUMBER_OF_MODULES+1 {
58-
for j in i+1..NUMBER_OF_MODULES+1 {
63+
let mut dp: Vec<Vec<Option<CompetitionResults>>> =
64+
vec![vec![None; NUMBER_OF_MODULES]; NUMBER_OF_MODULES];
65+
for i in 0..NUMBER_OF_MODULES {
66+
for j in 0..NUMBER_OF_MODULES {
67+
if dp[j][i].is_some() {
68+
let mut temp = dp[j][i].unwrap();
69+
let output: String =
70+
format!("{}\t", temp.algo2_wins as i64 - temp.algo1_wins as i64);
71+
let buf = output.as_bytes();
72+
let _ = write_result(buf, "./output.txt");
73+
mem::swap(&mut temp.algo1_wins, &mut temp.algo2_wins);
74+
dp[i][j] = Some(temp);
75+
continue;
76+
}
5977
competitions_run += 1;
60-
println!("\rTesting pair {} out of {}", competitions_run, (NUMBER_OF_MODULES+1)*NUMBER_OF_MODULES/2);
61-
let modules1 = 1 << i;
62-
let modules2 = 1 << j;
78+
println!(
79+
"\rTesting pair {} out of {}",
80+
competitions_run,
81+
(NUMBER_OF_MODULES + 1) * NUMBER_OF_MODULES / 2
82+
);
83+
84+
// Analyze is useless in this scenario
85+
let modules1 = 1 << j;
86+
let modules1 = if modules1 == ANALYZE { 0 } else { modules1 };
87+
let modules2 = 1 << i;
88+
let modules2 = if modules2 == ANALYZE { 0 } else { modules2 };
6389

64-
let result = do_competition(modules1, modules2, time_per_move1, time_per_move2, game_pairs).await;
90+
let result = do_competition(
91+
modules1,
92+
modules2,
93+
time_per_move1,
94+
time_per_move2,
95+
game_pairs,
96+
)
97+
.await;
6598

66-
let mut output: String = "\nAlgo 1 modules: ".to_owned() + &modules_to_string(modules1) + "\nAlgo 2 modules: " + &modules_to_string(modules2)
67-
+ &"\nGame pairs: " + &game_pairs.to_string() + &"\nTime per move, algo1: " + &format!("{:?}", time_per_move1) + "\nTime per move, algo2: " + &format!("{:?}", time_per_move2);
68-
let result = format!("\nCompetition results: {:#?}", &result);
69-
output = output + &result + &"\n\n\n";
99+
dp[i][j] = Some(result);
100+
let output: String =
101+
format!("{}\t", result.algo1_wins as i64 - result.algo2_wins as i64);
70102
let buf = output.as_bytes();
71-
let _ = write_result(buf, "./output.txt");
103+
let _ = write_result(buf, "./output.txt");
72104
}
105+
let _ = write_result("\n".as_bytes(), "./output.txt");
73106
}
74107
}
75-
76108
}
77109

78-
async fn do_competition(modules1: u32, modules2: u32, time_per_move1: Duration, time_per_move2: Duration, game_pairs: u32) -> CompetitionResults {
110+
async fn do_competition(
111+
modules1: u32,
112+
modules2: u32,
113+
time_per_move1: Duration,
114+
time_per_move2: Duration,
115+
game_pairs: u32,
116+
) -> CompetitionResults {
79117
let competition = Competition::new(
80118
Algorithm::new(modules1, time_per_move1),
81119
Algorithm::new(modules2, time_per_move2),
@@ -84,6 +122,5 @@ async fn do_competition(modules1: u32, modules2: u32, time_per_move1: Duration,
84122
// competition.analyze_algorithm_choices(|(game_info, _), _| {
85123
// game_info.outcome == GameOutcome::InconclusiveTooLong
86124
// });
87-
let results = competition.start_competition(game_pairs).await;
88-
results
125+
competition.start_competition(game_pairs).await
89126
}

src/modules/alpha_beta.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ pub(crate) fn calc_new(
1313
beta = beta.min(eval);
1414
}
1515
}
16-
return (alpha, beta);
16+
(alpha, beta)
1717
}

src/modules/analyze.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@ pub(crate) fn get_debug_data(
2626
let previous_best_move = previous_best_move.to_string();
2727
utils::vector_push_debug!(vec, previous_best_move);
2828
}
29-
return vec;
29+
vec
3030
}

src/modules/search_extensions.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@ pub fn calculate(
66
new_board: Board,
77
search_extensions: bool,
88
) -> u32 {
9-
let extend_by = if !search_extensions || num_extensions > 3 {
9+
if !search_extensions || num_extensions > 3 {
1010
0
1111
} else if num_legal_moves == 1 || new_board.checkers().popcnt() >= 2 {
1212
1
1313
} else {
1414
0
15-
};
16-
extend_by
15+
}
1716
}

src/modules/transposition_table.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub(crate) fn get_transposition_entry(
3838
) -> Option<TranspositionEntry> {
3939
let start = Instant::now();
4040

41-
let transposition_entry = transposition_table.get(&board).copied();
41+
let transposition_entry = transposition_table.get(board).copied();
4242

4343
let time_for_transposition_access = Instant::now() - start;
4444
stats.time_for_transposition_access += time_for_transposition_access;

0 commit comments

Comments
 (0)