Search in sources :

Example 76 with Formula

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

the class BackboneGenerationTest method testBackboneGeneration.

@Test
public void testBackboneGeneration() {
    final FormulaFactory f = new FormulaFactory();
    final Variable x = f.variable("x");
    final Variable y = f.variable("y");
    final Variable z = f.variable("z");
    final Formula formula1 = f.and(x.negate(), y);
    final Formula formula2 = f.or(x, z.negate());
    final Collection<Formula> collection = new ArrayList<>(Arrays.asList(formula1, formula2));
    assertThat(BackboneGeneration.compute(formula1).getCompleteBackbone()).containsExactly(x.negate(), y);
    assertThat(BackboneGeneration.compute(formula1, BackboneType.ONLY_NEGATIVE).getCompleteBackbone()).containsExactly(x.negate());
    assertThat(BackboneGeneration.compute(formula1, new ArrayList<>(Arrays.asList(x, z))).getCompleteBackbone()).containsExactly(x.negate());
    assertThat(BackboneGeneration.compute(formula1, new ArrayList<>(Arrays.asList(x, z)), BackboneType.ONLY_NEGATIVE).getCompleteBackbone()).containsExactly(x.negate());
    assertThat(BackboneGeneration.compute(collection).getCompleteBackbone()).containsExactly(x.negate(), y, z.negate());
    assertThat(BackboneGeneration.compute(collection, BackboneType.ONLY_NEGATIVE).getCompleteBackbone()).containsExactly(x.negate(), z.negate());
    assertThat(BackboneGeneration.compute(collection, new ArrayList<>(Arrays.asList(x, y))).getCompleteBackbone()).containsExactly(x.negate(), y);
    assertThat(BackboneGeneration.compute(collection, new ArrayList<>(Arrays.asList(x, y)), BackboneType.ONLY_NEGATIVE).getCompleteBackbone()).containsExactly(x.negate());
    assertThat(BackboneGeneration.computePositive(formula1).getCompleteBackbone()).containsExactly(y);
    assertThat(BackboneGeneration.computePositive(formula1, new ArrayList<>(Arrays.asList(x, z))).getCompleteBackbone()).isEmpty();
    assertThat(BackboneGeneration.computePositive(collection).getCompleteBackbone()).containsExactly(y);
    assertThat(BackboneGeneration.computePositive(collection, new ArrayList<>(Arrays.asList(x, y))).getCompleteBackbone()).containsExactly(y);
    assertThat(BackboneGeneration.computeNegative(formula1).getCompleteBackbone()).containsExactly(x.negate());
    assertThat(BackboneGeneration.computeNegative(formula1, new ArrayList<>(Arrays.asList(x, z))).getCompleteBackbone()).containsExactly(x.negate());
    assertThat(BackboneGeneration.computeNegative(collection).getCompleteBackbone()).containsExactly(x.negate(), z.negate());
    assertThat(BackboneGeneration.computeNegative(collection, new ArrayList<>(Arrays.asList(x, y))).getCompleteBackbone()).containsExactly(x.negate());
}
Also used : Formula(org.logicng.formulas.Formula) FormulaFactory(org.logicng.formulas.FormulaFactory) Variable(org.logicng.formulas.Variable) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test)

Example 77 with Formula

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

the class BackboneGenerationTest method testSmallFormulas.

@Test
public void testSmallFormulas() throws IOException, ParserException {
    final FormulaFactory f = new FormulaFactory();
    final Formula formula = FormulaReader.readPseudoBooleanFormula("src/test/resources/formulas/small_formulas.txt", f);
    final MiniSat solver = MiniSat.miniSat(f);
    solver.add(formula);
    final Backbone backbone = solver.execute(BackboneFunction.builder().variables(formula.variables()).build());
    assertThat(verifyBackbone(backbone, formula, formula.variables())).isTrue();
}
Also used : Formula(org.logicng.formulas.Formula) FormulaFactory(org.logicng.formulas.FormulaFactory) MiniSat(org.logicng.solvers.MiniSat) Test(org.junit.jupiter.api.Test)

Example 78 with Formula

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

the class BackboneGenerationTest method testDifferentConfigurations.

@Test
public void testDifferentConfigurations() throws IOException, ParserException {
    final List<MiniSatConfig> configs = new ArrayList<>();
    configs.add(MiniSatConfig.builder().bbCheckForComplementModelLiterals(false).build());
    configs.add(MiniSatConfig.builder().bbCheckForRotatableLiterals(false).build());
    configs.add(MiniSatConfig.builder().bbInitialUBCheckForRotatableLiterals(false).build());
    final FormulaFactory f = new FormulaFactory();
    final Formula formula = FormulaReader.readPseudoBooleanFormula("src/test/resources/formulas/large_formula.txt", f);
    MiniSat solver = MiniSat.miniSat(f);
    solver.add(formula);
    final Backbone backbone = solver.execute(BackboneFunction.builder().variables(formula.variables()).build());
    for (final MiniSatConfig config : configs) {
        solver = MiniSat.miniSat(f, config);
        solver.add(formula);
        assertThat(solver.execute(BackboneFunction.builder().variables(formula.variables()).build())).isEqualTo(backbone);
    }
}
Also used : Formula(org.logicng.formulas.Formula) MiniSatConfig(org.logicng.solvers.sat.MiniSatConfig) FormulaFactory(org.logicng.formulas.FormulaFactory) MiniSat(org.logicng.solvers.MiniSat) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test)

