Skip to content

Commit

Permalink
chore: cleaner code using derive macros
Browse files Browse the repository at this point in the history
  • Loading branch information
raklaptudirm committed Jun 22, 2024
1 parent 090cba9 commit a9ad268
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 52 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ edition = "2021"
[dependencies]
ataxx = { path = "ataxx" }
uxi = { path = "uxi" }
rand = "0.8.5"
derive-new = "0.5"
derive_more = "0.99.18"

[profile.release]
opt-level = 3
Expand Down
8 changes: 3 additions & 5 deletions src/mcts/params.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::f64::consts::SQRT_2;

use derive_new::new;

#[derive(Clone)]
pub struct Params {
cpuct: Param,
Expand All @@ -16,7 +18,7 @@ impl Params {
}
}

#[derive(Clone)]
#[derive(Clone, new)]
pub struct Param {
val: f64,

Expand All @@ -27,10 +29,6 @@ pub struct Param {
}

impl Param {
pub fn new(val: f64, min: f64, max: f64) -> Param {
Param { val, min, max }
}

#[allow(unused)]
pub fn set(&mut self, val: f64) {
self.val = val;
Expand Down
24 changes: 5 additions & 19 deletions src/mcts/tree/lru.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
//! the nodes of a Tree, allowing arbitrarily long searches as unused memory is
//! continuously freed by the cache.
use std::mem;
use std::ops::Deref;
use std::ops::DerefMut;

use derive_more::{Deref, DerefMut};

use super::Node;

Expand Down Expand Up @@ -160,25 +160,11 @@ impl Cache {

/// Entry is one of the entries in the LRU [Cache]. Externally, it is mainly
/// used by dereferencing it into a [Node] instead of directly using it.
#[derive(Clone)]
#[derive(Clone, Deref, DerefMut)]
pub struct Entry {
#[deref]
#[deref_mut]
val: Node,
prev: i32,
next: i32,
}

impl Deref for Entry {
type Target = Node;

/// Entry can be dereferenced into its Node value.
fn deref(&self) -> &Self::Target {
&self.val
}
}

impl DerefMut for Entry {
/// Entry can be mutably dereferenced into its Node value.
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.val
}
}
37 changes: 10 additions & 27 deletions src/mcts/tree/node.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
use ataxx::MoveStore;
use derive_new::new;

use super::super::policy;
use std::slice;

pub type NodePtr = i32;
pub type Score = f64;

#[derive(Clone)]
#[derive(Clone, new)]
pub struct Node {
#[new(value = "Edges::new()")]
pub edges: Edges,

pub parent_node: NodePtr,
pub parent_edge: EdgePtr,
}

impl Node {
pub fn new(parent_node: NodePtr, parent_edge: EdgePtr) -> Node {
Node {
edges: Edges::new(),
parent_node,
parent_edge,
}
}

pub fn expand(&mut self, position: &ataxx::Position, policy: policy::Fn) {
position.generate_moves_into(&mut self.edges);

Expand Down Expand Up @@ -50,16 +44,13 @@ impl Node {
self.edges.len() > 0
}
}
#[derive(Clone)]
#[derive(Clone, new)]
pub struct Edges {
#[new(value = "vec![]")]
edges: Vec<Edge>,
}

impl Edges {
pub fn new() -> Self {
Edges { edges: vec![] }
}

pub fn iter(&self) -> slice::Iter<'_, Edge> {
self.edges.iter()
}
Expand Down Expand Up @@ -94,30 +85,22 @@ impl Default for Node {

pub type EdgePtr = i32;

#[derive(Clone)]
#[derive(Clone, new)]
pub struct Edge {
pub mov: ataxx::Move,
#[new(value = "-1")]
pub ptr: NodePtr,

#[new(value = "0")]
pub visits: usize,
#[new(value = "0.0")]
pub scores: Score,

#[new(value = "0.0")]
pub policy: f64,
}

impl Edge {
pub fn new(m: ataxx::Move) -> Edge {
Edge {
mov: m,
ptr: -1,

visits: 0,
scores: 0.0,

policy: 0.0,
}
}

pub fn q(&self) -> f64 {
self.scores / self.visits.max(1) as f64
}
Expand Down

0 comments on commit a9ad268

Please sign in to comment.