oxiz-math 0.2.1

Mathematical foundations for OxiZ SMT solver
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
//! SIMD-Optimized Simplex Algorithm.
#![allow(clippy::needless_range_loop)] // Simplex algorithm uses explicit indexing
//!
//! Provides cache-friendly simplex operations for linear programming.

#[allow(unused_imports)]
use crate::prelude::*;
use core::ops::{Add, Div, Mul, Sub};
use num_traits::Float;

/// SIMD-friendly simplex tableau.
#[derive(Debug, Clone)]
pub struct SimplexTableau<T> {
    /// Tableau matrix (includes slack variables and RHS)
    pub tableau: Vec<Vec<T>>,
    /// Basic variable indices
    pub basis: Vec<usize>,
    /// Number of original variables
    pub num_vars: usize,
    /// Number of constraints
    pub num_constraints: usize,
}

impl<T> SimplexTableau<T>
where
    T: Clone + Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Div<Output = T> + Float,
{
    /// Create a new simplex tableau.
    pub fn new(constraints: Vec<Vec<T>>, objective: Vec<T>, rhs: Vec<T>) -> Self {
        let num_constraints = constraints.len();
        let num_vars = objective.len();

        // Build initial tableau with slack variables
        let total_vars = num_vars + num_constraints;
        let mut tableau = vec![vec![T::zero(); total_vars + 1]; num_constraints + 1];

        // Add constraints
        for (i, constraint) in constraints.iter().enumerate() {
            for (j, &val) in constraint.iter().enumerate() {
                tableau[i][j] = val;
            }
            // Add slack variable
            tableau[i][num_vars + i] = T::one();
            // Add RHS
            tableau[i][total_vars] = rhs[i];
        }

        // Add objective function (negated for maximization)
        for (j, &val) in objective.iter().enumerate() {
            tableau[num_constraints][j] = T::zero() - val;
        }

        // Initial basis is slack variables
        let basis = (num_vars..(num_vars + num_constraints)).collect();

        Self {
            tableau,
            basis,
            num_vars,
            num_constraints,
        }
    }

    /// Perform one iteration of the simplex algorithm.
    pub fn pivot(&mut self) -> Result<bool, SimplexError> {
        // Find entering variable (most negative coefficient in objective row)
        let entering_col = self.find_entering_variable()?;

        if entering_col.is_none() {
            return Ok(true); // Optimal solution found
        }

        let entering = entering_col.expect("entering_col should be valid");

        // Find leaving variable using minimum ratio test
        let leaving_row = self.find_leaving_variable(entering)?;

        if leaving_row.is_none() {
            return Err(SimplexError::Unbounded);
        }

        let leaving = leaving_row.expect("leaving_row should be valid");

        // Perform pivot operation
        self.perform_pivot(entering, leaving);

        // Update basis
        self.basis[leaving] = entering;

        Ok(false) // Not optimal yet
    }

    /// Find entering variable (most negative coefficient).
    fn find_entering_variable(&self) -> Result<Option<usize>, SimplexError> {
        let obj_row = &self.tableau[self.num_constraints];
        let total_vars = self.num_vars + self.num_constraints;

        let mut min_val = T::zero();
        let mut min_idx = None;

        for j in 0..total_vars {
            if obj_row[j] < min_val {
                min_val = obj_row[j];
                min_idx = Some(j);
            }
        }

        Ok(min_idx)
    }

    /// Find leaving variable using minimum ratio test.
    fn find_leaving_variable(&self, entering: usize) -> Result<Option<usize>, SimplexError> {
        let total_vars = self.num_vars + self.num_constraints;
        let rhs_col = total_vars;

        let mut min_ratio = T::infinity();
        let mut min_idx = None;

        for i in 0..self.num_constraints {
            let coeff = self.tableau[i][entering];

            if coeff > T::epsilon() {
                let rhs = self.tableau[i][rhs_col];
                let ratio = rhs / coeff;

                if ratio < min_ratio {
                    min_ratio = ratio;
                    min_idx = Some(i);
                }
            }
        }

        Ok(min_idx)
    }

    /// Perform pivot operation with SIMD-friendly access pattern.
    fn perform_pivot(&mut self, entering: usize, leaving: usize) {
        let total_vars = self.num_vars + self.num_constraints;
        let num_cols = total_vars + 1;

        // Get pivot element
        let pivot = self.tableau[leaving][entering];

        if pivot.abs() < T::epsilon() {
            return; // Avoid division by zero
        }

        // Normalize pivot row
        for j in 0..num_cols {
            self.tableau[leaving][j] = self.tableau[leaving][j] / pivot;
        }

        // Eliminate column in other rows
        // Process in chunks for cache locality
        const CHUNK_SIZE: usize = 8;

        for i in 0..=self.num_constraints {
            if i == leaving {
                continue;
            }

            let factor = self.tableau[i][entering];

            if factor.abs() < T::epsilon() {
                continue;
            }

            // Process columns in chunks
            for chunk_start in (0..num_cols).step_by(CHUNK_SIZE) {
                let chunk_end = (chunk_start + CHUNK_SIZE).min(num_cols);

                for j in chunk_start..chunk_end {
                    let update = self.tableau[leaving][j] * factor;
                    self.tableau[i][j] = self.tableau[i][j] - update;
                }
            }
        }
    }

    /// Get current solution.
    pub fn get_solution(&self) -> Vec<T> {
        let mut solution = vec![T::zero(); self.num_vars];
        let total_vars = self.num_vars + self.num_constraints;
        let rhs_col = total_vars;

        for (row, &var_idx) in self.basis.iter().enumerate() {
            if var_idx < self.num_vars {
                solution[var_idx] = self.tableau[row][rhs_col];
            }
        }

        solution
    }

    /// Get objective value.
    pub fn get_objective_value(&self) -> T {
        let total_vars = self.num_vars + self.num_constraints;
        let rhs_col = total_vars;
        self.tableau[self.num_constraints][rhs_col]
    }

    /// Check if current solution is feasible.
    pub fn is_feasible(&self) -> bool {
        let total_vars = self.num_vars + self.num_constraints;
        let rhs_col = total_vars;

        for i in 0..self.num_constraints {
            if self.tableau[i][rhs_col] < T::zero() - T::epsilon() {
                return false;
            }
        }

        true
    }
}

