| Crates.io | ockham |
| lib.rs | ockham |
| version | 0.1.1 |
| created_at | 2025-12-03 23:31:10.962158+00 |
| updated_at | 2025-12-03 23:36:23.992851+00 |
| description | A comprehensive Operations Research toolkit for linear programming, optimization, and mathematical modeling |
| homepage | |
| repository | https://github.com/ethqnol/ockham-rs |
| max_upload_size | |
| id | 1965472 |
| size | 254,131 |
comprehensive OR toolkit for LP, optimization & math modeling in rust. provides efficient, numerically stable algorithms w/ clean API.
Add Ockham to your Cargo.toml:
[dependencies]
ockham = "0.1.0"
use ockham::prelude::*;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a simple linear program: maximize 3x + 2y subject to x + y <= 10, x,y >= 0
let problem = ProblemBuilder::new()
.add_non_negative_variable("x")?
.add_non_negative_variable("y")?
.add_constraint_by_name(
&[("x", 1.0), ("y", 1.0)],
ConstraintType::LessEqual,
10.0,
)?
.objective_by_name(
&[("x", 3.0), ("y", 2.0)],
ObjectiveType::Maximize,
)?
.build()?;
// Solve the problem
let solution = solve(&problem)?;
if solution.is_optimal() {
println!("Optimal solution found!");
println!("x = {:.2}", solution.variables[0]);
println!("y = {:.2}", solution.variables[1]);
println!("Objective value = {:.2}", solution.objective_value);
}
Ok(())
}
The repository includes several detailed examples:
Run examples with:
cargo run --example basic_lp
cargo run --example production_planning
cargo run --example diet_problem
use ockham::prelude::*;
let mut builder = ProblemBuilder::new();
// Continuous variables
builder = builder.add_variable("x", 0.0, 100.0)?; // Bounded
builder = builder.add_non_negative_variable("y")?; // Non-negative
builder = builder.add_free_variable("z")?; // Unbounded
// Integer variables
builder = builder.add_integer_variable("i", 0.0, 10.0)?; // Integer
builder = builder.add_binary_variable("b")?; // Binary (0 or 1)
// Using variable names (recommended)
builder = builder.add_constraint_by_name(
&[("x", 2.0), ("y", 3.0)],
ConstraintType::LessEqual,
15.0,
)?;
// Using coefficient vectors
builder = builder.add_constraint(
vec![1.0, 1.0, 0.0],
ConstraintType::Equal,
5.0,
)?;
// Named constraints for debugging
builder = builder.add_named_constraint(
"capacity",
vec![2.0, 3.0],
ConstraintType::LessEqual,
100.0,
)?;
// Maximize profit
builder = builder.objective_by_name(
&[("x", 10.0), ("y", 15.0)],
ObjectiveType::Maximize,
)?;
// Minimize cost with constant term
let objective = Objective::new_with_constant(
vec![2.0, 3.0],
ObjectiveType::Minimize,
100.0, // Fixed cost
)?;
Automatically simplify problems before solving:
let presolve = Presolve::new();
let result = presolve.apply(&problem)?;
println!("{}", result.statistics);
Improve numerical stability:
let scaling = Scaling::new(ScalingMethod::Equilibration);
let scaled_problem = scaling.apply(&problem)?;
let config = SolverConfig::new()
.with_max_iterations(10_000)
.with_optimality_tolerance(1e-9)
.verbose()
.with_option("pivot_rule", "steepest_edge");
let solution = solve_with_config(&problem, &config)?;
let validator = Validator::new();
let validation = validator.validate(&problem);
if !validation.is_valid {
for error in &validation.errors {
eprintln!("Error: {}", error);
}
}
Ockham is designed for performance:
Run benchmarks:
cargo bench
default = ["serde"]
serde: Serialization support for problems and solutions
parallel: Parallel processing with Rayon
linalg: Advanced linear algebra with nalgebra
Licensed under MIT license (http://opensource.org/licenses/MIT)