Search in sources :

Example 71 with Formula

use of org.logicng.formulas.Formula in project LogicNG by logic-ng.

the class VariableProfileTest method testNAryOperator.

@Test
public void testNAryOperator() throws ParserException {
    final Map<Literal, Integer> expected = new HashMap<>();
    expected.put(this.f2.variable("a"), 1);
    expected.put(this.f2.variable("b"), 2);
    expected.put(this.f2.variable("c"), 3);
    final PropositionalParser p = new PropositionalParser(this.f);
    final Formula formula = p.parse("a & (b | c) & (~b | ~c) & c");
    assertThat(formula.apply(this.varProfile, true)).isEqualTo(expected);
    assertThat(formula.apply(this.varProfile, false)).isEqualTo(expected);
}
Also used : Formula(org.logicng.formulas.Formula) HashMap(java.util.HashMap) Literal(org.logicng.formulas.Literal) PropositionalParser(org.logicng.io.parsers.PropositionalParser) Test(org.junit.jupiter.api.Test)

Example 72 with Formula

use of org.logicng.formulas.Formula in project LogicNG by logic-ng.

the class VariableProfileTest method testNot.

@Test
public void testNot() throws ParserException {
    final Map<Literal, Integer> expected = new HashMap<>();
    expected.put(this.f2.variable("a"), 1);
    expected.put(this.f2.variable("b"), 2);
    expected.put(this.f2.variable("c"), 3);
    final PropositionalParser p = new PropositionalParser(this.f);
    final Formula formula = p.parse("~(a & (b | c) & ((~b | ~c) => c))");
    assertThat(formula.apply(this.varProfile, true)).isEqualTo(expected);
    assertThat(formula.apply(this.varProfile, false)).isEqualTo(expected);
}
Also used : Formula(org.logicng.formulas.Formula) HashMap(java.util.HashMap) Literal(org.logicng.formulas.Literal) PropositionalParser(org.logicng.io.parsers.PropositionalParser) Test(org.junit.jupiter.api.Test)

Example 73 with Formula

use of org.logicng.formulas.Formula in project LogicNG by logic-ng.

the class VariableProfileTest method testBinaryOperator.

@Test
public void testBinaryOperator() throws ParserException {
    final Map<Literal, Integer> expected = new HashMap<>();
    expected.put(this.f2.variable("a"), 1);
    expected.put(this.f2.variable("b"), 2);
    expected.put(this.f2.variable("c"), 3);
    final PropositionalParser p = new PropositionalParser(this.f);
    final Formula impl = p.parse("(a & (b | c) & (~b | ~c)) => c");
    final Formula equiv = p.parse("(a & (b | c) & (~b | ~c)) <=> c");
    assertThat(impl.apply(this.varProfile, true)).isEqualTo(expected);
    assertThat(impl.apply(this.varProfile, false)).isEqualTo(expected);
    assertThat(equiv.apply(this.varProfile, true)).isEqualTo(expected);
    assertThat(equiv.apply(this.varProfile, false)).isEqualTo(expected);
}
Also used : Formula(org.logicng.formulas.Formula) HashMap(java.util.HashMap) Literal(org.logicng.formulas.Literal) PropositionalParser(org.logicng.io.parsers.PropositionalParser) Test(org.junit.jupiter.api.Test)

Example 74 with Formula

use of org.logicng.formulas.Formula in project LogicNG by logic-ng.

the class ConnectedComponentsComputerTest method testFormulaSplit.

