Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: no additional variables during constraint construction #131

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -451,12 +451,11 @@ fn compile_bool2int(
let a = context.resolve_bool_variable(&exprs[0])?;
let b = context.resolve_integer_variable(&exprs[1])?;

Ok(constraints::binary_equals(
a,
context.solver.new_literal_for_predicate(predicate!(b == 1)),
Ok(
constraints::binary_equals(a.get_integer_variable(), b.scaled(1))
.post(context.solver, None)
.is_ok(),
)
.post(context.solver, None)
.is_ok())
}

fn compile_bool_or(
Expand Down
49 changes: 10 additions & 39 deletions pumpkin-solver/src/constraints/boolean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::num::NonZero;
use super::equals;
use super::less_than_or_equals;
use super::Constraint;
use crate::predicate;
use crate::variables::AffineView;
use crate::variables::DomainId;
use crate::variables::Literal;
Expand Down Expand Up @@ -49,7 +48,7 @@ impl Constraint for BooleanLessThanOrEqual {
solver: &mut Solver,
tag: Option<NonZero<u32>>,
) -> Result<(), ConstraintOperationError> {
let domains = self.create_domains(solver);
let domains = self.create_domains();

less_than_or_equals(domains, self.rhs).post(solver, tag)
}
Expand All @@ -60,34 +59,19 @@ impl Constraint for BooleanLessThanOrEqual {
reification_literal: Literal,
tag: Option<NonZero<u32>>,
) -> Result<(), ConstraintOperationError> {
let domains = self.create_domains(solver);
let domains = self.create_domains();

less_than_or_equals(domains, self.rhs).implied_by(solver, reification_literal, tag)
}
}

impl BooleanLessThanOrEqual {
fn create_domains(&self, solver: &mut Solver) -> Vec<AffineView<DomainId>> {
let domains = self
.bools
fn create_domains(&self) -> Vec<AffineView<DomainId>> {
self.bools
.iter()
.enumerate()
.map(|(index, bool)| {
let corresponding_domain_id = solver.new_bounded_integer(0, 1);
// bool -> [domain = 1]
let _ = solver.add_clause([
!(*bool).get_true_predicate(),
predicate![corresponding_domain_id >= 1],
]);
// !bool -> [domain = 0]
let _ = solver.add_clause([
bool.get_true_predicate(),
predicate![corresponding_domain_id <= 0],
]);
corresponding_domain_id.scaled(self.weights[index])
})
.collect::<Vec<_>>();
domains
.map(|(index, bool)| bool.get_integer_variable().scaled(self.weights[index]))
.collect()
}
}

Expand All @@ -103,7 +87,7 @@ impl Constraint for BooleanEqual {
solver: &mut Solver,
tag: Option<NonZero<u32>>,
) -> Result<(), ConstraintOperationError> {
let domains = self.create_domains(solver);
let domains = self.create_domains();

equals(domains, 0).post(solver, tag)
}
Expand All @@ -114,31 +98,18 @@ impl Constraint for BooleanEqual {
reification_literal: Literal,
tag: Option<NonZero<u32>>,
) -> Result<(), ConstraintOperationError> {
let domains = self.create_domains(solver);
let domains = self.create_domains();

equals(domains, 0).implied_by(solver, reification_literal, tag)
}
}

impl BooleanEqual {
fn create_domains(&self, solver: &mut Solver) -> Vec<AffineView<DomainId>> {
fn create_domains(&self) -> Vec<AffineView<DomainId>> {
self.bools
.iter()
.enumerate()
.map(|(index, bool)| {
let corresponding_domain_id = solver.new_bounded_integer(0, 1);
// bool -> [domain = 1]
let _ = solver.add_clause([
!(*bool).get_true_predicate(),
predicate![corresponding_domain_id >= 1],
]);
// !bool -> [domain = 0]
let _ = solver.add_clause([
(*bool).get_true_predicate(),
predicate![corresponding_domain_id <= 0],
]);
corresponding_domain_id.scaled(self.weights[index])
})
.map(|(index, bool)| bool.get_integer_variable().scaled(self.weights[index]))
.chain(std::iter::once(self.rhs.scaled(-1)))
.collect()
}
Expand Down
4 changes: 4 additions & 0 deletions pumpkin-solver/src/engine/variables/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ impl Literal {
}
}

pub fn get_integer_variable(&self) -> AffineView<DomainId> {
self.integer_variable
}

pub fn get_true_predicate(&self) -> Predicate {
self.lower_bound_predicate(1)
}
Expand Down
Loading