use of org.logicng.formulas.Literal in project LogicNG by logic-ng.
the class SATTest method testSelectionOrderSimple02.
@Test
public void testSelectionOrderSimple02() {
for (final SATSolver solver : this.solvers) {
final List<Variable> literals = new ArrayList<>();
for (int i = 0; i < 5; i++) {
final Variable lit = this.f.variable("x" + i);
literals.add(lit);
}
solver.add(this.f.cc(CType.EQ, 2, literals));
for (int i = 0; i < 10; ++i) {
assertThat(solver.satWithSelectionOrder(literals)).isEqualTo(Tristate.TRUE);
final Assignment assignment = solver.model();
testLocalMinimum(solver, assignment, literals);
testHighestLexicographicalAssignment(solver, assignment, literals);
solver.add(assignment.blockingClause(this.f, literals));
}
solver.reset();
solver.add(this.f.cc(CType.EQ, 2, literals));
final List<Literal> selectionOrder02 = Arrays.asList(this.f.literal("x4", true), this.f.literal("x0", false), this.f.literal("x1", true), this.f.literal("x2", true), this.f.literal("x3", true));
for (int i = 0; i < 10; ++i) {
assertThat(solver.satWithSelectionOrder(selectionOrder02)).isEqualTo(Tristate.TRUE);
final Assignment assignment = solver.model();
testLocalMinimum(solver, assignment, selectionOrder02);
testHighestLexicographicalAssignment(solver, assignment, selectionOrder02);
solver.add(assignment.blockingClause(this.f, selectionOrder02));
}
solver.reset();
}
}
use of org.logicng.formulas.Literal in project LogicNG by logic-ng.
the class SATTest method testUPZeroLiteralsDimacsFiles.
@Test
public void testUPZeroLiteralsDimacsFiles() throws IOException {
final File testFolder = new File("src/test/resources/sat");
final File[] files = testFolder.listFiles();
assert files != null;
for (final SATSolver solver : this.solvers) {
for (final File file : files) {
final String fileName = file.getName();
if (fileName.endsWith(".cnf")) {
readCNF(solver, file);
final boolean res = solver.sat() == Tristate.TRUE;
if (res) {
final SortedSet<Literal> upZeroLiterals = solver.execute(UpZeroLiteralsFunction.get());
final List<Literal> negations = new ArrayList<>(upZeroLiterals.size());
for (final Literal lit : upZeroLiterals) {
negations.add(lit.negate());
}
solver.add(this.f.or(negations));
// Test if CNF implies identified unit propagated literals on level zero, i.e., each literal is a backbone literal
assertThat(solver.sat()).isEqualTo(Tristate.FALSE);
}
}
}
solver.reset();
}
}
use of org.logicng.formulas.Literal in project LogicNG by logic-ng.
the class SATTest method testSelectionOrderSimple01.
@Test
public void testSelectionOrderSimple01() throws ParserException {
for (final SATSolver solver : this.solvers) {
final Formula formula = this.parser.parse("~(x <=> y)");
solver.add(formula);
List<Literal> selectionOrder = Arrays.asList(this.X, this.Y);
assertThat(solver.satWithSelectionOrder(selectionOrder)).isEqualTo(Tristate.TRUE);
Assignment assignment = solver.model();
assertThat(assignment.literals()).containsExactlyInAnyOrder(this.X, this.NY);
testLocalMinimum(solver, assignment, selectionOrder);
testHighestLexicographicalAssignment(solver, assignment, selectionOrder);
solver.setSolverToUndef();
selectionOrder = Arrays.asList(this.Y, this.X);
assertThat(solver.satWithSelectionOrder(selectionOrder)).isEqualTo(Tristate.TRUE);
assignment = solver.model();
assertThat(assignment.literals()).containsExactlyInAnyOrder(this.Y, this.NX);
testLocalMinimum(solver, assignment, selectionOrder);
testHighestLexicographicalAssignment(solver, assignment, selectionOrder);
solver.setSolverToUndef();
selectionOrder = Collections.singletonList(this.NX);
assertThat(solver.sat(selectionOrder)).isEqualTo(Tristate.TRUE);
assignment = solver.model();
assertThat(assignment.literals()).containsExactlyInAnyOrder(this.Y, this.NX);
testLocalMinimum(solver, assignment, selectionOrder);
testHighestLexicographicalAssignment(solver, assignment, selectionOrder);
solver.setSolverToUndef();
selectionOrder = Arrays.asList(this.NY, this.NX);
assertThat(solver.satWithSelectionOrder(selectionOrder)).isEqualTo(Tristate.TRUE);
assignment = solver.model();
assertThat(assignment.literals()).containsExactlyInAnyOrder(this.X, this.NY);
testLocalMinimum(solver, assignment, selectionOrder);
testHighestLexicographicalAssignment(solver, assignment, selectionOrder);
solver.reset();
}
}
use of org.logicng.formulas.Literal in project LogicNG by logic-ng.
the class SATTest method testUPZeroLiterals.
@Test
public void testUPZeroLiterals() throws ParserException {
// Note: The complete unit propagated set of literals on level 0 depends on each solver's added learned clauses during the solving process
final Map<Formula, SortedSet<Literal>> expectedSubsets = new HashMap<>();
expectedSubsets.put(this.f.verum(), new TreeSet<>());
expectedSubsets.put(this.parser.parse("a"), new TreeSet<>(Collections.singletonList(this.f.literal("a", true))));
expectedSubsets.put(this.parser.parse("a | b"), new TreeSet<>());
expectedSubsets.put(this.parser.parse("a & b"), new TreeSet<>(Arrays.asList(this.f.literal("a", true), this.f.literal("b", true))));
expectedSubsets.put(this.parser.parse("a & ~b"), new TreeSet<>(Arrays.asList(this.f.literal("a", true), this.f.literal("b", false))));
expectedSubsets.put(this.parser.parse("(a | c) & ~b"), new TreeSet<>(Collections.singletonList(this.f.literal("b", false))));
expectedSubsets.put(this.parser.parse("(b | c) & ~b & (~c | d)"), new TreeSet<>(Arrays.asList(this.f.literal("b", false), this.f.literal("c", true), this.f.literal("d", true))));
for (final SATSolver solver : this.solvers) {
for (final Formula formula : expectedSubsets.keySet()) {
solver.reset();
solver.add(formula);
final boolean res = solver.sat() == Tristate.TRUE;
assertThat(res).isTrue();
final SortedSet<Literal> upLiterals = solver.execute(UpZeroLiteralsFunction.get());
assertThat(upLiterals).containsAll(expectedSubsets.get(formula));
}
}
}
use of org.logicng.formulas.Literal in project LogicNG by logic-ng.
the class SATTest method testDimacsFilesWithSelectionOrder.
@Test
@LongRunningTag
public void testDimacsFilesWithSelectionOrder() throws IOException {
final Map<String, Boolean> expectedResults = new HashMap<>();
final BufferedReader reader = new BufferedReader(new FileReader("src/test/resources/sat/results.txt"));
while (reader.ready()) {
final String[] tokens = reader.readLine().split(";");
expectedResults.put(tokens[0], Boolean.valueOf(tokens[1]));
}
final File testFolder = new File("src/test/resources/sat");
final File[] files = testFolder.listFiles();
assert files != null;
for (final SATSolver solver : this.solvers) {
for (final File file : files) {
final String fileName = file.getName();
if (fileName.endsWith(".cnf")) {
readCNF(solver, file);
final List<Literal> selectionOrder = new ArrayList<>();
for (final Variable var : FormulaHelper.variables(solver.execute(FormulaOnSolverFunction.get()))) {
if (selectionOrder.size() < 10) {
selectionOrder.add(var.negate());
}
}
final boolean res = solver.satWithSelectionOrder(selectionOrder) == Tristate.TRUE;
assertThat(res).isEqualTo(expectedResults.get(fileName));
if (expectedResults.get(fileName)) {
final Assignment assignment = solver.model();
testLocalMinimum(solver, assignment, selectionOrder);
testHighestLexicographicalAssignment(solver, assignment, selectionOrder);
}
}
}
solver.reset();
}
}
Aggregations