pub struct KMeans {
pub imbalance_tol: f64,
pub delta_threshold: f64,
pub max_iter: usize,
pub max_balance_iter: usize,
pub erode: bool,
pub hilbert: bool,
pub mbr_early_break: bool,
}
Expand description
K-means algorithm
An implementation of the balanced k-means algorithm inspired from “Balanced k-means for Parallel Geometric Partitioning” by Moritz von Looz, Charilaos Tzovas and Henning Meyerhenke (2018, University of Cologne).
From an initial partition, the K-means algorithm will generate points clusters that will, at each iteration, exchage points with other clusters that are “closer”, and move by recomputing the clusters position (defined as the centroid of the points assigned to the cluster). Eventually the clusters will stop moving, yielding a new partition.
Example
use coupe::Partition as _;
use coupe::Point2D;
let points = [
Point2D::new(0., 0.),
Point2D::new(1., 0.),
Point2D::new(2., 0.),
Point2D::new(0., 5.),
Point2D::new(1., 5.),
Point2D::new(2., 5.),
Point2D::new(0., 10.),
Point2D::new(1., 10.),
Point2D::new(2., 10.),
];
let weights = [1.; 9];
// create an unbalanced partition:
// - p1: total weight = 1
// - p2: total weight = 1
// - p3: total weight = 7
let mut partition = [0, 2, 2, 2, 2, 2, 2, 2, 1];
coupe::KMeans { delta_threshold: 0.0, ..Default::default() }
.partition(&mut partition, (&points, &weights))?;
assert_eq!(partition[0], partition[1]);
assert_eq!(partition[0], partition[2]);
assert_eq!(partition[3], partition[4]);
assert_eq!(partition[3], partition[5]);
assert_eq!(partition[6], partition[7]);
assert_eq!(partition[6], partition[8]);
Fields§
§imbalance_tol: f64
§delta_threshold: f64
§max_iter: usize
§max_balance_iter: usize
§erode: bool
§hilbert: bool
§mbr_early_break: bool
Trait Implementations§
source§impl<'a, const D: usize> Partition<(&'a [Matrix<f64, Const<D>, Const<1>, ArrayStorage<f64, D, 1>>], &'a [f64])> for KMeanswhere
Const<D>: DimSub<Const<1>>,
DefaultAllocator: Allocator<f64, Const<D>, Const<D>, Buffer = ArrayStorage<f64, D, D>> + Allocator<f64, DimDiff<Const<D>, Const<1>>>,
impl<'a, const D: usize> Partition<(&'a [Matrix<f64, Const<D>, Const<1>, ArrayStorage<f64, D, 1>>], &'a [f64])> for KMeanswhere Const<D>: DimSub<Const<1>>, DefaultAllocator: Allocator<f64, Const<D>, Const<D>, Buffer = ArrayStorage<f64, D, D>> + Allocator<f64, DimDiff<Const<D>, Const<1>>>,
§type Error = Infallible
type Error = Infallible
Error details, should the algorithm fail to run.
impl Copy for KMeans
Auto Trait Implementations§
impl RefUnwindSafe for KMeans
impl Send for KMeans
impl Sync for KMeans
impl Unpin for KMeans
impl UnwindSafe for KMeans
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
§impl<T> Pointable for T
impl<T> Pointable for T
§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere SS: SubsetOf<SP>,
§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self
from the equivalent element of its
superset. Read more§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
self
is actually part of its subset T
(and can be converted to it).§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
Use with care! Same as
self.to_subset
but without any property checks. Always succeeds.§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self
to the equivalent element of its superset.