1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use super::*;
use crate::VariableIDSet;
impl Constraint {
/// Reduce binary powers in the constraint function.
///
/// For binary variables, x^n = x for any n >= 1, so we can reduce higher powers to linear terms.
///
/// Returns `true` if any reduction was performed, `false` otherwise.
pub fn reduce_binary_power(&mut self, binary_ids: &VariableIDSet) -> bool {
self.function.reduce_binary_power(binary_ids)
}
}
impl RemovedConstraint {
/// Reduce binary powers in the removed constraint function.
///
/// For binary variables, x^n = x for any n >= 1, so we can reduce higher powers to linear terms.
///
/// Returns `true` if any reduction was performed, `false` otherwise.
pub fn reduce_binary_power(&mut self, binary_ids: &VariableIDSet) -> bool {
self.constraint.reduce_binary_power(binary_ids)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::quadratic;
#[test]
fn test_constraint_reduce_binary_power() {
let binary_ids = crate::variable_ids!(1);
// Create a constraint with x1^2 + x2 <= 0
let function = Function::Quadratic(quadratic!(1, 1) + quadratic!(2));
let mut constraint = Constraint {
id: ConstraintID::from(1),
function,
equality: Equality::LessThanOrEqualToZero,
name: None,
subscripts: vec![],
parameters: FnvHashMap::default(),
description: None,
};
// Apply reduction
let changed = constraint.reduce_binary_power(&binary_ids);
assert!(changed);
// Check that x1^2 was reduced to x1
let expected_function = Function::Quadratic(quadratic!(1) + quadratic!(2));
assert_eq!(constraint.function, expected_function);
}
}