@Test
public void testFormulaSplit() throws IOException, ParserException {
    final FormulaFactory f = new FormulaFactory();
    f.putConfiguration(CCConfig.builder().amoEncoding(CCConfig.AMO_ENCODER.PURE).build());
    final Formula parsed = FormulaReader.readPseudoBooleanFormula("src/test/resources/formulas/formula1.txt", f);
    final List<Formula> formulas = new ArrayList<>();
    final List<Formula> originalFormulas = new ArrayList<>();
    for (final Formula formula : parsed) {
        originalFormulas.add(formula);
        if (formula instanceof PBConstraint) {
            formulas.add(formula);
        } else {
            formulas.add(formula.transform(new CNFFactorization()));
        }
    }
    final Graph<Variable> constraintGraph = ConstraintGraphGenerator.generateFromCnf(formulas);
    final Set<Set<Node<Variable>>> ccs = ConnectedComponentsComputation.compute(constraintGraph);
    final List<List<Formula>> split = ConnectedComponentsComputation.splitFormulasByComponent(originalFormulas, ccs);
    assertThat(split).hasSize(4);
    assertThat(split.get(0)).hasSize(1899);
    assertThat(split.get(1)).hasSize(3);
    assertThat(split.get(2)).hasSize(3);
    assertThat(split.get(3)).hasSize(3);
}
Also used : Formula(org.logicng.formulas.Formula) FormulaFactory(org.logicng.formulas.FormulaFactory) Variable(org.logicng.formulas.Variable) Set(java.util.Set) HashSet(java.util.HashSet) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) PBConstraint(org.logicng.formulas.PBConstraint) CNFFactorization(org.logicng.transformations.cnf.CNFFactorization) GraphTest(org.logicng.graphs.datastructures.GraphTest) Test(org.junit.jupiter.api.Test)

Example 75 with Formula

use of org.logicng.formulas.Formula in project LogicNG by logic-ng.

the class GraphTest method testFormulaGraph.

@Test
public void testFormulaGraph() {
    final FormulaFactory f = new FormulaFactory();
    final Graph<Formula> g = new Graph<>("Graph with Formula nodes.");
    final Variable a = f.variable("A");
    final Node<Formula> an = g.node(a);
    final Variable b = f.variable("B");
    final Node<Formula> bn = g.node(b);
    final Formula aNb = f.and(a, b);
    final Node<Formula> aNbn = g.node(aNb);
    g.connect(aNbn, an);
    g.connect(aNbn, bn);
    assertThat(aNbn.neighbours().size()).isEqualTo(2);
    assertThat(an.neighbours().contains(aNbn)).isTrue();
    assertThat(bn.neighbours().contains(aNbn)).isTrue();
    assertThat(an.graph().name()).isEqualTo(g.name());
    g.disconnect(aNbn, an);
    assertThat(an.neighbours().isEmpty()).isTrue();
    assertThat(g.nodes().size()).isEqualTo(3);
}
Also used : Formula(org.logicng.formulas.Formula) FormulaFactory(org.logicng.formulas.FormulaFactory) Variable(org.logicng.formulas.Variable) Test(org.junit.jupiter.api.Test)

Aggregations

Formula (org.logicng.formulas.Formula)349 Test (org.junit.jupiter.api.Test)190 FormulaFactory (org.logicng.formulas.FormulaFactory)129 ArrayList (java.util.ArrayList)58 Variable (org.logicng.formulas.Variable)55 Literal (org.logicng.formulas.Literal)54 Assignment (org.logicng.datastructures.Assignment)50 PropositionalParser (org.logicng.io.parsers.PropositionalParser)50 PBConstraint (org.logicng.formulas.PBConstraint)45 SATSolver (org.logicng.solvers.SATSolver)36 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)25 CardinalityConstraint (org.logicng.formulas.CardinalityConstraint)25 MethodSource (org.junit.jupiter.params.provider.MethodSource)24 TreeSet (java.util.TreeSet)21 BDDKernel (org.logicng.knowledgecompilation.bdds.jbuddy.BDDKernel)20 TautologyPredicate (org.logicng.predicates.satisfiability.TautologyPredicate)20 HashMap (java.util.HashMap)17 LinkedHashSet (java.util.LinkedHashSet)17 List (java.util.List)17 LogicNGTest (org.logicng.LogicNGTest)16