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
pub use ;
/// Determines if you want to minimize or maximize the [objective_function][Problem::objective_function]
/// This trait is a core definition for the library, as every optimization task is related to a [Problem].
///
/// ```
/// # use ordered_float::NotNan;
/// # use optimum::core::{Objective, Problem, Evaluation};
///
/// struct Knapsack {
/// max_weight: f64,
/// available_items: Vec<Item>,
/// }
///
/// struct Item {
/// value: f64,
/// weight: f64,
/// }
///
/// impl Problem for Knapsack {
/// /// We want to maximize the value we carry in the knapsack
/// const OBJECTIVE: Objective = Objective::Max;
/// /// Every position `i` of the [Vec] represents if item `i` was chosen
/// type Solution = Vec<bool>;
///
/// /// We can't use [f64] directly, because it isn't [Ord].
/// type Value = NotNan<f64>;
///
///
/// fn objective_function(&self, solution: Self::Solution) -> Evaluation<Self> {
/// let score = self.available_items
/// .iter()
/// .enumerate()
/// .filter(|&(i, _)| solution[i])
/// .map(|(_, item)| item.value)
/// .sum();
/// let value = NotNan::new(score).unwrap();
///
/// Evaluation::new(solution, value)
/// }
/// }
///
/// ```
///