Skip to main content

MaxParametricSolver

Struct MaxParametricSolver 

Source
pub struct MaxParametricSolver<'a, V, R, P>
where R: Copy + PartialOrd + Add<Output = R> + Sub<Output = R> + Mul<Output = R> + Div<Output = R> + Neg<Output = R> + Inv<Output = R>, V: Eq + Hash + Clone, P: ParametricAPI<V, R>,
{ /* private fields */ }
Expand description

Maximum parametric shortest path solver.

$$ d_j(\lambda) = \min_{k} \big( d_k(\lambda) + \text{cost}(k, j, \lambda) \big) $$

Finds the minimum ratio cycle in a directed graph using Howard’s algorithm for negative cycle detection.

Implementations§

Source§

impl<'a, V, R, P> MaxParametricSolver<'a, V, R, P>
where R: Copy + PartialOrd + Zero + One + Add<Output = R> + Sub<Output = R> + Mul<Output = R> + Div<Output = R> + Neg<Output = R> + Inv<Output = R>, V: Eq + Hash + Clone, P: ParametricAPI<V, R>,

Source

pub fn new(gra: &'a DiGraph<V, R>, omega: P) -> Self

Source

pub fn run( &mut self, dist: &mut [R], ratio: &mut R, ) -> Vec<EdgeReference<'a, R>>

The function run finds the minimum ratio and corresponding cycle in a given graph.

$$ r^* = \min_{C \in \text{cycles}} \frac{\sum_{(u,v) \in C} w_{uv}}{|C|} $$

Arguments:

  • dist: dist is a mutable reference to a slice of type R. It represents a distance matrix or array, where R is the type of the elements in the matrix.
  • ratio: The ratio parameter is a mutable reference to a value of type R. It represents the current ratio value that is being used in the algorithm. The algorithm will update this value if it finds a smaller ratio during its execution.

Returns:

a vector of EdgeReference<R>.

§Example
use petgraph::graph::DiGraph;
use petgraph::prelude::*;
use num::rational::Ratio;
use netoptim_rs::parametric::{MaxParametricSolver, ParametricAPI};
use petgraph::graph::EdgeReference;

struct TestParametricAPI;

impl ParametricAPI<(), Ratio<i32>> for TestParametricAPI {
    fn distance(&self, ratio: &Ratio<i32>, edge: &EdgeReference<Ratio<i32>>) -> Ratio<i32> {
        *edge.weight() - *ratio
    }

    fn zero_cancel(&self, cycle: &[EdgeReference<Ratio<i32>>]) -> Ratio<i32> {
        let mut sum_a = Ratio::new(0, 1);
        let mut sum_b = Ratio::new(0, 1);
        for edge in cycle {
            sum_a += *edge.weight();
            sum_b += Ratio::new(1, 1);
        }
        sum_a / sum_b
    }
}

let digraph = DiGraph::<(), Ratio<i32>>::from_edges(&[
    (0, 1, Ratio::new(1, 1)),
    (1, 2, Ratio::new(1, 1)),
    (2, 0, Ratio::new(-3, 1)),
]);

let mut solver = MaxParametricSolver::new(&digraph, TestParametricAPI);
let mut dist = [Ratio::new(0, 1), Ratio::new(0, 1), Ratio::new(0, 1)];
let mut ratio = Ratio::new(0, 1);

let cycle = solver.run(&mut dist, &mut ratio);
assert!(!cycle.is_empty());
assert_eq!(ratio, Ratio::new(-1, 3));

Trait Implementations§

Source§

impl<'a, V, R, P> Debug for MaxParametricSolver<'a, V, R, P>
where R: Copy + PartialOrd + Add<Output = R> + Sub<Output = R> + Mul<Output = R> + Div<Output = R> + Neg<Output = R> + Inv<Output = R> + Debug, V: Eq + Hash + Clone + Debug, P: ParametricAPI<V, R> + Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a, V, R, P> Freeze for MaxParametricSolver<'a, V, R, P>
where P: Freeze,

§

impl<'a, V, R, P> RefUnwindSafe for MaxParametricSolver<'a, V, R, P>

§

impl<'a, V, R, P> Send for MaxParametricSolver<'a, V, R, P>
where P: Send, V: Sync, R: Sync,

§

impl<'a, V, R, P> Sync for MaxParametricSolver<'a, V, R, P>
where P: Sync, V: Sync, R: Sync,

§

impl<'a, V, R, P> Unpin for MaxParametricSolver<'a, V, R, P>
where P: Unpin,

§

impl<'a, V, R, P> UnsafeUnpin for MaxParametricSolver<'a, V, R, P>
where P: UnsafeUnpin,

§

impl<'a, V, R, P> UnwindSafe for MaxParametricSolver<'a, V, R, P>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.