Skip to content

Commit aad02f0

Browse files
committed
Now prints results to file instead of terminal
Also added som preliminary parts to begin with a bigger test.
1 parent 5652190 commit aad02f0

File tree

5 files changed

+108
-21
lines changed

5 files changed

+108
-21
lines changed

Cargo.lock

+7-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/algorithms/the_algorithm.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::collections::HashMap;
22

33
use chess::{Action, BitBoard, Board, BoardStatus, ChessMove, Color, MoveGen, Piece};
4+
use serde::{Deserialize, Serialize};
45
use tokio::time::{Duration, Instant};
56

67
use crate::algorithms::{draw_checker, eval};
@@ -13,6 +14,7 @@ use crate::modules::{alpha_beta, analyze};
1314

1415
use super::utils::Evaluation;
1516

17+
1618
#[derive(Clone, Debug)]
1719
pub(crate) struct Algorithm {
1820
pub(crate) modules: u32,

src/io.rs

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use std::{fs::OpenOptions, io::{self, prelude::*}};
2+
3+
pub(crate) fn write_result(buf: &[u8], file: &str) -> std::io::Result<()> {
4+
let mut file = OpenOptions::new().write(true).create(true).append(true).open(file).unwrap();
5+
file.write(buf)?;
6+
Ok(())
7+
}
8+
9+
pub(crate)fn modules_to_string(modules: u32) -> String {
10+
let mut start: bool = true;
11+
let mut output: String = "".to_string();
12+
//For loop ranges is all available modules index. +1 because we need inclusive.
13+
for i in 0..9+1 {
14+
if (modules & (1 << i)).count_ones() == 1 {
15+
let module_string = match i {
16+
0 => "ANALYZE",
17+
1 => "ALPHA_BETA",
18+
2 => "TRANSPOSITION_TABLE",
19+
3 => "SEARCH_EXTENSIONS",
20+
4 => "SQUARE_CONTROL_METRIC",
21+
5 => "SKIP_BAD_MOVES",
22+
6 => "NAIVE_PSQT",
23+
7 => "PAWN_STRUCTURE",
24+
8 => "TAPERED_EVERY_PESTO_PSQT",
25+
9 => "TAPERED_INCREMENTAL_PESTO_PSQT",
26+
_ => "INVALID MODULE DETECTED"
27+
};
28+
if !start {
29+
output = output + &", " + module_string;
30+
} else {
31+
start = false;
32+
output = module_string.to_string();
33+
}
34+
}
35+
}
36+
output
37+
}

src/main.rs

+43-9
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,66 @@
11
use std::time::Duration;
22

3+
34
use crate::algorithms::the_algorithm::Algorithm;
45
#[allow(unused_imports)]
56
use crate::common::constants::modules::{
67
ALPHA_BETA, ANALYZE, NAIVE_PSQT, PAWN_STRUCTURE, SEARCH_EXTENSIONS, SKIP_BAD_MOVES,
78
SQUARE_CONTROL_METRIC, TAPERED_EVERY_PESTO_PSQT, TAPERED_INCREMENTAL_PESTO_PSQT,
89
TRANSPOSITION_TABLE,
910
};
11+
use crate::io::{write_result, modules_to_string};
1012

11-
use self::pitter::logic::Competition;
13+
use self::pitter::logic::{Competition, CompetitionResults};
1214

1315
mod algorithms;
1416
mod common;
1517
mod modules;
1618
mod pitter;
19+
mod io;
1720

1821
//If we should print the moves played and results of each game.
1922
pub(crate) const PRINT_GAME: bool = false;
2023

2124
#[tokio::main]
2225
async fn main() {
23-
//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
24-
//Put 0 for no modules.
25-
let modules1 = ALPHA_BETA | TAPERED_EVERY_PESTO_PSQT;
26-
let modules2 = ALPHA_BETA;
27-
let time_per_move1 = Duration::from_micros(4000);
28-
let time_per_move2 = Duration::from_micros(2000);
26+
if !TEST_SEVERAL {
27+
//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
28+
//Put 0 for no modules.
29+
//Setup modules
30+
let modules1 = ALPHA_BETA | TAPERED_INCREMENTAL_PESTO_PSQT;
31+
let modules2 = ALPHA_BETA | TAPERED_EVERY_PESTO_PSQT;
32+
let time_per_move1 = Duration::from_micros(2000);
33+
let time_per_move2 = Duration::from_micros(2000);
34+
let game_pairs = 500;
35+
36+
//Run competition
37+
let result = do_competition(modules1, modules2, time_per_move1, time_per_move2, game_pairs).await;
38+
39+
//Save results to file
40+
let mut output: String = "Algo 1 modules: ".to_owned() + &modules_to_string(modules1) + "\nAlgo 2 modules: " + &modules_to_string(modules2);
41+
let result = format!("\nCompetition results: {:#?}", &result);
42+
output = output + &result;
43+
let buf = output.as_bytes();
44+
let _ = write_result(buf, "./output.txt");
45+
46+
} else {
47+
/*let time_per_move1 = Duration::from_micros(2000);
48+
let time_per_move2 = Duration::from_micros(2000);
49+
50+
let mut modules1 = 1;
51+
let mut modules2 = 1;
52+
53+
for i in 0..log2() {
54+
for j in 0.. {
55+
56+
}
57+
}*/
58+
}
59+
60+
}
2961

62+
const TEST_SEVERAL: bool = false;
63+
async fn do_competition(modules1: u32, modules2: u32, time_per_move1: Duration, time_per_move2: Duration, game_pairs: u32) -> CompetitionResults {
3064
let competition = Competition::new(
3165
Algorithm::new(modules1, time_per_move1),
3266
Algorithm::new(modules2, time_per_move2),
@@ -35,6 +69,6 @@ async fn main() {
3569
// competition.analyze_algorithm_choices(|(game_info, _), _| {
3670
// game_info.outcome == GameOutcome::InconclusiveTooLong
3771
// });
38-
let results = competition.start_competition(1000).await;
39-
dbg!(results);
72+
let results = competition.start_competition(game_pairs).await;
73+
results
4074
}

src/pitter/logic.rs

+19-5
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ use tokio::time::{Duration, Instant};
55
use chess::{Action, Board, Color, Game, GameResult};
66
use tokio::sync::Mutex;
77

8+
use serde::{Serialize, Deserialize};
9+
810
use crate::algorithms::the_algorithm::Algorithm;
911
use crate::common::constants::modules::ANALYZE;
1012
use crate::common::utils::{self, Stats};
13+
use crate::io::write_result;
1114
use crate::PRINT_GAME;
1215

1316
pub(crate) struct Competition {
@@ -62,7 +65,7 @@ pub(crate) enum GameOutcome {
6265
}
6366

6467
#[allow(unused_assignments)]
65-
#[derive(Default, Debug, Copy, Clone)]
68+
#[derive(Default, Debug, Copy, Clone, Serialize, Deserialize)]
6669
pub(crate) struct CompetitionResults {
6770
/// How many pairs of games that Algo1 wins from both positions
6871
algo1_wins: usize,
@@ -270,8 +273,19 @@ impl Competition {
270273
sum_stats.1 / sum_stats.1.num_plies,
271274
);
272275

273-
println!("Stats for algo1: {:#?}", avg_stats.0);
274-
println!("Stats for algo2: {:#?}", avg_stats.1);
276+
//I don't know why I have to do this middle step terribleness,
277+
//but for some reason it won't compile otherwise.
278+
let algo1stats = format!("\nStats for algo1: {:#?}", avg_stats.0);
279+
let algo1stats = algo1stats.as_bytes();
280+
let algo2stats = format!("Stats for algo2: {:#?}", avg_stats.1);
281+
let algo2stats = algo2stats.as_bytes();
282+
283+
//Marking that the result from these calls won't be used.
284+
let _ = crate::io::write_result(algo1stats, "./output.txt");
285+
let _ = crate::io::write_result(algo2stats, "./output.txt");
286+
287+
//println!("Stats for algo1: {:#?}", avg_stats.0);
288+
//println!("Stats for algo2: {:#?}", avg_stats.1);
275289

276290
// Gives E0597 otherwise
277291
#[allow(clippy::let_and_return)]
@@ -359,7 +373,7 @@ impl Competition {
359373
i += 1;
360374
}
361375

362-
dbg!(&self.algo1.board_played_times.values());
363-
dbg!(&self.algo2.board_played_times.values());
376+
/*let _ = write_result(stringify!(&self.algo1.board_played_times.values()).as_bytes(), "./output.txt");
377+
let _ = write_result(stringify!(&self.algo2.board_played_times.values()).as_bytes(), "./output.txt");*/
364378
}
365379
}

0 commit comments

Comments
 (0)