-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrand_utils.rs
More file actions
158 lines (140 loc) · 4.1 KB
/
Copy pathrand_utils.rs
File metadata and controls
158 lines (140 loc) · 4.1 KB
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
use crate::CONFIG;
use num_traits::{NumCast, Saturating, Unsigned, Zero};
use rand::distributions::uniform::{SampleRange, SampleUniform};
use rand::distributions::{Standard, WeightedIndex};
use rand::prelude::*;
use zip_diff::zip::ZipArchive;
pub struct Ucb {
scores: Vec<f64>,
trials: Vec<f64>,
weighted_index: Option<WeightedIndex<f64>>,
}
impl Ucb {
pub fn new(len: usize) -> Self {
Self {
scores: vec![0.0; len],
trials: vec![0.0; len],
weighted_index: None,
}
}
pub fn construct(&mut self) {
for i in 0..self.scores.len() {
// recent results are more important than old results
self.scores[i] *= 0.995;
self.trials[i] *= 0.995;
}
let double_ln_total_trial: f64 = 2.0 * self.trials.iter().sum::<f64>().max(1.0).ln();
let weights = self
.scores
.iter()
.zip(self.trials.iter().map(|t| t.max(1.0)))
.map(|(score, trial)| {
let ucb = score / trial + (double_ln_total_trial / trial).sqrt();
(ucb * 5.0).exp() // softmax temperature
});
self.weighted_index = if CONFIG.argmax_ucb {
let mut max_weight = f64::NEG_INFINITY;
for w in weights.clone() {
if w > max_weight {
max_weight = w;
}
}
Some(
WeightedIndex::new(weights.map(|w| {
if w == max_weight {
1.0
} else {
1e-6 // not zero to avoid loop when always fail to mutate
}
}))
.unwrap(),
)
} else {
Some(WeightedIndex::new(weights).unwrap())
};
}
pub fn sample<R: Rng>(&self, rng: &mut R) -> usize {
self.weighted_index
.as_ref()
.expect("need to construt before sampling")
.sample(rng)
}
pub fn record(&mut self, id: usize, trial: f64, score: f64) {
self.trials[id] += trial;
self.scores[id] += score;
self.weighted_index = None;
}
pub fn scores(&self) -> &[f64] {
&self.scores
}
pub fn trials(&self) -> &[f64] {
&self.trials
}
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum HeaderLocation {
Lfh,
Cdh,
Both,
}
impl HeaderLocation {
pub fn lfh(self) -> bool {
matches!(self, Self::Lfh | Self::Both)
}
pub fn cdh(self) -> bool {
matches!(self, Self::Cdh | Self::Both)
}
}
impl Distribution<HeaderLocation> for Standard {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> HeaderLocation {
let i = (0..5).choose(rng).unwrap();
match i {
0 => HeaderLocation::Lfh,
1 => HeaderLocation::Cdh,
_ => HeaderLocation::Both,
}
}
}
pub fn rand_header<R: RngCore>(zip: &ZipArchive, rng: &mut R) -> Option<(usize, HeaderLocation)> {
let loc = rng.gen();
let len = match loc {
HeaderLocation::Lfh => zip.files.len(),
HeaderLocation::Cdh => zip.cd.len(),
HeaderLocation::Both => zip.files.len().min(zip.cd.len()),
};
let index = (0..len).choose(rng)?;
Some((index, loc))
}
/// returns a random number in 1..=32, returns x with possibility 2^-x
pub fn rand_len<R: RngCore>(rng: &mut R) -> usize {
rng.next_u64().trailing_zeros() as usize + 1
}
pub fn mutate_len<R, N>(size: &mut N, rng: &mut R)
where
R: RngCore,
N: Copy + Saturating + Zero + Unsigned + NumCast,
{
let delta = N::from(rand_len(rng)).unwrap();
if size.is_zero() || rng.gen() {
*size = size.saturating_add(delta);
} else {
*size = size.saturating_sub(delta);
}
}
pub fn rand_range<G, T, R>(rng: &mut G, range: R) -> Option<(T, T)>
where
G: Rng,
T: SampleUniform + Ord,
R: SampleRange<T> + Clone,
{
if range.is_empty() {
return None;
}
let x = rng.gen_range(range.clone());
let y = rng.gen_range(range);
if x < y {
Some((x, y))
} else {
Some((y, x))
}
}