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

Depth 2 temperature #64

Merged
merged 1 commit into from
Oct 11, 2024
Merged
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
16 changes: 13 additions & 3 deletions src/mcts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,19 @@ impl<'a> Searcher<'a> {

// relabel root policies with root PST value
if self.tree[node].has_children() {
self.tree[node].relabel_policy(&self.root_position, self.params, self.policy);
self.tree[node].relabel_policy(&self.root_position, self.params, self.policy, 1);

for action in &*self.tree[node].actions() {
if action.ptr().is_null() || !self.tree[action.ptr()].has_children() {
continue;
}

let mut position = self.root_position.clone();
position.make_move(Move::from(action.mov()));
self.tree[action.ptr()].relabel_policy(&position, self.params, self.policy, 2);
}
} else {
self.tree[node].expand::<true>(&self.root_position, self.params, self.policy);
self.tree[node].expand(&self.root_position, self.params, self.policy, 1);
}

let search_stats = SearchStats::default();
Expand Down Expand Up @@ -310,7 +320,7 @@ impl<'a> Searcher<'a> {
} else {
// expand node on the second visit
if self.tree[ptr].is_not_expanded() {
self.tree[ptr].expand::<false>(pos, self.params, self.policy);
self.tree[ptr].expand(pos, self.params, self.policy, *depth);
}

// select action to take via PUCT
Expand Down
1 change: 1 addition & 0 deletions src/mcts/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ macro_rules! make_mcts_params {

make_mcts_params! {
root_pst: f32 = 3.64, 1.0, 10.0, 0.4, 0.002;
depth_2_pst: f32 = 1.2, 1.0, 10.0, 0.4, 0.002;
root_cpuct: f32 = 0.314, 0.1, 5.0, 0.065, 0.002;
cpuct: f32 = 0.314, 0.1, 5.0, 0.065, 0.002;
cpuct_var_weight: f32 = 0.851, 0.0, 2.0, 0.085, 0.002;
Expand Down
33 changes: 25 additions & 8 deletions src/tree/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,12 @@ impl Node {
self.set_gini_impurity(0.0);
}

pub fn expand<const ROOT: bool>(
pub fn expand(
&self,
pos: &ChessState,
params: &MctsParams,
policy: &PolicyNetwork,
depth: usize,
) {
let mut actions = self.actions_mut();

Expand All @@ -121,16 +122,19 @@ impl Node {
max = max.max(policy);
});

let pst = match depth {
0 => unreachable!(),
1 => params.root_pst(),
2 => params.depth_2_pst(),
3.. => 1.0,
};

let mut total = 0.0;

for action in actions.iter_mut() {
let mut policy = f32::from_bits(action.ptr().inner());

policy = if ROOT {
((policy - max) / params.root_pst()).exp()
} else {
(policy - max).exp()
};
policy = ((policy - max) / pst).exp();

action.set_ptr(NodePtr::from_raw(f32::to_bits(policy)));

Expand All @@ -150,7 +154,13 @@ impl Node {
self.set_gini_impurity(gini_impurity);
}

pub fn relabel_policy(&self, pos: &ChessState, params: &MctsParams, policy: &PolicyNetwork) {
pub fn relabel_policy(
&self,
pos: &ChessState,
params: &MctsParams,
policy: &PolicyNetwork,
depth: u8,
) {
let feats = pos.get_policy_feats();
let mut max = f32::NEG_INFINITY;

Expand All @@ -163,10 +173,17 @@ impl Node {
max = max.max(policy);
}

let pst = match depth {
0 => unreachable!(),
1 => params.root_pst(),
2 => params.depth_2_pst(),
3.. => unreachable!(),
};

let mut total = 0.0;

for policy in &mut policies {
*policy = ((*policy - max) / params.root_pst()).exp();
*policy = ((*policy - max) / pst).exp();
total += *policy;
}

Expand Down
Loading