Skip to content

Commit

Permalink
fix: Another fix for comments inline
Browse files Browse the repository at this point in the history
  • Loading branch information
dandxy89 committed Jan 5, 2024
1 parent 237296b commit 0955e0a
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 34 deletions.
28 changes: 14 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions resources/infile_comments2.lp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Minimize
obj: 3x1 + 5x2 + 6x3 + 9x4 + 10x5 + 10x6
Subject To
\ c001: - 2 x1 + 6 x2 - 3 x3 + 4 x4 + 1 x5 - 2 x6 >= 2
\ c002: - 5 x1 - 3 x2 + 1 x3 + 3 x4 - 2 x5 + 1 x6 >= -2
\c003: 5 x1 - 1 x2 + 4 x3 - 2 x4 + 2 x5 - 1 x6 >= 3
Binaries
\ x1 x2 x3 x4 x5 x6
x1 x2 x3 x4 x5 x6 x7
End
36 changes: 18 additions & 18 deletions src/lp_file_format.pest
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ VALID_COMMENT_CHARS = _{
| "<"
| "="
}
VALID_CHARS = _{
VALID_LP_CHARS = _{
ASCII_ALPHANUMERIC
| "!"
| "#"
Expand All @@ -75,17 +75,17 @@ VALID_CHARS = _{
| "~"
}
VARIABLE = ${
!(FREE | END | SOS_SECTION | CONSTRAINT_SECTION | BOUND_SECTION | GENERALS_SECTION | BINARIES_SECTION | SEMI_CONTINUOUS_SECTION) ~ VALID_CHARS{1, 255}
!(FREE | END | SOS_SECTION | CONSTRAINT_SECTION | BOUND_SECTION | GENERALS_SECTION | BINARIES_SECTION | SEMI_CONTINUOUS_SECTION) ~ VALID_LP_CHARS{1, 255}
}

// Problem Name
PROBLEM_NAME = ${ VALID_CHARS+ }
PROBLEM_NAME = ${ VALID_LP_CHARS+ }
LP_PROBLEM_NAME = _{ "\\" ~ "Problem name:" ~ PROBLEM_NAME }

// Comments
// https://www.ibm.com/docs/en/icos/22.1.1?topic=representation-comments-in-lp-file-format
COMMENT_TEXT = _{ (VALID_COMMENT_CHARS | COLON)* }
COMMENTS = _{ "\\" ~ ASTERIX? ~ COMMENT_TEXT ~ ASTERIX? ~ NEWLINE? }
COMMENTS = _{ NEWLINE* ~ "\\" ~ ASTERIX? ~ COMMENT_TEXT ~ ASTERIX? }

// Problem sense in LP file format
// https://www.ibm.com/docs/en/icos/22.1.1?topic=representation-problem-sense-in-lp-file-format
Expand All @@ -100,28 +100,28 @@ PROBLEM_SENSE = _{ NEWLINE* ~ (MIN_SENSE | MAX_SENSE) }
OBJECTIVE_EXPR = {
OPERATOR? ~ FLOAT? ~ VARIABLE
| OPERATOR? ~ FLOAT
| NEWLINE ~ OPERATOR ~ FLOAT ~ VARIABLE
| NEWLINE* ~ OPERATOR ~ FLOAT ~ VARIABLE
}
OBJECTIVE_NAME = { VALID_CHARS{1, 255} }
OBJECTIVE = { NEWLINE* ~ (OBJECTIVE_NAME ~ COLON)? ~ OBJECTIVE_EXPR ~ (OBJECTIVE_EXPR)* }
OBJECTIVE_NAME = { VALID_LP_CHARS{1, 255} }
OBJECTIVE = { NEWLINE* ~ (OBJECTIVE_NAME ~ COLON)? ~ OBJECTIVE_EXPR+ }
OBJECTIVES = { OBJECTIVE* }

// Constraints
// https://www.ibm.com/docs/en/icos/22.1.1?topic=representation-constraints-in-lp-file-format
CONSTRAINT_SECTION = _{
(^"subject to" | ^"such that" | ^"S.T." | ^"st") ~ COLON? ~ NEWLINE?
(^"subject to" | ^"such that" | ^"S.T." | ^"st") ~ COLON? ~ NEWLINE*
}
GT = { ">" }
GTE = { ">=" }
LT = { "<" }
LTE = { "<=" }
EQ = { "=" }
CMP = _{ GTE | GT | LTE | LT | EQ }
CONSTRAINT_EXPR = { NEWLINE? ~ OPERATOR? ~ FLOAT? ~ VARIABLE }
CONSTRAINT_EXPR = { NEWLINE* ~ OPERATOR? ~ FLOAT? ~ VARIABLE }
CONSTRAINT_EXPRS = _{ CONSTRAINT_EXPR* }
CONSTRAINT_NAME = ${ VALID_CHARS{1, 255} ~ COLON }
CONSTRAINT_NAME = ${ VALID_LP_CHARS{1, 255} ~ COLON }
CONSTRAINT = {
NEWLINE* ~ (CONSTRAINT_NAME ~ COLON*)? ~ CONSTRAINT_EXPRS ~ NEWLINE? ~ CMP ~ FLOAT
NEWLINE* ~ (CONSTRAINT_NAME ~ COLON*)? ~ CONSTRAINT_EXPRS ~ NEWLINE* ~ CMP ~ FLOAT
}
CONSTRAINTS = { NEWLINE* ~ CONSTRAINT_SECTION ~ (COMMENTS | CONSTRAINT)+ }

Expand All @@ -136,38 +136,38 @@ UPPER_BOUND = { VARIABLE ~ LTE ~ FLOAT }
BOUND = _{
NEWLINE* ~ (FREE_VARIABLE | BOUNDED | UPPER_BOUND | LOWER_BOUND | LOWER_BOUND_REV)
}
BOUNDS = { NEWLINE* ~ BOUND_SECTION ~ (NEWLINE? ~ (COMMENTS | BOUND))* }
BOUNDS = { NEWLINE* ~ BOUND_SECTION ~ (COMMENTS | BOUND)* }

// Integers
// A list of variable names of integer variables. Unless otherwise specified in the
// bounds section, the default relaxation interval of the variables is [0, 1].
INTEGER_SECTION = _{ ^"Integers" }
INTEGERS = { NEWLINE* ~ INTEGER_SECTION ~ (NEWLINE? ~ (COMMENTS | VARIABLE))* }
INTEGERS = { NEWLINE* ~ INTEGER_SECTION ~ (NEWLINE* ~ (COMMENTS | VARIABLE))* }

// Generals
// A list of variable names of integer variables. Unless otherwise specified in the
// bounds section, the default relaxation interval of the variables is [0, +Infinity].
GENERALS_SECTION = _{ ^"Gen" ~ ^"eral"? ~ ^"s"? }
GENERALS = { NEWLINE* ~ GENERALS_SECTION ~ (NEWLINE? ~ (COMMENTS | VARIABLE))* }
GENERALS = { NEWLINE* ~ GENERALS_SECTION ~ (NEWLINE* ~ (COMMENTS | VARIABLE))* }

// Binaries
// A list of variable names of binary variables.
BINARIES_SECTION = _{ ^"Binar" ~ (^"ies" | ^"y") }
BINARIES = { NEWLINE* ~ BINARIES_SECTION ~ (NEWLINE? ~ (COMMENTS | VARIABLE))* }
BINARIES = { NEWLINE* ~ BINARIES_SECTION ~ (NEWLINE* ~ (COMMENTS | VARIABLE))* }

// Semi-Continuous
// To specify any of the variables as semi-continuous variables, that is as variables that
// may take the value 0 or values between the specified lower and upper bounds
SEMI_CONTINUOUS_SECTION = _{ ^"SEMI" ~ (^"S" | ^"-CONTINUOUS") }
SEMI_CONTINUOUS = { NEWLINE* ~ SEMI_CONTINUOUS_SECTION ~ (NEWLINE? ~ (COMMENTS | VARIABLE))* }
SEMI_CONTINUOUS = { NEWLINE* ~ SEMI_CONTINUOUS_SECTION ~ (NEWLINE* ~ (COMMENTS | VARIABLE))* }

// SOS: Special Ordered Set
SOS_SECTION = _{ ^"SOS" }
TYPE1 = { "S1::" }
TYPE2 = { "S2::" }
VARIABLE_AND_WEIGHT = { VARIABLE ~ ":" ~ FLOAT }
SOS_CONSTRAINT = { NEWLINE? ~ (CONSTRAINT_NAME ~ COLON?)? ~ (TYPE1 | TYPE2) ~ VARIABLE_AND_WEIGHT* }
SOS = { NEWLINE* ~ SOS_SECTION ~ (NEWLINE? ~ (COMMENTS | SOS_CONSTRAINT))* }
SOS_CONSTRAINT = { NEWLINE* ~ (CONSTRAINT_NAME ~ COLON?)? ~ (TYPE1 | TYPE2) ~ VARIABLE_AND_WEIGHT* }
SOS = { NEWLINE* ~ SOS_SECTION ~ (COMMENTS | SOS_CONSTRAINT)* }

// End of file
// https://www.ibm.com/docs/en/icos/22.1.1?topic=representation-end-file-in-lp-file-format
Expand Down
5 changes: 3 additions & 2 deletions tests/test_from_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ macro_rules! generate_test {
#[test]
fn $test_name() {
let result = read_file_from_resources($file).unwrap();
dbg!(&result);
// dbg!(&result);
assert_eq!(result.problem_sense, Sense::$sense);
assert_eq!(result.objectives.len(), $obj_len, "Failed Objective Count");
assert_eq!(result.constraints.len(), $con_len, "Failed Constraint Count");
Expand All @@ -22,7 +22,7 @@ macro_rules! generate_test {
#[test]
fn $test_name() {
let result = read_file_from_resources($file).unwrap();
dbg!(&result);
// dbg!(&result);
assert_eq!($name, result.problem_name);
assert_eq!(result.problem_sense, Sense::$sense);
assert_eq!(result.objectives.len(), $obj_len, "Failed Objective Count");
Expand Down Expand Up @@ -56,6 +56,7 @@ generate_test!(empty_bounds, "empty_bounds.lp", Minimize, 1, 1, 2);
generate_test!(blank_lines, "blank_lines.lp", Minimize, 1, 1, 3);
generate_test!(optional_labels, "optional_labels.lp", Minimize, 1, 1, 4);
generate_test!(infile_comments, "infile_comments.lp", Minimize, 1, 1, 7);
generate_test!(infile_comments2, "infile_comments2.lp", Minimize, 1, 0, 7);
generate_test!(missing_signs, "missing_signs.lp", Minimize, 1, 1, 6);

#[test]
Expand Down

0 comments on commit 0955e0a

Please sign in to comment.