Search in sources :

Example 11 with LongRunningTag

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

the class BackboneFunctionTest method testRealFormulaIncremental2.

@ParameterizedTest
@MethodSource("solvers")
@LongRunningTag
public void testRealFormulaIncremental2(final MiniSat solver) throws IOException, ParserException {
    solver.reset();
    final Formula formula = FormulaReader.readPseudoBooleanFormula("src/test/resources/formulas/small_formulas.txt", f);
    solver.add(formula);
    final List<String> expectedBackbones = new ArrayList<>();
    final BufferedReader reader = new BufferedReader(new FileReader("src/test/resources/backbones/backbone_small_formulas.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("v2609"));
    backbone = solver.backbone(formula.variables());
    assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(1)));
    assertThat(backbone.isSat()).isTrue();
    solver.add(f.variable("v2416"));
    backbone = solver.backbone(formula.variables());
    assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(2)));
    assertThat(backbone.isSat()).isTrue();
    solver.add(f.variable("v2048"));
    backbone = solver.backbone(formula.variables());
    assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(3)));
    assertThat(backbone.isSat()).isTrue();
    solver.add(f.variable("v39"));
    backbone = solver.backbone(formula.variables());
    assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(4)));
    assertThat(backbone.isSat()).isTrue();
    solver.add(f.variable("v1663"));
    backbone = solver.backbone(formula.variables());
    assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(5)));
    assertThat(backbone.isSat()).isTrue();
    solver.add(f.variable("v2238"));
    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) LongRunningTag(org.logicng.LongRunningTag) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 12 with LongRunningTag

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

the class BackboneFunctionTest method testRealFormulaIncrementalDecremental2.

@ParameterizedTest
@MethodSource("solvers")
@LongRunningTag
public void testRealFormulaIncrementalDecremental2(final MiniSat solver) throws IOException, ParserException {
    if (solver.underlyingSolver() instanceof MiniSat2Solver) {
        solver.reset();
        final Formula formula = FormulaReader.readPseudoBooleanFormula("src/test/resources/formulas/small_formulas.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_small_formulas.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("v2609"));
        backbone = solver.backbone(formula.variables());
        assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(1)));
        assertThat(backbone.isSat()).isTrue();
        solver.loadState(state);
        solver.add(f.parse("v2609 & v2416"));
        backbone = solver.backbone(formula.variables());
        assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(2)));
        assertThat(backbone.isSat()).isTrue();
        solver.loadState(state);
        solver.add(f.parse("v2609 & v2416 & v2048"));
        backbone = solver.backbone(formula.variables());
        assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(3)));
        assertThat(backbone.isSat()).isTrue();
        solver.loadState(state);
        solver.add(f.parse("v2609 & v2416 & v2048 & v39"));
        backbone = solver.backbone(formula.variables());
        assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(4)));
        assertThat(backbone.isSat()).isTrue();
        solver.loadState(state);
        solver.add(f.parse("v2609 & v2416 & v2048 & v39 & v1663"));
        backbone = solver.backbone(formula.variables());
        assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(5)));
        assertThat(backbone.isSat()).isTrue();
        solver.loadState(state);
        solver.add(f.parse("v2609 & v2416 & v2048 & v39 & v1663 & v2238"));
        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) LongRunningTag(org.logicng.LongRunningTag) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 13 with LongRunningTag

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

the class OptimizationFunctionTest method testCancellationPoints.

