use of org.logicng.solvers.sat.MiniSat2Solver in project LogicNG by logic-ng.
the class BackboneFunctionTest method testRealFormulaIncrementalDecremental1.
@ParameterizedTest
@MethodSource("solvers")
public void testRealFormulaIncrementalDecremental1(final MiniSat solver) throws IOException, ParserException {
if (solver.underlyingSolver() instanceof MiniSat2Solver) {
solver.reset();
final Formula formula = FormulaReader.readPseudoBooleanFormula("src/test/resources/formulas/large_formula.txt", f);
solver.add(formula);
final SolverState state = solver.saveState();
final List<String> expectedBackbones = new ArrayList<>();
final BufferedReader reader = new BufferedReader(new FileReader("src/test/resources/backbones/backbone_large_formula.txt"));
while (reader.ready()) {
expectedBackbones.add(reader.readLine());
}
reader.close();
Backbone backbone = solver.backbone(formula.variables());
assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(0)));
solver.add(f.variable("v411"));
backbone = solver.backbone(formula.variables());
assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(1)));
assertThat(backbone.isSat()).isTrue();
solver.loadState(state);
solver.add(f.parse("v411 & v385"));
backbone = solver.backbone(formula.variables());
assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(2)));
assertThat(backbone.isSat()).isTrue();
solver.loadState(state);
solver.add(f.parse("v411 & v385 & v275"));
backbone = solver.backbone(formula.variables());
assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(3)));
assertThat(backbone.isSat()).isTrue();
solver.loadState(state);
solver.add(f.parse("v411 & v385 & v275 & v188"));
backbone = solver.backbone(formula.variables());
assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(4)));
assertThat(backbone.isSat()).isTrue();
solver.loadState(state);
solver.add(f.parse("v411 & v385 & v275 & v188 & v103"));
backbone = solver.backbone(formula.variables());
assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(5)));
assertThat(backbone.isSat()).isTrue();
solver.loadState(state);
solver.add(f.parse("v411 & v385 & v275 & v188 & v103 & v404"));
backbone = solver.backbone(formula.variables());
assertThat(backbone.getCompleteBackbone()).isEmpty();
assertThat(backbone.isSat()).isFalse();
}
}
use of org.logicng.solvers.sat.MiniSat2Solver in project LogicNG by logic-ng.
the class EncodingsTest method testSequentialWeightCounterExceptionalBehavior.
@Test
public void testSequentialWeightCounterExceptionalBehavior() {
assertThatThrownBy(() -> {
final SequentialWeightCounter swc = new SequentialWeightCounter();
swc.encode(new MiniSat2Solver(), new LNGIntVector(), new LNGIntVector(), Integer.MAX_VALUE);
}).isInstanceOf(IllegalArgumentException.class).hasMessage("Overflow in the encoding.");
assertThatThrownBy(() -> {
final SequentialWeightCounter swc = new SequentialWeightCounter();
swc.encode(new MiniSat2Solver(), new LNGIntVector(), new LNGIntVector(), Integer.MAX_VALUE, new LNGIntVector(), 1);
}).isInstanceOf(IllegalArgumentException.class).hasMessage("Overflow in the encoding.");
}
use of org.logicng.solvers.sat.MiniSat2Solver 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"));
}
use of org.logicng.solvers.sat.MiniSat2Solver in project LogicNG by logic-ng.
the class BackboneFunctionTest method testConstants.
@ParameterizedTest
@MethodSource("solvers")
public void testConstants(final MiniSat solver) {
solver.reset();
SolverState state = null;
if (solver.underlyingSolver() instanceof MiniSat2Solver) {
state = solver.saveState();
}
solver.add(f.falsum());
Backbone backbone = solver.backbone(v("a b c"));
assertThat(backbone.isSat()).isFalse();
assertThat(backbone.getCompleteBackbone()).isEmpty();
if (solver.underlyingSolver() instanceof MiniSat2Solver) {
solver.loadState(state);
} else {
solver.reset();
}
solver.add(f.verum());
backbone = solver.backbone(v("a b c"));
assertThat(backbone.isSat()).isTrue();
assertThat(backbone.getCompleteBackbone()).isEmpty();
}
use of org.logicng.solvers.sat.MiniSat2Solver in project LogicNG by logic-ng.
the class BackboneFunctionTest method testRealFormulaIncrementalDecremental2.
@ParameterizedTest
@MethodSource("solvers")
@LongRunningTag
public void testRealFormulaIncrementalDecremental2(final MiniSat solver) throws IOException, ParserException {
if (solver.underlyingSolver() instanceof MiniSat2Solver) {
solver.reset();
final Formula formula = FormulaReader.readPseudoBooleanFormula("src/test/resources/formulas/small_formulas.txt", f);
solver.add(formula);
final SolverState state = solver.saveState();
final List<String> expectedBackbones = new ArrayList<>();
final BufferedReader reader = new BufferedReader(new FileReader("src/test/resources/backbones/backbone_small_formulas.txt"));
while (reader.ready()) {
expectedBackbones.add(reader.readLine());
}
reader.close();
Backbone backbone = solver.backbone(formula.variables());
assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(0)));
solver.add(f.variable("v2609"));
backbone = solver.backbone(formula.variables());
assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(1)));
assertThat(backbone.isSat()).isTrue();
solver.loadState(state);
solver.add(f.parse("v2609 & v2416"));
backbone = solver.backbone(formula.variables());
assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(2)));
assertThat(backbone.isSat()).isTrue();
solver.loadState(state);
solver.add(f.parse("v2609 & v2416 & v2048"));
backbone = solver.backbone(formula.variables());
assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(3)));
assertThat(backbone.isSat()).isTrue();
solver.loadState(state);
solver.add(f.parse("v2609 & v2416 & v2048 & v39"));
backbone = solver.backbone(formula.variables());
assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(4)));
assertThat(backbone.isSat()).isTrue();
solver.loadState(state);
solver.add(f.parse("v2609 & v2416 & v2048 & v39 & v1663"));
backbone = solver.backbone(formula.variables());
assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(5)));
assertThat(backbone.isSat()).isTrue();
solver.loadState(state);
solver.add(f.parse("v2609 & v2416 & v2048 & v39 & v1663 & v2238"));
backbone = solver.backbone(formula.variables());
assertThat(backbone.getCompleteBackbone()).isEmpty();
assertThat(backbone.isSat()).isFalse();
}
}
Aggregations