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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
pub use rand::{
distributions::{uniform::SampleUniform, Open01},
seq::SliceRandom,
Rng, SeedableRng,
};
use crate::genetic::AsScalar;
use rand_xoshiro::Xoshiro256Plus;
pub type Prng = Xoshiro256Plus;
pub type Seed = <Prng as SeedableRng>::Seed;
pub fn random_seed() -> Seed {
let mut rng = Prng::from_entropy();
rng.gen()
}
pub fn get_rng(seed: Seed) -> Prng {
Prng::from_seed(seed)
}
pub fn random_index<R>(rng: &mut R, length: usize) -> usize
where
R: Rng + Sized,
{
random_index_from_range(rng, 0, length)
}
pub fn random_index_from_range<R>(rng: &mut R, min: usize, max: usize) -> usize
where
R: Rng + Sized,
{
rng.gen_range(min..max)
}
pub fn random_cut_points<R>(rng: &mut R, length: usize) -> (usize, usize)
where
R: Rng + Sized,
{
random_cut_points_from_range(rng, 0, length)
}
pub fn random_cut_points_from_range<R>(rng: &mut R, min: usize, max: usize) -> (usize, usize)
where
R: Rng + Sized,
{
assert!(max >= min + 4);
let max_slice = max - min - 2;
loop {
let cutpoint1 = rng.gen_range(min..max);
let cutpoint2 = rng.gen_range(min..max);
if cutpoint1 < cutpoint2 {
if cutpoint2 - cutpoint1 >= max_slice {
continue;
}
return (cutpoint1, cutpoint2);
} else if cutpoint2 < cutpoint1 {
if cutpoint1 - cutpoint2 >= max_slice {
continue;
}
return (cutpoint2, cutpoint1);
}
}
}
pub fn random_n_cut_points<R>(rng: &mut R, n: usize, length: usize) -> Vec<usize>
where
R: Rng + Sized,
{
assert!(n > 0);
assert!(length >= 2 * n);
let mut cutpoints = Vec::with_capacity(n);
match n {
1 => {
cutpoints.push(random_index(rng, length));
}
2 => {
let (cp1, cp2) = random_cut_points(rng, length);
cutpoints.push(cp1);
cutpoints.push(cp2);
}
_ => {
let slice_len = length / n;
let mut start = 0;
let mut end = slice_len;
let mut count = 1;
loop {
let cutpoint = random_index_from_range(rng, start, end);
if cutpoint == 0 || cutpoint == length {
continue;
}
cutpoints.push(cutpoint);
count += 1;
if count > n {
break;
}
start = cutpoint + 1;
if count == n {
end = length;
} else {
end += slice_len;
}
}
}
}
cutpoints
}
pub fn random_probability<R>(rng: &mut R) -> f64
where
R: Rng + Sized,
{
rng.sample(Open01)
}
#[derive(Clone, Debug, PartialEq)]
pub struct WeightedDistribution<'a, T>
where
T: 'a + AsScalar,
{
values: &'a [T],
sum: f64,
weights: Vec<f64>,
}
impl<'a, T> WeightedDistribution<'a, T>
where
T: 'a + AsScalar,
{
pub fn from_scalar_values(values: &'a [T]) -> Self {
let (weights, weight_sum) = calc_weights_and_sum(values);
WeightedDistribution {
values,
weights,
sum: weight_sum,
}
}
pub fn select(&self, pointer: f64) -> usize {
assert!(pointer >= 0. && pointer <= self.sum);
weighted_select(pointer, &self.weights)
}
pub fn sum(&self) -> f64 {
self.sum
}
pub fn value(&self, index: usize) -> &T {
&self.values[index]
}
}
fn calc_weights_and_sum<'a, T>(values: &'a [T]) -> (Vec<f64>, f64)
where
T: 'a + AsScalar,
{
let mut weights = Vec::with_capacity(values.len());
let mut weight_sum: f64 = 0.;
for value in values.iter() {
let scalar = value.as_scalar();
weight_sum += scalar;
weights.push(scalar);
}
(weights, weight_sum)
}
fn weighted_select(pointer: f64, weights: &[f64]) -> usize {
let mut delta = pointer;
for (i, weight) in weights.iter().enumerate() {
delta -= *weight;
if delta <= 0. {
return i;
}
}
weights.len() - 1
}
#[cfg(test)]
mod tests;