From add6c7ec73f0b809e720365f3fd7a3b4a17ed1a1 Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Tue, 25 Jan 2022 14:36:33 +0100 Subject: [PATCH] pain --- src/basic_search.rs | 2 ++ src/lib.rs | 44 ++++++++++++++++++++++++++++---------------- 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/src/basic_search.rs b/src/basic_search.rs index 3311ee8..a1353eb 100644 --- a/src/basic_search.rs +++ b/src/basic_search.rs @@ -1,3 +1,5 @@ +#![allow(dead_code)] + use std::collections::VecDeque; struct Node { diff --git a/src/lib.rs b/src/lib.rs index 8703cf6..5c38c27 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,27 +1,23 @@ //! https://www.baeldung.com/java-monte-carlo-tree-search -#![allow(dead_code)] - mod basic_search; +pub use mcts::find_next_move; + pub trait GameState { fn points(&self) -> i32; - fn next(&self) -> &[Self] - where - Self: Sized; + fn next_states(&self) -> Box>; fn is_finished(&self) -> bool; } mod mcts { use crate::GameState; - use bumpalo::collections::Vec; + use bumpalo::collections::Vec as BumpVec; use bumpalo::Bump; use rand::Rng; - use std::cell::{Cell, RefCell}; - - type Tree<'tree, S> = RefCell>; + use std::cell::Cell; #[derive(Clone)] struct Node<'tree, S> { @@ -29,11 +25,9 @@ mod mcts { visited: Cell, score: Cell, parent: Option<&'tree Node<'tree, S>>, - children: Vec<'tree, Node<'tree, S>>, + children: BumpVec<'tree, Node<'tree, S>>, } - type RefNode<'tree, S> = RefCell>; - impl<'tree, S> Node<'tree, S> { fn new(state: S, alloc: &'tree Bump) -> Node { Node { @@ -41,7 +35,7 @@ mod mcts { visited: Cell::new(0), score: Cell::new(0), parent: None, - children: Vec::new_in(alloc), + children: BumpVec::new_in(alloc), } } @@ -69,7 +63,7 @@ mod mcts { let promising_node = select_promising_node(root_node); if !promising_node.state.is_finished() { - expand_node(promising_node); + expand_node(&alloc, promising_node); } if !promising_node.children.is_empty() { @@ -98,8 +92,26 @@ mod mcts { node } - fn expand_node(_node: &Node<'_, S>) { - todo!("next") + fn expand_node(alloc: &Bump, node: &Node<'_, S>) { + /* + List possibleStates = node.getState().getAllPossibleStates(); + possibleStates.forEach(state -> { + Node newNode = new Node(state); + newNode.setParent(node); + newNode.getState().setPlayerNo(node.getState().getOpponent()); + node.getChildArray().add(newNode); + }); + */ + let possible_states = node.state.next_states(); + for state in possible_states { + let child = Node { + state, + visited: Cell::new(0), + score: Cell::new(0), + parent: Some(node), + children: BumpVec::new_in(alloc), + }; + } } fn simulate_random_playout(_node: &Node<'_, S>) -> u64 {