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
use Row;
/// Candidate solution for an optimization problem.
///
/// The ABC algorithm is abstract enough to work on a variety of
/// problems.
///
/// # Examples
///
/// ```
/// extern crate rand;
/// # extern crate abc; fn main() {
///
/// use abc::{Solution, Row};
/// use rand::Rng;
///
/// // Because i32 and abc::Solution are both defined elsewhere,
/// // we cannot implement Solution for i32 directly. So, we use
/// // a struct as a thin wrapper.
/// #[derive(Clone)]
/// struct Number(i32);
///
/// impl Solution for Number {
/// fn make() -> Number {
/// let mut rng = rand::thread_rng();
/// let x = rng.gen_range(0, 100);
/// Number(x)
/// }
///
/// // Minimize the numerical value.
/// fn evaluate_fitness(&self) -> f64 {
/// let Number(x) = *self;
/// 1f64 / x as f64
/// }
///
/// fn explore(field: &[Row<Number>], n: usize) -> Number {
/// let mut rng = rand::thread_rng();
/// let Number(x) = field[n].solution;
/// Number(x + rng.gen_range(-10, 10))
/// }
/// }
/// # }
/// ```