microsat 0.0.1

A simple DPLL SAT solver
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
use core::panic;
use hashbrown::{HashMap, HashSet};
use std::cmp::{max, min, Ordering};
use std::fmt::Debug;
use std::sync::{Arc, RwLock};

use crate::cnf::{
    negate, to_variable, Action, ActionState, Assignment, Clause, ClauseId, Literal, Variable, CNF,
};
use crate::dimacs_parser::parse_dimacs;
use crate::stack::Stack;

#[derive(Clone, Copy, Debug)]
pub enum SolverHeuristic {
    MostLiteralOccurances,
    MostVariableOccurances,
    MinimizeClauseLength,
}

pub struct Expression {
    clauses: Vec<Clause>,
    variables: HashSet<Variable>,
    actions: Arc<RwLock<Stack<Action>>>,
    assignments: HashMap<Variable, bool>,

    literal_to_clause: HashMap<Literal, HashSet<ClauseId>>,
    unit_clauses: HashSet<ClauseId>,
    pure_literals: HashSet<Literal>,
    num_active_clauses: u16,
    num_empty_clauses: usize,
    max_clause_length: usize,
    pub heuristic: SolverHeuristic,
}

impl Clone for Expression {
    fn clone(&self) -> Self {
        let mut new_expression = Expression::new();
        for clause in &self.clauses {
            new_expression.add_clause(clause.clone());
        }

        new_expression
    }
}

impl Default for Expression {
    fn default() -> Self {
        Self::new()
    }
}

impl Expression {
    pub fn new() -> Expression {
        Expression {
            clauses: Vec::new(),
            variables: HashSet::new(),
            actions: Arc::new(RwLock::new(Stack::new(0))),
            assignments: HashMap::new(),

            literal_to_clause: HashMap::new(),
            unit_clauses: HashSet::new(),
            pure_literals: HashSet::new(),
            num_active_clauses: 0,
            num_empty_clauses: 0,
            max_clause_length: 0,
            heuristic: SolverHeuristic::MostLiteralOccurances,
        }
    }

    pub fn from_clauses(clauses: Vec<Clause>) -> Expression {
        let mut expression = Expression::new();
        for clause in clauses {
            expression.add_clause(clause);
        }

        expression
    }

    pub fn from_cnf_file(file_name: &str) -> Expression {
        return parse_dimacs(file_name);
    }

    pub fn get_clauses(&self) -> Vec<Clause> {
        self.clauses.clone()
    }

    pub fn set_heuristic(&mut self, heuristic: SolverHeuristic) {
        self.heuristic = heuristic;
    }

    /// Softly removes a clause from the expression.
    /// This means that the clause is not actually removed from the expression vector,
    /// but all references to it have been removed from the literals map, so it is unreferenced.
    fn remove_clause(&mut self, clause_id: ClauseId) {
        // Remove all of the literals in the clause from the variable_to_clause map
        for i in 0..self.clauses[clause_id as usize].len() {
            let literal = unsafe { self.clauses.get_unchecked(clause_id as usize).get(i) };
            let literal_clauses = self.literal_to_clause.get_mut(&literal).unwrap();

            // If there are no more clauses that contain the literal, the negation is a pure literal
            if literal_clauses.is_empty() {
                // This literal has no more instances.
                // If its negation has some number of instances, add it to the pure_literals set.
                let negated_literal = negate(literal);
                let negated_literal_clauses = self.literal_to_clause.get_mut(&negated_literal);

                if negated_literal_clauses.is_none() || negated_literal_clauses.unwrap().is_empty()
                {
                    self.pure_literals.insert(negated_literal);
                }
            }
        }

        self.num_active_clauses -= 1;
        self.unit_clauses.remove(&clause_id);
        self.actions
            .write()
            .unwrap()
            .push(Action::RemoveClause(clause_id));
    }

