use of org.logicng.solvers.SolverState in project LogicNG by logic-ng.
the class CnfMethodComparisonTest method computeBackbonePerVariable.
private Map<Variable, Backbone> computeBackbonePerVariable(final String fileName, final CNFConfig cnfConfig, final MiniSatConfig.CNFMethod cnfMethod) throws IOException, ParserException {
final long start = System.currentTimeMillis();
final FormulaFactory f = new FormulaFactory();
f.putConfiguration(cnfConfig);
final Formula formula = FormulaReader.readPseudoBooleanFormula(fileName, f);
final SATSolver solver = MiniSat.miniSat(f, MiniSatConfig.builder().cnfMethod(cnfMethod).build());
solver.add(formula);
final SolverState solverState = solver.saveState();
final Map<Variable, Backbone> result = new TreeMap<>();
int counter = 1000;
for (final Variable variable : formula.variables()) {
if (counter-- > 0) {
solver.add(variable);
if (solver.sat() == Tristate.TRUE) {
final Backbone backbone = solver.backbone(formula.variables());
result.put(variable, backbone);
}
solver.loadState(solverState);
}
}
final long stop = System.currentTimeMillis();
System.out.println(fileName + " " + cnfConfig.algorithm + " " + cnfConfig.fallbackAlgorithmForAdvancedEncoding + " " + cnfMethod + ": " + (stop - start) + " ms.");
return result;
}
use of org.logicng.solvers.SolverState in project LogicNG by logic-ng.
the class DRUPTest method testPropositionIncDec.
@Test
public void testPropositionIncDec() throws ParserException {
final SATSolver solver = this.solvers[0];
final StandardProposition p1 = new StandardProposition("P1", this.f.parse("((a & b) => c) & ((a & b) => d)"));
final StandardProposition p2 = new StandardProposition("P2", this.f.parse("(c & d) <=> ~e"));
final StandardProposition p3 = new StandardProposition("P3", this.f.parse("~e => f | g"));
final StandardProposition p4 = new StandardProposition("P4", this.f.parse("(f => ~a) & (g => ~b) & p & q"));
final StandardProposition p5 = new StandardProposition("P5", this.f.parse("a => b"));
final StandardProposition p6 = new StandardProposition("P6", this.f.parse("a"));
final StandardProposition p7 = new StandardProposition("P7", this.f.parse("g | h"));
final StandardProposition p8 = new StandardProposition("P8", this.f.parse("(x => ~y | z) & (z | w)"));
final StandardProposition p9 = new StandardProposition("P9", this.f.parse("a & b"));
final StandardProposition p10 = new StandardProposition("P10", this.f.parse("(p => q) & p"));
final StandardProposition p11 = new StandardProposition("P11", this.f.parse("a & ~q"));
solver.addPropositions(p1, p2, p3, p4);
final SolverState state1 = solver.saveState();
solver.addPropositions(p5, p6);
final SolverState state2 = solver.saveState();
solver.addPropositions(p7, p8);
assertThat(solver.sat()).isEqualTo(FALSE);
UNSATCore<Proposition> unsatCore = solver.unsatCore();
assertThat(unsatCore.propositions()).containsExactlyInAnyOrder(p1, p2, p3, p4, p5, p6);
solver.loadState(state2);
assertThat(solver.sat()).isEqualTo(FALSE);
unsatCore = solver.unsatCore();
assertThat(unsatCore.propositions()).containsExactlyInAnyOrder(p1, p2, p3, p4, p5, p6);
solver.loadState(state1);
solver.add(p9);
assertThat(solver.sat()).isEqualTo(FALSE);
unsatCore = solver.unsatCore();
assertThat(unsatCore.propositions()).containsExactlyInAnyOrder(p1, p2, p3, p4, p9);
solver.loadState(state1);
solver.add(p5);
solver.add(p6);
assertThat(solver.sat()).isEqualTo(FALSE);
unsatCore = solver.unsatCore();
assertThat(unsatCore.propositions()).containsExactlyInAnyOrder(p1, p2, p3, p4, p5, p6);
solver.loadState(state1);
solver.add(p10);
solver.add(p11);
assertThat(solver.sat()).isEqualTo(FALSE);
unsatCore = solver.unsatCore();
assertThat(unsatCore.propositions()).containsExactlyInAnyOrder(p4, p11);
}
use of org.logicng.solvers.SolverState in project LogicNG by logic-ng.
the class CCIncrementalFormulaTest method testSimpleIncrementalALK.
@Test
public void testSimpleIncrementalALK() {
for (final CCEncoder encoder : this.encoders) {
final CCEncoder initialEncoder = new CCEncoder(this.f);
final int numLits = 10;
final Variable[] vars = new Variable[numLits];
for (int i = 0; i < numLits; i++) {
vars[i] = this.f.variable("v" + i);
}
final Pair<List<Formula>, CCIncrementalData> cc = encoder.encodeIncremental((CardinalityConstraint) this.f.cc(CType.GE, 2, vars));
final CCIncrementalData incData = cc.second();
final SATSolver solver = MiniSat.miniSat(this.f);
// >= 4
solver.add(initialEncoder.encode((CardinalityConstraint) this.f.cc(CType.GE, 4, vars)));
// <= 7
solver.add(initialEncoder.encode((CardinalityConstraint) this.f.cc(CType.LE, 7, vars)));
solver.add(cc.first());
// >=2
assertSolverSat(solver);
// >= 3
solver.add(incData.newLowerBound(3));
assertSolverSat(solver);
// >= 4
solver.add(incData.newLowerBound(4));
assertSolverSat(solver);
// >= 5
solver.add(incData.newLowerBound(5));
assertSolverSat(solver);
// >= 6
solver.add(incData.newLowerBound(6));
assertSolverSat(solver);
// >= 7
solver.add(incData.newLowerBound(7));
assertSolverSat(solver);
final SolverState state = solver.saveState();
// >= 8
solver.add(incData.newLowerBound(8));
assertSolverUnsat(solver);
solver.loadState(state);
assertSolverSat(solver);
// <= 9
solver.add(incData.newLowerBound(9));
assertSolverUnsat(solver);
}
}
use of org.logicng.solvers.SolverState in project LogicNG by logic-ng.
the class BackboneGenerationTest method testBackboneType.
@Test
public void testBackboneType() {
final FormulaFactory f = new FormulaFactory();
final MiniSat solver = MiniSat.miniSat(f);
final Literal x = f.literal("x", true);
final Literal y = f.literal("y", true);
final Literal z = f.literal("z", true);
Formula formula = f.not(x);
SolverState before = solver.saveState();
solver.add(formula);
Backbone backbone = solver.execute(BackboneFunction.builder().variables(formula.variables()).type(BackboneType.POSITIVE_AND_NEGATIVE).build());
Backbone backbonePositive = solver.execute(BackboneFunction.builder().variables(formula.variables()).type(BackboneType.ONLY_POSITIVE).build());
Backbone backboneNegative = solver.execute(BackboneFunction.builder().variables(formula.variables()).type(BackboneType.ONLY_NEGATIVE).build());
assertThat(backbone.getCompleteBackbone()).containsExactly(x.negate());
assertThat(backbonePositive.getCompleteBackbone()).isEmpty();
assertThat(backboneNegative.getCompleteBackbone()).containsExactly(x.negate());
SortedSet<Literal> combinedPosNegBackbone = new TreeSet<>(backbonePositive.getCompleteBackbone());
combinedPosNegBackbone.addAll(backboneNegative.getCompleteBackbone());
assertThat(backbone.getCompleteBackbone()).isEqualTo(combinedPosNegBackbone);
solver.loadState(before);
formula = f.and(f.or(x, y.negate()), x.negate());
before = solver.saveState();
solver.add(formula);
backbone = solver.execute(BackboneFunction.builder().variables(formula.variables()).type(BackboneType.POSITIVE_AND_NEGATIVE).build());
backbonePositive = solver.execute(BackboneFunction.builder().variables(formula.variables()).type(BackboneType.ONLY_POSITIVE).build());
backboneNegative = solver.execute(BackboneFunction.builder().variables(formula.variables()).type(BackboneType.ONLY_NEGATIVE).build());
assertThat(backbone.getCompleteBackbone()).containsExactly(x.negate(), y.negate());
assertThat(backbonePositive.getCompleteBackbone()).isEmpty();
assertThat(backboneNegative.getCompleteBackbone()).containsExactly(x.negate(), y.negate());
combinedPosNegBackbone = new TreeSet<>(backbonePositive.getCompleteBackbone());
combinedPosNegBackbone.addAll(backboneNegative.getCompleteBackbone());
assertThat(backbone.getCompleteBackbone()).isEqualTo(combinedPosNegBackbone);
solver.loadState(before);
formula = f.and(f.or(x, y), f.or(x.negate(), y));
before = solver.saveState();
solver.add(formula);
backbone = solver.execute(BackboneFunction.builder().variables(formula.variables()).type(BackboneType.POSITIVE_AND_NEGATIVE).build());
backbonePositive = solver.execute(BackboneFunction.builder().variables(formula.variables()).type(BackboneType.ONLY_POSITIVE).build());
backboneNegative = solver.execute(BackboneFunction.builder().variables(formula.variables()).type(BackboneType.ONLY_NEGATIVE).build());
assertThat(backbone.getCompleteBackbone()).containsExactly(y);
assertThat(backbonePositive.getCompleteBackbone()).containsExactly(y);
assertThat(backboneNegative.getCompleteBackbone()).isEmpty();
combinedPosNegBackbone = new TreeSet<>(backbonePositive.getCompleteBackbone());
combinedPosNegBackbone.addAll(backboneNegative.getCompleteBackbone());
assertThat(backbone.getCompleteBackbone()).isEqualTo(combinedPosNegBackbone);
solver.loadState(before);
formula = f.and(f.and(f.or(x.negate(), y), x.negate()), f.and(z, f.or(x, y)));
before = solver.saveState();
solver.add(formula);
backbone = solver.execute(BackboneFunction.builder().variables(formula.variables()).type(BackboneType.POSITIVE_AND_NEGATIVE).build());
backbonePositive = solver.execute(BackboneFunction.builder().variables(formula.variables()).type(BackboneType.ONLY_POSITIVE).build());
backboneNegative = solver.execute(BackboneFunction.builder().variables(formula.variables()).type(BackboneType.ONLY_NEGATIVE).build());
assertThat(backbone.getCompleteBackbone()).containsExactly(x.negate(), y, z);
assertThat(backbone.getOptionalVariables()).containsExactly();
assertThat(backbonePositive.getCompleteBackbone()).containsExactly(y, z);
assertThat(backboneNegative.getCompleteBackbone()).containsExactly(x.negate());
combinedPosNegBackbone = new TreeSet<>(backbonePositive.getCompleteBackbone());
combinedPosNegBackbone.addAll(backboneNegative.getCompleteBackbone());
assertThat(backbone.getCompleteBackbone()).isEqualTo(combinedPosNegBackbone);
solver.loadState(before);
}
use of org.logicng.solvers.SolverState in project LogicNG by logic-ng.
the class BackboneFunctionTest method testSimpleBackbones.
@ParameterizedTest
@MethodSource("solvers")
public void testSimpleBackbones(final MiniSat solver) throws ParserException {
solver.reset();
SolverState state = null;
if (solver.underlyingSolver() instanceof MiniSat2Solver) {
state = solver.saveState();
}
solver.add(f.parse("a & b & ~c"));
Backbone backbone = solver.backbone(v("a b c"));
assertThat(backbone.isSat()).isTrue();
assertThat(backbone.getCompleteBackbone()).containsExactly(f.variable("a"), f.variable("b"), f.literal("c", false));
if (solver.underlyingSolver() instanceof MiniSat2Solver) {
solver.loadState(state);
} else {
solver.reset();
}
solver.add(f.parse("~a & ~b & c"));
backbone = solver.backbone(v("a c"));
assertThat(backbone.isSat()).isTrue();
assertThat(backbone.getCompleteBackbone()).containsExactly(f.literal("a", false), f.variable("c"));
}
Aggregations