GLPK API Client SDK
A Rust client SDK for interacting with the GLPK REST API for solving linear programming problems.
Features
- ✅ Full support for GLPK REST API
- ✅ Type-safe request/response models
- ✅ Builder pattern for easy request construction
- ✅ Authentication support for protected endpoints
- ✅ Async/await with tokio
- ✅ Comprehensive error handling
- ✅ Health check endpoint
- ✅ Multiple objective functions
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
= { = "1.0", = ["macros", "rt-multi-thread"] }
Quick Start
use ;
async
Authentication
When the API is running in protected mode (PROTECT=true), you need to provide an API key:
let client = new?
.with_api_key;
Or use environment variables:
use env;
let api_key = var?;
let client = new?
.with_api_key;
Examples
Simple Linear Programming Problem
use ;
let request = new
.add_variable
.add_variable
// Constraint: x + y ≤ 5
.add_constraint
// Maximize: 3x + 2y
.add_objective
.direction
.build?;
let response = client.solve.await?;
Multiple Objectives
let request = new
.add_variable
.add_variable
.add_variable
.add_constraint
.add_constraint
// Objective 1: Maximize x3
.add_objective
// Objective 2: Maximize x1 + 2*x2 + x3
.add_objective
.direction
.build?;
let response = client.solve.await?;
// Returns one solution for each objective
Health Check
let client = new?;
if client.health_check.await?
API Documentation
Types
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 Minimize
Builder Methods
add_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 request
Client Methods
new(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 problem
Sparse Matrix Format
The SDK uses sparse matrix format for efficiency:
// Matrix:
// [1 0 2]
// [0 3 0]
let matrix = new;
Error Handling
All errors are wrapped in the GlpkError enum:
use GlpkError;
match client.solve.await
Running Examples
Start the GLPK API server:
Run an example:
With authentication:
License
MIT OR Apache-2.0
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.