use of org.logicng.solvers.SolverState 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.SolverState 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();
}
}
use of org.logicng.solvers.SolverState in project LogicNG by logic-ng.
the class PlaistedGreenbaumTransformationSolverTest method random.
@Test
@RandomTag
public void random() {
for (int i = 0; i < 1000; i++) {
final FormulaFactory f = new FormulaFactory();
final SATSolver solver = MiniSat.miniSat(f, MiniSatConfig.builder().cnfMethod(MiniSatConfig.CNFMethod.FULL_PG_ON_SOLVER).auxiliaryVariablesInModels(false).build());
final FormulaRandomizer randomizer = new FormulaRandomizer(f, FormulaRandomizerConfig.builder().numVars(10).weightPbc(1).seed(i * 42).build());
final Formula randomFormula01 = randomSATFormula(randomizer, 4, f);
final Formula randomFormula02 = randomSATFormula(randomizer, 4, f);
solver.reset();
solver.add(randomFormula01);
if (solver.sat() == Tristate.TRUE) {
final List<Assignment> models = solver.enumerateAllModels();
final Formula dnf = f.or(models.stream().map(model -> f.and(model.literals())).collect(Collectors.toList()));
assertThat(f.equivalence(randomFormula01, dnf).holds(new TautologyPredicate(f))).isTrue();
}
final SolverState state = solver.saveState();
solver.add(randomFormula02);
if (solver.sat() == Tristate.TRUE) {
final List<Assignment> models = solver.enumerateAllModels();
final Formula dnf = f.or(models.stream().map(model -> f.and(model.literals())).collect(Collectors.toList()));
assertThat(f.equivalence(f.and(randomFormula01, randomFormula02), dnf).holds(new TautologyPredicate(f))).isTrue();
}
solver.loadState(state);
if (solver.sat() == Tristate.TRUE) {
final List<Assignment> models = solver.enumerateAllModels();
final Formula dnf = f.or(models.stream().map(model -> f.and(model.literals())).collect(Collectors.toList()));
assertThat(f.equivalence(randomFormula01, dnf).holds(new TautologyPredicate(f))).isTrue();
}
solver.add(randomFormula02);
if (solver.sat() == Tristate.TRUE) {
final List<Assignment> models = solver.enumerateAllModels();
final Formula dnf = f.or(models.stream().map(model -> f.and(model.literals())).collect(Collectors.toList()));
assertThat(f.equivalence(f.and(randomFormula01, randomFormula02), dnf).holds(new TautologyPredicate(f))).isTrue();
}
}
}
use of org.logicng.solvers.SolverState in project LogicNG by logic-ng.
the class IncDecTest method testIncDec.
@Test
public void testIncDec() {
for (final MiniSat s : this.solvers) {
s.add(this.f.variable("a"));
final SolverState state1 = s.saveState();
if (s.underlyingSolver() instanceof MiniCard) {
assertThat(state1.toString()).isEqualTo("SolverState{id=0, state=[1, 1, 0, 0, 1]}");
} else {
assertThat(state1.toString()).isEqualTo("SolverState{id=0, state=[1, 1, 0, 0, 1, 0, 0]}");
}
assertSolverSat(s);
s.add(this.pg.generate(5));
assertSolverUnsat(s);
s.loadState(state1);
assertSolverSat(s);
s.add(this.f.literal("a", false));
assertSolverUnsat(s);
s.loadState(state1);
assertSolverSat(s);
s.add(this.pg.generate(5));
final SolverState state2 = s.saveState();
if (s.underlyingSolver() instanceof MiniCard) {
assertThat(state2.toString()).isEqualTo("SolverState{id=1, state=[1, 31, 81, 0, 1]}");
} else {
assertThat(state2.toString()).isEqualTo("SolverState{id=1, state=[1, 31, 81, 0, 1, 0, 0]}");
}
s.add(this.pg.generate(4));
assertSolverUnsat(s);
s.loadState(state2);
assertSolverUnsat(s);
s.loadState(state1);
assertSolverSat(s);
}
}
use of org.logicng.solvers.SolverState in project LogicNG by logic-ng.
the class SATTest method testKnownVariables.
@Test
public void testKnownVariables() throws ParserException {
final PropositionalParser parser = new PropositionalParser(this.f);
final Formula phi = parser.parse("x1 & x2 & x3 & (x4 | ~x5)");
final SATSolver minisat = MiniSat.miniSat(this.f);
final SATSolver minicard = MiniSat.miniCard(this.f);
minisat.add(phi);
minicard.add(phi);
final SortedSet<Variable> expected = new TreeSet<>(Arrays.asList(this.f.variable("x1"), this.f.variable("x2"), this.f.variable("x3"), this.f.variable("x4"), this.f.variable("x5")));
assertThat(minisat.knownVariables()).isEqualTo(expected);
assertThat(minicard.knownVariables()).isEqualTo(expected);
final SolverState state = minisat.saveState();
final SolverState stateCard = minicard.saveState();
minisat.add(this.f.variable("x6"));
minicard.add(this.f.variable("x6"));
final SortedSet<Variable> expected2 = new TreeSet<>(Arrays.asList(this.f.variable("x1"), this.f.variable("x2"), this.f.variable("x3"), this.f.variable("x4"), this.f.variable("x5"), this.f.variable("x6")));
assertThat(minisat.knownVariables()).isEqualTo(expected2);
assertThat(minicard.knownVariables()).isEqualTo(expected2);
// load state for minisat
minisat.loadState(state);
minicard.loadState(stateCard);
assertThat(minisat.knownVariables()).isEqualTo(expected);
assertThat(minicard.knownVariables()).isEqualTo(expected);
}
Aggregations