Skip to content

Commit 9ba246f

Browse files
committed
Improved simplification for array_element constraints
1 parent e691e76 commit 9ba246f

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

crates/huub/src/constraints/bool_array_element.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
use std::iter::once;
66

7-
use pindakaas::{propositional_logic::Formula, ClauseDatabaseTools};
7+
use pindakaas::ClauseDatabaseTools;
88

99
use crate::{
1010
actions::{ReformulationActions, SimplificationActions},
@@ -32,11 +32,12 @@ pub struct BoolDecisionArrayElement {
3232

3333
impl<S: SimplificationActions> Constraint<S> for BoolDecisionArrayElement {
3434
fn simplify(&mut self, actions: &mut S) -> Result<SimplificationStatus, ReformulationError> {
35+
// Fix the bounds of the index is to the length of the array
36+
actions.set_int_lower_bound(self.index, 0)?;
37+
actions.set_int_upper_bound(self.index, self.array.len() as IntVal - 1)?;
38+
// Unify if the index is already fixed
3539
if let Some(i) = actions.get_int_val(self.index) {
36-
actions.add_constraint(Formula::Equiv(vec![
37-
self.array[i as usize].into(),
38-
self.result.into(),
39-
]));
40+
actions.unify_bool(self.array[i as usize], self.result)?;
4041
return Ok(SimplificationStatus::Subsumed);
4142
}
4243
Ok(SimplificationStatus::Fixpoint)

crates/huub/src/constraints/int_array_element.rs

+19-1
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,15 @@ impl<S: SimplificationActions> Constraint<S> for IntDecisionArrayElement {
7979
}
8080

8181
fn simplify(&mut self, actions: &mut S) -> Result<SimplificationStatus, ReformulationError> {
82-
// make sure idx is within the range of args
82+
// Fix the bounds of the index is to the length of the array
8383
actions.set_int_lower_bound(self.index, 0)?;
8484
actions.set_int_upper_bound(self.index, self.array.len() as IntVal - 1)?;
85+
// Unify if the index is already fixed
86+
if let Some(idx) = actions.get_int_val(self.index) {
87+
actions.unify_int(self.array[idx as usize], self.result)?;
88+
return Ok(SimplificationStatus::Subsumed);
89+
}
90+
// Match the bounds of the array elements to the result and vice versa
8591
let (min_lb, max_ub) =
8692
self.array
8793
.iter()
@@ -319,6 +325,18 @@ where
319325
}
320326

321327
impl<S: SimplificationActions> Constraint<S> for IntValArrayElement {
328+
fn simplify(&mut self, actions: &mut S) -> Result<SimplificationStatus, ReformulationError> {
329+
// Fix the bounds of the index is to the length of the array
330+
actions.set_int_lower_bound(self.index, 0)?;
331+
actions.set_int_upper_bound(self.index, self.array.len() as IntVal - 1)?;
332+
// Unify if the index is already fixed
333+
if let Some(idx) = actions.get_int_val(self.index) {
334+
actions.set_int_val(self.result, self.array[idx as usize])?;
335+
return Ok(SimplificationStatus::Subsumed);
336+
}
337+
Ok(SimplificationStatus::Fixpoint)
338+
}
339+
322340
fn to_solver(&self, slv: &mut dyn ReformulationActions) -> Result<(), ReformulationError> {
323341
let index = slv.get_solver_int(self.index);
324342
let result = slv.get_solver_int(self.result);

0 commit comments

Comments
 (0)