@LongRunningTag
@ParameterizedTest
@MethodSource("solvers")
public void testCancellationPoints(final SATSolver solver) throws IOException {
    final FormulaFactory f = new FormulaFactory();
    final SortedSet<Variable> selVars = new TreeSet<>();
    final List<Formula> clauses = DimacsReader.readCNF("src/test/resources/sat/c499_gr_rcs_w6.shuffled.cnf", f);
    final List<Formula> formulas = new ArrayList<>();
    for (final Formula clause : clauses) {
        final Variable selVar = f.variable("@SEL_" + selVars.size());
        selVars.add(selVar);
        formulas.add(f.equivalence(selVar, clause));
    }
    for (int numSatHandlerStarts = 1; numSatHandlerStarts < 5; numSatHandlerStarts++) {
        solver.reset();
        solver.add(formulas);
        final OptimizationHandler handler = new BoundedOptimizationHandler(numSatHandlerStarts, -1);
        final OptimizationFunction optimizationFunction = OptimizationFunction.builder().handler(handler).literals(selVars).maximize().build();
        final Assignment result = solver.execute(optimizationFunction);
        assertThat(handler.aborted()).isTrue();
        assertThat(result).isNull();
    }
}
Also used : Assignment(org.logicng.datastructures.Assignment) Formula(org.logicng.formulas.Formula) FormulaFactory(org.logicng.formulas.FormulaFactory) Variable(org.logicng.formulas.Variable) TreeSet(java.util.TreeSet) BoundedOptimizationHandler(org.logicng.handlers.BoundedOptimizationHandler) OptimizationHandler(org.logicng.handlers.OptimizationHandler) BoundedOptimizationHandler(org.logicng.handlers.BoundedOptimizationHandler) TimeoutOptimizationHandler(org.logicng.handlers.TimeoutOptimizationHandler) ArrayList(java.util.ArrayList) CardinalityConstraint(org.logicng.formulas.CardinalityConstraint) LongRunningTag(org.logicng.LongRunningTag) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 14 with LongRunningTag

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

the class OptimizationFunctionTest method testLargerFormulaMinimize.

@ParameterizedTest
@MethodSource("solvers")
@LongRunningTag
public void testLargerFormulaMinimize(final SATSolver solver) throws IOException, ParserException {
    final FormulaFactory f = new FormulaFactory(FormulaFactoryConfig.builder().formulaMergeStrategy(FormulaFactoryConfig.FormulaMergeStrategy.IMPORT).build());
    final Formula formula = FormulaReader.readPseudoBooleanFormula("src/test/resources/formulas/small_formulas.txt", f);
    final Assignment minimumModel = optimize(Collections.singleton(formula), formula.variables(), Collections.emptyList(), false, solver, null);
    testMinimumModel(formula, minimumModel, formula.variables());
}
Also used : Assignment(org.logicng.datastructures.Assignment) Formula(org.logicng.formulas.Formula) FormulaFactory(org.logicng.formulas.FormulaFactory) LongRunningTag(org.logicng.LongRunningTag) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 15 with LongRunningTag

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

the class OptimizationFunctionTest method testLargerFormulaMaximize.

@ParameterizedTest
@MethodSource("solvers")
@LongRunningTag
public void testLargerFormulaMaximize(final SATSolver solver) throws IOException, ParserException {
    final FormulaFactory f = new FormulaFactory(FormulaFactoryConfig.builder().formulaMergeStrategy(FormulaFactoryConfig.FormulaMergeStrategy.IMPORT).build());
    final Formula formula = FormulaReader.readPseudoBooleanFormula("src/test/resources/formulas/small_formulas.txt", f);
    final Assignment maximumModel = optimize(Collections.singleton(formula), formula.variables(), Collections.emptyList(), true, solver, null);
    testMaximumModel(formula, maximumModel, formula.variables());
}
Also used : Assignment(org.logicng.datastructures.Assignment) Formula(org.logicng.formulas.Formula) FormulaFactory(org.logicng.formulas.FormulaFactory) LongRunningTag(org.logicng.LongRunningTag) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

LongRunningTag (org.logicng.LongRunningTag)18 Test (org.junit.jupiter.api.Test)12 Formula (org.logicng.formulas.Formula)10 LogicNGTest (org.logicng.LogicNGTest)8 FormulaFactory (org.logicng.formulas.FormulaFactory)8 Variable (org.logicng.formulas.Variable)7 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)6 MethodSource (org.junit.jupiter.params.provider.MethodSource)6 CardinalityConstraint (org.logicng.formulas.CardinalityConstraint)6 SATSolver (org.logicng.solvers.SATSolver)6 ArrayList (java.util.ArrayList)5 Assignment (org.logicng.datastructures.Assignment)4 BufferedReader (java.io.BufferedReader)3 FileReader (java.io.FileReader)3 List (java.util.List)3 Backbone (org.logicng.backbones.Backbone)3 File (java.io.File)2 BoundedOptimizationHandler (org.logicng.handlers.BoundedOptimizationHandler)2 OptimizationHandler (org.logicng.handlers.OptimizationHandler)2 TimeoutOptimizationHandler (org.logicng.handlers.TimeoutOptimizationHandler)2