1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
use crate::game::{Game, Playout, SingleWinner};
use crate::policies::{
    mcts::{BaseMCTSPolicy, MCTSTreeNode, WithMCTSPolicy},
    MultiplayerPolicyBuilder,
};
use crate::settings;

use async_trait::async_trait;
use std::f32;
use std::fmt;
use std::sync::{Arc, RwLock};

/* UCT */

/// UCT move information.
#[derive(Debug, Clone, Copy)]
pub struct UCTMoveInfo {
    /// Node value.
    pub Q: f32,
    /// Number of times visited.
    pub N_a: f32,
}

/// UCT node information.
#[derive(Debug, Clone, Copy)]
pub struct UCTNodeInfo {
    /// Number of times visited
    pub count: f32,
}

/// UCT policy description.
pub struct UCTPolicy_<G: Game> {
    color: G::Player,
    uct_weight: f32,
}

#[async_trait]
impl<G> BaseMCTSPolicy<G> for UCTPolicy_<G>
where
    G::Move: Send,
    G: super::MCTSGame + SingleWinner,
{
    type NodeInfo = UCTNodeInfo;
    type MoveInfo = UCTMoveInfo;
    type PlayoutInfo = bool;

    fn get_value(
        &self,
        board: &G,
        _action: &G::Move,
        node_info: &Self::NodeInfo,
        move_info: &Self::MoveInfo,
        exploration: bool,
    ) -> f32 {
        let move_cb_multiplier = if board.turn() == self.color { 1. } else { -1. };

        let N = node_info.count;
        let cb = self.uct_weight * (N.ln() / (move_info.N_a + 1.)).sqrt();
        let value = if exploration {
            move_info.Q + move_cb_multiplier * cb
        } else {
            move_info.N_a
        };

        move_cb_multiplier * value
    }

    fn default_node(&self, _board: &G) -> Self::NodeInfo {
        UCTNodeInfo { count: 0. }
    }

    fn default_move(&self, _board: &G, _action: &G::Move) -> Self::MoveInfo {
        UCTMoveInfo { Q: 0., N_a: 0. }
    }

    fn backpropagate(
        &mut self,
        leaf: Arc<RwLock<MCTSTreeNode<G, Self>>>,
        _history: &[G::Move],
        playout: Self::PlayoutInfo,
    ) {
        let z = if playout { 1. } else { 0. };

        let mut current_node = leaf;
        while current_node.read().unwrap().parent.is_some() {
            // extract child
            let (tree_pointer, action) = current_node
                .read()
                .unwrap()
                .parent
                .as_ref()
                .map(|(t, a)| (t.upgrade().unwrap(), *a))
                .unwrap();

            current_node = tree_pointer;

            /* Store standard statistics */
            let mut node = current_node.write().unwrap();
            node.info.node.count += 1.;

            let move_info = node.info.moves.get_mut(&action).unwrap();
            move_info.N_a += 1.;
            move_info.Q += (z - move_info.Q) / move_info.N_a;
        }
    }
    /*
    fn backpropagate(
        &self,
        _index: usize,
        info: &mut MCTSNode<G, Self>,
        action: &G::Move,
        history: &[G::Move],
        playout: &Self::PlayoutInfo,
    ) {
        let z = if *playout { 1. } else { 0. };
        info.node.count += 1.;
        let move_info = info.moves.get_mut(action).unwrap();
        move_info.N_a += 1.;
        move_info.Q += (z - move_info.Q) / move_info.N_a;
    }

    fn backpropagate_new_node(
        &self,
        node: &mut MCTSNode<G, Self>,
        history: &[G::Move],
        playout: &Self::PlayoutInfo,
    ) {
    }*/

    async fn simulate(&self, board: &G) -> <Self as BaseMCTSPolicy<G>>::PlayoutInfo {
        board.playout_board(self.color).await.0.winner() == Some(self.color)
    }
}

/// UCT policy as an MCTS policy.
pub type UCTPolicy<G> = WithMCTSPolicy<G, UCTPolicy_<G>>;

/// UCT policy builder.
type UCT = settings::UCT;

impl fmt::Display for UCT {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        writeln!(f, "UCT")?;
        writeln!(f, "|| uct_weight: {}", self.uct_weight)?;
        writeln!(f, "|| N_PLAYOUT: {}", self.playouts)
    }
}

impl<G> MultiplayerPolicyBuilder<G> for UCT
where
    G::Move: Send,
    G::Player: Send,
    G: super::MCTSGame + SingleWinner,
{
    type P = UCTPolicy<G>;

    fn create(&self, color: G::Player) -> Self::P {
        WithMCTSPolicy::new(
            UCTPolicy_ {
                color,
                uct_weight: self.uct_weight,
            },
            self.playouts,
        )
    }
}