Search in sources :

Example 61 with Formula

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

the class TimeoutBDDHandlerTest method testTimeoutHandlerFixedEnd.

@Test
public void testTimeoutHandlerFixedEnd() {
    final Formula formula = pg.generate(10);
    final VariableOrderingProvider provider = VariableOrdering.BFS.provider();
    final BDDKernel kernel = new BDDKernel(this.f, provider.getOrder(formula), 100, 100);
    final TimeoutBDDHandler handler = new TimeoutBDDHandler(System.currentTimeMillis() + 100L, TimeoutHandler.TimerType.FIXED_END);
    final BDD result = BDDFactory.build(formula, kernel, handler);
    assertThat(handler.aborted).isTrue();
    assertThat(result).isEqualTo(new BDD(BDDKernel.BDD_ABORT, kernel));
}
Also used : Formula(org.logicng.formulas.Formula) VariableOrderingProvider(org.logicng.knowledgecompilation.bdds.orderings.VariableOrderingProvider) BDDKernel(org.logicng.knowledgecompilation.bdds.jbuddy.BDDKernel) BDD(org.logicng.knowledgecompilation.bdds.BDD) Test(org.junit.jupiter.api.Test)

Example 62 with Formula

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

the class TimeoutMaxSATHandlerTest method testThatSatHandlerIsHandledProperly.

@Test
public void testThatSatHandlerIsHandledProperly() throws IOException {
    final List<Formula> formulas = DimacsReader.readCNF("src/test/resources/sat/unsat/pret60_40.cnf", this.f);
    for (final MaxSATSolver solver : this.solvers) {
        final int weight = solver.isWeighted() ? 2 : 1;
        formulas.forEach(c -> solver.addSoftFormula(c, weight));
        final TimeoutSATHandler satHandler = Mockito.mock(TimeoutSATHandler.class);
        final TimeoutMaxSATHandler handler = Mockito.mock(TimeoutMaxSATHandler.class);
        when(handler.satHandler()).thenReturn(satHandler);
        lenient().when(handler.foundLowerBound(anyInt(), any())).thenReturn(true);
        lenient().when(handler.foundUpperBound(anyInt(), any())).thenReturn(true);
        final AtomicInteger count = new AtomicInteger(0);
        when(satHandler.detectedConflict()).thenReturn(true);
        when(satHandler.aborted()).then(invocationOnMock -> count.addAndGet(1) > 1);
        final MaxSAT.MaxSATResult solve = solver.solve(handler);
        assertThat(solve).isEqualTo(MaxSAT.MaxSATResult.UNDEF);
        verify(handler, times(1)).started();
        verify(handler, atLeast(1)).satHandler();
        verify(handler, times(1)).finishedSolving();
        verify(satHandler, times(2)).aborted();
    }
}
Also used : Formula(org.logicng.formulas.Formula) MaxSAT(org.logicng.solvers.maxsat.algorithms.MaxSAT) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MaxSATSolver(org.logicng.solvers.MaxSATSolver) Test(org.junit.jupiter.api.Test)

Example 63 with Formula

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

the class DRUPTest method testUnsatCoresFromDimacs.

@Test
public void testUnsatCoresFromDimacs() throws IOException {
    final List<List<Formula>> cnfs = new ArrayList<>(3);
    cnfs.add(DimacsReader.readCNF("src/test/resources/drup/simple_input.cnf", this.f));
    cnfs.add(DimacsReader.readCNF("src/test/resources/drup/pg4_input.cnf", this.f));
    cnfs.add(DimacsReader.readCNF("src/test/resources/drup/avg_input.cnf", this.f, "var"));
    for (final SATSolver solver : this.solvers) {
        for (final List<Formula> cnf : cnfs) {
            solver.add(cnf);
            assertSolverUnsat(solver);
            final UNSATCore<Proposition> unsatCore = solver.unsatCore();
            verifyCore(unsatCore, cnf);
            solver.reset();
        }
    }
}
Also used : SATSolver(org.logicng.solvers.SATSolver) Formula(org.logicng.formulas.Formula) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) StandardProposition(org.logicng.propositions.StandardProposition) Proposition(org.logicng.propositions.Proposition) ExtendedProposition(org.logicng.propositions.ExtendedProposition) LogicNGTest(org.logicng.LogicNGTest) Test(org.junit.jupiter.api.Test)

Example 64 with Formula

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

the class DRUPTest method verifyCore.

/**
 * Checks that each formula of the core is part of the original problem and that the core is really unsat.
 * @param originalCore the original core
 * @param cnf          the original problem
 */
private void verifyCore(final UNSATCore<Proposition> originalCore, final List<Formula> cnf) {
    final List<Formula> core = new ArrayList<>(originalCore.propositions().size());
    for (final Proposition prop : originalCore.propositions()) {
        core.add(prop.formula());
    }
    final SoftAssertions softly = new SoftAssertions();
    softly.assertThat(cnf).as("Core contains only original clauses").containsAll(core);
    final MiniSat solver = MiniSat.glucose(this.f, MiniSatConfig.builder().proofGeneration(true).incremental(false).build(), GlucoseConfig.builder().build());
    solver.add(core);
    softly.assertThat(solver.sat()).as("Core is unsatisfiable").isEqualTo(Tristate.FALSE);
    softly.assertAll();
}
Also used : Formula(org.logicng.formulas.Formula) MiniSat(org.logicng.solvers.MiniSat) SoftAssertions(org.assertj.core.api.SoftAssertions) ArrayList(java.util.ArrayList) StandardProposition(org.logicng.propositions.StandardProposition) Proposition(org.logicng.propositions.Proposition) ExtendedProposition(org.logicng.propositions.ExtendedProposition)

Example 65 with Formula

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

the class DRUPTest method testUnsatCoresAimTestset.

@Test
public void testUnsatCoresAimTestset() throws IOException {
    final File testFolder = new File("src/test/resources/sat/unsat");
    final File[] files = testFolder.listFiles();
    assert files != null;
    int count = 0;
    for (final SATSolver solver : this.solvers) {
        for (final File file : files) {
            final String fileName = file.getName();
            if (fileName.endsWith(".cnf")) {
                final List<Formula> cnf = DimacsReader.readCNF(file, this.f);
                solver.add(cnf);
                assertSolverUnsat(solver);
                final UNSATCore<Proposition> unsatCore = solver.unsatCore();
                verifyCore(unsatCore, cnf);
                solver.reset();
                count++;
            }
        }
        solver.reset();
    }
    assertThat(count).isEqualTo(36 * this.solvers.length);
}
Also used : SATSolver(org.logicng.solvers.SATSolver) Formula(org.logicng.formulas.Formula) StandardProposition(org.logicng.propositions.StandardProposition) Proposition(org.logicng.propositions.Proposition) ExtendedProposition(org.logicng.propositions.ExtendedProposition) File(java.io.File) LogicNGTest(org.logicng.LogicNGTest) 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