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
//! Strategies for when and how to run DVO during BDD generation

#![allow(rustdoc::private_intra_doc_links)]

use std::time::{Duration, Instant};

use enum_dispatch::enum_dispatch;
use indicatif::ProgressBar;

use super::DDManager;
use crate::{bdd_node::NodeID, if_some};

/// Dummy DVO implementation that does nothing
#[derive(Default)]
pub struct NoDVOSchedule {}

impl DVOSchedule for NoDVOSchedule {
    fn run_dvo(
        &mut self,
        _num_clause: usize,
        _man: &mut DDManager,
        f: NodeID,
        _bar: &Option<ProgressBar>,
    ) -> NodeID {
        f
    }
}

/// Always perform sifting of all variables until the number of
/// nodes does not change anymore.
#[derive(Default)]
pub struct AlwaysUntilConvergence {}

impl DVOSchedule for AlwaysUntilConvergence {
    fn run_dvo(
        &mut self,
        _num_clause: usize,
        man: &mut DDManager,
        mut f: NodeID,
        bar: &Option<ProgressBar>,
    ) -> NodeID {
        log::info!("DVO... ");
        let mut last_size = man.count_active(f);
        loop {
            f = man.sift_all_vars(f, bar.is_some(), None);
            let new_size = man.count_active(f);

            if_some!(bar, set_message(format!("{} nodes", new_size)));

            if new_size == last_size {
                break;
            }
            last_size = new_size;
        }
        f
    }
}

/// Run one iteration of sifting for all variables, every time it's called.
/// See [DDManager::sift_single_var()] for `max_increase` parameter.
#[derive(Default)]
pub struct AlwaysOnce {
    pub max_increase: Option<u32>,
}

impl DVOSchedule for AlwaysOnce {
    fn run_dvo(
        &mut self,
        _num_clause: usize,
        man: &mut DDManager,
        mut f: NodeID,
        bar: &Option<ProgressBar>,
    ) -> NodeID {
        log::info!("DVO... ");
        f = man.sift_all_vars(f, bar.is_some(), self.max_increase);
        let new_size = man.count_active(f);
        if_some!(bar, set_message(format!("{} nodes", new_size)));
        f
    }
}

/// Call the underlying strategy if the node count exceeds the specified limit
pub struct AtThreshold {
    pub active_nodes_threshold: u32,
    pub underlying_schedule: Box<DVOScheduleEnum>,
}

impl DVOSchedule for AtThreshold {
    fn run_dvo(
        &mut self,
        num_clause: usize,
        man: &mut DDManager,
        f: NodeID,
        bar: &Option<ProgressBar>,
    ) -> NodeID {
        if man.count_active(f) <= self.active_nodes_threshold {
            f
        } else {
            self.underlying_schedule.run_dvo(num_clause, man, f, bar)
        }
    }
}

/// Performs sifting until the number of nodes does not change anymore,
/// but only if the initial number of nodes exceeds a configurable threshold.
pub struct SiftingAtThreshold {
    underlying: AtThreshold,
}

impl DVOSchedule for SiftingAtThreshold {
    fn run_dvo(
        &mut self,
        num_clause: usize,
        man: &mut DDManager,
        f: NodeID,
        bar: &Option<ProgressBar>,
    ) -> NodeID {
        self.underlying.run_dvo(num_clause, man, f, bar)
    }
}

impl SiftingAtThreshold {
    pub fn new(active_nodes_threshold: u32) -> SiftingAtThreshold {
        SiftingAtThreshold {
            underlying: AtThreshold {
                active_nodes_threshold,
                underlying_schedule: Box::new(AlwaysUntilConvergence::default().into()),
            },
        }
    }
}

/// Calls the underlying DVO mode if the specified duration has passed since the last
/// invocation, or the nodes table exceeds the size specified in `limit`.
pub struct TimeSizeLimit {
    pub interval: Duration,
    pub limit: usize,
    last_dvo: Instant,
    pub underlying_schedule: Box<DVOScheduleEnum>,
}

impl TimeSizeLimit {
    pub fn new(
        interval: Duration,
        limit: usize,
        underlying_schedule: Box<DVOScheduleEnum>,
    ) -> TimeSizeLimit {
        TimeSizeLimit {
            interval,
            limit,
            last_dvo: Instant::now(),
            underlying_schedule,
        }
    }
}

impl DVOSchedule for TimeSizeLimit {
    fn run_dvo(
        &mut self,
        num_clause: usize,
        man: &mut DDManager,
        f: NodeID,
        bar: &Option<ProgressBar>,
    ) -> NodeID {
        if (Instant::now() - self.last_dvo) > self.interval || man.nodes.len() > self.limit {
            let r = self.underlying_schedule.run_dvo(num_clause, man, f, bar);
            self.last_dvo = Instant::now();
            r
        } else {
            f
        }
    }
}

/// This contains all available DVO implementations
#[enum_dispatch]
pub enum DVOScheduleEnum {
    NoDVOSchedule,
    AlwaysUntilConvergence,
    AtThreshold,
    SiftingAtThreshold,
    TimeSizeLimit,
    AlwaysOnce,
}

impl Default for DVOScheduleEnum {
    fn default() -> Self {
        NoDVOSchedule::default().into()
    }
}

/// Implements run_dvo()
#[enum_dispatch(DVOScheduleEnum)]
pub(crate) trait DVOSchedule {
    /// This gets called after a CNF clause has been integrated.
    /// The current root node is f, the implementation must return the
    /// new root node ID, even if it does not change.
    /// * `num_clause`: The index of the current clause, in integration order
    ///   (which may differ from the order defined in the input CNF).
    fn run_dvo(
        &mut self,
        num_clause: usize,
        man: &mut DDManager,
        f: NodeID,
        bar: &Option<ProgressBar>,
    ) -> NodeID;
}