Search in sources :

Example 6 with Backbone

use of org.logicng.backbones.Backbone in project LogicNG by logic-ng.

the class BackboneFunctionTest method testRealFormulaIncrementalDecremental1.

@ParameterizedTest
@MethodSource("solvers")
public void testRealFormulaIncrementalDecremental1(final MiniSat solver) throws IOException, ParserException {
    if (solver.underlyingSolver() instanceof MiniSat2Solver) {
        solver.reset();
        final Formula formula = FormulaReader.readPseudoBooleanFormula("src/test/resources/formulas/large_formula.txt", f);
        solver.add(formula);
        final SolverState state = solver.saveState();
        final List<String> expectedBackbones = new ArrayList<>();
        final BufferedReader reader = new BufferedReader(new FileReader("src/test/resources/backbones/backbone_large_formula.txt"));
        while (reader.ready()) {
            expectedBackbones.add(reader.readLine());
        }
        reader.close();
        Backbone backbone = solver.backbone(formula.variables());
        assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(0)));
        solver.add(f.variable("v411"));
        backbone = solver.backbone(formula.variables());
        assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(1)));
        assertThat(backbone.isSat()).isTrue();
        solver.loadState(state);
        solver.add(f.parse("v411 & v385"));
        backbone = solver.backbone(formula.variables());
        assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(2)));
        assertThat(backbone.isSat()).isTrue();
        solver.loadState(state);
        solver.add(f.parse("v411 & v385 & v275"));
        backbone = solver.backbone(formula.variables());
        assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(3)));
        assertThat(backbone.isSat()).isTrue();
        solver.loadState(state);
        solver.add(f.parse("v411 & v385 & v275 & v188"));
        backbone = solver.backbone(formula.variables());
        assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(4)));
        assertThat(backbone.isSat()).isTrue();
        solver.loadState(state);
        solver.add(f.parse("v411 & v385 & v275 & v188 & v103"));
        backbone = solver.backbone(formula.variables());
        assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(5)));
        assertThat(backbone.isSat()).isTrue();
        solver.loadState(state);
        solver.add(f.parse("v411 & v385 & v275 & v188 & v103 & v404"));
        backbone = solver.backbone(formula.variables());
        assertThat(backbone.getCompleteBackbone()).isEmpty();
        assertThat(backbone.isSat()).isFalse();
    }
}
Also used : Backbone(org.logicng.backbones.Backbone) Formula(org.logicng.formulas.Formula) MiniSat2Solver(org.logicng.solvers.sat.MiniSat2Solver) SolverState(org.logicng.solvers.SolverState) ArrayList(java.util.ArrayList) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 7 with Backbone

use of org.logicng.backbones.Backbone in project LogicNG by logic-ng.

the class BackboneFunctionTest method testRealFormulaIncremental1.

@ParameterizedTest
@MethodSource("solvers")
public void testRealFormulaIncremental1(final MiniSat solver) throws IOException, ParserException {
    solver.reset();
    final Formula formula = FormulaReader.readPseudoBooleanFormula("src/test/resources/formulas/large_formula.txt", f);
    solver.add(formula);
    final List<String> expectedBackbones = new ArrayList<>();
    final BufferedReader reader = new BufferedReader(new FileReader("src/test/resources/backbones/backbone_large_formula.txt"));
    while (reader.ready()) {
        expectedBackbones.add(reader.readLine());
    }
    reader.close();
    Backbone backbone = solver.backbone(formula.variables());
    assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(0)));
    solver.add(f.variable("v411"));
    backbone = solver.backbone(formula.variables());
    assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(1)));
    assertThat(backbone.isSat()).isTrue();
    solver.add(f.variable("v385"));
    backbone = solver.backbone(formula.variables());
    assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(2)));
    assertThat(backbone.isSat()).isTrue();
    solver.add(f.variable("v275"));
    backbone = solver.backbone(formula.variables());
    assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(3)));
    assertThat(backbone.isSat()).isTrue();
    solver.add(f.variable("v188"));
    backbone = solver.backbone(formula.variables());
    assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(4)));
    assertThat(backbone.isSat()).isTrue();
    solver.add(f.variable("v103"));
    backbone = solver.backbone(formula.variables());
    assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(5)));
    assertThat(backbone.isSat()).isTrue();
    solver.add(f.variable("v404"));
    backbone = solver.backbone(formula.variables());
    assertThat(backbone.getCompleteBackbone()).isEmpty();
    assertThat(backbone.isSat()).isFalse();
}
Also used : Backbone(org.logicng.backbones.Backbone) Formula(org.logicng.formulas.Formula) ArrayList(java.util.ArrayList) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 8 with Backbone

use of org.logicng.backbones.Backbone in project LogicNG by logic-ng.

the class CnfMethodComparisonTest method compareFullBackbonesOnLargeFormulas.

