Skip to content

Commit

Permalink
WIP: Rework trait system
Browse files Browse the repository at this point in the history
  • Loading branch information
hhirtz committed Sep 22, 2021
1 parent 01bd7c2 commit 1102e04
Show file tree
Hide file tree
Showing 5 changed files with 562 additions and 709 deletions.
14 changes: 4 additions & 10 deletions src/algorithms/fiduccia_mattheyses.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
use itertools::Itertools;
use nalgebra::{allocator::Allocator, DefaultAllocator, DimName};
use sprs::CsMatView;

use crate::partition::Partition;
use crate::PointND;
use crate::ProcessUniqueId;
use std::collections::HashMap;

pub fn fiduccia_mattheyses<'a, D>(
initial_partition: &mut Partition<'a, PointND<D>, f64>,
pub fn fiduccia_mattheyses(
ids: &mut [ProcessUniqueId],
adjacency: CsMatView<f64>,
weights: &[f64],
max_passes: impl Into<Option<usize>>,
max_flips_per_pass: impl Into<Option<usize>>,
max_imbalance_per_flip: impl Into<Option<f64>>,
max_bad_move_in_a_row: usize,
) where
D: DimName,
DefaultAllocator: Allocator<f64, D>,
<DefaultAllocator as Allocator<f64, D>>::Buffer: Send + Sync,
)
{
let max_passes = max_passes.into();
let max_flips_per_pass = max_flips_per_pass.into();
let max_imbalance_per_flip = max_imbalance_per_flip.into();
let (_points, weights, ids) = initial_partition.as_raw_mut();

fiduccia_mattheyses_impl(
weights,
Expand Down
23 changes: 6 additions & 17 deletions src/algorithms/kernighan_lin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,20 @@
//! At each iteration, two nodes of different partition will be swapped, decreasing the overall cutsize
//! of the partition. The swap is performed in such a way that the added partition imbalanced is controlled.
use crate::geometry::PointND;
use crate::partition::Partition;
use crate::ProcessUniqueId;

use itertools::Itertools;
use nalgebra::allocator::Allocator;
use nalgebra::DefaultAllocator;
use nalgebra::DimName;
use sprs::CsMatView;

pub(crate) fn kernighan_lin<'a, D>(
initial_partition: &mut Partition<'a, PointND<D>, f64>,
pub(crate) fn kernighan_lin(
ids: &mut [ProcessUniqueId],
adjacency: CsMatView<f64>,
weights: &[f64],
max_passes: impl Into<Option<usize>>,
max_flips_per_pass: impl Into<Option<usize>>,
max_imbalance_per_flip: impl Into<Option<f64>>,
max_bad_move_in_a_row: usize,
) where
D: DimName,
DefaultAllocator: Allocator<f64, D>,
<DefaultAllocator as Allocator<f64, D>>::Buffer: Send + Sync,
)
{
// To adapt Kernighan-Lin to a partition of more than 2 parts,
// we apply the algorithm to each pair of adjacent parts (two parts
Expand All @@ -33,7 +26,6 @@ pub(crate) fn kernighan_lin<'a, D>(
let max_passes = max_passes.into();
let max_flips_per_pass = max_flips_per_pass.into();
let max_imbalance_per_flip = max_imbalance_per_flip.into();
let (_points, weights, ids) = initial_partition.as_raw_mut();

kernighan_lin_2_impl(
weights,
Expand All @@ -46,18 +38,15 @@ pub(crate) fn kernighan_lin<'a, D>(
);
}

fn kernighan_lin_2_impl<D>(
fn kernighan_lin_2_impl(
weights: &[f64],
adjacency: CsMatView<f64>,
initial_partition: &mut [ProcessUniqueId],
max_passes: Option<usize>,
max_flips_per_pass: Option<usize>,
_max_imbalance_per_flip: Option<f64>,
max_bad_move_in_a_row: usize,
) where
D: DimName,
DefaultAllocator: Allocator<f64, D>,
<DefaultAllocator as Allocator<f64, D>>::Buffer: Send + Sync,
)
{
let unique_ids = initial_partition
.iter()
Expand Down
5 changes: 2 additions & 3 deletions src/algorithms/kk.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::collections::BinaryHeap;
use std::ops::Sub;
use std::ops::SubAssign;

use num::Zero;

Expand Down Expand Up @@ -53,7 +52,7 @@ where
/// Implementation of the Karmarkar-Karp algorithm
pub fn kk<T, I>(partition: &mut [usize], weights: I, num_parts: usize)
where
T: Zero + Ord + Sub<Output = T> + SubAssign + Copy,
T: Zero + Ord + Sub<Output = T> + Copy,
I: IntoIterator<Item = T>,
<I as IntoIterator>::IntoIter: ExactSizeIterator,
{
Expand Down Expand Up @@ -110,7 +109,7 @@ where

let emin = e[e.len() - 1].0;
for ei in &mut e {
ei.0 -= emin;
ei.0 = ei.0 - emin;
}
opposites.push(tuples);
m.push(e);
Expand Down
Loading

0 comments on commit 1102e04

Please # to comment.