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
use crate::{Domain, IdGen, Obs, Optimizer, Result};
use rand::distributions::Distribution;
use rand::Rng;
use std::marker::PhantomData;
#[derive(Debug)]
pub struct RandomOptimizer<P, V> {
param_domain: P,
_value: PhantomData<V>,
}
impl<P, V> RandomOptimizer<P, V>
where
P: Domain + Distribution<<P as Domain>::Point>,
{
pub fn new(param_domain: P) -> Self {
Self {
param_domain,
_value: PhantomData,
}
}
}
impl<P, V> Optimizer for RandomOptimizer<P, V>
where
P: Domain + Distribution<<P as Domain>::Point>,
{
type Param = P::Point;
type Value = V;
fn ask<R: Rng, G: IdGen>(&mut self, mut rng: R, idg: G) -> Result<Obs<Self::Param>> {
track!(Obs::new(idg, self.param_domain.sample(&mut rng)))
}
fn tell(&mut self, _obs: Obs<Self::Param, Self::Value>) -> Result<()> {
Ok(())
}
}
impl<P, V> Default for RandomOptimizer<P, V>
where
P: Default + Domain + Distribution<<P as Domain>::Point>,
{
fn default() -> Self {
Self::new(P::default())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::domains::DiscreteDomain;
use crate::generators::SerialIdGenerator;
use rand;
use trackable::result::TestResult;
#[test]
fn random_works() -> TestResult {
let mut opt = RandomOptimizer::new(track!(DiscreteDomain::new(10))?);
let mut rng = rand::thread_rng();
let mut idg = SerialIdGenerator::new();
let obs = track!(opt.ask(&mut rng, &mut idg))?;
track!(opt.tell(obs))?;
Ok(())
}
}