    /// Re-enables a clause that had been softly removed, so all of its literals are still present in the vector.
    fn enable_clause(&mut self, clause_id: ClauseId) {
        self.num_active_clauses += 1;

        let clause = unsafe { &self.clauses.get_unchecked(clause_id as usize) };
        if clause.len() == 1 {
            self.unit_clauses.insert(clause_id);
        }

        for i in 0..clause.len() {
            let literal = unsafe { self.clauses.get_unchecked(clause_id as usize).get(i) };
            let should_check_pure_literal;
            {
                let literal_clauses = self.literal_to_clause.get_mut(&literal).unwrap();
                literal_clauses.insert(clause_id);
                should_check_pure_literal = literal_clauses.len() == 1;
            }

            if should_check_pure_literal {
                // TODO: Can we avoid doing this check again? Does it do too much?
                self.check_pure_literal(literal);
            }
        }
    }

    /// Removes a literal from all of the clauses that it is in
    fn remove_literal_from_clauses(&mut self, literal: Literal) {
        let clauses_result = self.literal_to_clause.get(&literal);
        if clauses_result.is_none() {
            return;
        }

        let actions = self.actions.clone();
        let mut actions = actions.write().unwrap();

        actions.push(Action::RemoveLiteralFromClausesStart());

        let literal_clauses = clauses_result.unwrap();
        for clause_id in literal_clauses {
            let clause = &mut self.clauses[*clause_id as usize];
            clause.remove(literal);

            if clause.len() == 1 {
                self.unit_clauses.insert(*clause_id);
            }

            if clause.is_empty() {
                self.num_empty_clauses += 1;
                self.unit_clauses.remove(clause_id);
            }

            actions.push(Action::RemoveLiteralFromClause(*clause_id));
        }

        actions.push(Action::RemoveLiteralFromClausesEnd(literal));
    }

    /// Removes all clauses with the specified literal.
    fn remove_clauses_with_literal(&mut self, literal: Literal) {
        let literal_clauses;
        {
            let literal_clauses_ref = self.literal_to_clause.get(&literal);
            if literal_clauses_ref.is_none() {
                return;
            }
            // TODO: Prevent cloning
            literal_clauses = literal_clauses_ref.unwrap().clone();
        }

        for clause_id in literal_clauses {
            self.remove_clause(clause_id);
        }
    }

    fn check_pure_literal(&mut self, literal: Literal) {
        let negated_literal = negate(literal);
        let literal_clauses = self.literal_to_clause.get(&literal);
        let has_instances = literal_clauses.is_some() && !literal_clauses.unwrap().is_empty();

        let negated_literal_clauses = self.literal_to_clause.get(&negated_literal);
        let negated_has_instances =
            negated_literal_clauses.is_some() && !negated_literal_clauses.unwrap().is_empty();

        if has_instances && !negated_has_instances {
            self.pure_literals.insert(literal);
            self.pure_literals.remove(&negated_literal);
        } else if !has_instances && negated_has_instances {
            self.pure_literals.insert(negated_literal);
            self.pure_literals.remove(&literal);
        } else {
            self.pure_literals.remove(&literal);
            self.pure_literals.remove(&negated_literal);
        }
    }

    fn assign_variable(&mut self, variable: Variable, value: bool) {

        self.assignments.insert(variable, value);
        self.actions
            .write()
            .unwrap()
            .push(Action::AssignVariable(variable));
        let literal = if value {
            variable as Literal
        } else {
            -(variable as Literal)
        };
        let negated_literal = negate(literal);
        self.remove_clauses_with_literal(literal);
        self.remove_literal_from_clauses(negated_literal);

        self.pure_literals.remove(&literal);
        self.pure_literals.remove(&negated_literal);
    }

    fn unassign_variable(&mut self, variable: Variable) {
        self.assignments.remove(&variable);
    }

    pub fn optimize(&mut self) {
        // Remove all of the empty clauses
        self.actions = Arc::new(RwLock::new(Stack::new(
            self.clauses.len() * self.max_clause_length,
        ))); // Pre-allocate a reasonable amount of space
    }

    pub fn is_satisfied_by(&self, assignment: &Assignment) -> bool {
        for clause in &self.clauses {
            let mut satisfied = false;
            for literal in clause.literals() {
                let variable = to_variable(*literal);
                let value = assignment.get(&variable);
                if value.is_none() {
                    continue;
                }

                if *value.unwrap() == (*literal > 0) {
                    satisfied = true;
                    break;
                }
            }

            if !satisfied {
                return false;
            }
        }

        true
    }

