Skip to content

Commit 7ebec40

Browse files
committed
Fix network simplex initialization
1 parent 23047b0 commit 7ebec40

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

src/lp/network_simplex.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,32 @@
22
Network simplex algorithm, inspired by networkx library implementation (https://networkx.org/documentation/stable/_modules/networkx/algorithms/flow/networksimplex.html)
33
*/
44
use std::collections::HashMap;
5+
use std::fmt::Debug;
56
use std::hash::Hash;
67
use std::iter::repeat;
78
use std::usize;
89

9-
#[derive(Clone)]
10+
#[derive(Clone, Debug)]
1011
struct Node {
1112
supply: f64,
1213
}
1314

14-
#[derive(Clone)]
15+
#[derive(Clone, Debug)]
1516
struct Edge {
1617
start: usize,
1718
end: usize,
1819
capacity: f64,
1920
cost: f64,
2021
}
2122

22-
#[derive(Clone)]
23-
pub struct Graph<T: Eq + Hash> {
23+
#[derive(Clone, Debug)]
24+
pub struct Graph<T: Eq + Hash + Debug> {
2425
nodes: Vec<Node>,
2526
node_label_to_index: HashMap<T, usize>,
2627
edges: Vec<Edge>,
2728
}
2829

29-
impl<T: Eq + Hash> Graph<T> {
30+
impl<T: Eq + Hash + Debug> Graph<T> {
3031
pub fn new() -> Graph<T> {
3132
Graph {
3233
nodes: vec![],
@@ -69,7 +70,8 @@ impl<T: Eq + Hash> Graph<T> {
6970
}
7071
}
7172

72-
struct Solution<'a, T: Eq + Hash> {
73+
#[derive(Debug)]
74+
struct Solution<'a, T: Eq + Hash + Debug> {
7375
graph: &'a Graph<T>,
7476
edge_count: usize,
7577
potentials: Vec<f64>,
@@ -118,7 +120,7 @@ impl Iterator for SubtreeIterator<'_> {
118120
}
119121
}
120122

121-
impl<T: Eq + Hash> Solution<'_, T> {
123+
impl<T: Eq + Hash + Debug> Solution<'_, T> {
122124
fn new(graph: &mut Graph<T>, eps: f64) -> Solution<T> {
123125
let n = graph.nodes.len();
124126
let m = graph.edges.len();
@@ -129,27 +131,28 @@ impl<T: Eq + Hash> Solution<'_, T> {
129131
let faux_inf = 3.0 * supplies.iter().chain(vec![cost_sum, capacity_sum].iter()).max_by(|a, b| a.partial_cmp(b).unwrap()).unwrap();
130132
// Add artificial root and edges
131133
for (i, u) in graph.nodes.iter().enumerate() {
132-
if u.supply < eps {
133-
graph.edges.push(Edge {
134+
let edge = if u.supply < -eps {
135+
Edge {
134136
start: n,
135137
end: i,
136138
capacity: faux_inf,
137139
cost: faux_inf,
138-
})
140+
}
139141
} else {
140-
graph.edges.push(Edge {
142+
Edge {
141143
start: i,
142144
end: n,
143145
capacity: faux_inf,
144146
cost: faux_inf,
145-
})
146-
}
147+
}
148+
};
149+
graph.edges.push(edge);
147150
}
148151
let bfs = Solution {
149152
graph,
150153
edge_count: m,
151154
potentials: graph.nodes.iter().map(|node| {
152-
if node.supply >= -eps {
155+
if node.supply >= 0.0 {
153156
faux_inf
154157
} else {
155158
-faux_inf
@@ -432,7 +435,7 @@ fn argmin<S: Copy>(edges: Vec<S>, func: impl Fn(S) -> f64) -> Option<S> {
432435
argmin
433436
}
434437

435-
pub fn network_simplex<T: Eq + Hash + Clone>(graph: Graph<T>, eps: f64) -> Vec<f64> {
438+
pub fn network_simplex<T: Eq + Hash + Clone + Debug>(graph: Graph<T>, eps: f64) -> Vec<f64> {
436439
let mut graph = graph.clone();
437440
let mut solution = Solution::new(&mut graph, eps);
438441

0 commit comments

Comments
 (0)