@ParameterizedTest
@MethodSource("cnfConfigurations")
@LongRunningTag
public void compareFullBackbonesOnLargeFormulas(final CNFConfig cnfConfig, final MiniSatConfig.CNFMethod cnfMethod) throws IOException, ParserException {
    final String baseDir = "src/test/resources/formulas/";
    final List<String> fileNames = Arrays.asList("formula1.txt", "formula2.txt", "formula3.txt", "large_formula.txt", "small_formulas.txt");
    for (final String fileName : fileNames) {
        final String filePath = baseDir + fileName;
        final Backbone backboneReference = computeBackbone(filePath, CNFConfig.builder().build(), MiniSatConfig.builder().build().getCnfMethod());
        final Backbone backbone = computeBackbone(filePath, cnfConfig, cnfMethod);
        assertThat(backboneReference).isEqualTo(backbone);
    }
}
Also used : Backbone(org.logicng.backbones.Backbone) LongRunningTag(org.logicng.LongRunningTag) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 9 with Backbone

use of org.logicng.backbones.Backbone in project LogicNG by logic-ng.

the class CnfMethodComparisonTest method computeBackbonePerVariable.

private Map<Variable, Backbone> computeBackbonePerVariable(final String fileName, final CNFConfig cnfConfig, final MiniSatConfig.CNFMethod cnfMethod) throws IOException, ParserException {
    final long start = System.currentTimeMillis();
    final FormulaFactory f = new FormulaFactory();
    f.putConfiguration(cnfConfig);
    final Formula formula = FormulaReader.readPseudoBooleanFormula(fileName, f);
    final SATSolver solver = MiniSat.miniSat(f, MiniSatConfig.builder().cnfMethod(cnfMethod).build());
    solver.add(formula);
    final SolverState solverState = solver.saveState();
    final Map<Variable, Backbone> result = new TreeMap<>();
    int counter = 1000;
    for (final Variable variable : formula.variables()) {
        if (counter-- > 0) {
            solver.add(variable);
            if (solver.sat() == Tristate.TRUE) {
                final Backbone backbone = solver.backbone(formula.variables());
                result.put(variable, backbone);
            }
            solver.loadState(solverState);
        }
    }
    final long stop = System.currentTimeMillis();
    System.out.println(fileName + " " + cnfConfig.algorithm + " " + cnfConfig.fallbackAlgorithmForAdvancedEncoding + " " + cnfMethod + ": " + (stop - start) + " ms.");
    return result;
}
Also used : SATSolver(org.logicng.solvers.SATSolver) Backbone(org.logicng.backbones.Backbone) Formula(org.logicng.formulas.Formula) FormulaFactory(org.logicng.formulas.FormulaFactory) SolverState(org.logicng.solvers.SolverState) Variable(org.logicng.formulas.Variable) TreeMap(java.util.TreeMap)

Example 10 with Backbone

use of org.logicng.backbones.Backbone in project LogicNG by logic-ng.

the class BackboneFunctionTest method testSimpleFormulasWithBuilderUsage.

@ParameterizedTest
@MethodSource("solvers")
public void testSimpleFormulasWithBuilderUsage(final MiniSat solver) throws ParserException {
    solver.reset();
    solver.add(f.parse("(a => c | d) & (b => d | ~e) & (a | b)"));
    Backbone backbone = solver.execute(BackboneFunction.builder().variables(f.variable("a"), f.variable("b"), f.variable("c"), f.variable("d"), f.variable("e"), f.variable("f")).build());
    assertThat(backbone.isSat()).isTrue();
    assertThat(backbone.getCompleteBackbone()).isEmpty();
    solver.add(f.parse("a => b"));
    solver.add(f.parse("b => a"));
    solver.add(f.parse("~d"));
    backbone = solver.backbone(v("a b c d e f g h"));
    assertThat(backbone.isSat()).isTrue();
    assertThat(backbone.getCompleteBackbone()).containsExactly(f.variable("a"), f.variable("b"), f.variable("c"), f.literal("d", false), f.literal("e", false));
}
Also used : Backbone(org.logicng.backbones.Backbone) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

Backbone (org.logicng.backbones.Backbone)14 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)9 MethodSource (org.junit.jupiter.params.provider.MethodSource)9 Formula (org.logicng.formulas.Formula)7 SolverState (org.logicng.solvers.SolverState)6 BufferedReader (java.io.BufferedReader)4 FileReader (java.io.FileReader)4 ArrayList (java.util.ArrayList)4 MiniSat2Solver (org.logicng.solvers.sat.MiniSat2Solver)4 LongRunningTag (org.logicng.LongRunningTag)3 Assignment (org.logicng.datastructures.Assignment)2 FormulaFactory (org.logicng.formulas.FormulaFactory)2 SATSolver (org.logicng.solvers.SATSolver)2 SortedSet (java.util.SortedSet)1 TreeMap (java.util.TreeMap)1 Literal (org.logicng.formulas.Literal)1 Variable (org.logicng.formulas.Variable)1 PrimeResult (org.logicng.primecomputation.PrimeResult)1