    fn get_most_literal_occurances(&self) -> (Variable, bool) {
        let mut max_occurances = 0;
        let mut best_literal = 0;

        for literal_clause in &self.literal_to_clause {
            let literal = literal_clause.0;
            if literal_clause.1.is_empty() || self.assignments.contains_key(&to_variable(*literal))
            {
                continue;
            }

            let occurances = literal_clause.1.len();
            if occurances > max_occurances {
                max_occurances = occurances;
                best_literal = *literal;
            }
        }

        if best_literal != 0 {
            return (to_variable(best_literal), best_literal > 0);
        }

        panic!("No branch variable found");
    }

    fn get_most_variable_occurances(&self) -> (Variable, bool) {
        let mut max_occurances = 0;
        let mut best_variable = 0;

        for variable in &self.variables {
            let positive_literal = *variable as Literal;
            let negative_literal = -positive_literal;

            if self.assignments.contains_key(variable) {
                continue;
            }

            let positive_occurances = self.literal_to_clause.get(&positive_literal).unwrap().len();
            let negative_occurances = self.literal_to_clause.get(&negative_literal).unwrap().len();

            let occurances = positive_occurances + negative_occurances;
            if occurances > max_occurances {
                max_occurances = occurances;
                best_variable = *variable;
            }
        }

        if best_variable != 0 {
            return (best_variable, true);
        }

        panic!("No branch variable found");
    }

    const ALPHA: usize = 1;
    const BETA: usize = 1;
    fn get_lexicographically_maximizing_literal(&self) -> (Variable, bool) {
        let mut best_variables = self
            .variables
            .iter()
            .filter(|x| !self.assignments.contains_key(*x))
            .collect::<Vec<&Variable>>();

        for clause_size in 2..5 {
            let mut best_heuristic_value = 0;
            let mut new_best_variables: Vec<&Variable> = Vec::new();

            for variable in best_variables {
                let positive_literal = *variable as Literal;
                let negative_literal = -positive_literal;

                let positive_occurrences = self
                    .literal_to_clause
                    .get(&positive_literal)
                    .unwrap()
                    .iter()
                    .filter(|clause_id| self.clauses[**clause_id as usize].len() == clause_size)
                    .count();
                let negative_occurences = self
                    .literal_to_clause
                    .get(&negative_literal)
                    .unwrap()
                    .iter()
                    .filter(|clause_id| self.clauses[**clause_id as usize].len() == clause_size)
                    .count();

                let heuristic_value = Self::ALPHA * max(positive_occurrences, negative_occurences)
                    + Self::BETA * min(positive_occurrences, negative_occurences);

                match heuristic_value.cmp(&best_heuristic_value) {
                    Ordering::Greater => {
                        best_heuristic_value = heuristic_value;
                        new_best_variables.clear();
                        new_best_variables.push(variable);
                    }
                    Ordering::Equal => {
                        new_best_variables.push(variable);
                    }
                    _ => {}
                }
            }

            best_variables = new_best_variables;

            if best_variables.len() == 1 {
                break;
            }
        }

        let variable = *best_variables[0];
        let positive_literal = variable as Literal;
        let negative_literal = -positive_literal;

        let positive_occurrences = self.literal_to_clause.get(&positive_literal).unwrap().len();
        let negative_occurrences = self.literal_to_clause.get(&negative_literal).unwrap().len();

        (variable, positive_occurrences > negative_occurrences)
    }
}

impl CNF for Expression {
    fn add_clause(&mut self, clause: Clause) {
        let clause_id = self.clauses.len() as ClauseId;

        for literal in clause.literals() {
            {
                let variable: Variable = to_variable(*literal);
                self.variables.insert(variable);

                if !self.literal_to_clause.contains_key(literal) {
                    self.literal_to_clause.insert(*literal, HashSet::new());
                }

                if !self.literal_to_clause.contains_key(&negate(*literal)) {
                    self.literal_to_clause
                        .insert(negate(*literal), HashSet::new());
                }

                let literal_clauses = self.literal_to_clause.get_mut(literal).unwrap();
                literal_clauses.insert(clause_id);
            }
            // Check if the literal is a pure literal
            self.check_pure_literal(*literal);
        }

        // Make sure we add it if it is a unit clause
        if clause.len() == 1 {
            self.unit_clauses.insert(clause_id);
        }

        if clause.len() > self.max_clause_length {
            self.max_clause_length = clause.len();
        }

        self.clauses.push(clause);
        self.num_active_clauses += 1;
    }

