summaryrefslogtreecommitdiff
path: root/src/strategy/monte_carlo_tree.rs
blob: b168bcd4005eb4a5da59d09a2dfa946cfe30d439 (plain)
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
use engine::command::*;
use engine::status::GameStatus;
use engine::bitwise_engine::{Player, BitwiseGameState};
use engine::constants::*;
use engine::geometry::*;

use rand::{Rng, XorShiftRng, SeedableRng};
use time::{Duration, PreciseTime};


enum SearchTree {
    Leaf(NodeStats),
    FullyExploredNode(FullyExploredStats),
    PartiallyExploredNode(PartiallyExploredStats)
}

struct NodeStats {
    wins: u32,
    attempts: u32
}

struct FullyExploredStats {
    wins: u32,
    attempts: u32,
    explored: Vec<(Command, SearchTree)>
}

struct PartiallyExploredStats {
    wins: u32,
    attempts: u32,
    explored: Vec<(Command, SearchTree)>,
    unexplored: Vec<Command>
}

impl SearchTree {
    fn create_node(state: &Player) -> SearchTree {
        SearchTree::PartiallyExploredNode(PartiallyExploredStats {
            wins: 0,
            attempts: 0,
            explored: Vec::new(),
            unexplored: Vec::new() //TODO
        })
    }
}

impl FullyExploredStats {
    fn node_with_highest_ucb<'a>(&'a self) -> &'a (Command, SearchTree) {
        //TODO
        &self.explored[0]
    }
}

impl PartiallyExploredStats {
    fn add_node(&mut self, command: Command) {
        //TODO
    }
}


pub fn choose_move(state: &BitwiseGameState, start_time: PreciseTime, max_time: Duration) -> Command {
    use self::SearchTree::*;
    
    // create root node as partially explored node
    // creating a new node needs to populate all (valid) unexplored moves

    let root = SearchTree::create_node(&state.player);

    loop {
        //tree_search(&state, &mut root);
    }
    
    Command::Nothing    
}

/*
fn tree_search(state: &BitwiseGameState, tree: &mut SearchTree) -> bool {
    match tree {
        Leaf(stats) => {
            // ???
            false
        },
        FullyExploredNode(stats) => {
            let (next_command, next_tree) = stats.node_with_highest_ucb();
            tree_search(state, &mut next_tree)
            // TODO: Swap players?
            // TODO: Back-propagation?
        },
        PartiallyExploredNode(stats) => {
            //   choose random command and add as partially explored node to the tree
            //   simulate to end with random commands
            //   back-propagate (remember to keep a stack of commands to that point node)
            //   convert to fully explored if applicable
        }
    }
    
}
*/