Skip to content

Commit

Permalink
clippy fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Specy committed Nov 5, 2024
1 parent 070d360 commit 00b2d32
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 93 deletions.
7 changes: 0 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,3 @@ ndarray = "0.16.1"
rand = "0.8.5"
rand_pcg = "0.3.1"
env_logger = "0.11.5"


[profile.test]
opt-level = 1
[profile.dev]
opt-level = 1

2 changes: 1 addition & 1 deletion examples/tsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ fn find_min_cut(size: usize, weights: &mut [f64]) -> (f64, Vec<bool>) {
}

assert!(best_cut_weight.is_finite());
return (best_cut_weight, best_cut);
(best_cut_weight, best_cut)
}

#[cfg(test)]
Expand Down
41 changes: 20 additions & 21 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ impl Problem {
self.var_domains.iter().any(|v| *v == VarDomain::Integer)
}

/// Add a new integer variable to the problem.
/// Add a new binary variable to the problem.
///
/// `obj_coeff` is a coefficient of the term in the objective function corresponding to this variable.
pub fn add_binary_var(&mut self, obj_coeff: f64) -> Variable {
Expand Down Expand Up @@ -535,7 +535,6 @@ pub use mps::MpsFile;

#[cfg(test)]
mod tests {
use sprs::vec;

use super::*;

Expand All @@ -544,8 +543,8 @@ mod tests {
let mut problem = Problem::new(OptimizationDirection::Maximize);
let v1 = problem.add_real_var(3.0, (12.0, f64::INFINITY));
let v2 = problem.add_real_var(4.0, (5.0, f64::INFINITY));
problem.add_constraint(&[(v1, 1.0), (v2, 1.0)], ComparisonOp::Le, 20.0);
problem.add_constraint(&[(v2, -4.0), (v1, 1.0)], ComparisonOp::Ge, -20.0);
problem.add_constraint([(v1, 1.0), (v2, 1.0)], ComparisonOp::Le, 20.0);
problem.add_constraint([(v2, -4.0), (v1, 1.0)], ComparisonOp::Ge, -20.0);

let sol = problem.solve().unwrap();
assert_eq!(sol[v1], 12.0);
Expand Down Expand Up @@ -602,9 +601,9 @@ mod tests {
let mut problem = Problem::new(OptimizationDirection::Maximize);
let v1 = problem.add_real_var(1.0, (0.0, f64::INFINITY));
let v2 = problem.add_real_var(2.0, (f64::NEG_INFINITY, f64::INFINITY));
problem.add_constraint(&[(v1, 1.0), (v2, 1.0)], ComparisonOp::Le, 4.0);
problem.add_constraint(&[(v1, 1.0), (v2, 1.0)], ComparisonOp::Ge, 2.0);
problem.add_constraint(&[(v1, 1.0), (v2, -1.0)], ComparisonOp::Ge, 0.0);
problem.add_constraint([(v1, 1.0), (v2, 1.0)], ComparisonOp::Le, 4.0);
problem.add_constraint([(v1, 1.0), (v2, 1.0)], ComparisonOp::Ge, 2.0);
problem.add_constraint([(v1, 1.0), (v2, -1.0)], ComparisonOp::Ge, 0.0);

let sol = problem.solve().unwrap();
assert_eq!(sol[v1], 2.0);
Expand All @@ -617,8 +616,8 @@ mod tests {
let mut problem = Problem::new(OptimizationDirection::Maximize);
let v1 = problem.add_real_var(1.0, (0.0, 3.0));
let v2 = problem.add_real_var(2.0, (0.0, 3.0));
problem.add_constraint(&[(v1, 1.0), (v2, 1.0)], ComparisonOp::Le, 4.0);
problem.add_constraint(&[(v1, 1.0), (v2, 1.0)], ComparisonOp::Ge, 1.0);
problem.add_constraint([(v1, 1.0), (v2, 1.0)], ComparisonOp::Le, 4.0);
problem.add_constraint([(v1, 1.0), (v2, 1.0)], ComparisonOp::Ge, 1.0);

let orig_sol = problem.solve().unwrap();

Expand Down Expand Up @@ -652,15 +651,15 @@ mod tests {
let mut problem = Problem::new(OptimizationDirection::Minimize);
let v1 = problem.add_real_var(2.0, (0.0, f64::INFINITY));
let v2 = problem.add_real_var(1.0, (0.0, f64::INFINITY));
problem.add_constraint(&[(v1, 1.0), (v2, 1.0)], ComparisonOp::Le, 4.0);
problem.add_constraint(&[(v1, 1.0), (v2, 1.0)], ComparisonOp::Ge, 2.0);
problem.add_constraint([(v1, 1.0), (v2, 1.0)], ComparisonOp::Le, 4.0);
problem.add_constraint([(v1, 1.0), (v2, 1.0)], ComparisonOp::Ge, 2.0);

let orig_sol = problem.solve().unwrap();

{
let sol = orig_sol
.clone()
.add_constraint(&[(v1, -1.0), (v2, 1.0)], ComparisonOp::Le, 0.0)
.add_constraint([(v1, -1.0), (v2, 1.0)], ComparisonOp::Le, 0.0)
.unwrap();

assert_eq!(sol[v1], 1.0);
Expand All @@ -673,7 +672,7 @@ mod tests {
.clone()
.fix_var(v2, 1.5)
.unwrap()
.add_constraint(&[(v1, -1.0), (v2, 1.0)], ComparisonOp::Le, 0.0)
.add_constraint([(v1, -1.0), (v2, 1.0)], ComparisonOp::Le, 0.0)
.unwrap();
assert_eq!(sol[v1], 1.5);
assert_eq!(sol[v2], 1.5);
Expand All @@ -683,7 +682,7 @@ mod tests {
{
let sol = orig_sol
.clone()
.add_constraint(&[(v1, -1.0), (v2, 1.0)], ComparisonOp::Ge, 3.0)
.add_constraint([(v1, -1.0), (v2, 1.0)], ComparisonOp::Ge, 3.0)
.unwrap();

assert_eq!(sol[v1], 0.0);
Expand All @@ -697,8 +696,8 @@ mod tests {
let mut problem = Problem::new(OptimizationDirection::Minimize);
let v1 = problem.add_real_var(0.0, (0.0, f64::INFINITY));
let v2 = problem.add_real_var(-1.0, (0.0, f64::INFINITY));
problem.add_constraint(&[(v1, 3.0), (v2, 2.0)], ComparisonOp::Le, 6.0);
problem.add_constraint(&[(v1, -3.0), (v2, 2.0)], ComparisonOp::Le, 0.0);
problem.add_constraint([(v1, 3.0), (v2, 2.0)], ComparisonOp::Le, 6.0);
problem.add_constraint([(v1, -3.0), (v2, 2.0)], ComparisonOp::Le, 0.0);

let mut sol = problem.solve().unwrap();
assert_eq!(sol[v1], 1.0);
Expand All @@ -719,8 +718,8 @@ mod tests {
#[test]
fn knapsak_solve() {
let mut problem = Problem::new(OptimizationDirection::Maximize);
let weights = vec![10, 60, 30, 40, 30, 20, 20, 2];
let values = vec![1, 10, 15, 40, 60, 90, 100, 15];
let weights = [10, 60, 30, 40, 30, 20, 20, 2];
let values = [1, 10, 15, 40, 60, 90, 100, 15];
let capacity = 102;
let mut vars = vec![];
for i in 0..weights.len() {
Expand All @@ -729,7 +728,7 @@ mod tests {
}
let entries = vars
.iter()
.map(|v| (v.clone(), weights[v.0] as f64))
.map(|v| (*v, weights[v.0] as f64))
.collect::<Vec<_>>();
problem.add_constraint(&entries, ComparisonOp::Le, capacity as f64);
let sol = problem.solve().unwrap();
Expand All @@ -744,7 +743,7 @@ mod tests {
#[test]
fn dominating_set_solve() {
let mut problem = Problem::new(OptimizationDirection::Minimize);
let vars = vec![
let vars = [
problem.add_binary_var(1.0),
problem.add_binary_var(1.0),
problem.add_binary_var(1.0),
Expand All @@ -762,7 +761,7 @@ mod tests {
];
for row in rows {
problem.add_constraint(
&row.iter()
row.iter()
.enumerate()
.map(|(i, v)| (vars[i], *v as f64))
.collect::<Vec<_>>(),
Expand Down
23 changes: 10 additions & 13 deletions src/lu.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::cmp::Ordering;

use crate::sparse::{Error, Perm, ScatteredVec, SparseMat, TriangleMat};

#[derive(Clone)]
Expand Down Expand Up @@ -57,8 +59,8 @@ impl LUFactors {
scratch.dense_rhs.resize(rhs.len(), 0.0);

if let Some(row_perm) = &self.row_perm {
for i in 0..rhs.len() {
scratch.dense_rhs[row_perm.orig2new[i]] = rhs[i];
for (i, rhs_el) in rhs.iter().enumerate() {
scratch.dense_rhs[row_perm.orig2new[i]] = *rhs_el;
}
} else {
scratch.dense_rhs.copy_from_slice(rhs);
Expand All @@ -72,7 +74,7 @@ impl LUFactors {
rhs[col_perm.new2orig[i]] = scratch.dense_rhs[i];
}
} else {
rhs.copy_from_slice(&mut scratch.dense_rhs);
rhs.copy_from_slice(&scratch.dense_rhs);
}
}

Expand Down Expand Up @@ -168,7 +170,7 @@ pub fn lu_factorize<'a>(

scratch.mark_nonzero.run(
&mut scratch.rhs,
|new_i| &lower.col_rows(new_i),
|new_i| lower.col_rows(new_i),
|new_i| new_i < i_col,
|orig_r| orig2new_row[orig_r],
);
Expand Down Expand Up @@ -253,12 +255,10 @@ pub fn lu_factorize<'a>(
}

let new_r = orig2new_row[orig_r];
if new_r < i_col {
upper.push(new_r, val);
} else if new_r == i_col {
upper_diag.push(pivot_val);
} else {
lower.push(orig_r, val / pivot_val);
match new_r.cmp(&i_col) {
Ordering::Less => upper.push(new_r, val),
Ordering::Equal => upper_diag.push(pivot_val),
Ordering::Greater => lower.push(orig_r, val / pivot_val),
}
}

Expand Down Expand Up @@ -658,9 +658,6 @@ mod tests {
assert!(diff.norm(1.0) < 1e-5);
}

type ArrayVec = ndarray::Array1<f64>;
let dense_rhs: Vec<_> = (0..size).map(|_| rng.gen_range(0.0..1.0)).collect();

let sparse_rhs = {
let mut res = CsVec::empty(size);
for i in 0..size {
Expand Down
11 changes: 6 additions & 5 deletions src/mps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ impl MpsFile {
let name = tokens.next()?;

if name != cur_name {
if var_name2idx.get(name).is_some() {
if var_name2idx.contains_key(name) {
return Err(lines.err(&format!("variable {} already declared", name)));
}

Expand All @@ -163,7 +163,7 @@ impl MpsFile {
cur_def.obj_coeff = val;
} else if let Some(idx) = constr_name2idx.get(key) {
constraints[*idx].lhs.add(cur_var, val);
} else if free_rows.get(key).is_none() {
} else if !free_rows.contains(key) {
return Err(lines.err(&format!("unknown constraint: {}", key)));
}
}
Expand Down Expand Up @@ -337,6 +337,7 @@ struct Lines<R: io::BufRead> {
}

impl<R: io::BufRead> Lines<R> {
#[allow(clippy::wrong_self_convention)]
fn to_next(&mut self) -> io::Result<()> {
loop {
self.idx += 1;
Expand Down Expand Up @@ -390,14 +391,14 @@ impl<'a> Tokens<'a> {
}

fn parse_f64(input: &str, line_idx: usize) -> io::Result<f64> {
input.parse().or_else(|_| {
Err(io::Error::new(
input.parse().map_err(|_| {
io::Error::new(
io::ErrorKind::InvalidData,
format!(
"line {}: couldn't parse float from string: `{}`",
line_idx, input
),
))
)
})
}

Expand Down
11 changes: 6 additions & 5 deletions src/ordering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ pub fn order_colamd<'a>(
rows[i].end += prev_end;
}

for c in 0..size {
for &r in cols[c].elems(&row_storage) {
for (c, col) in cols.iter().enumerate().take(size) {
for &r in col.elems(&row_storage) {
let row = &mut rows[r];
col_storage[row.begin] = c;
row.begin += 1;
Expand Down Expand Up @@ -134,8 +134,7 @@ pub fn order_colamd<'a>(
// all columns on the stack are of size 1
stack.clear();
stack.push(c);
while !stack.is_empty() {
let c = stack.pop().unwrap();
while let Some(c) = stack.pop() {
let r = *cols[c]
.elems(&row_storage)
.iter()
Expand Down Expand Up @@ -490,7 +489,7 @@ pub fn find_diag_matching<'a>(
});

'dfs_loop: while !dfs_stack.is_empty() {
let mut cur_step = dfs_stack.last_mut().unwrap();
let cur_step = dfs_stack.last_mut().unwrap();
let c = cur_step.col;
let col_rows = get_col(c);

Expand Down Expand Up @@ -546,8 +545,10 @@ pub fn find_diag_matching<'a>(
/// Lower block triangular form of a matrix.
#[derive(Clone, Debug)]
pub struct BlockDiagForm {
#[allow(unused)]
/// Row permutation: for each original row its new row number so that diag is nonzero.
pub row2col: Vec<usize>,
#[allow(unused)]
/// For each block its set of columns (the order of blocks is lower block triangular)
pub block_cols: Vec<Vec<usize>>,
}
Expand Down
Loading

0 comments on commit 00b2d32

Please sign in to comment.