/// Solve linear program using simplex algorithm.
pub fn simd_simplex_solve<T>(
    constraints: Vec<Vec<T>>,
    objective: Vec<T>,
    rhs: Vec<T>,
    max_iterations: usize,
) -> Result<SimplexSolution<T>, SimplexError>
where
    T: Clone + Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Div<Output = T> + Float,
{
    let mut tableau = SimplexTableau::new(constraints, objective, rhs);

    if !tableau.is_feasible() {
        return Err(SimplexError::Infeasible);
    }

    let mut iterations = 0;

    loop {
        if iterations >= max_iterations {
            return Err(SimplexError::MaxIterationsReached);
        }

        let optimal = tableau.pivot()?;

        if optimal {
            break;
        }

        iterations += 1;
    }

    Ok(SimplexSolution {
        solution: tableau.get_solution(),
        objective_value: tableau.get_objective_value(),
        iterations,
    })
}

/// Result of simplex optimization.
#[derive(Debug, Clone)]
pub struct SimplexSolution<T> {
    /// Optimal solution
    pub solution: Vec<T>,
    /// Optimal objective value
    pub objective_value: T,
    /// Number of iterations
    pub iterations: usize,
}

/// Errors in simplex algorithm.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SimplexError {
    /// Problem is infeasible
    Infeasible,
    /// Problem is unbounded
    Unbounded,
    /// Maximum iterations reached
    MaxIterationsReached,
}

impl core::fmt::Display for SimplexError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::Infeasible => write!(f, "linear program is infeasible"),
            Self::Unbounded => write!(f, "linear program is unbounded"),
            Self::MaxIterationsReached => write!(f, "maximum iterations reached"),
        }
    }
}

impl core::error::Error for SimplexError {}

