Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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 @@ -133,7 +133,11 @@ impl AnalysisMode {
// We first calculate the value in the heap of the new_predicate, and retrieve the
// predicate which is currently the next to be resolved upon (i.e., the maximum in the
// heap).
let heap_value = get_heap_value(new_predicate, context);
let trail_position = context
.get_state()
.trail_position(new_predicate)
.expect("Predicate should be true during conflict analysis");
let heap_value = get_heap_value(new_predicate, trail_position, context);
let max = working_nogood.peek_max_current_checkpoint();

// Then we check whether `new_predicate` would *not* be the next predicate to be resolved
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,15 +206,17 @@ impl ResolutionResolver {
//
// This is done in two steps:
// 1) Pop the predicate last assigned on the trail from the nogood.
let next_predicate = self
let (next_predicate, trail_position) = self
.working_nogood
.pop_max_predicate(&self.predicate_id_generator, self.mode);

// 2) Get the reason for the predicate and add it to the nogood.
// 2) Get the reason for the predicate and add it to the nogood; the trail position is
// already known from the heap, so it does not need to be looked up again.
self.reason_buffer.clear();

let _ = context.get_propagation_reason(
let _ = context.get_propagation_reason_at(
next_predicate,
trail_position,
self.working_nogood
.get_current_nogood(&self.predicate_id_generator),
&mut self.reason_buffer,
Expand Down Expand Up @@ -546,7 +548,8 @@ mod tests {
assert_eq!(
resolver
.working_nogood
.pop_max_predicate(&resolver.predicate_id_generator, mode),
.pop_max_predicate(&resolver.predicate_id_generator, mode)
.0,
predicate!(z >= 5)
);

Expand Down Expand Up @@ -620,7 +623,8 @@ mod tests {
assert_eq!(
resolver
.working_nogood
.pop_max_predicate(&resolver.predicate_id_generator, mode),
.pop_max_predicate(&resolver.predicate_id_generator, mode)
.0,
predicate!(z >= 5)
);
assert!(!mode.should_continue_resolving(
Expand Down
71 changes: 47 additions & 24 deletions pumpkin-crates/conflict-resolvers/src/resolvers/working_nogood.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use pumpkin_core::asserts::pumpkin_assert_eq_simple;
use pumpkin_core::asserts::pumpkin_assert_moderate;
use pumpkin_core::asserts::pumpkin_assert_simple;
use pumpkin_core::conflict_resolving::ConflictAnalysisContext;
use pumpkin_core::containers::HashMap;
Expand All @@ -10,6 +11,8 @@ use pumpkin_core::predicates::PredicateIdGenerator;
use pumpkin_core::propagation::PredicateId;
use pumpkin_core::propagation::ReadDomains;
use pumpkin_core::state::CurrentNogood;
#[cfg(doc)]
use pumpkin_core::state::State;
use pumpkin_core::statistics::Statistic;
use pumpkin_core::statistics::StatisticLogger;
use pumpkin_core::statistics::moving_averages::MovingAverage;
Expand Down Expand Up @@ -88,9 +91,13 @@ impl WorkingNogood {
}

/// Adds a predicate to the current working nogood which is from the current checkpoint.
///
/// The `trail_position` is the trail position of the `predicate` (see
/// [`State::trail_position`]).
fn add_predicate_current_checkpoint(
&mut self,
predicate: Predicate,
trail_position: usize,
context: &mut ConflictAnalysisContext<'_>,
predicate_id_generator: &mut PredicateIdGenerator,
mode: AnalysisMode,
Expand All @@ -110,7 +117,7 @@ impl WorkingNogood {
}

// We first retrieve the value that the predicate will get in the heap
let heap_value = get_heap_value(predicate, context);
let heap_value = get_heap_value(predicate, trail_position, context);

// Next, we restore the key in the heap
self.to_process_heap.restore_key(predicate_id);
Expand Down Expand Up @@ -354,7 +361,7 @@ impl WorkingNogood {
// which the extended UIP is calculated, this could be multiple elements.
let mut previous_predicate: Option<Predicate> = None;
while self.num_current_checkpoint() > 0 {
let predicate = self.pop_max_predicate(predicate_id_generator, mode);
let (predicate, _) = self.pop_max_predicate(predicate_id_generator, mode);

pumpkin_assert_eq_simple!(
previous_predicate.unwrap_or(predicate).get_domain(),
Expand Down Expand Up @@ -389,22 +396,33 @@ impl WorkingNogood {
}
}

/// Returns the next [`Predicate`] to resolve upon based on the trail.
/// Returns the next [`Predicate`] to resolve upon based on the trail, together with its trail
/// position.
///
/// The trail position is derived from the value of the predicate in the heap (see
/// [`get_heap_value`]), so that it does not need to be looked up again.
pub(crate) fn pop_max_predicate(
&mut self,
predicate_id_generator: &PredicateIdGenerator,
mode: AnalysisMode,
) -> Predicate {
) -> (Predicate, usize) {
let (_, &heap_value) = self
.to_process_heap
.peek_max()
.expect("there should be a predicate to resolve upon");
let next_predicate_id = self.to_process_heap.pop_max().unwrap();
let predicate = predicate_id_generator.get_predicate(next_predicate_id);
// The heap value is twice the trail position, plus one if the predicate is not
// explicitly on the trail.
let trail_position = (heap_value / 2) as usize;

mode.predicate_removed_from_nogood(predicate, &mut self.unique_variable_helper);

if self.iterative_minimisation {
self.iterative_minimiser.remove_predicate(predicate);
}

predicate
(predicate, trail_position)
}

/// Add the predicate to the current conflict nogood if we know it needs to be added.
Expand Down Expand Up @@ -448,17 +466,20 @@ impl WorkingNogood {
context: &mut ConflictAnalysisContext,
predicate_id_generator: &mut PredicateIdGenerator,
) {
let dec_level = context
.get_checkpoint_for_predicate(predicate)
// We look up the checkpoint and the trail position of the predicate once, since both are
// used below.
let (checkpoint, trail_position) = context
.get_state()
.checkpoint_and_trail_position(predicate)
.unwrap_or_else(|| {
panic!(
"Expected predicate {predicate} to be assigned but bounds were ({}, {})",
context.lower_bound(&predicate.get_domain()),
context.lower_bound(&predicate.get_domain()),
context.upper_bound(&predicate.get_domain()),
)
});
// Ignore root level predicates.
if dec_level == 0 {
if checkpoint == 0 {
context.explain_root_assignment(predicate);

if self.iterative_minimisation {
Expand All @@ -473,7 +494,7 @@ impl WorkingNogood {
// All-decision Learning
// If the variables are not decisions then we want to potentially add them to the heap,
// otherwise we add it to the decision predicates which have been discovered previously
else if mode.predicate_should_be_processed(predicate, dec_level, context) {
else if mode.predicate_should_be_processed(predicate, checkpoint, context) {
let predicate_id = predicate_id_generator.get_id(predicate);
// The first time we encounter the predicate, we initialise its value in the
// heap.
Expand All @@ -500,6 +521,7 @@ impl WorkingNogood {

self.add_predicate_current_checkpoint(
predicate,
trail_position,
context,
predicate_id_generator,
mode,
Expand Down Expand Up @@ -746,21 +768,22 @@ impl WorkingNogood {
/// The idea is that implied predicates (i.e., predicates which are not explicitly on the trail)
/// should be resolved upon before the predicates which are explicitly on the trail.
///
/// Panics if the provided [`Predicate`] is not currently true on the trail.
pub(crate) fn get_heap_value(predicate: Predicate, context: &ConflictAnalysisContext<'_>) -> u32 {
if context.get_state().is_on_trail(predicate) {
context
.get_state()
.trail_position(predicate)
.expect("Predicate should be true during conflict analysis") as u32
* 2
/// The `trail_position` should be the trail position of the provided [`Predicate`] (see
/// [`State::trail_position`]); it is passed in to avoid looking it up again.
pub(crate) fn get_heap_value(
predicate: Predicate,
trail_position: usize,
context: &ConflictAnalysisContext<'_>,
) -> u32 {
pumpkin_assert_moderate!(context.get_state().trail_position(predicate) == Some(trail_position));

if context
.get_state()
.is_on_trail_at(predicate, trail_position)
{
trail_position as u32 * 2
} else {
context
.get_state()
.trail_position(predicate)
.expect("Predicate should be true during conflict analysis") as u32
* 2
+ 1
trail_position as u32 * 2 + 1
}
}

Expand Down
1 change: 1 addition & 0 deletions pumpkin-crates/core/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ pub mod variables {
pub use crate::engine::variables::DomainId;
pub use crate::engine::variables::IntegerVariable;
pub use crate::engine::variables::Literal;
pub use crate::engine::variables::RangeOfUnderlyingDomain;
pub use crate::engine::variables::TransformableVariable;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,27 @@ impl ConflictAnalysisContext<'_> {
)
}

/// The same as [`ConflictAnalysisContext::get_propagation_reason`], given that
/// `trail_position` is the trail position of the provided [`Predicate`]; this avoids looking
/// up the trail position again if it is already known.
pub fn get_propagation_reason_at(
&mut self,
predicate: Predicate,
trail_position: usize,
current_nogood: CurrentNogood<'_>,
reason_buffer: &mut (impl Extend<Predicate> + AsRef<[Predicate]>),
) -> Option<InferenceCode> {
Self::get_propagation_reason_inner_at(
predicate,
trail_position,
current_nogood,
self.proof_log,
self.unit_nogood_inference_codes,
reason_buffer,
self.state,
)
}

/// Returns the last decision which was made by the solver (if such a decision exists).
pub fn find_last_decision(&mut self) -> Option<Predicate> {
self.state.assignments.find_last_decision()
Expand Down Expand Up @@ -355,13 +376,41 @@ impl ConflictAnalysisContext<'_> {
reason_buffer: &mut (impl Extend<Predicate> + AsRef<[Predicate]>),
state: &mut State,
) -> Option<InferenceCode> {
let inference_code = state.get_propagation_reason(predicate, reason_buffer, current_nogood);
let trail_position = state
.trail_position(predicate)
.unwrap_or_else(|| panic!("The predicate {predicate:?} must be true"));

Self::get_propagation_reason_inner_at(
predicate,
trail_position,
current_nogood,
proof_log,
unit_nogood_inference_codes,
reason_buffer,
state,
)
}

/// The same as [`ConflictAnalysisContext::get_propagation_reason_inner`], given that
/// `trail_position` is the trail position of the provided [`Predicate`].
pub(crate) fn get_propagation_reason_inner_at(
predicate: Predicate,
trail_position: usize,
current_nogood: CurrentNogood<'_>,
proof_log: &mut ProofLog,
unit_nogood_inference_codes: &HashMap<Predicate, InferenceCode>,
reason_buffer: &mut (impl Extend<Predicate> + AsRef<[Predicate]>),
state: &mut State,
) -> Option<InferenceCode> {
let inference_code = state.get_propagation_reason_at(
predicate,
trail_position,
reason_buffer,
current_nogood,
);

if let Some(ref ic) = inference_code {
let trail_index = state.trail_position(predicate).expect(
"an inference code is only present if the propagated predicate is on the trail",
);
let trail_entry = state.assignments.get_trail_entry(trail_index);
let trail_entry = state.assignments.get_trail_entry(trail_position);
let Some(reason_ref) = trail_entry.reason else {
return inference_code;
};
Expand Down
Loading
Loading