use of org.logicng.solvers.SATSolver in project LogicNG by logic-ng.
the class IncDecTest method testIncDecDeep.
@Test
public void testIncDecDeep() {
for (final SATSolver s : this.solvers) {
s.add(this.f.variable("a"));
final SolverState state1 = s.saveState();
s.add(this.f.variable("b"));
assertSolverSat(s);
final SolverState state2 = s.saveState();
s.add(this.f.literal("a", false));
assertSolverUnsat(s);
s.loadState(state1);
try {
s.loadState(state2);
assert false;
} catch (final IllegalArgumentException e) {
// fine
}
s.add(this.f.literal("b", false));
assertSolverSat(s);
final SolverState state3 = s.saveState();
s.add(this.f.literal("a", false));
assertSolverUnsat(s);
s.loadState(state3);
s.add(this.f.variable("c"));
final SolverState state4 = s.saveState();
final SolverState state5 = s.saveState();
s.loadState(state4);
try {
s.loadState(state5);
assert false;
} catch (final IllegalArgumentException e) {
// fine
}
assertSolverSat(s);
s.loadState(state1);
assertSolverSat(s);
try {
s.loadState(state3);
assert false;
} catch (final IllegalArgumentException e) {
// fine
}
}
}
use of org.logicng.solvers.SATSolver in project LogicNG by logic-ng.
the class SATTest method testModelEnumerationWithHandler02.
@Test
public void testModelEnumerationWithHandler02() {
for (int i = 0; i < this.solvers.length - 1; i++) {
final SATSolver s = this.solvers[i];
final SortedSet<Variable> lits = new TreeSet<>();
final SortedSet<Variable> firstFive = new TreeSet<>();
for (int j = 0; j < 20; j++) {
final Variable lit = this.f.variable("x" + j);
lits.add(lit);
if (j < 5) {
firstFive.add(lit);
}
}
s.add(this.f.cc(CType.GE, 1, lits));
final NumberOfModelsHandler handler = new NumberOfModelsHandler(29);
final List<Assignment> modelsWithHandler = s.execute(ModelEnumerationFunction.builder().additionalVariables(Collections.singletonList(firstFive.first())).handler(handler).build());
assertThat(handler.aborted()).isTrue();
assertThat(modelsWithHandler.size()).isEqualTo(29);
for (final Assignment model : modelsWithHandler) {
for (final Variable lit : lits) {
assertThat(model.positiveVariables().contains(lit) || model.negativeVariables().contains(lit)).isTrue();
}
}
s.reset();
}
}
use of org.logicng.solvers.SATSolver in project LogicNG by logic-ng.
the class SATTest method testPigeonHole7.
@Test
public void testPigeonHole7() {
for (final SATSolver s : this.solvers) {
s.add(this.pg.generate(7));
assertSolverUnsat(s);
assertThat(s.model()).isNull();
s.reset();
}
}
use of org.logicng.solvers.SATSolver 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.solvers.SATSolver in project LogicNG by logic-ng.
the class SATTest method testFormula1.
@Test
public void testFormula1() throws ParserException {
for (final SATSolver s : this.solvers) {
s.add(this.parser.parse("(x => y) & (~x => y) & (y => z) & (z => ~x)"));
assertSolverSat(s);
assertThat(s.model().size()).isEqualTo(3);
assertThat(s.model().evaluateLit(this.f.variable("x"))).isFalse();
assertThat(s.model().evaluateLit(this.f.variable("y"))).isTrue();
assertThat(s.model().evaluateLit(this.f.variable("z"))).isTrue();
s.add(this.f.variable("x"));
assertSolverUnsat(s);
s.reset();
}
}
Aggregations