logicng 0.1.0-alpha.3

A Library for Creating, Manipulating, and Solving Boolean Formulas
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
551
552
553
554
555
556
557
558
559
560
use itertools::Itertools;

use crate::datastructures::Assignment;
use crate::formulas::{FormulaFactory, Literal, StringLiteral, Variable};

#[derive(Debug, Clone)]
/// A `Model` stores a set of positive and negative [`Variable`]s.
///
/// `Model` stores all variables in [`Vec`]s, this allows for a fast creation of
/// new models, but makes it unsuitable for evaluating formulas. It also does
/// not check for duplicates. For that you have to use [`Assignment`], which
/// uses hash-sets to store the variables.
///
/// # Conversion to `Assignment` and vice-versa
///
/// Depending of your use case it might be better to have a `Model` or an
/// [`Assignment`]. Both data-structures implement the `From` trait, such that
/// you can easily swap between both.
///
/// Convert from `Model` to [`Assignment`]:
/// ```
/// # use crate::logicng::formulas::FormulaFactory;
/// # use crate::logicng::datastructures::{Model, Assignment};
/// let f = FormulaFactory::new();
///
/// let a = f.var("a");
/// let b = f.var("b");
/// let model = Model::new(vec![a], vec![b]);
/// let assignment = Assignment::from(model);
/// ```
///
/// /// Convert from [`Assignment`] to `Model`:
/// ```
/// # use crate::logicng::formulas::FormulaFactory;
/// # use crate::logicng::datastructures::{Model, Assignment};
/// let f = FormulaFactory::new();
///
/// let a = f.var("a");
/// let b = f.var("b");
/// let assignment = Assignment::from_variables(&[a], &[b]);
/// let model = Model::from(assignment);
/// ```
pub struct Model {
    pos: Vec<Variable>,
    neg: Vec<Variable>,
}

impl Model {
    /// Creates a new model.
    ///
    /// # Examples
    ///
    /// Basic usage:
    ///
    /// ```
    /// # use logicng::datastructures::Model;
    /// # use logicng::formulas::FormulaFactory;
    /// let f = FormulaFactory::new();
    ///
    /// let a = f.var("a");
    /// let b = f.var("b");
    ///
    /// let model = Model::new(vec![a], vec![b]);
    /// ```
    pub fn new<P, N>(pos: P, neg: N) -> Self
    where
        P: Into<Vec<Variable>>,
        N: Into<Vec<Variable>>, {
        Self { pos: pos.into(), neg: neg.into() }
    }

    /// Creates a new model from slices.
    ///
    /// # Examples
    ///
    /// Basic usage:
    ///
    /// ```
    /// # use logicng::datastructures::Model;
    /// # use logicng::formulas::FormulaFactory;
    /// let f = FormulaFactory::new();
    ///
    /// let a = f.var("a");
    /// let b = f.var("b");
    ///
    /// let model = Model::from_variables(&[a], &[b]);
    /// ```
    pub fn from_variables(pos: &[Variable], neg: &[Variable]) -> Self {
        Self { pos: pos.into(), neg: neg.into() }
    }

    /// Converts a literal into a model.
    ///
    /// A positive literal is added to the positive variables, and a negative
    /// literal to the negative variables.
    ///
    /// # Examples
    ///
    /// Basic usage:
    ///
    /// ```
    /// # use logicng::datastructures::Model;
    /// # use logicng::formulas::FormulaFactory;
    /// let f = FormulaFactory::new();
    ///
    /// let a = f.lit("a", true);
    /// let b = f.lit("b", false);
    ///
    /// let model1 = Model::from_lit(a);
    /// let model2 = Model::from_lit(b);
    ///
    /// assert_eq!(model1.pos(), &vec![a.variable()]);
    /// assert_eq!(model2.neg(), &vec![b.variable()]);
    /// ```
    pub fn from_lit(lit: Literal) -> Self {
        if lit.phase() {
            Self { pos: vec![lit.variable()], neg: vec![] }
        } else {
            Self { pos: vec![], neg: vec![lit.variable()] }
        }
    }

    /// Creates a model from a single vector of literals.
    ///
    /// All positive literals are added to the positive variables, and all
    /// negative literals to the negative variables.
    ///
    /// # Examples
    ///
    /// Basic usage:
    ///
    /// ```
    /// # use logicng::datastructures::Model;
    /// # use logicng::formulas::FormulaFactory;
    /// let f = FormulaFactory::new();
    ///
    /// let a = f.lit("a", true);
    /// let b = f.lit("b", false);
    ///
    /// let model = Model::from_literals(&[a, b]);
    ///
    /// assert_eq!(model.pos(), &[a.variable()]);
    /// assert_eq!(model.neg(), &[b.variable()]);
    /// ```
    pub fn from_literals(literals: &[Literal]) -> Self {
        let mut pos = Vec::with_capacity(literals.len());
        let mut neg = Vec::with_capacity(literals.len());
        for lit in literals {
            if lit.phase() {
                pos.push(lit.variable());
            } else {
                neg.push(lit.variable());
            }
        }
        Self { pos, neg }
    }