/// Dual simplex algorithm for handling infeasibility.
pub fn simd_dual_simplex<T>(
    constraints: Vec<Vec<T>>,
    objective: Vec<T>,
    rhs: Vec<T>,
    max_iterations: usize,
) -> Result<SimplexSolution<T>, SimplexError>
where
    T: Clone + Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Div<Output = T> + Float,
{
    let mut tableau = SimplexTableau::new(constraints, objective, rhs);

    let mut iterations = 0;

    loop {
        if iterations >= max_iterations {
            return Err(SimplexError::MaxIterationsReached);
        }

        // Check if optimal
        if tableau.is_feasible() && is_dual_feasible(&tableau) {
            break;
        }

        // Find leaving variable (most negative RHS)
        let leaving_row = find_dual_leaving_variable(&tableau)?;

        if leaving_row.is_none() {
            return Err(SimplexError::Infeasible);
        }

        let leaving = leaving_row.expect("leaving_row should be valid");

        // Find entering variable
        let entering_col = find_dual_entering_variable(&tableau, leaving)?;

        if entering_col.is_none() {
            return Err(SimplexError::Infeasible);
        }

        let entering = entering_col.expect("entering_col should be valid");

        // Perform pivot
        tableau.perform_pivot(entering, leaving);
        tableau.basis[leaving] = entering;

        iterations += 1;
    }

    Ok(SimplexSolution {
        solution: tableau.get_solution(),
        objective_value: tableau.get_objective_value(),
        iterations,
    })
}

fn is_dual_feasible<T>(tableau: &SimplexTableau<T>) -> bool
where
    T: Clone + Float,
{
    let total_vars = tableau.num_vars + tableau.num_constraints;
    let obj_row = &tableau.tableau[tableau.num_constraints];

    for j in 0..total_vars {
        if obj_row[j] < T::zero() - T::epsilon() {
            return false;
        }
    }

    true
}

fn find_dual_leaving_variable<T>(tableau: &SimplexTableau<T>) -> Result<Option<usize>, SimplexError>
where
    T: Clone + Float,
{
    let total_vars = tableau.num_vars + tableau.num_constraints;
    let rhs_col = total_vars;

    let mut min_val = T::zero();
    let mut min_idx = None;

    for i in 0..tableau.num_constraints {
        let rhs = tableau.tableau[i][rhs_col];
        if rhs < min_val {
            min_val = rhs;
            min_idx = Some(i);
        }
    }

    Ok(min_idx)
}

fn find_dual_entering_variable<T>(
    tableau: &SimplexTableau<T>,
    leaving: usize,
) -> Result<Option<usize>, SimplexError>
where
    T: Clone + Float + Div<Output = T>,
{
    let total_vars = tableau.num_vars + tableau.num_constraints;
    let obj_row = &tableau.tableau[tableau.num_constraints];
    let leaving_row = &tableau.tableau[leaving];

    let mut min_ratio = T::infinity();
    let mut min_idx = None;

    for j in 0..total_vars {
        let coeff = leaving_row[j];

        if coeff < T::zero() - T::epsilon() {
            let obj_coeff = obj_row[j];
            let ratio = obj_coeff / (T::zero() - coeff);

            if ratio < min_ratio {
                min_ratio = ratio;
                min_idx = Some(j);
            }
        }
    }

    Ok(min_idx)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_simplex_basic() {
        // Maximize: 3x + 2y
        // Subject to: x + y <= 4, 2x + y <= 5, x, y >= 0
        let constraints = vec![vec![1.0, 1.0], vec![2.0, 1.0]];
        let objective = vec![3.0, 2.0];
        let rhs = vec![4.0, 5.0];

        let result = simd_simplex_solve(constraints, objective, rhs, 100).expect("simplex failed");

        assert!(result.iterations > 0);
        assert!(result.objective_value > 0.0);
    }

    #[test]
    fn test_simplex_unbounded() {
        // Maximize: x + y
        // Subject to: -x + y <= 1
        let constraints = vec![vec![-1.0, 1.0]];
        let objective = vec![1.0, 1.0];
        let rhs = vec![1.0];

        let result = simd_simplex_solve(constraints, objective, rhs, 100);

        assert!(result.is_err());
    }

    #[test]
    fn test_tableau_creation() {
        let constraints = vec![vec![1.0, 1.0]];
        let objective = vec![1.0, 1.0];
        let rhs = vec![5.0];

        let tableau = SimplexTableau::new(constraints, objective, rhs);

        assert_eq!(tableau.num_vars, 2);
        assert_eq!(tableau.num_constraints, 1);
        assert_eq!(tableau.basis.len(), 1);
    }
}