veripb-propagator 0.1.2

VeriPB library for a pseudo-Boolean propagation engine.
Documentation
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
use std::rc::Rc;

use veripb_formula::prelude::*;

use crate::{
    error::PropagatorError,
    trail::{Propagation, Reason, Trail},
    watcher::{LevelZeroPropagationResult, WatchInit, WatchUpdate, Watcher},
};

#[derive(Debug, PartialEq, Eq)]
pub enum PropagationResult {
    Unknown,
    Conflict,
}

impl PropagationResult {
    /// Check if the `PropagationResult` is a conflict or not.
    #[inline]
    pub fn is_conflict(&self) -> bool {
        matches!(*self, PropagationResult::Conflict)
    }
}

#[derive(Debug)]
pub struct Propagator<W>
where
    W: Watcher,
{
    watchlist: Vec<Vec<Rc<DBConstraint>>>,
    watchers: Vec<Option<Box<W>>>,
    empty_watcher_pos: usize,
    unregistered_watchers: Vec<usize>,
    trail_head: usize,
    /// A list of propagation that will always happen. E.g., this are unit clauses or cardinality constraints with a degree equal to the number of literals in the constraint.
    assignment_independent_propagations: Vec<Propagation>,
    unhandled_assignment_independent_watchers: Vec<usize>,
    touched_watchers: Vec<usize>,
}