    /// Creates a new model from slices of names.
    ///
    /// Uses names of already existing variables and adds those variables to the
    /// model. If a name has no existing variable in the formula factory, the
    /// function will return an error.
    ///
    /// # Examples
    ///
    /// Basic usage:
    ///
    /// ```
    /// # use logicng::datastructures::Model;
    /// # use logicng::formulas::FormulaFactory;
    /// # fn main() -> Result<(), String> {
    /// let f = FormulaFactory::new();
    ///
    /// let a = f.var("a");
    /// let b = f.var("b");
    ///
    /// let model = Model::from_names(&["a"], &["b"], &f)?;
    ///
    /// assert_eq!(model.pos(), &vec![a]);
    /// assert_eq!(model.neg(), &vec![b]);
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// If a name is not a variable, an error will be returned:
    /// ```
    /// # use logicng::datastructures::Model;
    /// # use logicng::formulas::FormulaFactory;
    /// let f = FormulaFactory::new();
    ///
    /// let a = f.var("a");
    ///
    /// let model = Model::from_names(&["a"], &["b"], &f);
    ///
    /// assert!(model.is_err());
    /// ```
    pub fn from_names(pos: &[&str], neg: &[&str], f: &FormulaFactory) -> Result<Self, String> {
        let pos = names_to_indices(pos, f)?;
        let neg = names_to_indices(neg, f)?;
        Ok(Self { pos, neg })
    }

    /// Returns all positive variables of this model.
    ///
    /// # Examples
    ///
    /// Basic usage:
    ///
    /// ```
    /// # use logicng::datastructures::Model;
    /// # use logicng::formulas::FormulaFactory;
    /// let f = FormulaFactory::new();
    ///
    /// let a = f.var("a");
    /// let b = f.var("b");
    ///
    /// let model = Model::new(vec![a], vec![b]);
    ///
    /// assert_eq!(model.pos(), &vec![a]);
    /// ```
    pub fn pos(&self) -> &[Variable] {
        &self.pos
    }

    /// Returns all negative variables of this model.
    ///
    /// # Examples
    ///
    /// Basic usage:
    ///
    /// ```
    /// # use logicng::datastructures::Model;
    /// # use logicng::formulas::FormulaFactory;
    /// let f = FormulaFactory::new();
    ///
    /// let a = f.var("a");
    /// let b = f.var("b");
    ///
    /// let model = Model::new(vec![a], vec![b]);
    ///
    /// assert_eq!(model.neg(), &vec![b]);
    /// ```
    pub fn neg(&self) -> &[Variable] {
        &self.neg
    }

    /// Returns the overall number of positive and negative variables.
    ///
    /// A variable added as positive and negative is counted twice. Also, any
    /// variable added multiple times is counted multiple times.
    ///
    /// # Examples
    ///
    /// Basic usage:
    ///
    /// ```
    /// # use logicng::datastructures::Model;
    /// # use logicng::formulas::FormulaFactory;
    /// let f = FormulaFactory::new();
    ///
    /// let a = f.var("a");
    /// let b = f.var("b");
    ///
    /// let model = Model::from_variables(&[a], &[b]);
    ///
    /// assert_eq!(model.len(), 2);
    /// ```
    pub fn len(&self) -> usize {
        self.pos.len() + self.neg.len()
    }

    /// Returns `true` if there is no variable in this model.
    ///
    /// # Examples
    ///
    /// Basic usage:
    ///
    /// ```
    /// # use logicng::datastructures::Model;
    /// # use logicng::formulas::FormulaFactory;
    /// let f = FormulaFactory::new();
    ///
    /// let a = f.var("a");
    /// let b = f.var("b");
    ///
    /// let model1 = Model::from_variables(&[a], &[b]);
    /// let model2 = Model::from_variables(&[], &[]);
    ///
    /// assert_eq!(model1.is_empty(), false);
    /// assert_eq!(model2.is_empty(), true);
    /// ```
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Returns a vector of [`Literal`] representing this model.
    ///
    /// All positive variables are returned as positive literals and all
    /// negative variables are returned as negative literals. The vector first
    /// consists of all positive literals and then of all negative literals.
    ///
    /// # Examples
    ///
    /// Basic usage:
    ///
    /// ```
    /// # use logicng::datastructures::Model;
    /// # use logicng::formulas::FormulaFactory;
    /// let f = FormulaFactory::new();
    ///
    /// let a = f.lit("a", true);
    /// let b = f.lit("b", false);
    ///
    /// let model = Model::from_literals(&[a, b]);
    ///
    /// assert_eq!(model.literals(), vec![a, b]);
    /// ```
    pub fn literals(&self) -> Vec<Literal> {
        let mut result = Vec::with_capacity(self.len());
        self.pos.iter().for_each(|var| result.push(Literal::Pos(*var)));
        self.neg.iter().for_each(|var| result.push(Literal::Neg(*var)));
        result
    }

