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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
pub use *;
pub use *;
pub use *;
pub use *;
// use crate::objectives::{Objective, Scored, pareto};
// #[cfg(feature = "serde")]
// use serde::{Deserialize, Serialize};
// use std::{cmp::Ordering, hash::Hash, ops::Range, sync::Arc};
// const DEFAULT_ENTROPY_BINS: usize = 20;
// pub struct FrontAddResult {
// pub added_count: usize,
// pub removed_count: usize,
// pub comparisons: usize,
// pub filter_count: usize,
// pub size: usize,
// }
// /// A `Front<T>` is a collection of `T`'s that are non-dominated with respect to each other.
// /// This is useful for multi-objective optimization problems where the goal is to find
// /// the best solutions that are not dominated by any other solution.
// /// This results in what is called the Pareto front.
// #[derive(Clone)]
// #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
// pub struct Front<T>
// where
// T: Scored,
// {
// values: Vec<Arc<T>>,
// range: Range<usize>,
// objective: Objective,
// // ---- scratch / cache (not part of logical state) ----
// #[cfg_attr(feature = "serde", serde(skip))]
// scratch_remove: Vec<usize>,
// #[cfg_attr(feature = "serde", serde(skip))]
// scratch_keep_idx: Vec<usize>,
// // Flat score matrix: [n, m] row-major
// #[cfg_attr(feature = "serde", serde(skip))]
// scratch_scores: Vec<f32>,
// // Crowding distance per item (len = n)
// #[cfg_attr(feature = "serde", serde(skip))]
// scratch_dist: Vec<f32>,
// // Indices 0..n used for per-dimension sorts in crowding distance
// #[cfg_attr(feature = "serde", serde(skip))]
// scratch_order: Vec<usize>,
// }
// impl<T> Front<T>
// where
// T: Scored,
// {
// pub fn new(range: Range<usize>, objective: Objective) -> Self {
// Front {
// values: Vec::new(),
// range,
// objective: objective.clone(),
// scratch_remove: Vec::new(),
// scratch_keep_idx: Vec::new(),
// scratch_scores: Vec::new(),
// scratch_dist: Vec::new(),
// scratch_order: Vec::new(),
// }
// }
// pub fn range(&self) -> Range<usize> {
// self.range.clone()
// }
// pub fn objective(&self) -> Objective {
// self.objective.clone()
// }
// pub fn is_empty(&self) -> bool {
// self.values.is_empty()
// }
// pub fn values(&self) -> &[Arc<T>] {
// &self.values
// }
// pub fn crowding_distance(&self) -> Option<Vec<f32>> {
// let scores = self
// .values
// .iter()
// .filter_map(|s| s.score())
// .collect::<Vec<_>>();
// if scores.is_empty() {
// return None;
// }
// Some(pareto::crowding_distance(&scores))
// }
// pub fn entropy(&self) -> Option<f32> {
// let scores = self
// .values
// .iter()
// .filter_map(|s| s.score())
// .collect::<Vec<_>>();
// if scores.is_empty() {
// return None;
// }
// Some(pareto::entropy(&scores, DEFAULT_ENTROPY_BINS))
// }
// pub fn add_all(&mut self, items: Vec<T>) -> FrontAddResult
// where
// T: Eq + Hash + Clone + Send + Sync + 'static,
// {
// let mut updated = false;
// let mut to_remove = Vec::new();
// let mut added_count = 0;
// let mut removed_count = 0;
// let mut comparisons = 0;
// let mut filter_count = 0;
// for new_member in items.into_iter() {
// let mut is_dominated = true;
// for existing_val in self.values.iter() {
// let equals = &new_member == existing_val.as_ref();
// if self.dom_cmp(existing_val.as_ref(), &new_member) == Ordering::Greater || equals {
// // If an existing value dominates the new value, return false
// is_dominated = false;
// comparisons += 1;
// break;
// } else if self.dom_cmp(&new_member, existing_val.as_ref()) == Ordering::Greater {
// // If the new value dominates an existing value, continue checking
// to_remove.push(Arc::clone(existing_val));
// comparisons += 1;
// continue;
// }
// }
// if is_dominated {
// updated = true;
// self.values.push(Arc::new(new_member));
// added_count += 1;
// for rem in to_remove.drain(..) {
// self.values.retain(|x| x.as_ref() != rem.as_ref());
// removed_count += 1;
// }
// }
// if updated && self.values.len() > self.range.end {
// self.filter();
// filter_count += 1;
// }
// to_remove.clear();
// updated = false;
// }
// FrontAddResult {
// added_count,
// removed_count,
// comparisons,
// filter_count,
// size: self.values.len(),
// }
// }
// fn dom_cmp(&self, one: &T, two: &T) -> Ordering {
// let one_score = one.score();
// let two_score = two.score();
// if one_score.is_none() || two_score.is_none() {
// return Ordering::Equal;
// }
// if let (Some(one), Some(two)) = (one_score, two_score) {
// if pareto::dominance(one, two, &self.objective) {
// return Ordering::Greater;
// } else if pareto::dominance(two, one, &self.objective) {
// return Ordering::Less;
// }
// }
// Ordering::Equal
// }
// fn filter(&mut self) {
// if let Some(crowding_distances) = self.crowding_distance() {
// let mut enumerated = crowding_distances.iter().enumerate().collect::<Vec<_>>();
// enumerated.sort_unstable_by(|a, b| b.1.partial_cmp(a.1).unwrap_or(Ordering::Equal));
// self.values = enumerated
// .iter()
// .take(self.range.start)
// .map(|(i, _)| Arc::clone(&self.values[*i]))
// .collect::<Vec<Arc<T>>>();
// }
// }
// }
// impl<T> Default for Front<T>
// where
// T: Scored,
// {
// fn default() -> Self {
// Front::new(0..0, Objective::default())
// }
// }