Browse Source

Changed simplex tableau generation to use ndarray types

karmarkar
Thomas Johnson 3 years ago
parent
commit
c973a92d8f
  1. 1
      Cargo.toml
  2. 21
      src/affine_scaling.rs
  3. 3
      src/main.rs

1
Cargo.toml

@ -9,3 +9,4 @@ edition = "2018"
[dependencies]
colored = "1.9.3"
nalgebra = "0.21.0"
ndarray = "0.13.1"

21
src/affine_scaling.rs

@ -6,6 +6,7 @@ use core::ops::{Add, AddAssign, Mul, MulAssign, Neg, Shl};
use std::collections::{HashMap, HashSet};
use nalgebra::{DMatrix, DVector};
use ndarray::{Array1, Array2};
pub type Fl = f64;
@ -510,7 +511,7 @@ impl StandardFormLP {
pub fn ineqs_and_objective_to_simplex_tableau(
ineqs: impl IntoIterator<Item = LeqInequality>,
objective: LinearCombination,
) -> DMatrix<Fl> {
) -> (Array2<Fl>, Array1<Fl>, Array1<Fl>) {
let mut counter = NewVariableCounter::new();
let constraints = ineqs
.into_iter()
@ -522,17 +523,20 @@ pub fn ineqs_and_objective_to_simplex_tableau(
let vars = varset.into_iter().collect::<Vec<_>>(); // Order needs to be deterministic
let ncstr = constraints.len();
let nvars = vars.len();
let mut tableau = DMatrix::zeros(ncstr + 1, nvars + 1);
let mut c = Array1::zeros(nvars);
#[allow(non_snake_case)]
let mut A = Array2::zeros((ncstr, nvars));
let mut b = Array1::zeros(ncstr);
for (i, constraint) in constraints.iter().enumerate() {
tableau[(i + 1, nvars)] = constraint.get_rhs();
b[i] = constraint.get_rhs();
for (j, var) in vars.iter().enumerate() {
tableau[(i + 1, j)] = constraint.get_coeff(var);
A[(i, j)] = constraint.get_coeff(var);
}
}
for (j, var) in vars.iter().enumerate() {
tableau[(0, j)] = objective.get_coeff(var);
c[j] = objective.get_coeff(var);
}
tableau
(A, b, c)
}
impl Display for StandardFormLP {
@ -772,7 +776,10 @@ mod test {
let tableau =
ineqs_and_objective_to_simplex_tableau(constraints.clone(), objective.clone());
print!("tableau: {}", tableau);
println!("tableau: A = ");
println!("{}", tableau.0);
println!("b = {}", tableau.1);
println!("c = {}", tableau.2);
let sf = StandardFormLP::from_ineqs_and_objective(constraints, objective);
println!("");

3
src/main.rs

@ -3,6 +3,9 @@
#![feature(specialization)]
#![feature(non_ascii_idents)]
extern crate nalgebra;
extern crate ndarray;
#[macro_use]
/// Variables -- both boolean, and otherwise.
pub mod variable;

Loading…
Cancel
Save