Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
37 changed files
with
1,015 additions
and
938 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,24 @@ | ||
use std::iter::Peekable; | ||
|
||
use pest::iterators::Pairs; | ||
use unique_id::{sequence::SequenceGenerator, Generator}; | ||
|
||
use crate::{model::prefix::Prefix, Rule}; | ||
|
||
pub mod coefficient; | ||
pub mod constraint; | ||
pub mod lp_problem; | ||
pub mod objective; | ||
pub mod parse_model; | ||
pub mod prefix; | ||
pub mod sense; | ||
pub mod sos; | ||
pub mod variable; | ||
|
||
fn get_name(parts: &mut Peekable<Pairs<'_, Rule>>, gen: &mut SequenceGenerator, rule: Rule) -> String { | ||
if parts.peek().unwrap().as_rule() == rule { | ||
parts.next().unwrap().as_str().to_string() | ||
} else { | ||
format!("{}{}", rule.prefix(), gen.next_id()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use pest::iterators::Pair; | ||
use unique_id::sequence::SequenceGenerator; | ||
|
||
use crate::{ | ||
model::{ | ||
constraint::Constraint, | ||
lp_problem::{LPPart, LPProblem}, | ||
objective::Objective, | ||
sense::Sense, | ||
sos::SOSClass, | ||
variable::get_bound, | ||
}, | ||
Rule, | ||
}; | ||
|
||
type ResultVec<T> = anyhow::Result<Vec<T>>; | ||
|
||
#[allow(clippy::wildcard_enum_match_arm)] | ||
/// # Errors | ||
/// Returns an error if the `compose` fails | ||
pub fn compose(pair: Pair<'_, Rule>, mut parsed: LPProblem, gen: &mut SequenceGenerator) -> anyhow::Result<LPProblem> { | ||
match pair.as_rule() { | ||
// Problem Name | ||
Rule::PROBLEM_NAME => return Ok(parsed.with_problem_name(pair.as_str())), | ||
// Problem sense | ||
Rule::MIN_SENSE => return Ok(parsed.with_sense(Sense::Minimize)), | ||
Rule::MAX_SENSE => return Ok(parsed.with_sense(Sense::Maximize)), | ||
// Problem Objectives | ||
Rule::OBJECTIVES => { | ||
let parts: ResultVec<_> = pair.into_inner().map(|p| <Objective as LPPart>::try_into(p, gen)).collect(); | ||
parsed.add_objective(parts?); | ||
} | ||
// Problem Constraints | ||
Rule::CONSTRAINTS => { | ||
let parts: ResultVec<_> = pair.into_inner().map(|p| <Constraint as LPPart>::try_into(p, gen)).collect(); | ||
parsed.add_constraints(parts?); | ||
} | ||
Rule::SOS => { | ||
let parts: ResultVec<_> = pair.into_inner().map(|p| <SOSClass as LPPart>::try_into(p, gen)).collect(); | ||
parsed.add_constraints(parts?); | ||
} | ||
// Problem Bounds | ||
Rule::BOUNDS => { | ||
for bound_pair in pair.into_inner() { | ||
if let Some((name, kind)) = get_bound(bound_pair) { | ||
parsed.set_variable_bounds(name, kind); | ||
} | ||
} | ||
} | ||
// Variable Bounds | ||
r @ (Rule::INTEGERS | Rule::GENERALS | Rule::BINARIES | Rule::SEMI_CONTINUOUS) => { | ||
for p in pair.into_inner() { | ||
if matches!(p.as_rule(), Rule::VARIABLE) { | ||
parsed.set_variable_bounds(p.as_str(), r.into()); | ||
} | ||
} | ||
} | ||
// Otherwise, skip! | ||
_ => (), | ||
} | ||
Ok(parsed) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
use crate::Rule; | ||
|
||
pub trait Prefix { | ||
fn prefix(&self) -> &'static str; | ||
} | ||
|
||
impl Prefix for Rule { | ||
fn prefix(&self) -> &'static str { | ||
match self { | ||
Self::OBJECTIVE_NAME => "obj_", | ||
Self::CONSTRAINT_NAME => "con_", | ||
_ => "", | ||
} | ||
} | ||
} |
Oops, something went wrong.