-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(csp): Implement max/min expressions.
- Loading branch information
Showing
4 changed files
with
184 additions
and
4 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 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,93 @@ | ||
use crate::core::state::Term; | ||
use crate::core::{IntCst, SignedVar}; | ||
use crate::model::lang::IAtom; | ||
use crate::reif::ReifExpr; | ||
use itertools::Itertools; | ||
use std::fmt::{Debug, Formatter}; | ||
|
||
/// Constraint equivalent to `lhs = max { e | e \in rhs } | ||
pub struct EqMax { | ||
lhs: IAtom, | ||
rhs: Vec<IAtom>, | ||
} | ||
|
||
/// Constraint equivalent to `lhs = min { e | e \in rhs } | ||
pub struct EqMin { | ||
lhs: IAtom, | ||
rhs: Vec<IAtom>, | ||
} | ||
|
||
impl From<EqMax> for ReifExpr { | ||
fn from(em: EqMax) -> Self { | ||
// (lhs_var + lhs_cst) = max_i { var_i + cst_i } | ||
// (lhs_var) = max_i { var_i + cst_i - lhs_cst} | ||
let lhs_var = SignedVar::plus(em.lhs.var.variable()); | ||
let lhs_cst = em.lhs.shift; | ||
let rhs = em | ||
.rhs | ||
.iter() | ||
.map(|term| NFEqMaxItem { | ||
var: SignedVar::plus(term.var.variable()), | ||
cst: term.shift - lhs_cst, | ||
}) | ||
.sorted() | ||
.collect_vec(); | ||
|
||
ReifExpr::EqMax(NFEqMax { lhs: lhs_var, rhs }) | ||
} | ||
} | ||
|
||
impl From<EqMin> for ReifExpr { | ||
fn from(em: EqMin) -> Self { | ||
// (lhs_var + lhs_cst) = min_i { var_i + cst_i } | ||
// (lhs_var + lhs_cst) = - max_i { - var_i - cst_i } | ||
// - lhs_var - lhs_cst = max_i { - var_i - cst_i } | ||
// - lhs_var = max_i { - var_i - cst_i + lhs_cst} | ||
let lhs_var = em.lhs.var.variable(); | ||
let lhs = SignedVar::minus(lhs_var); | ||
let lhs_cst = em.lhs.shift; | ||
let rhs = em | ||
.rhs | ||
.iter() | ||
.map(|term| NFEqMaxItem { | ||
var: SignedVar::minus(term.var.variable()), | ||
cst: -term.shift + lhs_cst, | ||
}) | ||
.sorted() | ||
.collect_vec(); | ||
|
||
ReifExpr::EqMax(NFEqMax { lhs, rhs }) | ||
} | ||
} | ||
|
||
#[derive(Eq, PartialEq, Hash, Clone)] | ||
pub struct NFEqMax { | ||
pub lhs: SignedVar, | ||
pub rhs: Vec<NFEqMaxItem>, | ||
} | ||
|
||
impl Debug for NFEqMax { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "{:?} = max ", self.lhs)?; | ||
f.debug_set().entries(self.rhs.iter()).finish() | ||
} | ||
} | ||
|
||
#[derive(Eq, PartialEq, Ord, PartialOrd, Hash, Clone)] | ||
pub struct NFEqMaxItem { | ||
pub var: SignedVar, | ||
pub cst: IntCst, | ||
} | ||
|
||
#[allow(clippy::comparison_chain)] | ||
impl Debug for NFEqMaxItem { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "{:?}", self.var)?; | ||
if self.cst > 0 { | ||
write!(f, " + {}", self.cst)?; | ||
} else if self.cst < 0 { | ||
write!(f, " - {}", -self.cst)?; | ||
} | ||
Ok(()) | ||
} | ||
} |
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