Skip to content

Commit

Permalink
fix: Constraint Sense should be an Enum of possible values
Browse files Browse the repository at this point in the history
  • Loading branch information
dandxy89 committed Jan 24, 2024
1 parent e0fbc30 commit 2a93a40
Show file tree
Hide file tree
Showing 32 changed files with 829 additions and 790 deletions.
11 changes: 9 additions & 2 deletions src/lp_parts.rs
Expand Up @@ -5,7 +5,14 @@ use unique_id::{sequence::SequenceGenerator, Generator};

use crate::{
common::RuleExt,
model::{constraint::Constraint, lp_problem::LPProblem, objective::Objective, sense::Sense, sos::SOSClass, variable::VariableType},
model::{
constraint::Constraint,
lp_problem::LPProblem,
objective::Objective,
sense::{Cmp, Sense},
sos::SOSClass,
variable::VariableType,
},
Rule,
};

Expand Down Expand Up @@ -40,7 +47,7 @@ fn compose_constraint(pair: Pair<'_, Rule>, gen: &mut SequenceGenerator) -> anyh
}
let coefficients: anyhow::Result<Vec<_>> =
coefficients.into_iter().filter(|p| !matches!(p.as_rule(), Rule::PLUS | Rule::MINUS)).map(|p| p.into_inner().try_into()).collect();
let sense = parts.next().unwrap().as_str().to_string();
let sense = Cmp::from_str(parts.next().unwrap().as_str())?;
let rhs = parts.next().unwrap().as_str().parse()?;
Ok(Constraint::Standard { name, coefficients: coefficients?, sense, rhs })
}
Expand Down
4 changes: 2 additions & 2 deletions src/model/constraint.rs
@@ -1,4 +1,4 @@
use crate::model::{coefficient::Coefficient, sos::SOSClass};
use crate::model::{coefficient::Coefficient, sense::Cmp, sos::SOSClass};

#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
Expand All @@ -8,7 +8,7 @@ use crate::model::{coefficient::Coefficient, sos::SOSClass};
)))]
pub enum Constraint {
/// Standard LP constraint
Standard { name: String, coefficients: Vec<Coefficient>, sense: String, rhs: f64 },
Standard { name: String, coefficients: Vec<Coefficient>, sense: Cmp, rhs: f64 },
/// Special Order Set (SOS)
SOS { name: String, kind: SOSClass, coefficients: Vec<Coefficient> },
}
Expand Down
32 changes: 32 additions & 0 deletions src/model/sense.rs
@@ -1,3 +1,5 @@
use std::str::FromStr;

#[derive(Debug, Default, PartialEq, Eq, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "diff", derive(diff::Diff))]
Expand All @@ -9,3 +11,33 @@ pub enum Sense {
Minimize,
Maximize,
}

#[derive(Debug, Default, PartialEq, Eq, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "diff", derive(diff::Diff))]
#[cfg_attr(feature = "diff", diff(attr(
#[derive(Debug, PartialEq, serde::Serialize, serde::Deserialize)]
)))]
pub enum Cmp {
#[default]
GreaterThan,
LessThan,
Equal,
GreaterOrEqual,
LessOrEqual,
}

impl FromStr for Cmp {
type Err = anyhow::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
">=" => Ok(Self::GreaterOrEqual),
"<=" => Ok(Self::LessOrEqual),
">" => Ok(Self::GreaterThan),
"<" => Ok(Self::LessThan),
"=" => Ok(Self::Equal),
_ => Err(anyhow::anyhow!("Unrecognized cmp: {s}")),
}
}
}
6 changes: 3 additions & 3 deletions tests/diff_feature.rs
@@ -1,7 +1,7 @@
use std::path::PathBuf;

use lp_parser_rs::{
model::{coefficient::Coefficient, constraint::Constraint, lp_problem::LPProblem},
model::{coefficient::Coefficient, constraint::Constraint, lp_problem::LPProblem, sense::Cmp},
parse::{parse_file, parse_lp_file},
};

Expand All @@ -24,13 +24,13 @@ fn test_constraint() {
let a = Constraint::Standard {
name: "a".to_string(),
coefficients: vec![Coefficient { var_name: "ca".to_string(), coefficient: 1.0 }],
sense: ">".to_string(),
sense: Cmp::GreaterThan,
rhs: 1.0,
};
let b = Constraint::Standard {
name: "b".to_string(),
coefficients: vec![Coefficient { var_name: "ca".to_string(), coefficient: 1.0 }],
sense: ">".to_string(),
sense: Cmp::GreaterThan,
rhs: 1.0,
};
insta::assert_yaml_snapshot!(a.diff(&a));
Expand Down
2 changes: 1 addition & 1 deletion tests/snapshots/diff_feature__constraint-2.snap
Expand Up @@ -5,6 +5,6 @@ expression: a.diff(&b)
Standard:
name: b
coefficients: []
sense: ~
sense: NoChange
rhs: 0

2 changes: 1 addition & 1 deletion tests/snapshots/diff_feature__file_comparison-2.snap
Expand Up @@ -19,7 +19,7 @@ constraints:
changes:
- var_name: ~
coefficient: -1
sense: ~
sense: NoChange
rhs: 0
removed: []

14 changes: 7 additions & 7 deletions tests/snapshots/diff_feature__file_comparison-3.snap
Expand Up @@ -201,7 +201,7 @@ constraints:
changes:
- var_name: __dummy
coefficient: 1
sense: "<="
sense: LessOrEqual
rhs: 1
"c1_0_0_1:":
Standard:
Expand Down Expand Up @@ -306,7 +306,7 @@ constraints:
coefficient: 1
- var_name: x_59_6_0
coefficient: 1
sense: "<="
sense: LessOrEqual
rhs: 1
"c1_9_4_4:":
Standard:
Expand Down Expand Up @@ -339,7 +339,7 @@ constraints:
coefficient: 1
- var_name: x_5_6_9
coefficient: 1
sense: "<="
sense: LessOrEqual
rhs: 1
"c2_0_0:":
Standard:
Expand Down Expand Up @@ -404,7 +404,7 @@ constraints:
coefficient: 1
- var_name: x_0_6_9
coefficient: 1
sense: "="
sense: Equal
rhs: 1
"c2_9_2:":
Standard:
Expand Down Expand Up @@ -501,7 +501,7 @@ constraints:
coefficient: 1
- var_name: x_9_6_9
coefficient: 1
sense: "="
sense: Equal
rhs: 1
"c3_0_10:":
Standard:
Expand All @@ -524,7 +524,7 @@ constraints:
coefficient: 1
- var_name: x_0_6_10
coefficient: 1
sense: "<="
sense: LessOrEqual
rhs: 1
"c3_9_9:":
Standard:
Expand All @@ -547,7 +547,7 @@ constraints:
coefficient: 1
- var_name: x_9_6_9
coefficient: 1
sense: "<="
sense: LessOrEqual
rhs: 1
removed:
- "c1:"
Expand Down

0 comments on commit 2a93a40

Please sign in to comment.