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

Optimize repetition detection #231

Closed
wants to merge 2 commits into from
Closed
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
4 changes: 2 additions & 2 deletions .github/workflows/gauntlet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ jobs:
env:
CURRENT_BRANCH: ${{ github.head_ref }}
run: |
./gauntlet.sh master 1 4 256 100 || echo "GAUNTLET_FAILED=1" >> $GITHUB_ENV
./gauntlet.sh master 1 4 256 150 || echo "GAUNTLET_FAILED=1" >> $GITHUB_ENV

- uses: mshick/add-pr-comment@v2
with:
Expand Down Expand Up @@ -126,7 +126,7 @@ jobs:
env:
CURRENT_BRANCH: ${{ github.head_ref }}
run: |
./gauntlet.sh $(git describe --tags --abbrev=0) 1 4 256 100 || echo "GAUNTLET_FAILED=1" >> $GITHUB_ENV
./gauntlet.sh $(git describe --tags --abbrev=0) 1 4 256 150 || echo "GAUNTLET_FAILED=1" >> $GITHUB_ENV

- uses: mshick/add-pr-comment@v2
with:
Expand Down
2 changes: 1 addition & 1 deletion gauntlet.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ readonly UPSTREAM=${1:-"master"}
readonly CONCURRENCY_GAMES=${2:-"4"}
readonly ENGINE_THREADS=${3:-"1"}
readonly ENGINE_HASH=${4:-"64"}
readonly ROUNDS=${5:-"300"}
readonly ROUNDS=${5:-"500"}
readonly TIME_CONTROL=${6:-"5+0.2"}

function run_gauntlet {
Expand Down
5 changes: 4 additions & 1 deletion src/engine/commands/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ pub fn execute_position(new_position: &Position, game_history: &[Position], engi
engine.position = *new_position;
engine.game_history = game_history
.iter()
.map(|position| HistoryEntry { hash: position.zobrist_hash(), reversible: true })
.map(|position| HistoryEntry {
board_hash: position.board.zobrist_hash(),
reversible: true,
})
.collect();
}

Expand Down
24 changes: 11 additions & 13 deletions src/search/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,31 @@ use crate::position::{board::ZobristHash, Position};

#[derive(Debug, Copy, Clone)]
pub struct HistoryEntry {
pub hash: ZobristHash,
pub board_hash: ZobristHash,
pub reversible: bool,
}

pub struct BranchHistory(pub Vec<HistoryEntry>);

impl BranchHistory {
pub fn visit_position(&mut self, position: &Position, reversible: bool) {
self.0.push(HistoryEntry { hash: position.zobrist_hash(), reversible });
self.0.push(HistoryEntry { board_hash: position.board.zobrist_hash(), reversible });
}

pub fn leave_position(&mut self) {
self.0.pop();
}

pub fn repeated(&self, position: &Position) -> u8 {
let mut count = 0;
let hash = position.zobrist_hash();
for entry in self.0.iter().rev() {
if entry.hash == hash {
count += 1;
}
if !entry.reversible {
break;
}
}
count
let board_hash = position.board.zobrist_hash();
self.0
.iter()
.rev()
.skip_while(|entry| entry.board_hash != board_hash) // Skip until we find the current position.
.step_by(2) // We can only repeat when it is our turn to move.
.take_while(|entry| entry.reversible || entry.board_hash == board_hash)
.filter(|entry| entry.board_hash == board_hash)
.count() as u8
}
}

Expand Down
Loading