use std::fmt::{Display, Formatter};
use std::fmt;
use relp_num::Rational64;
use crate::data::linear_algebra::{SparseTuple, SparseTupleVec};
use crate::data::linear_program::elements::{ConstraintRelation, Objective};
use crate::data::linear_program::elements::VariableType;
use crate::io::error::Import;
#[allow(clippy::type_complexity)]
mod convert;
pub mod number;
mod parse;
mod token;
pub use parse::{parse_fixed, parse_free};
pub fn parse(
program: &impl AsRef<str>,
) -> Result<MPS<Rational64>, Import> {
parse_free(program)
}
#[derive(Debug, PartialEq)]
pub struct MPS<F> {
name: String,
objective: Objective,
cost_row_name: String,
cost_values: SparseTupleVec<F>,
rows: Vec<Row>,
columns: Vec<Column<F>>,
rhss: Vec<Rhs<F>>,
ranges: Vec<Range<F>>,
bounds: Vec<Bound<F>>,
}
#[allow(clippy::too_many_arguments)]
impl<F> MPS<F> {
#[must_use]
pub fn new(
name: String,
objective: Objective,
cost_row_name: String,
cost_values: SparseTupleVec<F>,
rows: Vec<Row>,
columns: Vec<Column<F>>,
rhss: Vec<Rhs<F>>,
ranges: Vec<Range<F>>,
bounds: Vec<Bound<F>>,
) -> Self {
Self {
name,
objective,
cost_row_name,
cost_values,
rows,
columns,
rhss,
ranges,
bounds,
}
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
enum Section {
Rows,
Columns,
Rhs,
Bounds,
Ranges,
Endata,
}
impl Display for Section {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Section::Rows => "ROWS",
Section::Columns => "COLUMNS",
Section::Rhs => "RHS",
Section::Bounds => "BOUNDS",
Section::Ranges => "RANGES",
Section::Endata => "ENDATA",
})
}
}
#[derive(Debug, Eq, PartialEq)]
enum RowType {
Cost,
Constraint(ConstraintRelation),
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum BoundType<F> {
LowerContinuous(F),
UpperContinuous(F),
Fixed(F),
Free,
LowerMinusInfinity,
UpperInfinity,
Binary,
LowerInteger(F),
UpperInteger(F),
SemiContinuous(F, F),
}
#[derive(Debug, Eq, PartialEq, Clone)]
pub struct Row {
pub name: String,
pub constraint_type: ConstraintRelation,
}
#[derive(Debug, PartialEq)]
pub struct Column<F> {
pub name: String,
pub variable_type: VariableType,
pub values: Vec<SparseTuple<F>>,
}
#[derive(Debug, PartialEq)]
pub struct Rhs<F> {
pub name: String,
pub values: Vec<SparseTuple<F>>,
}
#[derive(Debug, PartialEq)]
pub struct Range<F> {
pub name: String,
pub values: Vec<SparseTuple<F>>,
}
#[derive(Debug, PartialEq)]
pub struct Bound<F> {
pub name: String,
pub values: Vec<SparseTuple<BoundType<F>>>,
}