impl<W> Propagator<W>
where
    W: Watcher,
{
    /// Create a new propagator with capacity for specified variables.
    #[inline]
    pub fn with_size(num_variables: usize) -> Self {
        Propagator {
            watchlist: vec![Vec::new(); num_variables * 2],
            watchers: Vec::new(),
            empty_watcher_pos: 0,
            unregistered_watchers: Vec::new(),
            trail_head: 0,
            assignment_independent_propagations: Vec::new(),
            unhandled_assignment_independent_watchers: Vec::new(),
            touched_watchers: Vec::new(),
        }
    }

    /// Resize the propagator to fit a size of `num_variable` many variables. Since the watchlist is indexed by the literal, we need to allocate twice the number of variables.
    #[inline]
    pub fn resize(&mut self, num_variables: usize) {
        self.watchlist.resize(num_variables * 2, Vec::new());
    }

    #[inline]
    fn register_watches(&mut self, new_watches: &[Lit], constraint: &Rc<DBConstraint>) {
        for lit in new_watches {
            self.watchlist[lit.get_lit_data()].push(constraint.clone());
        }
    }

    #[inline]
    fn register_watch(&mut self, new_watch: Lit, constraint: &Rc<DBConstraint>) {
        self.watchlist[new_watch.get_lit_data()].push(constraint.clone());
    }

    /// Add `constraint` to the propagator.
    ///
    /// The propagator should create a watcher to watch the constraint and add the watcher to the watchers handled by the propagator. This operation should not add watches to the watchlist of the propagator.
    #[inline]
    pub fn add(&mut self, constraint: &Rc<DBConstraint>) -> Result<(), PropagatorError> {
        if constraint.header.borrow().propagator_id.is_some() {
            return Err(PropagatorError::AttachingAttached);
        }
        let watcher = W::from_db_constraint(constraint);
        for watcher in self.watchers[self.empty_watcher_pos..].iter() {
            if watcher.is_none() {
                break;
            }
            self.empty_watcher_pos += 1;
        }
        constraint.header.borrow_mut().propagator_id = Some(self.empty_watcher_pos);
        if self.empty_watcher_pos == self.watchers.len() {
            self.watchers.push(Some(Box::new(watcher)));
        } else {
            // We checked that the index is inside the size of the vector.
            *unsafe { self.watchers.get_unchecked_mut(self.empty_watcher_pos) } =
                Some(Box::new(watcher));
        }
        self.unregistered_watchers.push(self.empty_watcher_pos);
        self.unhandled_assignment_independent_watchers
            .push(self.empty_watcher_pos);
        self.empty_watcher_pos += 1;
        Ok(())
    }

    /// Remove `constraint` from the propagator.
    ///
    /// Remove the constraint from the propagator and delete the constraint from the watchlist, as there is no easy way to remove the constraint lazily. This is because the constraint could immediately be added to a different propagator.
    #[inline]
    pub fn remove(&mut self, constraint: &Rc<DBConstraint>) -> Result<(), PropagatorError> {
        let mut header = constraint.header.borrow_mut();
        if let Some(propagator_id) = header.propagator_id {
            if let Some(mut watcher) = self.watchers[propagator_id].take() {
                // Track first free space.
                if propagator_id < self.empty_watcher_pos {
                    self.empty_watcher_pos = propagator_id;
                }
                header.propagator_id = None;
                if watcher.is_propagating_independent_of_assignment() {
                    let reason = Reason::Constraint(constraint.clone());
                    self.assignment_independent_propagations
                        .retain(|p| !Rc::ptr_eq(p.reason.unwrap(), reason.unwrap()));
                    if watcher.no_watches_required() {
                        return Ok(());
                    }
                }
                // Watches are removed eagerly.
                for lit in watcher.get_watches() {
                    if lit.is_undef() {
                        continue;
                    }
                    let list = self.watchlist.get_mut(lit.get_lit_data()).unwrap();
                    let index = list.iter().position(|c| Rc::ptr_eq(c, constraint)).unwrap();
                    list.swap_remove(index);
                }

                Ok(())
            } else {
                Err(PropagatorError::AttachedToOtherPropagator)
            }
        } else {
            Ok(())
        }
    }

    /// Check if propagtor has a constraint that is a reason in the saved trail.
    pub fn has_saved_reason(&self) -> bool {
        for watcher in self.watchers.iter().flatten() {
            if watcher.get_constraint().header.borrow().is_saved_reason {
                return true;
            }
        }
        false
    }

    /// Remove all constraints from this propagator and return the removed constraints.
    pub fn remove_all(&mut self, removed_constraints: &mut Vec<Rc<DBConstraint>>) {
        for watcher in self.watchers.iter().flatten() {
            let constraint = watcher.get_constraint();
            constraint.header.borrow_mut().propagator_id = None;
            removed_constraints.push(constraint.clone());
        }

        for list in self.watchlist.iter_mut() {
            list.clear();
        }
        self.watchers.clear();
        self.assignment_independent_propagations.clear();
        self.empty_watcher_pos = 0;
        self.unregistered_watchers.clear();
        self.unhandled_assignment_independent_watchers.clear();
        self.trail_head = 0;
        self.touched_watchers.clear();
    }

    /// Reset the propagator to the assignment.
    #[inline]
    pub fn reset(&mut self, prev_trail_len: usize) {
        self.trail_head = prev_trail_len;
    }

    /// Register unregistered constraints to the watchlist.
    #[inline]
    fn register_unregistered_to_watchlist(
        &mut self,
        trail: &mut Trail,
        mark_reasons: bool,
        remember_watcher: bool,
    ) -> Option<PropagationResult> {
        while let Some(watcher_pos) = self.unregistered_watchers.pop() {
            if let Some(watcher) = self.watchers.get_mut(watcher_pos).unwrap() {
                // Do not add watches for constraints that are already fully propagated.
                if watcher.no_watches_required() {
                    // Check if the constraint is also conflicting.
                    if watcher.get_constraint().is_contradicting() {
                        // Directly terminate with conflict, if constraint is conflicting.
                        trail.conflict = Some(Reason::Constraint(watcher.get_constraint().clone()));
                        if mark_reasons {
                            watcher.get_constraint().header.borrow_mut().is_saved_reason = true;
                        }
                        self.unregistered_watchers.push(watcher_pos);
                        return Some(PropagationResult::Conflict);
                    }
                    continue;
                }

                if remember_watcher {
                    self.touched_watchers.push(watcher_pos);
                }
                match watcher.init_watches(&trail.assignment) {
                    WatchInit::Conflict => {
                        trail.conflict = Some(Reason::Constraint(watcher.get_constraint().clone()));
                        if mark_reasons {
                            watcher.get_constraint().header.borrow_mut().is_saved_reason = true;
                        }
                        self.unregistered_watchers.push(watcher_pos);
                        return Some(PropagationResult::Conflict);
                    }
                    WatchInit::Watching {
                        propagated,
                        watches,
                    } => {
                        // Initialize watches for unregistered constraint.
                        let constraint = watcher.get_constraint().clone();
                        self.register_watches(&watches, &constraint);
                        for &lit in propagated.iter() {
                            if trail
                                .push(
                                    Propagation::new(lit, Reason::Constraint(constraint.clone())),
                                    mark_reasons,
                                )
                                .is_err()
                            {
                                return Some(PropagationResult::Conflict);
                            }
                        }
                    }
                }
            }
        }
        None
    }

    /// Propagate using `trail`.
    #[inline]
    pub fn propagate(
        &mut self,
        trail: &mut Trail,
        mark_reasons: bool,
        remember_watcher: bool,
    ) -> PropagationResult {
        if self.watchers.is_empty() {
            return PropagationResult::Unknown;
        }
        if let Some(value) =
            self.register_unregistered_to_watchlist(trail, mark_reasons, remember_watcher)
        {
            return value;
        }

        // Update watches for the negated literals on the trail.
        while self.trail_head < trail.len() {
            let Propagation { mut lit, reason: _ } = trail.trail[self.trail_head];
            lit.negate();
            let list = unsafe { self.watchlist.get_unchecked_mut(lit.get_lit_data()) };
            if list.is_empty() {
                self.trail_head += 1;
                continue;
            }
            let mut list = std::mem::take(list);
            while let Some(constraint) = list.pop() {
                let propagator_id =
                    unsafe { constraint.header.borrow().propagator_id.unwrap_unchecked() };
                let watcher = unsafe {
                    self.watchers
                        .get_unchecked_mut(propagator_id)
                        .as_mut()
                        .unwrap_unchecked()
                };
                if remember_watcher {
                    self.touched_watchers.push(propagator_id);
                }
                match watcher.update_watch(lit, &trail.assignment) {
                    WatchUpdate::None => {}
                    WatchUpdate::Conflict => {
                        trail.conflict = Some(Reason::Constraint(constraint.clone()));
                        if mark_reasons {
                            constraint.header.borrow_mut().is_saved_reason = true;
                        }
                        self.watchlist[lit.get_lit_data()].push(constraint);
                        self.watchlist[lit.get_lit_data()].append(&mut list);
                        return PropagationResult::Conflict;
                    }
                    WatchUpdate::Unknown {
                        propagated,
                        new_watches,
                    } => {
                        self.register_watches(&new_watches, &constraint);
                        for &propagated_lit in propagated.iter() {
                            if trail
                                .push(
                                    Propagation {
                                        lit: propagated_lit,
                                        reason: Reason::Constraint(constraint.clone()),
                                    },
                                    mark_reasons,
                                )
                                .is_err()
                            {
                                self.watchlist[lit.get_lit_data()].append(&mut list);
                                return PropagationResult::Conflict;
                            }
                        }
                    }
                    WatchUpdate::UnknownSingle {
                        propagated,
                        new_watches,
                    } => {
                        self.register_watch(new_watches, &constraint);
                        if let Some(propagated_lit) = propagated {
                            if trail
                                .push(
                                    Propagation {
                                        lit: propagated_lit,
                                        reason: Reason::Constraint(constraint.clone()),
                                    },
                                    mark_reasons,
                                )
                                .is_err()
                            {
                                self.watchlist[lit.get_lit_data()].append(&mut list);
                                return PropagationResult::Conflict;
                            }
                        }
                    }
                }
            }

            self.trail_head += 1;
        }

        PropagationResult::Unknown
    }

    #[inline]
    pub fn get_assignment_independent_propagations(&mut self) -> &Vec<Propagation> {
        // Check for propagation at level zero of new watchers.
        while let Some(watcher_pos) = self.unhandled_assignment_independent_watchers.pop() {
            if let Some(watcher) = &self.watchers[watcher_pos] {
                match &mut watcher.get_assignment_independent_propagations() {
                    LevelZeroPropagationResult::Partial(propagations) => {
                        self.assignment_independent_propagations
                            .append(propagations);
                    }
                    LevelZeroPropagationResult::Complete(propagations) => {
                        self.assignment_independent_propagations
                            .append(propagations);
                    }
                    LevelZeroPropagationResult::None => {}
                }
            }
        }

        &self.assignment_independent_propagations
    }

    #[inline]
    pub fn get_new_assignment_independent_propagations(
        &mut self,
        new_propagations: &mut Vec<Propagation>,
    ) {
        // Check for propagation at level zero of new watchers.
        while let Some(watcher_pos) = self.unhandled_assignment_independent_watchers.pop() {
            if let Some(watcher) = &self.watchers[watcher_pos] {
                match &mut watcher.get_assignment_independent_propagations() {
                    LevelZeroPropagationResult::Partial(propagations) => {
                        new_propagations.extend(propagations.iter().cloned());
                        self.assignment_independent_propagations
                            .append(propagations);
                    }
                    LevelZeroPropagationResult::Complete(propagations) => {
                        new_propagations.extend(propagations.iter().cloned());
                        self.assignment_independent_propagations
                            .append(propagations);
                    }
                    LevelZeroPropagationResult::None => {}
                }
            }
        }
    }

    #[inline]
    pub fn clear(&mut self) {
        self.watchers.drain(..).flatten().for_each(|watcher| {
            watcher.get_watches().for_each(|lit| {
                let opt_watchlist = self.watchlist.get_mut(lit.get_lit_data());
                if let Some(watchlist) = opt_watchlist {
                    watchlist.clear()
                }
            });
            watcher.get_constraint().header.borrow_mut().propagator_id = None;
        });
        self.watchers.clear();
        self.empty_watcher_pos = 0;
        self.unregistered_watchers.clear();
        self.trail_head = 0;
        self.assignment_independent_propagations.clear();
        self.unhandled_assignment_independent_watchers.clear();
        self.touched_watchers.clear();
    }

    #[inline]
    pub fn reset_last_pos(&mut self) {
        if self.watchers.len() < self.touched_watchers.len() {
            self.watchers
                .iter_mut()
                .flatten()
                .for_each(|w| w.reset_last_pos());
        } else {
            self.touched_watchers.iter().for_each(|&pos| {
                if let Some(watcher) = unsafe { self.watchers.get_unchecked_mut(pos) } {
                    watcher.reset_last_pos();
                }
            })
        }
        self.touched_watchers.clear();
    }

    #[inline]
    pub fn increase_slack(&mut self, mut lit: Lit) {
        lit.negate();
        for constraint in unsafe { self.watchlist.get_unchecked(lit.get_lit_data()).iter() } {
            let watcher = unsafe {
                self.watchers
                    .get_unchecked_mut(constraint.header.borrow().propagator_id.unwrap_unchecked())
                    .as_mut()
                    .unwrap_unchecked()
            };
            watcher.increase_slack(lit);
        }
    }
}

impl<W: Watcher> Default for Propagator<W> {
    fn default() -> Self {
        Propagator {
            watchlist: Vec::new(),
            watchers: Vec::new(),
            empty_watcher_pos: 0,
            unregistered_watchers: Vec::new(),
            trail_head: 0,
            assignment_independent_propagations: Vec::new(),
            unhandled_assignment_independent_watchers: Vec::new(),
            touched_watchers: Vec::new(),
        }
    }
}