Skip to content

Commit

Permalink
More consistent naming of constraints and their propagators
Browse files Browse the repository at this point in the history
  • Loading branch information
Dekker1 committed Jan 31, 2025
1 parent b3eb0bc commit e641b80
Show file tree
Hide file tree
Showing 18 changed files with 617 additions and 599 deletions.
13 changes: 6 additions & 7 deletions crates/huub/src/constraints.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
//! Module containing the definitions for propagators and their implementations.
pub mod all_different_int;
pub mod array_int_element;
pub mod array_int_minimum;
pub mod array_var_bool_element;
pub mod array_var_int_element;
pub mod bool_array_element;
pub mod disjunctive_strict;
pub mod int_abs;
pub mod int_all_different;
pub mod int_array_element;
pub mod int_array_minimum;
pub mod int_div;
pub mod int_in_set;
pub mod int_linear;
pub mod int_pow;
pub mod int_table;
pub mod int_times;
pub mod set_in_reif;
pub mod table_int;

use std::{
error::Error,
Expand Down
61 changes: 0 additions & 61 deletions crates/huub/src/constraints/array_int_element.rs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Structures and algorithms for the `array_var_bool_element` constraint, which
//! Structures and algorithms for the Boolean array element constraint, which
//! enforces that a resulting variable equals an element of an array of Boolean
//! decision variables, chosen by an index variable.
Expand All @@ -15,12 +15,13 @@ use crate::{
};

#[derive(Clone, Debug, Eq, Hash, PartialEq)]
/// Representation of the `array_var_bool_element` constraint within a model.
/// Representation of the `array_element` constraint with an array of Boolean
/// decision variables within a model.
///
/// This constraint enforces that a result Boolean decision variable takes the
/// value equal the element of the given array of Boolean decision varaibles at
/// the index given by the index integer decision variable.
pub struct ArrayVarBoolElement {
pub struct BoolDecisionArrayElement {
/// The array of Boolean decision variables
pub(crate) array: Vec<BoolDecision>,
/// The index variable
Expand All @@ -29,7 +30,7 @@ pub struct ArrayVarBoolElement {
pub(crate) result: BoolDecision,
}

impl<S: SimplificationActions> Constraint<S> for ArrayVarBoolElement {
impl<S: SimplificationActions> Constraint<S> for BoolDecisionArrayElement {
fn simplify(&mut self, actions: &mut S) -> Result<SimplificationStatus, ReformulationError> {
if let Some(i) = actions.get_int_val(self.index) {
actions.add_constraint(Formula::Equiv(vec![
Expand Down
4 changes: 2 additions & 2 deletions crates/huub/src/constraints/int_abs.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Structures and algorithms for the `int_abs` constraint, which enforces that
//! one variable is takes absolute value of another.
//! Structures and algorithms for the integer absolute value constraint, which
//! enforces that one variable is takes absolute value of another.
use std::iter::once;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Structure and algorithms for the `all_different_int` constraint, which
//! Structure and algorithms for the integer all different constraint, which
//! enforces that a list of integer variables each take a different value.
use itertools::{Either, Itertools};
Expand All @@ -21,19 +21,19 @@ use crate::{
///
/// This constraint enforces that all the given integer decisions take different
/// values.
pub struct AllDifferentInt {
pub struct IntAllDifferent {
/// List of integer decision variables that must take different values.
pub(crate) vars: Vec<IntDecision>,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
/// Value consistent propagator for the `all_different_int` constraint.
pub struct AllDifferentIntValue {
pub struct IntAllDifferentValue {
/// List of integer variables that must take different values.
vars: Vec<IntView>,
}

impl<S: SimplificationActions> Constraint<S> for AllDifferentInt {
impl<S: SimplificationActions> Constraint<S> for IntAllDifferent {
fn simplify(&mut self, actions: &mut S) -> Result<SimplificationStatus, ReformulationError> {
let (vals, vars): (Vec<_>, Vec<_>) = self.vars.iter().partition_map(|&var| {
if let Some(val) = actions.get_int_val(var) {
Expand Down Expand Up @@ -61,12 +61,12 @@ impl<S: SimplificationActions> Constraint<S> for AllDifferentInt {

fn to_solver(&self, slv: &mut dyn ReformulationActions) -> Result<(), ReformulationError> {
let vars: Vec<_> = self.vars.iter().map(|v| slv.get_solver_int(*v)).collect();
AllDifferentIntValue::new_in(slv, vars);
IntAllDifferentValue::new_in(slv, vars);
Ok(())
}
}

impl AllDifferentIntValue {
impl IntAllDifferentValue {
/// Create a new [`AllDifferentIntValue`] propagator and post it in the
/// solver.
pub fn new_in<P: PropagatorInitActions + ?Sized>(solver: &mut P, vars: Vec<IntView>) {
Expand All @@ -83,7 +83,7 @@ impl AllDifferentIntValue {
}
}

impl<P, E> Propagator<P, E> for AllDifferentIntValue
impl<P, E> Propagator<P, E> for IntAllDifferentValue
where
P: PropagationActions,
E: ExplanationActions,
Expand Down Expand Up @@ -113,7 +113,7 @@ mod tests {
use tracing_test::traced_test;

use crate::{
constraints::all_different_int::AllDifferentIntValue,
constraints::int_all_different::IntAllDifferentValue,
solver::{
int_var::{EncodingType, IntVar},
IntView, SolveResult, Solver,
Expand Down Expand Up @@ -144,7 +144,7 @@ mod tests {
EncodingType::Eager,
);

AllDifferentIntValue::new_in(&mut slv, vec![a, b, c]);
IntAllDifferentValue::new_in(&mut slv, vec![a, b, c]);

slv.assert_all_solutions(&[a, b, c], |sol| sol.iter().all_unique());
}
Expand Down Expand Up @@ -172,7 +172,7 @@ mod tests {
EncodingType::Eager,
);

AllDifferentIntValue::new_in(&mut slv, vec![a, b, c]);
IntAllDifferentValue::new_in(&mut slv, vec![a, b, c]);

slv.assert_unsatisfiable();
}
Expand All @@ -197,15 +197,15 @@ mod tests {
}
}

AllDifferentIntValue::new_in(&mut slv, vars.clone());
IntAllDifferentValue::new_in(&mut slv, vars.clone());

all_vars.push(vars);
});
// add all different propagator for each column
for i in 0..9 {
let col_vars: Vec<IntView> = (0..9).map(|j| all_vars[j][i]).collect();

AllDifferentIntValue::new_in(&mut slv, col_vars);
IntAllDifferentValue::new_in(&mut slv, col_vars);
}
// add all different propagator for each 3 by 3 grid
for i in 0..3 {
Expand All @@ -217,7 +217,7 @@ mod tests {
}
}

AllDifferentIntValue::new_in(&mut slv, block_vars);
IntAllDifferentValue::new_in(&mut slv, block_vars);
}
}
assert_eq!(
Expand Down
Loading

0 comments on commit e641b80

Please sign in to comment.