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.
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>
impl<'a, V, R, P> MaxParametricSolver<'a, V, R, P>
Sourcepub fn new(grph: &'a DiGraph<V, R>, omega: P) -> Self
pub fn new(grph: &'a DiGraph<V, R>, omega: P) -> Self
The function creates a new instance of a struct with a given directed graph and a value.
Arguments:
grph: Thegrphparameter is a reference to a directed graph (DiGraph) with vertices of typeVand edges of typeR.omega: Theomegaparameter is of typeP. It represents some value or parameter that is used in the implementation of thenewfunction. The specific meaning or purpose ofomegawould depend on the context and the code that uses this function.
Returns:
The new function is returning an instance of the struct that it is defined in.
Sourcepub fn run(
&mut self,
dist: &mut [R],
ratio: &mut R,
) -> Vec<EdgeReference<'a, R>>
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.
Arguments:
dist:distis a mutable reference to a slice of typeR. It represents a distance matrix or array, whereRis the type of the elements in the matrix.ratio: Theratioparameter is a mutable reference to a value of typeR. 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>
impl<'a, V, R, P> Debug for MaxParametricSolver<'a, V, R, P>
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>
impl<'a, V, R, P> Sync for MaxParametricSolver<'a, V, R, P>
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> 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