cnvx is a Rust library for modeling and solving optimization problems. It provides a clean and flexible API with an emphasis on clarity and performance.
Additionally, cnvx offers a command-line interface (CLI) for solving models directly from files in various formats, making it easy to experiment without writing Rust code.
- Define decision variables with bounds, types (continuous, binary, integer), and custom constraints.
- Add linear or convex objectives with expressive operators.
- Modular API with prelude imports for ergonomic usage.
- Lightweight and extendable for integrating new solvers and algorithms.
- CLI support for solving optimization models from standard formats like GMPL, or MPS.
cnvx can be customized using Cargo features:
lp– enable linear programming solvers (default)mop– enable multi-objective problem support (planned)sat– enable satisfiability problem support (planned)nlp– enable non-linear problem support (planned)
These features allow the use of specific solvers and functionalities while keeping the core library lightweight.
cnvx can be used either as a library in your Rust projects or via its command-line interface (CLI).
# Library usage
cargo add cnvx --features "lp"
# CLI usage
cargo install cnvx-cliThe following example demonstrates how to define and solve a simple linear programming problem using cnvx:
use cnvx::prelude::*;
fn main() -> Result<(), SolveError> {
// Create a new optimization model
let mut model = Model::new();
// Variable definitions
let x = model.add_var().finish();
let y = model.add_var().finish();
// Objective function
model.add_objective(Objective::maximize(3.0 * x + 2.0 * y).name("profit"));
// Constraints
model += (x + y).eq(4.0);
model += (2.0 * x + 3.0 * y).eq(9.0);
// Solve the model using the a linear programming solver
let solver = LpSolver::new();
let sol = solver.solve(&model)?;
println!(
"Solution: x = {}, y = {}, objective = {}",
sol.value(x),
sol.value(y),
sol.objective_value.unwrap_or_default()
);
Ok(())
}Contributions to cnvx are welcome via pull requests and issues.
To ensure certain quality standards, this project uses pre-commit hooks. These hooks can be registerd by running the following command:
cp .githooks/pre-commit .git/hooksTo make use of some more advanced formatting rules, the nightly toolchain is required. Note that the nightly toolchain is only used for formatting. The nightly version can be installed using the following command:
rustup toolchain install --component rustfmt -- nightlyLicensed under the MIT License. See the LICENSE file for more details.