bandit_machines 0.7.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +95 -0
- data/Cargo.lock +321 -0
- data/Cargo.toml +29 -0
- data/LICENSE.txt +21 -0
- data/README.md +367 -0
- data/ext/bandit_machines_native/core/CHANGELOG.md +70 -0
- data/ext/bandit_machines_native/core/Cargo.toml +21 -0
- data/ext/bandit_machines_native/core/README.md +152 -0
- data/ext/bandit_machines_native/core/src/bandit.rs +576 -0
- data/ext/bandit_machines_native/core/src/lib.rs +286 -0
- data/ext/bandit_machines_native/core/src/math.rs +66 -0
- data/ext/bandit_machines_native/core/src/pick.rs +71 -0
- data/ext/bandit_machines_native/extconf.rb +2 -0
- data/ext/bandit_machines_native/ffi/Cargo.toml +21 -0
- data/ext/bandit_machines_native/ffi/extconf.rb +14 -0
- data/ext/bandit_machines_native/ffi/src/convert.rs +238 -0
- data/ext/bandit_machines_native/ffi/src/init.rs +128 -0
- data/ext/bandit_machines_native/ffi/src/lib.rs +246 -0
- data/lib/bandit_machines/version.rb +5 -0
- data/lib/bandit_machines.rb +74 -0
- data/sig/bandit_machines.rbs +157 -0
- metadata +87 -0
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
#![no_std]
|
|
2
|
+
#![cfg_attr(not(test), deny(clippy::indexing_slicing))]
|
|
3
|
+
#![doc = include_str!("../README.md")]
|
|
4
|
+
|
|
5
|
+
mod bandit;
|
|
6
|
+
pub mod math;
|
|
7
|
+
mod pick;
|
|
8
|
+
|
|
9
|
+
pub use bandit::Bandit;
|
|
10
|
+
|
|
11
|
+
/// How arms that score exactly the same are told apart.
|
|
12
|
+
///
|
|
13
|
+
/// Applies to cold-start sweeps, UCB1 indices and greedy means alike. Scores
|
|
14
|
+
/// are compared as integers, so "the same" is exact and platform independent.
|
|
15
|
+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
16
|
+
pub enum TieBreak {
|
|
17
|
+
/// The tied arm with the lowest index.
|
|
18
|
+
LowestIndex,
|
|
19
|
+
/// The tied arm with the highest index.
|
|
20
|
+
HighestIndex,
|
|
21
|
+
/// The tied arm with the fewest tries (rewards plus outstanding tickets),
|
|
22
|
+
/// then the lowest index.
|
|
23
|
+
LeastTried,
|
|
24
|
+
/// Uniform among the tied arms, using the caller's `pick` draw.
|
|
25
|
+
Draw,
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/// Reward range, warming prior and tie rule shared by every arm.
|
|
29
|
+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
30
|
+
pub struct Config {
|
|
31
|
+
max_reward: u32,
|
|
32
|
+
prior: u32,
|
|
33
|
+
tie_break: TieBreak,
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
impl Config {
|
|
37
|
+
/// Rewards are integers in `0..=max_reward`; `prior` is the mean assumed
|
|
38
|
+
/// for an arm that has outstanding tickets but no reward yet.
|
|
39
|
+
///
|
|
40
|
+
/// `None` when `max_reward` is zero or `prior` exceeds it.
|
|
41
|
+
#[must_use]
|
|
42
|
+
pub const fn new(max_reward: u32, prior: u32, tie_break: TieBreak) -> Option<Self> {
|
|
43
|
+
if max_reward == 0 || prior > max_reward {
|
|
44
|
+
return None;
|
|
45
|
+
}
|
|
46
|
+
Some(Self {
|
|
47
|
+
max_reward,
|
|
48
|
+
prior,
|
|
49
|
+
tie_break,
|
|
50
|
+
})
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/// Largest reward one ticket may report.
|
|
54
|
+
#[must_use]
|
|
55
|
+
pub const fn max_reward(&self) -> u32 {
|
|
56
|
+
self.max_reward
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/// Mean assumed for an arm with tickets out and no reward yet.
|
|
60
|
+
#[must_use]
|
|
61
|
+
pub const fn prior(&self) -> u32 {
|
|
62
|
+
self.prior
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/// Rule separating exactly tied arms.
|
|
66
|
+
#[must_use]
|
|
67
|
+
pub const fn tie_break(&self) -> TieBreak {
|
|
68
|
+
self.tie_break
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/// A fraction in `0..=1`, written `numerator / denominator`.
|
|
73
|
+
///
|
|
74
|
+
/// Used as the exploration probability of epsilon-greedy and as the retention
|
|
75
|
+
/// factor of a decay.
|
|
76
|
+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
77
|
+
pub struct Ratio {
|
|
78
|
+
numerator: u32,
|
|
79
|
+
denominator: u32,
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
impl Ratio {
|
|
83
|
+
/// Zero: never explore, or forget everything.
|
|
84
|
+
pub const ZERO: Self = Self::raw(0, 1);
|
|
85
|
+
|
|
86
|
+
/// One: always explore, or keep everything.
|
|
87
|
+
pub const ONE: Self = Self::raw(1, 1);
|
|
88
|
+
|
|
89
|
+
/// `None` when `denominator` is zero or `numerator` exceeds it.
|
|
90
|
+
#[must_use]
|
|
91
|
+
pub const fn new(numerator: u32, denominator: u32) -> Option<Self> {
|
|
92
|
+
if denominator == 0 || numerator > denominator {
|
|
93
|
+
return None;
|
|
94
|
+
}
|
|
95
|
+
Some(Self::raw(numerator, denominator))
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/// Top of the fraction.
|
|
99
|
+
#[must_use]
|
|
100
|
+
pub const fn numerator(&self) -> u32 {
|
|
101
|
+
self.numerator
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/// Bottom of the fraction; never zero.
|
|
105
|
+
#[must_use]
|
|
106
|
+
pub const fn denominator(&self) -> u32 {
|
|
107
|
+
self.denominator
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const fn raw(numerator: u32, denominator: u32) -> Self {
|
|
111
|
+
Self {
|
|
112
|
+
numerator,
|
|
113
|
+
denominator,
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/// The two uniform `u64` draws one epsilon-greedy selection may consume.
|
|
119
|
+
///
|
|
120
|
+
/// The bandit holds no generator. `gate` decides explore versus exploit;
|
|
121
|
+
/// `pick` chooses the explored arm, or breaks a tie under [`TieBreak::Draw`].
|
|
122
|
+
/// A draw `d` is mapped onto `0..n` as `floor(d * n / 2^64)`, which is uniform
|
|
123
|
+
/// to within `n / 2^64` when `d` is uniform over all of `u64`.
|
|
124
|
+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
125
|
+
pub struct Draws {
|
|
126
|
+
/// Explore when `floor(gate * denominator / 2^64) < numerator`.
|
|
127
|
+
pub gate: u64,
|
|
128
|
+
/// Index draw for the explored arm or for a drawn tie.
|
|
129
|
+
pub pick: u64,
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/// Why a selection landed on its arm.
|
|
133
|
+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
134
|
+
pub enum Reason {
|
|
135
|
+
/// The arm had no rewards and no outstanding tickets.
|
|
136
|
+
ColdStart,
|
|
137
|
+
/// Epsilon-greedy explored: uniform over the open arms.
|
|
138
|
+
Explore,
|
|
139
|
+
/// Epsilon-greedy exploited: the highest mean.
|
|
140
|
+
Exploit,
|
|
141
|
+
/// UCB1: the highest upper confidence index.
|
|
142
|
+
UpperBound,
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/// Why a call was refused. A refused call changes nothing.
|
|
146
|
+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
147
|
+
pub enum Refusal {
|
|
148
|
+
/// The arm index is not below the compiled arm capacity.
|
|
149
|
+
UnknownArm,
|
|
150
|
+
/// No arm is open for selection.
|
|
151
|
+
NoOpenArm,
|
|
152
|
+
/// Every ticket slot holds an outstanding selection.
|
|
153
|
+
TicketsFull,
|
|
154
|
+
/// Every ticket serial of this bandit's scope has been issued, or an arm
|
|
155
|
+
/// has no generation left.
|
|
156
|
+
Exhausted,
|
|
157
|
+
/// The ticket's arm was reset after the selection was made.
|
|
158
|
+
StaleTicket,
|
|
159
|
+
/// The ticket names nothing here: it was already rewarded, abandoned or
|
|
160
|
+
/// revoked, or this bandit never issued it (another scope). Also the
|
|
161
|
+
/// answer for a serial of another scope.
|
|
162
|
+
UnknownTicket,
|
|
163
|
+
/// The reward is above the configured maximum.
|
|
164
|
+
RewardOutOfRange,
|
|
165
|
+
/// The arm's reward count or reward sum would overflow; decay first.
|
|
166
|
+
Saturated,
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
impl core::fmt::Display for Refusal {
|
|
170
|
+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
|
171
|
+
f.write_str(match self {
|
|
172
|
+
Self::UnknownArm => "the arm index is not below the arm capacity",
|
|
173
|
+
Self::NoOpenArm => "no arm is open for selection",
|
|
174
|
+
Self::TicketsFull => "every ticket slot holds an outstanding selection",
|
|
175
|
+
Self::Exhausted => "the scope's ticket serials or the arm's generations are used up",
|
|
176
|
+
Self::StaleTicket => "the ticket's arm was reset after the selection",
|
|
177
|
+
Self::UnknownTicket => "the ticket is spent or belongs to another bandit",
|
|
178
|
+
Self::RewardOutOfRange => "the reward is above the configured maximum",
|
|
179
|
+
Self::Saturated => "the arm's reward count or sum would overflow",
|
|
180
|
+
})
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
impl core::error::Error for Refusal {}
|
|
185
|
+
|
|
186
|
+
/// What an honoured [`Bandit::open`] did.
|
|
187
|
+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
188
|
+
pub enum Opening {
|
|
189
|
+
/// The arm was closed and is now eligible for selection.
|
|
190
|
+
Opened,
|
|
191
|
+
/// The arm was already open: an idempotent repeat that changed nothing.
|
|
192
|
+
AlreadyOpen,
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/// What an honoured [`Bandit::close`] did.
|
|
196
|
+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
197
|
+
pub enum Closing {
|
|
198
|
+
/// The arm was open and is no longer eligible for selection.
|
|
199
|
+
Closed,
|
|
200
|
+
/// The arm was already closed: an idempotent repeat that changed nothing.
|
|
201
|
+
AlreadyClosed,
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/// Proof of one selection, to be redeemed by exactly one reward or abandon.
|
|
205
|
+
///
|
|
206
|
+
/// Only the bandit that issued it honours it: its serial carries that
|
|
207
|
+
/// bandit's scope. It names the arm's generation at selection time, so a
|
|
208
|
+
/// reward that arrives after the arm was reset is refused.
|
|
209
|
+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
210
|
+
pub struct Ticket {
|
|
211
|
+
pub(crate) slot: usize,
|
|
212
|
+
pub(crate) arm: usize,
|
|
213
|
+
pub(crate) generation: u64,
|
|
214
|
+
pub(crate) serial: u64,
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
impl Ticket {
|
|
218
|
+
/// Arm the selection landed on.
|
|
219
|
+
#[must_use]
|
|
220
|
+
pub const fn arm(&self) -> usize {
|
|
221
|
+
self.arm
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/// Generation of that arm when it was selected.
|
|
225
|
+
#[must_use]
|
|
226
|
+
pub const fn generation(&self) -> u64 {
|
|
227
|
+
self.generation
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/// The ticket's scoped id: the issuing bandit's scope in the top 16 bits,
|
|
231
|
+
/// its position in that bandit's selection order (counted from one) below.
|
|
232
|
+
/// Strictly increasing within one bandit and never reissued.
|
|
233
|
+
#[must_use]
|
|
234
|
+
pub const fn serial(&self) -> u64 {
|
|
235
|
+
self.serial
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/// The scope of the bandit that issued this ticket.
|
|
239
|
+
#[must_use]
|
|
240
|
+
pub const fn scope(&self) -> u16 {
|
|
241
|
+
periscope_machines::scope_of(self.serial)
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/// A selected arm, its ticket and the rule that picked it.
|
|
246
|
+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
247
|
+
pub struct Selection {
|
|
248
|
+
/// Redeem with [`Bandit::reward`] or [`Bandit::abandon`].
|
|
249
|
+
pub ticket: Ticket,
|
|
250
|
+
/// Why this arm.
|
|
251
|
+
pub reason: Reason,
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
impl Selection {
|
|
255
|
+
/// Arm the selection landed on.
|
|
256
|
+
#[must_use]
|
|
257
|
+
pub const fn arm(&self) -> usize {
|
|
258
|
+
self.ticket.arm
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/// Snapshot of one arm.
|
|
263
|
+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
264
|
+
pub struct ArmStats {
|
|
265
|
+
/// Whether the arm is eligible for selection.
|
|
266
|
+
pub open: bool,
|
|
267
|
+
/// Bumped by every reset; tickets from older generations are stale.
|
|
268
|
+
pub generation: u64,
|
|
269
|
+
/// Rewards counted, after any decay.
|
|
270
|
+
pub rewarded: u64,
|
|
271
|
+
/// Sum of those rewards, after any decay.
|
|
272
|
+
pub reward_sum: u64,
|
|
273
|
+
/// Outstanding tickets for this arm.
|
|
274
|
+
pub pending: u64,
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
impl ArmStats {
|
|
278
|
+
/// Rewards counted plus outstanding tickets: what exploration counts.
|
|
279
|
+
#[must_use]
|
|
280
|
+
pub const fn tries(&self) -> u64 {
|
|
281
|
+
self.rewarded.saturating_add(self.pending)
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
#[cfg(test)]
|
|
286
|
+
mod tests;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
//! Deterministic fixed-point UCB1 exploration bonus and empirical mean.
|
|
2
|
+
//!
|
|
3
|
+
//! Every value is an unsigned Q8.56 number: the integer `v` stands for
|
|
4
|
+
//! `v / 2^56`. Only integer operations are used, so results are bit-identical
|
|
5
|
+
//! on every target. The logarithms come from `sliderule-machines`; the bonus and
|
|
6
|
+
//! the mean are built on them here. Everything rounds towards zero, and the
|
|
7
|
+
//! errors are one-sided: the computed value never exceeds the true one.
|
|
8
|
+
|
|
9
|
+
use sliderule_machines::{ln_floor, log2_floor};
|
|
10
|
+
|
|
11
|
+
/// Fractional bits of every fixed-point value in this module.
|
|
12
|
+
pub const FRACTION_BITS: u32 = 56;
|
|
13
|
+
|
|
14
|
+
/// `1.0` in Q8.56.
|
|
15
|
+
pub const ONE: u64 = 1 << FRACTION_BITS;
|
|
16
|
+
|
|
17
|
+
/// Binary logarithm of `value` in Q8.56; zero is treated as one.
|
|
18
|
+
///
|
|
19
|
+
/// Error bound: `0 <= log2(value) - result / 2^56 < 2^-56 + 2^-60`. Exact for
|
|
20
|
+
/// powers of two. This is [`sliderule_machines::log2_floor`] at 56 fraction bits.
|
|
21
|
+
#[must_use]
|
|
22
|
+
pub const fn log2_q56(value: u64) -> u64 {
|
|
23
|
+
log2_floor::<FRACTION_BITS>(value)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/// Natural logarithm of `value` in Q8.56; zero is treated as one.
|
|
27
|
+
///
|
|
28
|
+
/// Error bound: `0 <= ln(value) - result / 2^56 < 2^-55`. This is
|
|
29
|
+
/// [`sliderule_machines::ln_floor`] at 56 fraction bits.
|
|
30
|
+
#[must_use]
|
|
31
|
+
pub const fn ln_q56(value: u64) -> u64 {
|
|
32
|
+
ln_floor::<FRACTION_BITS>(value)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/// UCB1 exploration bonus `sqrt(2 * ln(total) / tries)` in Q8.56.
|
|
36
|
+
///
|
|
37
|
+
/// `tries` of zero is treated as one. Error bound for `tries <= total`:
|
|
38
|
+
/// `0 <= sqrt(2 ln(total) / tries) - result / 2^56 < 2^-26`.
|
|
39
|
+
#[must_use]
|
|
40
|
+
#[allow(clippy::cast_possible_truncation)] // Every narrowed value is below 2^64.
|
|
41
|
+
pub const fn bonus_q56(total: u64, tries: u64) -> u64 {
|
|
42
|
+
let tries = if tries == 0 { 1 } else { tries };
|
|
43
|
+
// ln(2^64) < 45, so ln_q56 < 45 * 2^56 < 2^62 and doubling it fits.
|
|
44
|
+
let radicand = (2 * ln_q56(total)) / tries;
|
|
45
|
+
((radicand as u128) << FRACTION_BITS).isqrt() as u64
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/// Empirical mean `sum / (count * range)` in Q8.56, clamped to `1.0`.
|
|
49
|
+
///
|
|
50
|
+
/// `count` or `range` of zero is treated as one. Error bound for
|
|
51
|
+
/// `sum <= count * range`: `0 <= mean - result / 2^56 < 2^-56`.
|
|
52
|
+
#[must_use]
|
|
53
|
+
#[allow(clippy::cast_possible_truncation)] // Every narrowed value is below 2^64.
|
|
54
|
+
pub const fn mean_q56(sum: u64, count: u64, range: u32) -> u64 {
|
|
55
|
+
let count = if count == 0 { 1 } else { count };
|
|
56
|
+
let range = if range == 0 { 1 } else { range };
|
|
57
|
+
let scaled = ((sum as u128) << FRACTION_BITS) / ((count as u128) * (range as u128));
|
|
58
|
+
if scaled > ONE as u128 {
|
|
59
|
+
ONE
|
|
60
|
+
} else {
|
|
61
|
+
scaled as u64
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
#[cfg(test)]
|
|
66
|
+
mod tests;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
//! Allocation-free arg-max over arms with an explicit tie rule.
|
|
2
|
+
|
|
3
|
+
use crate::TieBreak;
|
|
4
|
+
|
|
5
|
+
/// Map a uniform draw onto `0..count` as `floor(draw * count / 2^64)`.
|
|
6
|
+
#[allow(clippy::cast_possible_truncation)] // The high half of a 128-bit product.
|
|
7
|
+
pub const fn draw_below(draw: u64, count: u64) -> u64 {
|
|
8
|
+
let wide = (draw as u128) * (count as u128);
|
|
9
|
+
(wide >> 64) as u64
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/// One candidate arm as the chooser sees it.
|
|
13
|
+
#[derive(Debug, Clone, Copy)]
|
|
14
|
+
pub struct Candidate<S> {
|
|
15
|
+
/// Score to maximise.
|
|
16
|
+
pub score: S,
|
|
17
|
+
/// Rewards plus outstanding tickets, for [`TieBreak::LeastTried`].
|
|
18
|
+
pub tries: u64,
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/// Index of the best-scoring candidate, ties settled by `rule`.
|
|
22
|
+
///
|
|
23
|
+
/// `candidate(index)` answers `None` for arms that do not take part. The
|
|
24
|
+
/// closure is called for every index on each of at most two passes.
|
|
25
|
+
pub fn best<S: Ord + Copy>(
|
|
26
|
+
arms: usize,
|
|
27
|
+
rule: TieBreak,
|
|
28
|
+
pick: u64,
|
|
29
|
+
candidate: impl Fn(usize) -> Option<Candidate<S>>,
|
|
30
|
+
) -> Option<usize> {
|
|
31
|
+
let mut top: Option<S> = None;
|
|
32
|
+
let mut tied = 0_u64;
|
|
33
|
+
let mut first = None;
|
|
34
|
+
let mut last = None;
|
|
35
|
+
let mut fewest: Option<(u64, usize)> = None;
|
|
36
|
+
for index in 0..arms {
|
|
37
|
+
let Some(entry) = candidate(index) else {
|
|
38
|
+
continue;
|
|
39
|
+
};
|
|
40
|
+
if top.is_none_or(|score| entry.score > score) {
|
|
41
|
+
top = Some(entry.score);
|
|
42
|
+
tied = 0;
|
|
43
|
+
first = None;
|
|
44
|
+
fewest = None;
|
|
45
|
+
}
|
|
46
|
+
if top != Some(entry.score) {
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
tied += 1;
|
|
50
|
+
first = first.or(Some(index));
|
|
51
|
+
last = Some(index);
|
|
52
|
+
if fewest.is_none_or(|(tries, _)| entry.tries < tries) {
|
|
53
|
+
fewest = Some((entry.tries, index));
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
let top = top?;
|
|
57
|
+
match rule {
|
|
58
|
+
TieBreak::LowestIndex => first,
|
|
59
|
+
TieBreak::HighestIndex => last,
|
|
60
|
+
TieBreak::LeastTried => fewest.map(|(_, index)| index),
|
|
61
|
+
TieBreak::Draw => {
|
|
62
|
+
let Ok(skip) = usize::try_from(draw_below(pick, tied)) else {
|
|
63
|
+
debug_assert!(false, "a draw below the tied count fits usize");
|
|
64
|
+
return None;
|
|
65
|
+
};
|
|
66
|
+
(0..arms)
|
|
67
|
+
.filter(|index| candidate(*index).is_some_and(|entry| entry.score == top))
|
|
68
|
+
.nth(skip)
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
[package]
|
|
2
|
+
name = "bandit_machines_native"
|
|
3
|
+
version = "0.1.7"
|
|
4
|
+
edition.workspace = true
|
|
5
|
+
rust-version.workspace = true
|
|
6
|
+
license.workspace = true
|
|
7
|
+
publish = false
|
|
8
|
+
|
|
9
|
+
[lib]
|
|
10
|
+
crate-type = ["cdylib"]
|
|
11
|
+
test = false
|
|
12
|
+
doctest = false
|
|
13
|
+
|
|
14
|
+
[dependencies]
|
|
15
|
+
bandit-machines.workspace = true
|
|
16
|
+
magnus.workspace = true
|
|
17
|
+
rb-sys.workspace = true
|
|
18
|
+
matryona-machines = { workspace = true, features = ["ruby"] }
|
|
19
|
+
|
|
20
|
+
[lints]
|
|
21
|
+
workspace = true
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
require 'mkmf'
|
|
3
|
+
require 'pathname'
|
|
4
|
+
require 'rb_sys/mkmf'
|
|
5
|
+
|
|
6
|
+
create_rust_makefile('bandit_machines_native/bandit_machines_native') do |e|
|
|
7
|
+
e.ext_dir = Pathname(__dir__).relative_path_from(Pathname(Dir.pwd)).to_s
|
|
8
|
+
e.profile = ENV.fetch('RB_SYS_CARGO_PROFILE', :release).to_sym
|
|
9
|
+
end
|
|
10
|
+
makefile = File.join(Dir.pwd, 'Makefile')
|
|
11
|
+
File.write(makefile, File.read(makefile).gsub(
|
|
12
|
+
/^RB_SYS_CARGO_MANIFEST_DIR \?=.*$/,
|
|
13
|
+
"RB_SYS_CARGO_MANIFEST_DIR ?= #{File.expand_path(__dir__)}"
|
|
14
|
+
))
|