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
use std::fmt::Debug;
use std::cmp::PartialOrd;
use std::default::Default;
use std::ops::{Index, IndexMut};
use rand;
use rand::distributions::{IndependentSample, Range};
use ndarray::Array1;
pub trait ArgminParameter
: Clone
+ Default
+ Debug
+ Index<usize, Output = <Self as ArgminParameter>::Element>
+ IndexMut<usize> {
type Element: PartialOrd + Clone;
fn modify(&self) -> (Self, usize);
fn random(&Self, &Self) -> Self;
}
macro_rules! random_vec_iter {
($type:ty) => {
fn random(lower_bound: &$type, upper_bound: &$type) -> $type {
let mut rng = rand::thread_rng();
let out: $type = lower_bound
.iter()
.zip(upper_bound.iter())
.map(|(l, u)| {
if l >= u {
panic!("Parameter: lower_bound must be lower than upper_bound.");
}
let range = Range::new(*l, *u);
range.ind_sample(&mut rng)
})
.collect();
out
}
}
}
macro_rules! modify_one_parameter {
() => {
fn modify(&self) -> (Self, usize) {
let pos = Range::new(0, self.len());
let range = Range::new(-1.0, 1.0);
let mut rng = rand::thread_rng();
let mut param = self.clone();
let idx = pos.ind_sample(&mut rng);
param[idx] = self[idx] + range.ind_sample(&mut rng);
(param, idx)
}
}
}
macro_rules! implement_argmin_parameter {
($param:ty, $element:ty) => {
impl ArgminParameter for $param {
type Element = $element;
modify_one_parameter!();
random_vec_iter!($param);
}
}
}
implement_argmin_parameter!(Vec<f64>, f64);
implement_argmin_parameter!(Vec<f32>, f32);
implement_argmin_parameter!(Array1<f64>, f64);
implement_argmin_parameter!(Array1<f32>, f32);