use std::collections::HashMap;
use std::error::Error;
use std::fs::read_dir;
use std::path::Path;
use rs_graph::dimacs;
use rs_graph::mcf::{NetworkSimplex, SolutionState};
use rs_graph::{IndexGraph, Net};
#[test]
fn test_network_simplex() -> Result<(), Box<dyn Error>> {
let mut values = HashMap::new();
for entry in read_dir(Path::new("tests/mcf"))? {
let entry = entry?;
if entry.path().extension().map(|ext| ext == "sol").unwrap_or(false) {
let (value, _) = dimacs::min::read_solution_from_file::<isize>(&entry.path().to_string_lossy())?;
if let Some(file_stem) = entry.path().file_stem().map(|s| s.to_string_lossy().to_string()) {
values.insert(file_stem, value);
}
}
}
for entry in read_dir(Path::new("tests/mcf"))? {
let entry = entry?;
if entry.path().extension().map(|ext| ext == "min").unwrap_or(false) {
let instance = dimacs::min::read_from_file(&entry.path().to_string_lossy())?;
let g: Net = instance.graph;
let balances = instance.balances;
let lower = instance.lower;
let upper = instance.upper;
let costs = instance.costs;
let mut spx = NetworkSimplex::new(&g);
spx.set_balances(|u| balances[g.node_id(u)]);
spx.set_lowers(|e| lower[g.edge_id(e)]);
spx.set_uppers(|e| upper[g.edge_id(e)]);
spx.set_costs(|e| costs[g.edge_id(e)]);
let state = spx.solve();
assert_eq!(state, SolutionState::Optimal);
if let Some(value) = entry
.path()
.file_name()
.and_then(|s| values.get(s.to_string_lossy().as_ref()))
{
assert_eq!(*value, spx.value());
} else {
panic!("Can't find solution file for {:?}", entry.path());
}
}
}
Ok(())
}