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
/*! Gene mutation operators.
# Examples
```
# use genotype::*;
# use mutation::*;
struct TotallyRandomGen;
impl MutationGen for TotallyRandomGen {
fn gen(&mut self) -> Param {
0.4
}
}
// as seen before
struct Weight(Param);
impl RangedParam for Weight {
fn range(&self) -> (Param, Param) {
(40.0, 100.0)
}
// ...
#
# fn get(&self) -> Param { self.0 }
#
# fn get_mut(&mut self) -> &mut Param {&mut self.0}
}
struct Human {
weight: Weight,
}
impl ParamHolder for Human {
// ...
# fn param_count(&self) -> usize {
# 1
# }
#
# fn get_param(&mut self, index: usize) -> &mut RangedParam {
# match index {
# 0 => &mut self.weight,
# _ => panic!("Bad index"),
# }
# }
}
# fn main() {
# use std::cell::RefCell;
# use std::rc::Rc;
let human = Rc::new(RefCell::new(
Human { weight: Weight(0.1) }
));
// unscaled value is initially 0.1
assert!((human.borrow().weight.get_scaled() - 46.0) < 0.00001);
let mut gen = TotallyRandomGen{};
mutate(human.clone(), &mut gen);
// unscaled value has mutated to 0.5
assert!((human.borrow().weight.get_scaled() - 70.0) < 0.00001);
# }
```
*/
use *;
use RefCell;
use Rc;
/// Produces values to add to an unscaled gene value.
///
/// Keep in mind that the unscaled value is clamped between 0.0 and 1.0.
/// Mutates the given `ParamHolder` with the given `MutationGen` by iterating through all genes and
/// adding to each the result of calling the mutation generator.
///
/// See [examples](index.html#example).