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));
}
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();
}
}
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();
}
}
}
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();
}
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);
}
Aggregations