use of org.logicng.solvers.SolverState in project LogicNG by logic-ng.
the class CCIncrementalSolverTest method testSimpleIncrementalAMK.
@Test
public void testSimpleIncrementalAMK() {
for (final CCConfig config : this.configs) {
this.f.putConfiguration(this.configs[2]);
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 SATSolver solver = MiniSat.miniSat(this.f);
// >= 4
solver.add(this.f.cc(CType.GE, 4, vars));
// <= 7
solver.add(this.f.cc(CType.LE, 7, vars));
this.f.putConfiguration(config);
final CCIncrementalData incData = solver.addIncrementalCC((CardinalityConstraint) this.f.cc(CType.LE, 9, vars));
// <= 9
assertSolverSat(solver);
// <= 8
incData.newUpperBoundForSolver(8);
assertSolverSat(solver);
// <= 7
incData.newUpperBoundForSolver(7);
assertSolverSat(solver);
// <= 6
incData.newUpperBoundForSolver(6);
assertSolverSat(solver);
// <= 5
incData.newUpperBoundForSolver(5);
assertSolverSat(solver);
// <= 4
incData.newUpperBoundForSolver(4);
assertSolverSat(solver);
final SolverState state = solver.saveState();
// <= 3
incData.newUpperBoundForSolver(3);
assertSolverUnsat(solver);
solver.loadState(state);
assertSolverSat(solver);
// <= 2
incData.newUpperBoundForSolver(2);
assertSolverUnsat(solver);
}
}
use of org.logicng.solvers.SolverState in project LogicNG by logic-ng.
the class CCIncrementalSolverTest method testSimpleIncrementalALK.
@Test
public void testSimpleIncrementalALK() {
for (final CCConfig config : this.configs) {
this.f.putConfiguration(this.configs[2]);
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 SATSolver solver = this.solvers[2];
solver.reset();
// >= 4
solver.add(this.f.cc(CType.GE, 4, vars));
// <= 7
solver.add(this.f.cc(CType.LE, 7, vars));
this.f.putConfiguration(config);
final CCIncrementalData incData = solver.addIncrementalCC((CardinalityConstraint) this.f.cc(CType.GE, 2, vars));
// >=2
assertSolverSat(solver);
// >= 3
incData.newLowerBoundForSolver(3);
assertSolverSat(solver);
// >= 4
incData.newLowerBoundForSolver(4);
assertSolverSat(solver);
// >= 5
incData.newLowerBoundForSolver(5);
assertSolverSat(solver);
// >= 6
incData.newLowerBoundForSolver(6);
assertSolverSat(solver);
// >= 7
incData.newLowerBoundForSolver(7);
assertSolverSat(solver);
final SolverState state = solver.saveState();
// >= 8
incData.newLowerBoundForSolver(8);
assertSolverUnsat(solver);
solver.loadState(state);
assertSolverSat(solver);
// <= 9
incData.newLowerBoundForSolver(9);
assertSolverUnsat(solver);
}
}
use of org.logicng.solvers.SolverState in project LogicNG by logic-ng.
the class BackboneGenerationTest method testSimpleBackbones.
@Test
public void testSimpleBackbones() {
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);
final Literal u = f.literal("u", true);
final Literal v = f.literal("v", true);
final Collection<Variable> variables = new ArrayList<>(Arrays.asList(f.variable("x"), f.variable("y"), f.variable("z"), f.variable("u"), f.variable("v")));
Formula formula = f.verum();
SolverState before = solver.saveState();
solver.add(formula);
assertThat(solver.execute(BackboneFunction.builder().variables(Collections.emptyList()).build()).getCompleteBackbone()).isEmpty();
solver.loadState(before);
formula = x;
before = solver.saveState();
solver.add(formula);
assertThat(solver.execute(BackboneFunction.builder().variables(variables).build()).getCompleteBackbone()).containsExactly(x);
solver.loadState(before);
formula = f.and(x, y);
before = solver.saveState();
solver.add(formula);
assertThat(solver.execute(BackboneFunction.builder().variables(variables).build()).getCompleteBackbone()).containsExactly(x, y);
solver.loadState(before);
formula = f.or(x, y);
before = solver.saveState();
solver.add(formula);
assertThat(solver.execute(BackboneFunction.builder().variables(variables).build()).getCompleteBackbone()).isEmpty();
solver.loadState(before);
formula = x.negate();
before = solver.saveState();
solver.add(formula);
assertThat(solver.execute(BackboneFunction.builder().variables(variables).build()).getCompleteBackbone()).containsExactly(x.negate());
solver.loadState(before);
formula = f.or(f.and(x, y, z), f.and(x, y, u), f.and(x, u, z));
before = solver.saveState();
solver.add(formula);
assertThat(solver.execute(BackboneFunction.builder().variables(variables).build()).getCompleteBackbone()).containsExactly(x);
solver.loadState(before);
formula = f.and(f.or(x, y, z), f.or(x, y, u), f.or(x, u, z));
before = solver.saveState();
solver.add(formula);
assertThat(solver.execute(BackboneFunction.builder().variables(variables).build()).getCompleteBackbone()).isEmpty();
solver.loadState(before);
formula = f.and(f.or(x.negate(), y), x);
before = solver.saveState();
solver.add(formula);
assertThat(solver.execute(BackboneFunction.builder().variables(variables).build()).getCompleteBackbone()).containsExactly(x, y);
solver.loadState(before);
formula = f.and(f.or(x, y), f.or(x.negate(), y));
before = solver.saveState();
solver.add(formula);
assertThat(solver.execute(BackboneFunction.builder().variables(variables).build()).getCompleteBackbone()).containsExactly(y);
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);
assertThat(solver.execute(BackboneFunction.builder().variables(variables).build()).getCompleteBackbone()).containsExactly(x.negate(), y, z);
solver.loadState(before);
formula = f.and(f.or(x, y), f.or(u, v), z);
solver.add(formula);
assertThat(solver.execute(BackboneFunction.builder().variables(variables).build()).getCompleteBackbone()).containsExactly(z);
}
use of org.logicng.solvers.SolverState 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.SolverState 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();
}
}
Aggregations