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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
use crate::Dominate;
use std::slice::Iter;
use std::iter::FromIterator;
/// Represents a Pareto front.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ParetoFront<T: Dominate>
{
front: Vec<T>
}
impl<T: Dominate> ParetoFront<T>
{
/// Constructs a new, empty, Pareto front.
pub fn new() -> Self
{
ParetoFront { front: Vec::new() }
}
/// Removes all elements in the front that are dominated by `new_element`,
/// starting at index `index_start`.
fn _remove_dominated_starting_at(&mut self, new_element: &T, index_start: usize)
{
// lists all elements dominated by `new_element`, starting at index `index_start`
let mut index_dominated_elements = Vec::new();
for (index, element) in self.front.iter().enumerate().skip(index_start)
{
if new_element.dominate(element)
{
index_dominated_elements.push(index);
}
}
// removes the elements at the listed indexes
// in reverse order to take into acount that each removed index shift all the following indexes
for index in index_dominated_elements.into_iter().rev()
{
self.front.swap_remove(index);
}
}
/// Removes all the elements in the Pareto front that are dominated by `new_element`.
/// Returns `true` if `new_element` should be in the Pareto front.
/// Returns `false` if `new_element` was dominated and, thus, shouldn't be added to the front.
///
/// This operation has `O(n)` complexity (where `n` is the number of elements currently in the Pareto front)
/// but is optimized to favour early stopping and cache friendly.
///
/// This operation might *not* preserve the ordering of the elements in the front.
fn _remove_dominated(&mut self, new_element: &T) -> bool
{
// for all elements of the pareto front, check whether they are dominated or dominate `new_element`
for (index, element) in self.front.iter().enumerate()
{
if element.dominate(new_element)
{
// `new_element` is dominated by `element`, it is thus not part of the Pareto front
// swap `element` with the previous element in order to percolate the best elements to the top
// NOTE: in my benchmarks this brings clear performance benefits by putting "killer" elements first
if index > 0
{
self.front.swap(index, index - 1);
}
return false;
}
else if new_element.dominate(element)
{
// `new_element` dominates `element`, it is thus part of the Pareto front
self.front.swap_remove(index);
// looks at the rest of the Pareto front to remove any further element that are dominated
self._remove_dominated_starting_at(new_element, index);
return true;
}
}
// `new_element` has not been dominated, it is thus part of the Pareto front
true
}
/// Returns `true` if at least one element on the Pareto front dominates `new_element`.
///
/// This operation has `O(n)` complexity (where `n` is the number of elements currently in the Pareto front)
/// but is optimized to favour early stopping and cache friendly.
pub fn dominate(&self, new_element: &T) -> bool
{
self.front.iter().any(|element| element.dominate(new_element))
}
/// Adds `new_element` to the Pareto front.
/// Returns `true` if the element is now in the Pareto front.
/// Returns `false` if the element was dominated and, thus, not added to the front.
///
/// This operation has `O(n)` complexity (where `n` is the number of elements currently in the Pareto front)
/// but is optimized to favour early stopping and cache friendly.
///
/// This operation might *not* preserve the ordering of the elements in the front.
///
/// ```rust
/// # use pareto_front::{Dominate, ParetoFront};
/// #
/// # /// type that will be pushed in the Pareto front
/// # #[derive(PartialEq)]
/// # struct ParetoElement
/// # {
/// # cost: usize, // to be minimized
/// # quality: f32, // to be maximized
/// # }
/// #
/// # /// implement the `Dominate` trait so that the elements can be pushed into the front
/// # impl Dominate for ParetoElement
/// # {
/// # /// returns `true` is `self` is better than `x` on all fields that matter to us
/// # fn dominate(&self, x: &Self) -> bool
/// # {
/// # (self.cost <= x.cost) && (self.quality >= x.quality) && (self != x)
/// # }
/// # }
/// #
/// # // data to be put in the front
/// # let x = ParetoElement { cost: 35, quality: 0.5 };
/// #
/// // a Pareto front
/// let mut front = ParetoFront::new();
///
/// // inserts in the Pareto front
/// let is_pareto_optimal = front.push(x);
/// ```
pub fn push(&mut self, new_element: T) -> bool
{
// removes dominated elements from the front and checks whether `new_element` should be added
let is_pareto_optimal = self._remove_dominated(&new_element);
// adds `new_element` if needed
if is_pareto_optimal
{
self.front.push(new_element);
}
is_pareto_optimal
}
/// Adds the content of `pareto_front` to the Pareto front.
///
/// This operation has `O(n*m)` complexity
/// where `n` is the number of elements in `self`
/// and `m` is the number of elements in `pareto_front`
/// but is optimized to favour early stopping.
pub fn merge(&mut self, pareto_front: ParetoFront<T>)
{
// set the largest front aside
let mut largest_front = pareto_front.front;
if largest_front.len() < self.front.len()
{
std::mem::swap(&mut self.front, &mut largest_front);
}
// for all the elements in the largest front, remove dominated elements from the smallest front
// the largest front keeps only the elements that should be in the final Pareto front
largest_front.retain(|x| self._remove_dominated(x));
// extends the largest front with the content of the smallest front
// and make it our front
std::mem::swap(&mut self.front, &mut largest_front);
self.front.extend(largest_front);
}
/// Extracts a slice containing the entire Pareto front.
pub fn as_slice(&self) -> &[T]
{
self.front.as_slice()
}
/// Returns the number of elements currently in the Pareto front.
pub fn len(&self) -> usize
{
self.front.len()
}
/// Returns `true` if the Pareto front contains no elements.
pub fn is_empty(&self) -> bool
{
self.front.is_empty()
}
/// Returns an iterator over the Pareto front.
pub fn iter(&self) -> Iter<'_, T>
{
self.front.iter()
}
// no `iter_mut` as the mutation could invalidate the front
}
impl<T: Dominate> Default for ParetoFront<T>
{
/// Default value.
fn default() -> Self
{
// Manually implemented so as to not require `T` to implement `Default`.
Self::new()
}
}
impl<T: Dominate> From<ParetoFront<T>> for Vec<T>
{
/// Converts the Pareto front into a vector.
/// This operation is free as the underlying datastructure is a vector.
fn from(front: ParetoFront<T>) -> Vec<T>
{
front.front
}
}
impl<T: Dominate> IntoIterator for ParetoFront<T>
{
type Item = T;
type IntoIter = std::vec::IntoIter<T>;
/// Creates an iterator from a `ParetoFront`.
fn into_iter(self) -> Self::IntoIter
{
self.front.into_iter()
}
}
impl<T: Dominate> FromIterator<T> for ParetoFront<T>
{
/// Implements the `FromIterator` trait to enable the collection of an iterator into a `ParetoFront`.
///
/// ```rust
/// # use pareto_front::{Dominate, ParetoFront};
/// #
/// # /// type that will be pushed in the Pareto front
/// # #[derive(PartialEq)]
/// # struct ParetoElement
/// # {
/// # cost: usize, // to be minimized
/// # quality: f32, // to be maximized
/// # }
/// #
/// # /// implement the `Dominate` trait so that the elements can be pushed into the front
/// # impl Dominate for ParetoElement
/// # {
/// # /// returns `true` is `self` is better than `x` on all fields that matter to us
/// # fn dominate(&self, x: &Self) -> bool
/// # {
/// # (self.cost <= x.cost) && (self.quality >= x.quality) && (self != x)
/// # }
/// # }
/// #
/// # // data to be put in the front
/// # let x = ParetoElement { cost: 35, quality: 0.5 };
/// # let y = ParetoElement { cost: 35, quality: 0.5 };
/// # let z = ParetoElement { cost: 35, quality: 0.5 };
/// #
/// // builds a Pareto front from an iterator
/// let front : ParetoFront<_> = vec![x, y, z].into_iter().collect();
/// ```
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
{
let mut front = ParetoFront::new();
for x in iter
{
front.push(x);
}
front
}
}
impl<T: Dominate> Extend<T> for ParetoFront<T>
{
/// Implements the `Extend` trait to extend a `ParetoFront` with the content of an iterator.
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
{
// Note: I tried a divide and conquer type of approach
// (creating a new pareto front from `iter` and merging it)
// but it was slightly slower for all problem sizes
for x in iter
{
self.push(x);
}
}
}