3 个版本 (稳定)
1.0.1 | 2021 年 10 月 27 日 |
---|---|
1.0.0 | 2021 年 10 月 24 日 |
0.1.0 | 2021 年 10 月 22 日 |
#1739 在 算法 中
每月 123 次下载
27KB
262 行
帕累托前沿 (crates.io)
pareto_front
包是一个 Rust 库,用于增量构建 帕累托前沿。
这在多目标优化中特别有用,在多目标优化中,人们可能需要跟踪各种权衡,而不是一个可以轻松跟踪的单个最大值,这些权衡在优化过程中被发现,并且没有一个是所有轴上的最佳选择。
该包力求保持小巧且真正快速且正确。
功能
该包提供对 ParetoFront
类型的访问,可以创建(空或从迭代器中创建),通过添加新的潜在元素来更新(使用 push
或 extend
方法)并转换为迭代器、切片或向量。
开启 pareto_front_concurrent
功能将解锁 ConcurrentParetoFront
类型,可用于在并行算法中构建帕累托前沿,而无需在 ParetoFront
周围放置锁。
开启 pareto_front_serde
功能允许您使用 serde 来序列化和反序列化 ParetoFront
类型。
用法
要插入帕累托前沿的元素应实现 Dominate
特性
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)
}
}
可以使用 push
方法向帕累托前沿添加新元素(还可以将迭代器收集到帕累托前沿中)
// data to be put in the front
let x = ParetoElement { cost: 35, quality: 0.5 };
let y = ParetoElement { cost: 350, quality: 0.05 };
let z = ParetoElement { cost: 5, quality: 0.25 };
// insertions in the Pareto front
let mut front = ParetoFront::new();
front.push(x);
front.push(y);
// note that `push` returns a boolean to tell you if the point you just inserted is part of the current Pareto front
let z_is_pareto_optimal = front.push(z);
生成的帕累托前沿可以转换为迭代器、切片或向量。
依赖项
~200KB