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
167
168
169
//! C++ Bindings for libconnect6

extern crate connect6;
extern crate rand;

pub mod cppbind;
pub use cppbind::ffi_test::*;

/// Return Connect6 playing results with given cpp callback.
///
/// # Arguments
///
/// * `callback` - callback for cpp_policy, void(float* boards, int* result).
/// * `cpp_alloc_path` - cppbind::RawPath allocator for obtaining memory from cpp ffi.
/// * `cpp_alloc_result` - cppbind::RawPlayResult allocator for obtaining memory from cpp ffi.
/// * `debug` - bool, enable debug mode. if enable, selection and board status will be printed
/// * `num_game_thread` - i32, number of threads asynchronously self-playing connect6
///
#[no_mangle]
pub extern "C" fn cpp_play(
    callback: cppbind::PolicyCallback,
    cpp_alloc_path: cppbind::AllocatorType<cppbind::RawPath>,
    cpp_alloc_result: cppbind::AllocatorType<cppbind::RawPlayResult>,
    debug: bool,
    num_game_thread: i32,
) -> cppbind::RawVec<cppbind::RawPlayResult> {
    use connect6::agent;

    let alloc_path = cppbind::Allocator::new(cpp_alloc_path);
    let alloc_result = cppbind::Allocator::new(cpp_alloc_result);

    let raw_result = if num_game_thread == 1 {
        let mut cpp_policy = cppbind::CppPolicy::new(callback);
        let mut agent = if debug {
            agent::Agent::debug(&mut cpp_policy)
        } else {
            agent::Agent::new(&mut cpp_policy)
        };

        let result = agent.play().unwrap();
        vec![cppbind::RawPlayResult::with_result(&result, &alloc_path)]
    } else {
        let policy_gen = || cppbind::CppPolicy::new(callback);
        let agent = if debug {
            agent::AsyncAgent::debug(policy_gen)
        } else {
            agent::AsyncAgent::new(policy_gen)
        };

        agent
            .run(num_game_thread)
            .iter()
            .map(|x| cppbind::RawPlayResult::with_result(x, &alloc_path))
            .collect::<Vec<_>>()
    };

    cppbind::RawVec::with_vec(raw_result, &alloc_result)
}

/// Return Connect6 self-playing results with given cpp callback and hyperparameters
///
/// # Arguments
///
/// * `callback` - callback for cppbind, void(int player, float* values, float* boards, int length).
/// * `cpp_alloc_path` - cppbind::RawPath allocator for obtaining memory from cpp ffi.
/// * `cpp_alloc_result` - cppbind::RawPlayResult allocator for obtaining memory from cpp ffi.
/// * `num_simulation` - i32, number of simulations for each turn.
/// * `epsilon` - f32, ratio for applying exploit, exploration. lower epsilon, more exploit
/// * `dirichlet_alpha` - f64, hyperparameter for dirichlet distribution
/// * `c_puct` - f32, ratio of q-value and puct, hyperparameter of AlphaZero MCTS
/// * `debug` - bool, enable debug mode. if enable, selection and board status will be printed
/// * `num_game_thread` - i32, number of threads asynchronously self-playing connect6
///
#[no_mangle]
pub extern "C" fn cpp_self_play(
    callback: cppbind::Callback,
    cpp_alloc_path: cppbind::AllocatorType<cppbind::RawPath>,
    cpp_alloc_result: cppbind::AllocatorType<cppbind::RawPlayResult>,
    num_simulation: i32,
    epsilon: f32,
    dirichlet_alpha: f64,
    c_puct: f32,
    debug: bool,
    num_game_thread: i32,
) -> cppbind::RawVec<cppbind::RawPlayResult> {
    use connect6::{agent, policy};

    let param = policy::HyperParameter {
        num_simulation,
        epsilon,
        dirichlet_alpha,
        c_puct,
    };

    let alloc_path = cppbind::Allocator::new(cpp_alloc_path);
    let alloc_result = cppbind::Allocator::new(cpp_alloc_result);

    let raw_result = if num_game_thread == 1 {
        let cppeval = Box::new(cppbind::CppEval::new(callback));
        let mut alphazero = policy::AlphaZero::with_param(cppeval, param);
        let mut agent = if debug {
            agent::Agent::debug(&mut alphazero)
        } else {
            agent::Agent::new(&mut alphazero)
        };

        let result = agent.play().unwrap();
        vec![cppbind::RawPlayResult::with_result(&result, &alloc_path)]
    } else {
        let policy_gen =
            || policy::AlphaZero::with_param(Box::new(cppbind::CppEval::new(callback)), param);
        let async_agent = if debug {
            agent::AsyncAgent::debug(policy_gen)
        } else {
            agent::AsyncAgent::new(policy_gen)
        };

        async_agent
            .run(num_game_thread)
            .iter()
            .map(|x| cppbind::RawPlayResult::with_result(x, &alloc_path))
            .collect::<Vec<_>>()
    };

    cppbind::RawVec::with_vec(raw_result, &alloc_result)
}

/// Returns Connect6 results with given cpp policy and user selection as io_policy
///
/// # Arguments
///
/// * `callback` - callback for cppbind, void(int player, float* values, float* boards, int length).
/// * `cpp_alloc_path` - cppbind::RawPath allocator for obtaining memory from cpp ffi.
/// * `num_simulation` - i32, number of simulations for each turn.
/// * `epsilon` - f32, ratio for applying exploit, exploration. lower epsilon, more exploit.
/// * `dirichlet_alpha` - f64, hyperparameter for dirichlet distribution.
/// * `c_puct` - f32, ratio of q-value and puct, hyperparameter of AlphaZero MCTS.
///
#[no_mangle]
pub extern "C" fn cpp_play_with(
    callback: cppbind::Callback,
    cpp_alloc_path: cppbind::AllocatorType<cppbind::RawPath>,
    num_simulation: i32,
    epsilon: f32,
    dirichlet_alpha: f64,
    c_puct: f32,
) -> cppbind::RawPlayResult {
    use connect6::{agent, policy};

    let param = policy::HyperParameter {
        num_simulation,
        epsilon,
        dirichlet_alpha,
        c_puct,
    };

    let cppeval = Box::new(cppbind::CppEval::new(callback));
    let mut cpp_policy = policy::AlphaZero::with_param(cppeval, param);

    let mut stdin = std::io::stdin();
    let mut stdout = std::io::stdout();
    let mut io_policy = policy::IoPolicy::new(&mut stdin, &mut stdout);

    let mut multi_policy = policy::MultiPolicy::new(&mut cpp_policy, &mut io_policy);
    let result = agent::Agent::debug(&mut multi_policy).play();

    let alloc_path = cppbind::Allocator::new(cpp_alloc_path);
    cppbind::RawPlayResult::with_result(&result.unwrap(), &alloc_path)
}