Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ members = [
[package]
name = "problemreductions"
version = "0.6.0"
edition = "2021"
edition = "2024"
rust-version = "1.88"
description = "A library for reducing NP-hard problems"
license = "MIT"
repository = "https://github.com/CodingThrust/problem-reductions"
Expand Down
2 changes: 1 addition & 1 deletion benches/solver_benchmarks.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Benchmarks for the BruteForce solver on various problem types.

use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main};
use problemreductions::models::formula::*;
use problemreductions::models::graph::*;
use problemreductions::models::misc::*;
Expand Down
2 changes: 1 addition & 1 deletion examples/detect_isolated_problems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
//!
//! Run with: `cargo run --example detect_isolated_problems`

use problemreductions::rules::analysis::check_connectivity;
use problemreductions::rules::ReductionGraph;
use problemreductions::rules::analysis::check_connectivity;

fn main() {
let graph = ReductionGraph::new();
Expand Down
2 changes: 1 addition & 1 deletion examples/detect_unreachable_from_3sat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
//!
//! Run with: `cargo run --example detect_unreachable_from_3sat`

use problemreductions::rules::analysis::{check_reachability_from_3sat, UnreachableReason};
use problemreductions::rules::ReductionGraph;
use problemreductions::rules::analysis::{UnreachableReason, check_reachability_from_3sat};

fn main() {
let graph = ReductionGraph::new();
Expand Down
63 changes: 31 additions & 32 deletions examples/export_mapping_stages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

use problemreductions::rules::unitdiskmapping::{
_internal::{
create_copylines, mis_overhead_copyline, mis_overhead_copyline_triangular, CopyLine,
MappingGrid,
CopyLine, MappingGrid, create_copylines, mis_overhead_copyline,
mis_overhead_copyline_triangular,
},
ksg, triangular,
};
Expand Down Expand Up @@ -110,21 +110,21 @@ fn extract_grid_nodes(grid: &MappingGrid) -> Vec<GridNodeExport> {
let (rows, cols) = grid.size();
for r in 0..rows {
for c in 0..cols {
if let Some(cell) = grid.get(r, c) {
if !cell.is_empty() {
let state = match cell {
CellState::Occupied { .. } => "O",
CellState::Doubled { .. } => "D",
CellState::Connected { .. } => "C",
CellState::Empty => ".",
};
nodes.push(GridNodeExport {
row: r as i64, // 0-indexed - DO NOT change!
col: c as i64, // 0-indexed - DO NOT change!
weight: cell.weight(),
state: state.to_string(),
});
}
if let Some(cell) = grid.get(r, c)
&& !cell.is_empty()
{
let state = match cell {
CellState::Occupied { .. } => "O",
CellState::Doubled { .. } => "D",
CellState::Connected { .. } => "C",
CellState::Empty => ".",
};
nodes.push(GridNodeExport {
row: r as i64, // 0-indexed - DO NOT change!
col: c as i64, // 0-indexed - DO NOT change!
weight: cell.weight(),
state: state.to_string(),
});
}
}
}
Expand Down Expand Up @@ -159,21 +159,20 @@ fn crossat_triangular(

fn get_vertex_order_from_julia(graph_name: &str) -> Option<Vec<usize>> {
let path = format!("tests/data/{}_triangular_trace.json", graph_name);
if let Ok(content) = fs::read_to_string(&path) {
if let Ok(data) = serde_json::from_str::<serde_json::Value>(&content) {
if let Some(copy_lines) = data["copy_lines"].as_array() {
let mut lines: Vec<_> = copy_lines
.iter()
.filter_map(|cl| {
let vertex = cl["vertex"].as_u64()? as usize;
let vslot = cl["vslot"].as_u64()? as usize;
Some((vertex - 1, vslot)) // Convert to 0-indexed
})
.collect();
lines.sort_by_key(|(_, vslot)| *vslot);
return Some(lines.into_iter().map(|(v, _)| v).collect());
}
}
if let Ok(content) = fs::read_to_string(&path)
&& let Ok(data) = serde_json::from_str::<serde_json::Value>(&content)
&& let Some(copy_lines) = data["copy_lines"].as_array()
{
let mut lines: Vec<_> = copy_lines
.iter()
.filter_map(|cl| {
let vertex = cl["vertex"].as_u64()? as usize;
let vslot = cl["vslot"].as_u64()? as usize;
Some((vertex - 1, vslot)) // Convert to 0-indexed
})
.collect();
lines.sort_by_key(|(_, vslot)| *vslot);
return Some(lines.into_iter().map(|(v, _)| v).collect());
}
None
}
Expand Down
2 changes: 1 addition & 1 deletion examples/export_petersen_mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
//! - `docs/paper/static/petersen_square_unweighted.json` - Unweighted King's subgraph
//! - `docs/paper/static/petersen_triangular.json` - Weighted triangular lattice

use problemreductions::rules::unitdiskmapping::{ksg, triangular, MappingResult};
use problemreductions::rules::unitdiskmapping::{MappingResult, ksg, triangular};
use serde::Serialize;
use std::fs;
use std::path::Path;
Expand Down
2 changes: 1 addition & 1 deletion examples/export_schemas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//!
//! Run with: `cargo run --example export_schemas [output_path]`

use problemreductions::registry::{collect_schemas, ProblemSchemaEntry};
use problemreductions::registry::{ProblemSchemaEntry, collect_schemas};
use std::path::{Path, PathBuf};

pub fn run(output_path: &Path) {
Expand Down
3 changes: 2 additions & 1 deletion problemreductions-cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
[package]
name = "problemreductions-cli"
version = "0.6.0"
edition = "2021"
edition = "2024"
rust-version = "1.88"
description = "CLI tool for exploring NP-hard problem reductions"
license = "MIT"
repository = "https://github.com/CodingThrust/problem-reductions"
Expand Down
2 changes: 1 addition & 1 deletion problemreductions-cli/src/bin/pred_sym.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use clap::{Parser, Subcommand};
use problemreductions::{big_o_normal_form, evaluate_approximate, Expr, ProblemParameters};
use problemreductions::{Expr, ProblemParameters, big_o_normal_form, evaluate_approximate};

#[derive(Parser)]
#[command(
Expand Down
64 changes: 32 additions & 32 deletions problemreductions-cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,10 +392,10 @@ pub fn print_subcommand_help_hint(error_msg: &str) {
let cmd = Cli::command();
for (pattern, name) in subcmds {
if first_line.contains(pattern) {
if let Some(sub) = cmd.find_subcommand(name) {
if let Some(help) = sub.get_after_help() {
eprintln!("\n{help}");
}
if let Some(sub) = cmd.find_subcommand(name)
&& let Some(help) = sub.get_after_help()
{
eprintln!("\n{help}");
}
return;
}
Expand Down Expand Up @@ -471,16 +471,18 @@ mod tests {
.kind(),
ErrorKind::UnknownArgument
);
assert!(Cli::try_parse_from([
"pred",
"create",
"ThreePartition",
"--sizes",
"1,1,1",
"--bound",
"9223372036854775808",
])
.is_err());
assert!(
Cli::try_parse_from([
"pred",
"create",
"ThreePartition",
"--sizes",
"1,1,1",
"--bound",
"9223372036854775808",
])
.is_err()
);
Cli::try_parse_from([
"pred",
"create",
Expand Down Expand Up @@ -567,25 +569,23 @@ mod tests {

#[test]
fn extract_requires_exactly_one_mapping_input() {
assert!(Cli::try_parse_from([
"pred",
"extract",
"bundle.json",
"--config",
"[true,false]"
])
.is_ok());
assert!(
Cli::try_parse_from(["pred", "extract", "bundle.json", "--config", "[true,false]"])
.is_ok()
);
assert!(Cli::try_parse_from(["pred", "extract", "bundle.json"]).is_err());
assert!(Cli::try_parse_from(["pred", "extract", "bundle.json", "--value", "2"]).is_ok());
assert!(Cli::try_parse_from([
"pred",
"extract",
"bundle.json",
"--config",
"[true,false]",
"--value",
"2"
])
.is_err());
assert!(
Cli::try_parse_from([
"pred",
"extract",
"bundle.json",
"--config",
"[true,false]",
"--value",
"2"
])
.is_err()
);
}
}
4 changes: 2 additions & 2 deletions problemreductions-cli/src/commands/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::dispatch::ProblemJsonOutput;
use crate::output::OutputConfig;
use crate::problem_name::{resolve_problem_ref, unknown_problem_error};
use crate::util;
use anyhow::{bail, Context, Result};
use anyhow::{Context, Result, bail};
use num_bigint::BigUint;
use problemreductions::export::{ModelExample, ProblemRef, ProblemSide, RuleExample};
use problemreductions::models::formula::Quantifier;
Expand All @@ -19,7 +19,7 @@ use std::collections::{BTreeMap, BTreeSet};
mod schema_support;
use self::schema_support::*;
pub(crate) use self::schema_support::{
create_inputs_for, resolve_schema_field_type, InputValueKind,
InputValueKind, create_inputs_for, resolve_schema_field_type,
};

fn all_data_flags_empty(args: &CreateArgs) -> bool {
Expand Down
43 changes: 25 additions & 18 deletions problemreductions-cli/src/commands/create/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1059,9 +1059,10 @@ fn test_create_generalized_hex_requires_sink() {
};

let err = create(&args, &out).unwrap_err();
assert!(err
.to_string()
.contains("missing required construction input(s): sink"));
assert!(
err.to_string()
.contains("missing required construction input(s): sink")
);
}

#[test]
Expand Down Expand Up @@ -1202,9 +1203,10 @@ fn test_create_production_planning_requires_all_period_vectors() {
};

let err = create(&args, &out).unwrap_err();
assert!(err
.to_string()
.contains("missing required construction input(s): production_costs"));
assert!(
err.to_string()
.contains("missing required construction input(s): production_costs")
);
}

#[test]
Expand Down Expand Up @@ -1241,9 +1243,10 @@ fn test_create_production_planning_rejects_mismatched_period_lengths() {
};

let err = create(&args, &out).unwrap_err();
assert!(err
.to_string()
.contains("all per-period vectors must have length num_periods"));
assert!(
err.to_string()
.contains("all per-period vectors must have length num_periods")
);
}

#[test]
Expand Down Expand Up @@ -1465,9 +1468,10 @@ fn test_create_longest_path_requires_edge_lengths() {
};

let err = create(&args, &out).unwrap_err();
assert!(err
.to_string()
.contains("missing required construction input(s): edge_lengths"));
assert!(
err.to_string()
.contains("missing required construction input(s): edge_lengths")
);
}

#[test]
Expand Down Expand Up @@ -1522,9 +1526,10 @@ fn test_create_undirected_flow_lower_bounds_requires_lower_bounds() {
};

let err = create(&args, &out).unwrap_err();
assert!(err
.to_string()
.contains("missing required construction input(s): lower_bounds"));
assert!(
err.to_string()
.contains("missing required construction input(s): lower_bounds")
);
}

fn empty_args() -> CreateArgs {
Expand Down Expand Up @@ -2143,9 +2148,11 @@ fn test_create_kclique() {
let problem: KClique<SimpleGraph> = serde_json::from_value(created.data).unwrap();
assert_eq!(problem.k(), 3);
assert_eq!(problem.num_vertices(), 5);
assert!(problem
.evaluate(&vec![false, false, true, true, true])
.unwrap());
assert!(
problem
.evaluate(&vec![false, false, true, true, true])
.unwrap()
);

let _ = std::fs::remove_file(output_path);
}
Expand Down
2 changes: 1 addition & 1 deletion problemreductions-cli/src/commands/evaluate.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::dispatch::{load_problem, read_input, ProblemJson};
use crate::dispatch::{ProblemJson, load_problem, read_input};
use crate::output::OutputConfig;
use anyhow::{Context, Result};
use std::path::Path;
Expand Down
2 changes: 1 addition & 1 deletion problemreductions-cli/src/commands/extract.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::cli::ExtractArgs;
use crate::dispatch::{extract_bundle_value, read_input, BundleReplay, ReductionBundle};
use crate::dispatch::{BundleReplay, ReductionBundle, extract_bundle_value, read_input};
use crate::output::OutputConfig;
use anyhow::{Context, Result};

Expand Down
Loading
Loading