    /// Returns a vector of [`StringLiteral`] representing this model.
    ///
    /// All positive variables are returned as positive literals and all
    /// negative variables are returned as negative literals. The vector first
    /// consists of all positive literals and then of all negative literals.
    ///
    /// # Examples
    ///
    /// Basic usage:
    ///
    /// ```
    /// # use logicng::datastructures::Model;
    /// # use logicng::formulas::FormulaFactory;
    /// let f = FormulaFactory::new();
    ///
    /// let a = f.lit("a", true);
    /// let b = f.lit("b", false);
    ///
    /// let model = Model::from_literals(&[a, b]);
    ///
    /// assert_eq!(model.string_literals(&f), vec![a.to_string_lit(&f), b.to_string_lit(&f)]);
    /// ```
    pub fn string_literals<'c>(&self, f: &'c FormulaFactory) -> Vec<StringLiteral<'c>> {
        let mut result = Vec::with_capacity(self.len());
        self.pos.iter().for_each(|var| result.push(Literal::Pos(*var).to_string_lit(f)));
        self.neg.iter().for_each(|var| result.push(Literal::Neg(*var).to_string_lit(f)));
        result
    }

    /// Compares two models and returns `true` if they are the same. The order
    /// of the elements and duplicates do not matter.
    ///
    /// This function can be slow, because both models will be converted to
    /// [`Assignment`]. Consider converting this model to an `Assignment`
    /// yourself, if you want compare it multiple times.
    ///
    /// # Example
    ///
    /// Basic usage:
    /// ```
    /// # use logicng::datastructures::Model;
    /// # use logicng::formulas::FormulaFactory;
    /// let f = FormulaFactory::new();
    ///
    /// let a = f.lit("a", true);
    /// let b = f.lit("b", false);
    ///
    /// let model1 = Model::from_literals(&[a, b]);
    /// let model2 = Model::from_literals(&[a, b, b]);
    /// let model3 = Model::from_literals(&[a]);
    ///
    /// assert!(model1.compare(&model2));
    /// assert!(!model1.compare(&model3));
    /// ```
    pub fn compare(&self, other: &Self) -> bool {
        Assignment::from(self) == Assignment::from(other)
    }

    /// Creates a string representation of this model.
    ///
    /// # Examples
    ///
    /// Basic usage:
    ///
    /// ```
    /// # use logicng::datastructures::Model;
    /// # use logicng::formulas::FormulaFactory;
    /// let f = FormulaFactory::new();
    ///
    /// let a = f.var("a");
    /// let b = f.var("b");
    ///
    /// let model = Model::from_variables(&[a], &[b]);
    ///
    /// assert_eq!(model.to_string(&f), "Model{pos=[a], neg=[b]}");
    /// ```
    pub fn to_string(&self, f: &FormulaFactory) -> String {
        format!(
            "Model{{pos=[{}], neg=[{}]}}",
            self.pos.iter().map(|v| v.to_string_lit(f)).join(", "),
            self.neg.iter().map(|v| v.to_string_lit(f)).join(", ")
        )
    }
}

impl AsRef<Self> for Model {
    fn as_ref(&self) -> &Self {
        self
    }
}

impl<M: AsRef<Model>> From<M> for Assignment {
    fn from(model: M) -> Self {
        let m = model.as_ref();
        Self::from_variables(&m.pos, &m.neg)
    }
}

fn names_to_indices(names: &[&str], f: &FormulaFactory) -> Result<Vec<Variable>, String> {
    let mut result = Vec::with_capacity(names.len());
    for name in names {
        let index = match f.variables.lookup(&(*name).to_string()) {
            Some(i) => Variable::FF(i),
            None => {
                return Err(format!("Variable {} is not known in the given FormulaFactory", *name));
            }
        };
        result.push(index);
    }
    Ok(result)
}

#[cfg(test)]
mod tests {
    use crate::datastructures::{Assignment, Model};
    use crate::formulas::{FormulaFactory, StringLiteral};
    use crate::util::test_util::{lits, lits_list, vars_list};
    use std::collections::BTreeSet;