    fn remove_unit_clause(&mut self) -> Option<ClauseId> {
        if self.unit_clauses.is_empty() {
            return None;
        }

        let clause_id: ClauseId = *self.unit_clauses.iter().next().unwrap();

        let literal = unsafe { self.clauses.get_unchecked(clause_id as usize).literals()[0] };

        self.assign_variable(to_variable(literal), literal > 0);
        Some(clause_id)
    }

    fn remove_pure_literal(&mut self) -> Option<Literal> {
        if self.pure_literals.is_empty() {
            return None;
        }

        let literal: Literal = *self.pure_literals.iter().next().unwrap();

        self.assign_variable(to_variable(literal), literal > 0);
        Some(literal)
    }

    fn construct_assignment(&mut self) -> Assignment {
        let mut assignments = HashMap::new();

        // Copy the existing assignments array to another one
        for (k, v) in self.assignments.iter() {
            assignments.insert(*k, *v);
        }

        // Assign all of the remaining variables to true
        for variable in &self.variables {
            if !assignments.contains_key(variable) {
                assignments.insert(*variable, true);
            }
        }
        assignments
    }

    #[inline]
    fn is_satisfied(&self) -> bool {
        self.num_active_clauses == 0
    }

    #[inline]
    fn is_unsatisfiable(&self) -> bool {
        self.num_empty_clauses > 0
    }

    fn get_branch_variable(&self) -> (Variable, bool) {
        match self.heuristic {
            SolverHeuristic::MostLiteralOccurances => self.get_most_literal_occurances(),
            SolverHeuristic::MostVariableOccurances => self.get_most_variable_occurances(),
            SolverHeuristic::MinimizeClauseLength => {
                self.get_lexicographically_maximizing_literal()
            }
        }
    }

    fn branch_variable(&mut self, variable: Variable, value: bool) {
        self.assign_variable(variable, value);
    }

    fn get_action_state(&self) -> ActionState {
        return self.actions.read().unwrap().len();
    }

    fn restore_action_state(&mut self, state: ActionState) {
        let actions = self.actions.clone();
        let mut actions = actions.write().unwrap();
        while actions.len() > state {
            let action = actions.pop().unwrap();
            match action {
                Action::RemoveClause(clause_id) => self.enable_clause(clause_id),
                Action::RemoveLiteralFromClausesEnd(literal) => {
                    let removing_literal_clauses =
                        self.literal_to_clause.get_mut(&literal).unwrap();

                    let mut should_exit = false;

                    while !should_exit {
                        let next_action = actions.pop().unwrap();
                        match next_action {
                            Action::RemoveLiteralFromClause(clause_id) => {
                                let clause =
                                    unsafe { self.clauses.get_unchecked_mut(clause_id as usize) };
                                clause.insert(literal);
                                if clause.len() == 1 {
                                    self.num_empty_clauses -= 1;
                                    self.unit_clauses.insert(clause_id);
                                } else if clause.len() == 2 {
                                    self.unit_clauses.remove(&clause_id);
                                }

                                removing_literal_clauses.insert(clause_id);
                            }
                            Action::RemoveLiteralFromClausesStart() => {
                                should_exit = true;
                            }
                            _ => panic!("Did not encounter a start literal!"),
                        }
                    }
                }
                Action::AssignVariable(variable) => {
                    self.unassign_variable(variable);
                }
                _ => break,
            }
        }
    }

    /// Inference is possibly when there are some "Active" clauses, 
    /// and either pure literals or unit clauses.
    fn is_inference_possible(&self) -> bool {
        self.num_empty_clauses == 0
            && self.num_active_clauses > 0
            && (!self.pure_literals.is_empty() || !self.unit_clauses.is_empty())
    }
}