use std::{
fmt::{Debug, Display},
ops::Neg,
};
use nalgebra::{Const, DMatrix, DVector, Dyn};
use num::Num;
pub mod simplex;
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum OptimizationKind {
Min,
Max,
}
impl OptimizationKind {
pub fn inv(self) -> Self {
match self {
OptimizationKind::Min => OptimizationKind::Max,
OptimizationKind::Max => OptimizationKind::Min,
}
}
}
#[derive(Debug, PartialEq, Clone)]
pub struct LinearProblem<N> {
pub(crate) kind: OptimizationKind,
pub(crate) mat_a: DMatrix<N>,
pub(crate) vec_b: DVector<N>,
pub(crate) vec_c: DVector<N>,
}
impl<N: Num + Neg<Output = N> + Copy> LinearProblem<N> {
pub fn new_eq(
kind: OptimizationKind,
mat_a: DMatrix<N>,
vec_b: DVector<N>,
mut vec_c: DVector<N>,
) -> Self {
assert_eq!(mat_a.nrows(), vec_b.nrows());
assert_eq!(mat_a.ncols(), vec_c.nrows());
match kind {
OptimizationKind::Min => vec_c.iter_mut().for_each(|x| *x = -*x),
OptimizationKind::Max => {}
}
Self {
kind,
mat_a,
vec_b,
vec_c,
}
}
pub fn new_lt(
kind: OptimizationKind,
mut mat_a: DMatrix<N>,
vec_b: DVector<N>,
mut vec_c: DVector<N>,
) -> Self
where
N: Debug + 'static,
{
assert_eq!(mat_a.nrows(), vec_b.nrows());
assert_eq!(mat_a.ncols(), vec_c.nrows());
let nslk = mat_a.nrows();
let nvar = mat_a.ncols();
mat_a = mat_a.resize(nslk, nvar + nslk, N::zero());
vec_c = vec_c.resize_generic(Dyn(nvar + nslk), Const, N::zero());
for i in 0..nslk {
mat_a[(i, nvar + i)] = N::one();
}
Self::new_eq(kind, mat_a, vec_b, vec_c)
}
pub fn new_gt(
kind: OptimizationKind,
mut mat_a: DMatrix<N>,
vec_b: DVector<N>,
mut vec_c: DVector<N>,
) -> Self
where
N: Debug + 'static,
{
assert_eq!(mat_a.nrows(), vec_b.nrows());
assert_eq!(mat_a.ncols(), vec_c.nrows());
let nslk = mat_a.nrows();
let nvar = mat_a.ncols();
mat_a = mat_a.resize(nslk, nvar + nslk, N::zero());
vec_c = vec_c.resize_generic(Dyn(nvar + nslk), Const, N::zero());
for i in 0..nslk {
mat_a[(i, nvar + i)] = -N::one();
}
Self::new_eq(kind, mat_a, vec_b, vec_c)
}
pub fn new_eq_unb(
kind: OptimizationKind,
mut mat_a: DMatrix<N>,
vec_b: DVector<N>,
mut vec_c: DVector<N>,
) -> Self
where
N: Debug + 'static,
{
assert_eq!(mat_a.nrows(), vec_b.nrows());
assert_eq!(mat_a.ncols(), vec_c.nrows());
let ninq = mat_a.nrows();
let nvar = mat_a.ncols();
mat_a = mat_a.resize(ninq, nvar * 2, N::zero());
vec_c = vec_c.resize_generic(Dyn(nvar * 2), Const, N::zero());
for c in 0..nvar {
for r in 0..ninq {
mat_a[(r, nvar + c)] = -mat_a[(r, c)];
}
vec_c[nvar + c] = -vec_c[c];
}
Self::new_eq(kind, mat_a, vec_b, vec_c)
}
pub fn new_lt_unb(
kind: OptimizationKind,
mut mat_a: DMatrix<N>,
vec_b: DVector<N>,
mut vec_c: DVector<N>,
) -> Self
where
N: Debug + 'static,
{
assert_eq!(mat_a.nrows(), vec_b.nrows());
assert_eq!(mat_a.ncols(), vec_c.nrows());
let ninq = mat_a.nrows();
let nvar = mat_a.ncols();
mat_a = mat_a.resize(ninq, nvar * 2, N::zero());
vec_c = vec_c.resize_generic(Dyn(nvar * 2), Const, N::zero());
for c in 0..nvar {
for r in 0..ninq {
mat_a[(r, nvar + c)] = -mat_a[(r, c)];
}
vec_c[nvar + c] = -vec_c[c];
}
Self::new_lt(kind, mat_a, vec_b, vec_c)
}
pub fn new_gt_unb(
kind: OptimizationKind,
mut mat_a: DMatrix<N>,
vec_b: DVector<N>,
mut vec_c: DVector<N>,
) -> Self
where
N: Debug + 'static,
{
assert_eq!(mat_a.nrows(), vec_b.nrows());
assert_eq!(mat_a.ncols(), vec_c.nrows());
let ninq = mat_a.nrows();
let nvar = mat_a.ncols();
mat_a = mat_a.resize(ninq, nvar * 2, N::zero());
vec_c = vec_c.resize_generic(Dyn(nvar * 2), Const, N::zero());
for c in 0..nvar {
for r in 0..ninq {
mat_a[(r, nvar + c)] = -mat_a[(r, c)];
}
vec_c[nvar + c] = -vec_c[c];
}
Self::new_gt(kind, mat_a, vec_b, vec_c)
}
pub fn dual(&self) -> Self
where
N: Debug + 'static,
{
Self::new_gt_unb(
self.kind.inv(),
self.mat_a.transpose(),
self.vec_c.clone_owned(),
self.vec_b.clone_owned(),
)
}
}
impl<N> Display for LinearProblem<N>
where
N: Display,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "maximize ")?;
write!(f, "[")?;
for (i, coeff) in self.vec_c.iter().enumerate() {
if i > 0 {
write!(f, ", ")?;
}
write!(f, "{}", coeff)?;
}
write!(f, "]^T * x\n")?;
writeln!(f, "subject to:")?;
for i in 0..self.mat_a.nrows() {
write!(f, " [")?;
for j in 0..self.mat_a.ncols() {
if j > 0 {
write!(f, ", ")?;
}
write!(f, "{}", self.mat_a[(i, j)])?;
}
write!(f, "] * x = {}\n", self.vec_b[i])?;
}
write!(f, " x >= 0")?;
Ok(())
}
}