    #[test]
    fn test_constructor1() {
        let f = &FormulaFactory::new();
        let vars1 = vars_list("a b c", f);
        let vars2 = vars_list("x y", f);
        let model = Model::from_variables(&vars1, &vars2);
        assert_eq!(vars1, model.pos());
        assert_eq!(vars2, model.neg());
        assert_eq!(5, model.len());
    }

    #[test]
    fn test_constructor2() {
        let f = &FormulaFactory::new();
        let vars1 = vars_list("a b c", f);
        let vars2 = vars_list("x y", f);
        let lits = lits_list("a b c ~x ~y", f);
        let model = Model::from_literals(&lits);
        assert_eq!(vars1, model.pos());
        assert_eq!(vars2, model.neg());
        assert_eq!(5, model.len());
    }

    #[test]
    fn test_constructor3() {
        let f = &FormulaFactory::new();
        let vars1 = vars_list("a b c", f);
        let vars2 = vars_list("x y", f);
        let model = Model::from_names(&["a", "b", "c"], &["x", "y"], f).unwrap();
        assert_eq!(vars1, model.pos());
        assert_eq!(vars2, model.neg());
        assert_eq!(5, model.len());
    }

    #[test]
    fn test_is_empty() {
        let f = &FormulaFactory::new();
        let vars1 = vars_list("b a", f);
        let vars2 = vars_list("x y", f);
        let a1 = Model::new(vec![], vec![]);
        let a2 = Model::new(vars1.clone(), vec![]);
        let a3 = Model::new(vec![], vars2.clone());
        let a4 = Model::new(vars1, vars2);
        assert!(a1.is_empty());
        assert!(!a2.is_empty());
        assert!(!a3.is_empty());
        assert!(!a4.is_empty());
    }

    #[test]
    fn test_literals() {
        let f = &FormulaFactory::new();
        let vars1 = vars_list("b a", f);
        let vars2 = vars_list("x y", f);
        let a1 = Model::new(vec![], vec![]);
        let a2 = Model::new(vars1.clone(), vec![]);
        let a3 = Model::new(vec![], vars2.clone());
        let a4 = Model::new(vars1, vars2);
        assert_eq!(a1.literals(), vec![]);
        assert_eq!(lits_list("b a", f), a2.literals());
        assert_eq!(lits_list("~x ~y", f), a3.literals());
        assert_eq!(lits_list("b a ~x ~y", f), a4.literals());
    }

    #[test]
    fn test_string_literals() {
        let f = &FormulaFactory::new();
        let vars1 = vars_list("b a", f);
        let vars2 = vars_list("x y", f);
        let a1 = Model::new(vec![], vec![]);
        let a2 = Model::new(vars1.clone(), vec![]);
        let a3 = Model::new(vec![], vars2.clone());
        let a4 = Model::new(vars1, vars2);
        assert_eq!(Vec::<StringLiteral>::new(), a1.string_literals(f));
        assert_eq!(vec![StringLiteral::new("b", true), StringLiteral::new("a", true)], a2.string_literals(f));
        assert_eq!(vec![StringLiteral::new("x", false), StringLiteral::new("y", false),], a3.string_literals(f));
        assert_eq!(
            vec![
                StringLiteral::new("b", true),
                StringLiteral::new("a", true),
                StringLiteral::new("x", false),
                StringLiteral::new("y", false),
            ],
            a4.string_literals(f)
        );
    }

    #[test]
    fn test_to_assignment() {
        let f = &FormulaFactory::new();
        let vars1 = vars_list("b a", f);
        let vars2 = vars_list("x y", f);
        let a1 = Model::new(vec![], vec![]);
        let a2 = Model::new(vars1.clone(), vec![]);
        let a3 = Model::new(vec![], vars2.clone());
        let a4 = Model::new(vars1, vars2);
        assert_eq!(Assignment::from_set(BTreeSet::new()), a1.into());
        assert_eq!(Assignment::from_set(lits("b a", f)), a2.into());
        assert_eq!(Assignment::from_set(lits("~x ~y", f)), a3.into());
        assert_eq!(Assignment::from_set(lits("b a ~x ~y", f)), a4.into());
    }

    #[test]
    fn test_to_string() {
        let f = &FormulaFactory::new();
        let vars1 = vars_list("b a", f);
        let vars2 = vars_list("x y", f);
        let a1 = Model::new(vec![], vec![]);
        let a2 = Model::new(vars1.clone(), vec![]);
        let a3 = Model::new(vec![], vars2.clone());
        let a4 = Model::new(vars1, vars2);
        assert_eq!("Model{pos=[], neg=[]}", a1.to_string(f));
        assert_eq!("Model{pos=[b, a], neg=[]}", a2.to_string(f));
        assert_eq!("Model{pos=[], neg=[x, y]}", a3.to_string(f));
        assert_eq!("Model{pos=[b, a], neg=[x, y]}", a4.to_string(f));
    }
}