| Crates.io | glpk-api-sdk |
| lib.rs | glpk-api-sdk |
| version | 0.1.0 |
| created_at | 2026-01-09 08:52:37.72793+00 |
| updated_at | 2026-01-09 08:52:37.72793+00 |
| description | Rust client SDK for GLPK REST API |
| homepage | |
| repository | https://github.com/ourstudio-se/rust-glpk-api |
| max_upload_size | |
| id | 2031805 |
| size | 72,837 |
A Rust client SDK for interacting with the GLPK REST API for solving linear programming problems.
Add this to your Cargo.toml:
[dependencies]
glpk-api-sdk = "0.1.0"
tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] }
use glpk_api_sdk::{GlpkClient, SolveRequestBuilder, SolverDirection, Variable};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a client
let client = GlpkClient::new("http://localhost:9000")?;
// Build a request
let request = SolveRequestBuilder::new()
.add_variable(Variable::new("x1", 0, 100))
.add_variable(Variable::new("x2", 0, 100))
.add_constraint(vec![0, 0], vec![0, 1], vec![2, 3], 100)
.add_objective([("x1", 1.0), ("x2", 2.0)].into())
.direction(SolverDirection::Maximize)
.build()?;
// Solve
let response = client.solve(request).await?;
// Process results
for solution in response.solutions {
println!("Status: {:?}", solution.status);
println!("Objective: {}", solution.objective);
println!("Solution: {:?}", solution.solution);
}
Ok(())
}
When the API is running in protected mode (PROTECT=true), you need to provide an API key:
let client = GlpkClient::new("http://localhost:9000")?
.with_api_key("your-api-key");
Or use environment variables:
use std::env;
let api_key = env::var("GLPK_API_KEY")?;
let client = GlpkClient::new("http://localhost:9000")?
.with_api_key(api_key);
use glpk_api_sdk::{GlpkClient, SolveRequestBuilder, SolverDirection, Variable};
let request = SolveRequestBuilder::new()
.add_variable(Variable::new("x", 0, 10))
.add_variable(Variable::new("y", 0, 10))
// Constraint: x + y ≤ 5
.add_constraint(vec![0, 0], vec![0, 1], vec![1, 1], 5)
// Maximize: 3x + 2y
.add_objective([("x", 3.0), ("y", 2.0)].into())
.direction(SolverDirection::Maximize)
.build()?;
let response = client.solve(request).await?;
let request = SolveRequestBuilder::new()
.add_variable(Variable::new("x1", 0, 1))
.add_variable(Variable::new("x2", 0, 1))
.add_variable(Variable::new("x3", 0, 1))
.add_constraint(vec![0, 0], vec![0, 1], vec![1, 1], 1)
.add_constraint(vec![1, 1], vec![0, 2], vec![1, 1], 1)
// Objective 1: Maximize x3
.add_objective([("x3", 1.0)].into())
// Objective 2: Maximize x1 + 2*x2 + x3
.add_objective([("x1", 1.0), ("x2", 2.0), ("x3", 1.0)].into())
.direction(SolverDirection::Maximize)
.build()?;
let response = client.solve(request).await?;
// Returns one solution for each objective
let client = GlpkClient::new("http://localhost:9000")?;
if client.health_check().await? {
println!("Server is healthy");
}
Variable - Decision variable with id and boundsIntegerSparseMatrix - Sparse matrix in coordinate formatSparseLEIntegerPolyhedron - Constraint polyhedron (Ax ≤ b)SolveRequest - Complete solve requestSolveResponse - Response with solutionsSolution - Single solution with status and valuesStatus - Solution status enum (Optimal, Infeasible, etc.)SolverDirection - Maximize or Minimizeadd_variable(variable) - Add a decision variableadd_variables(variables) - Add multiple variablesadd_constraint(rows, cols, vals, b) - Add a constraintadd_objective(objective) - Add an objective functionadd_objectives(objectives) - Add multiple objectivesdirection(direction) - Set optimization directionbuild() - Build the requestnew(base_url) - Create a new clientwith_client(base_url, client) - Create with custom reqwest clientwith_api_key(key) - Set API key for authenticationhealth_check() - Check server healthsolve(request) - Solve linear programming problemThe SDK uses sparse matrix format for efficiency:
// Matrix:
// [1 0 2]
// [0 3 0]
let matrix = IntegerSparseMatrix::new(
vec![0, 0, 1], // rows
vec![0, 2, 1], // cols
vec![1, 2, 3], // vals
2, // nrows
3, // ncols
);
All errors are wrapped in the GlpkError enum:
use glpk_api_sdk::GlpkError;
match client.solve(request).await {
Ok(response) => { /* handle success */ },
Err(GlpkError::AuthenticationFailed) => { /* handle auth error */ },
Err(GlpkError::ApiError(msg)) => { /* handle API error */ },
Err(e) => { /* handle other errors */ },
}
Start the GLPK API server:
cd ..
cargo run
Run an example:
cd client-sdk
cargo run --example basic_usage
With authentication:
export GLPK_API_KEY="your-api-key"
cargo run --example with_authentication
MIT OR Apache-2.0
Contributions are welcome! Please feel free to submit a Pull Request.