use of org.logicng.formulas.Formula in project LogicNG by logic-ng.
the class PBEncoderTest method testCC1.
@Test
public void testCC1() {
for (final PBEncoder encoder : this.encoders) {
final int numLits = 100;
final int rhs = 1;
final List<Literal> lits = new ArrayList<>();
final List<Integer> coeffs = new ArrayList<>();
final Variable[] problemLits = new Variable[numLits];
for (int i = 0; i < numLits; i++) {
final Variable var = this.f.variable("v" + i);
lits.add(var);
problemLits[i] = var;
coeffs.add(1);
}
final List<Formula> clauses = encoder.encode((PBConstraint) this.f.pbc(CType.LE, rhs, lits, coeffs));
final SATSolver solver = MiniSat.miniSat(this.f);
solver.add(clauses);
assertSolverSat(solver);
assertThat(solver.enumerateAllModels(problemLits)).hasSize(numLits + 1).allMatch(m -> m.positiveVariables().size() <= rhs);
}
}
use of org.logicng.formulas.Formula in project LogicNG by logic-ng.
the class ModelTest method testCNFWithAuxiliaryVarsRestrictedToOriginal.
@ParameterizedTest
@MethodSource("solvers")
public void testCNFWithAuxiliaryVarsRestrictedToOriginal(final SATSolver solver) throws ParserException {
solver.reset();
final Formula formula = f.parse("(A => B & C) & (~A => C & ~D) & (C => (D & E | ~E & B)) & ~F");
final Formula cnf = formula.transform(new TseitinTransformation(0));
solver.add(cnf);
solver.sat();
final Assignment model = solver.model(formula.variables());
assertThat(formula.evaluate(model)).isTrue();
final List<Assignment> allModels = solver.enumerateAllModels(formula.variables());
assertThat(allModels).hasSize(4);
assertThat(model.formula(f).variables()).isEqualTo(formula.variables());
for (final Assignment assignment : allModels) {
assertThat(formula.evaluate(assignment)).isTrue();
assertThat(assignment.formula(f).variables()).isEqualTo(formula.variables());
}
}
use of org.logicng.formulas.Formula in project LogicNG by logic-ng.
the class ModelTest method testNonCNFOnlyFormulaVars.
@ParameterizedTest
@MethodSource("solvers")
public void testNonCNFOnlyFormulaVars(final SATSolver solver) throws ParserException {
solver.reset();
final Formula formula = f.parse("(A => B & C) & (~A => C & ~D) & (C => (D & E | ~E & B)) & ~F");
solver.add(formula);
solver.sat();
final Assignment model = solver.model(formula.variables());
assertThat(formula.evaluate(model)).isTrue();
assertThat(model.formula(f).variables()).isEqualTo(formula.variables());
final List<Assignment> allModels = solver.enumerateAllModels(formula.variables());
assertThat(allModels).hasSize(4);
for (final Assignment assignment : allModels) {
assertThat(formula.evaluate(assignment)).isTrue();
assertThat(assignment.formula(f).variables()).isEqualTo(formula.variables());
}
}
use of org.logicng.formulas.Formula 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.formulas.Formula in project LogicNG by logic-ng.
the class BackboneFunctionTest method testRealFormulaIncremental1.
@ParameterizedTest
@MethodSource("solvers")
public void testRealFormulaIncremental1(final MiniSat solver) throws IOException, ParserException {
solver.reset();
final Formula formula = FormulaReader.readPseudoBooleanFormula("src/test/resources/formulas/large_formula.txt", f);
solver.add(formula);
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.add(f.variable("v385"));
backbone = solver.backbone(formula.variables());
assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(2)));
assertThat(backbone.isSat()).isTrue();
solver.add(f.variable("v275"));
backbone = solver.backbone(formula.variables());
assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(3)));
assertThat(backbone.isSat()).isTrue();
solver.add(f.variable("v188"));
backbone = solver.backbone(formula.variables());
assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(4)));
assertThat(backbone.isSat()).isTrue();
solver.add(f.variable("v103"));
backbone = solver.backbone(formula.variables());
assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(5)));
assertThat(backbone.isSat()).isTrue();
solver.add(f.variable("v404"));
backbone = solver.backbone(formula.variables());
assertThat(backbone.getCompleteBackbone()).isEmpty();
assertThat(backbone.isSat()).isFalse();
}
Aggregations