Skip to main content

cnvx_lp/
dual_simplex.rs

1use cnvx_core::{Model, Solution, SolveError, Solver};
2use cnvx_math::{DenseMatrix, Matrix};
3
4/// Generic Simplex solver.
5pub struct DualSimplexSolver<'model> {
6    state: State<'model>,
7    pub tolerance: f64,
8    pub max_iter: usize,
9    pub logging: bool,
10}
11
12impl<'model> Default for DualSimplexSolver<'model> {
13    fn default() -> Self {
14        panic!("Use SimplexSolver::new(model) to construct");
15    }
16}
17
18impl<'model> Solver<'model> for DualSimplexSolver<'model> {
19    fn new(model: &'model Model) -> Self {
20        Self {
21            state: State::Dense(DualSimplexState::new(model)),
22            tolerance: 1e-8,
23            max_iter: 1000,
24            logging: false,
25        }
26    }
27
28    fn solve(&mut self) -> Result<Solution, SolveError> {
29        // TODO: implement primal simplex iterations
30
31        Err(SolveError::Unsupported(
32            "DualSimplexSolver.solve() not implemented yet".to_string(),
33        ))
34    }
35
36    fn get_objective_value(&self) -> f64 {
37        match &self.state {
38            State::Dense(s) => s.objective,
39        }
40    }
41
42    fn get_solution(&self) -> Vec<f64> {
43        // TODO: reconstruct full solution vector from x_b and non-basic vars
44        vec![]
45    }
46}
47
48enum State<'model> {
49    Dense(DualSimplexState<'model, DenseMatrix>),
50}
51
52/// State used internally by the simplex solver.
53#[derive(Clone)]
54pub struct DualSimplexState<'model, A: Matrix> {
55    pub model: &'model Model,
56    pub iteration: usize,
57    pub basis: Vec<usize>,
58    pub non_basis: Vec<usize>,
59    pub x_b: Vec<f64>,
60    pub a: A,
61    pub b: Vec<f64>,
62    pub c: Vec<f64>,
63    pub objective: f64,
64}
65
66impl<'model, A: Matrix> DualSimplexState<'model, A> {
67    pub fn new(model: &'model Model) -> Self {
68        Self {
69            model,
70            iteration: 0,
71            basis: vec![],
72            non_basis: vec![],
73            x_b: vec![],
74            a: A::new(0, 0),
75            b: vec![],
76            c: vec![],
77            objective: 0.0,
78        }
79    }
80}