Example 79 with Formula

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

the class BackboneGenerationTest method testSimpleBackbones.

@Test
public void testSimpleBackbones() {
    final FormulaFactory f = new FormulaFactory();
    final MiniSat solver = MiniSat.miniSat(f);
    final Literal x = f.literal("x", true);
    final Literal y = f.literal("y", true);
    final Literal z = f.literal("z", true);
    final Literal u = f.literal("u", true);
    final Literal v = f.literal("v", true);
    final Collection<Variable> variables = new ArrayList<>(Arrays.asList(f.variable("x"), f.variable("y"), f.variable("z"), f.variable("u"), f.variable("v")));
    Formula formula = f.verum();
    SolverState before = solver.saveState();
    solver.add(formula);
    assertThat(solver.execute(BackboneFunction.builder().variables(Collections.emptyList()).build()).getCompleteBackbone()).isEmpty();
    solver.loadState(before);
    formula = x;
    before = solver.saveState();
    solver.add(formula);
    assertThat(solver.execute(BackboneFunction.builder().variables(variables).build()).getCompleteBackbone()).containsExactly(x);
    solver.loadState(before);
    formula = f.and(x, y);
    before = solver.saveState();
    solver.add(formula);
    assertThat(solver.execute(BackboneFunction.builder().variables(variables).build()).getCompleteBackbone()).containsExactly(x, y);
    solver.loadState(before);
    formula = f.or(x, y);
    before = solver.saveState();
    solver.add(formula);
    assertThat(solver.execute(BackboneFunction.builder().variables(variables).build()).getCompleteBackbone()).isEmpty();
    solver.loadState(before);
    formula = x.negate();
    before = solver.saveState();
    solver.add(formula);
    assertThat(solver.execute(BackboneFunction.builder().variables(variables).build()).getCompleteBackbone()).containsExactly(x.negate());
    solver.loadState(before);
    formula = f.or(f.and(x, y, z), f.and(x, y, u), f.and(x, u, z));
    before = solver.saveState();
    solver.add(formula);
    assertThat(solver.execute(BackboneFunction.builder().variables(variables).build()).getCompleteBackbone()).containsExactly(x);
    solver.loadState(before);
    formula = f.and(f.or(x, y, z), f.or(x, y, u), f.or(x, u, z));
    before = solver.saveState();
    solver.add(formula);
    assertThat(solver.execute(BackboneFunction.builder().variables(variables).build()).getCompleteBackbone()).isEmpty();
    solver.loadState(before);
    formula = f.and(f.or(x.negate(), y), x);
    before = solver.saveState();
    solver.add(formula);
    assertThat(solver.execute(BackboneFunction.builder().variables(variables).build()).getCompleteBackbone()).containsExactly(x, y);
    solver.loadState(before);
    formula = f.and(f.or(x, y), f.or(x.negate(), y));
    before = solver.saveState();
    solver.add(formula);
    assertThat(solver.execute(BackboneFunction.builder().variables(variables).build()).getCompleteBackbone()).containsExactly(y);
    solver.loadState(before);
    formula = f.and(f.and(f.or(x.negate(), y), x.negate()), f.and(z, f.or(x, y)));
    before = solver.saveState();
    solver.add(formula);
    assertThat(solver.execute(BackboneFunction.builder().variables(variables).build()).getCompleteBackbone()).containsExactly(x.negate(), y, z);
    solver.loadState(before);
    formula = f.and(f.or(x, y), f.or(u, v), z);
    solver.add(formula);
    assertThat(solver.execute(BackboneFunction.builder().variables(variables).build()).getCompleteBackbone()).containsExactly(z);
}
Also used : Formula(org.logicng.formulas.Formula) FormulaFactory(org.logicng.formulas.FormulaFactory) Variable(org.logicng.formulas.Variable) SolverState(org.logicng.solvers.SolverState) MiniSat(org.logicng.solvers.MiniSat) Literal(org.logicng.formulas.Literal) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test)

Example 80 with Formula

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

the class BackboneGenerationTest method testCancellationPoints.

@Test
public void testCancellationPoints() throws IOException {
    final FormulaFactory f = new FormulaFactory();
    final List<Formula> formulas = DimacsReader.readCNF("src/test/resources/sat/term1_gr_rcs_w4.shuffled.cnf", f);
    for (int numStarts = 0; numStarts < 10; numStarts++) {
        final SATHandler handler = new BoundedSatHandler(numStarts);
        final Backbone result = BackboneGeneration.compute(formulas, FormulaHelper.variables(formulas), BackboneType.POSITIVE_AND_NEGATIVE, handler);
        assertThat(handler.aborted()).isTrue();
        assertThat(result).isNull();
    }
}
Also used : Formula(org.logicng.formulas.Formula) FormulaFactory(org.logicng.formulas.FormulaFactory) SATHandler(org.logicng.handlers.SATHandler) BoundedSatHandler(org.logicng.handlers.BoundedSatHandler) 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