use rs_graph::dimacs::min;
use rs_graph::{mps, Net};
use rustop::opts;
fn int_vec(x: Vec<f64>, what: &str) -> Result<Vec<isize>, Box<dyn std::error::Error>> {
x.into_iter()
.map(|v| {
if v.fract() == 0.0 {
Ok(v.trunc() as isize)
} else {
Err(format!("Found non-integral {} value: {}", what, v).into())
}
})
.collect()
}
fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
let (args, _) = opts! {
synopsis "Convert a file in MPS format to Dimacs format";
param mps_file:String, desc:"Name of the MPS input file";
param dimacs_file:String, desc:"Name of the Dimacs output file";
}
.parse_or_exit();
let mps_inst = mps::read::<Net, _>(zopen::read(&args.mps_file)?)?;
let min_inst = min::Instance {
graph: mps_inst.graph,
balances: int_vec(mps_inst.balances, "balance")?,
lower: int_vec(mps_inst.lower, "lower")?,
upper: int_vec(mps_inst.upper, "upper")?,
costs: int_vec(mps_inst.costs, "cost")?,
};
min::write(&mut zopen::write(&args.dimacs_file)?, &min_inst)?;
Ok(())
}