Skip to main content

glpk_api_sdk/
types.rs

1use serde::{Deserialize, Serialize};
2use std::collections::HashMap;
3
4/// Variable bounds (lower_bound, upper_bound)
5pub type Bound = (i32, i32);
6
7/// A variable in the linear programming problem
8#[derive(Debug, Clone, Serialize, Deserialize)]
9pub struct Variable {
10    /// Variable identifier
11    pub id: String,
12    /// Variable bounds (lower, upper)
13    pub bound: Bound,
14}
15
16impl Variable {
17    /// Create a new variable with the given id and bounds
18    pub fn new(id: impl Into<String>, lower: i32, upper: i32) -> Self {
19        Self {
20            id: id.into(),
21            bound: (lower, upper),
22        }
23    }
24}
25
26/// Matrix shape specification
27#[derive(Debug, Clone, Serialize, Deserialize)]
28pub struct Shape {
29    /// Number of rows
30    pub nrows: usize,
31    /// Number of columns
32    pub ncols: usize,
33}
34
35/// Sparse matrix representation using coordinate format
36#[derive(Debug, Clone, Serialize, Deserialize)]
37pub struct IntegerSparseMatrix {
38    /// Row indices (0-based)
39    pub rows: Vec<i32>,
40    /// Column indices (0-based)
41    pub cols: Vec<i32>,
42    /// Values at the specified positions
43    pub vals: Vec<i32>,
44    /// Matrix dimensions
45    pub shape: Shape,
46}
47
48impl IntegerSparseMatrix {
49    /// Create a new sparse matrix
50    pub fn new(rows: Vec<i32>, cols: Vec<i32>, vals: Vec<i32>, nrows: usize, ncols: usize) -> Self {
51        Self {
52            rows,
53            cols,
54            vals,
55            shape: Shape { nrows, ncols },
56        }
57    }
58}
59
60/// A polyhedron defined by linear constraints Ax ≤ b
61#[derive(Debug, Clone, Serialize, Deserialize)]
62pub struct SparseLEIntegerPolyhedron {
63    /// Constraint coefficient matrix
64    #[serde(rename = "A")]
65    pub a: IntegerSparseMatrix,
66    /// Right-hand side constraint values
67    pub b: Vec<i32>,
68    /// Decision variables
69    pub variables: Vec<Variable>,
70}
71
72/// Direction for optimization
73#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
74#[serde(rename_all = "lowercase")]
75pub enum SolverDirection {
76    /// Maximize the objective function
77    Maximize,
78    /// Minimize the objective function
79    Minimize,
80}
81
82/// Objective function as a mapping from variable names to coefficients
83pub type Objective = HashMap<String, f64>;
84
85/// Request to solve one or more linear programming problems
86#[derive(Debug, Clone, Serialize, Deserialize)]
87pub struct SolveRequest {
88    /// The constraint polyhedron
89    pub polyhedron: SparseLEIntegerPolyhedron,
90    /// One or more objective functions to optimize
91    pub objectives: Vec<Objective>,
92    /// Whether to maximize or minimize
93    pub direction: SolverDirection,
94}
95
96/// Solution status codes
97#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
98pub enum Status {
99    /// Solution status is undefined
100    Undefined = 1,
101    /// Solution is feasible
102    Feasible = 2,
103    /// Problem is infeasible
104    Infeasible = 3,
105    /// No feasible solution exists
106    NoFeasible = 4,
107    /// Optimal solution found
108    Optimal = 5,
109    /// Problem is unbounded
110    Unbounded = 6,
111    /// Simplex method failed
112    SimplexFailed = 7,
113    /// Mixed-integer programming failed
114    MIPFailed = 8,
115    /// Search space is empty
116    EmptySpace = 9,
117}
118
119/// A single solution for one objective function
120#[derive(Debug, Clone, Serialize, Deserialize)]
121pub struct Solution {
122    /// Solution status
123    pub status: Status,
124    /// Objective value achieved
125    pub objective: i32,
126    /// Variable assignments
127    pub solution: HashMap<String, i64>,
128    /// Error message, if any
129    pub error: Option<String>,
130}
131
132/// Response from the solve endpoint
133#[derive(Debug, Clone, Serialize, Deserialize)]
134pub struct SolveResponse {
135    /// One solution per objective function
136    pub solutions: Vec<Solution>,
137}