use super::*;
use crate::{CoefficientError, VariableIDSet};
impl Constraint<Created> {
pub fn reduce_binary_power(
&mut self,
binary_ids: &VariableIDSet,
) -> Result<bool, CoefficientError> {
let Some(replacement) = self.plan_binary_power_reduction(binary_ids)? else {
return Ok(false);
};
*self = replacement;
Ok(true)
}
pub(crate) fn plan_binary_power_reduction(
&self,
binary_ids: &VariableIDSet,
) -> Result<Option<Self>, CoefficientError> {
Ok(self
.stage
.function
.plan_binary_power_reduction(binary_ids)?
.map(|function| Constraint {
equality: self.equality,
stage: CreatedData { function },
}))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::quadratic;
#[test]
fn test_constraint_reduce_binary_power() {
let binary_ids = crate::variable_ids!(1);
let function = Function::Quadratic((quadratic!(1, 1) + quadratic!(2)).unwrap());
let mut constraint: Constraint<Created> = Constraint {
equality: Equality::LessThanOrEqualToZero,
stage: CreatedData { function },
};
let changed = constraint.reduce_binary_power(&binary_ids).unwrap();
assert!(changed);
let expected_function = Function::Quadratic((quadratic!(1) + quadratic!(2)).unwrap());
assert_eq!(constraint.stage.function, expected_function);
}
}