use of org.logicng.formulas.PBConstraint in project LogicNG by logic-ng.
the class PBSolvingTest method testPBGreater.
@Test
public void testPBGreater() {
for (final PBEncoder encoder : this.encoders) {
for (final SATSolver solver : this.solvers) {
solver.reset();
final int[] coeffs10 = new int[] { 3, 2, 2, 2, 2, 2, 2, 2, 2, 2 };
solver.add(encoder.encode((PBConstraint) this.f.pbc(CType.GE, 17, this.literals10, coeffs10)));
assertSolverSat(solver);
assertThat(solver.enumerateAllModels(this.literals10)).hasSize(47).allMatch(model -> model.positiveVariables().size() >= 8);
solver.reset();
solver.add(encoder.encode((PBConstraint) this.f.pbc(CType.GT, 16, this.literals10, coeffs10)));
assertSolverSat(solver);
assertThat(solver.enumerateAllModels(this.literals10)).hasSize(47).allMatch(model -> model.positiveVariables().size() >= 8);
solver.reset();
solver.add(encoder.encode((PBConstraint) this.f.pbc(CType.GE, 21, this.literals10, coeffs10)));
assertSolverSat(solver);
assertThat(solver.enumerateAllModels(this.literals10)).hasSize(1);
solver.reset();
solver.add(encoder.encode((PBConstraint) this.f.pbc(CType.GE, 22, this.literals10, coeffs10)));
assertSolverUnsat(solver);
solver.reset();
solver.add(encoder.encode((PBConstraint) this.f.pbc(CType.GT, 42, this.literals10, coeffs10)));
assertSolverUnsat(solver);
}
}
}
use of org.logicng.formulas.PBConstraint in project LogicNG by logic-ng.
the class ConstraintGraphGeneratorTest method testRealExample.
@Test
public void testRealExample() throws IOException, ParserException {
final FormulaFactory f = new FormulaFactory();
f.putConfiguration(CCConfig.builder().amoEncoding(CCConfig.AMO_ENCODER.PURE).build());
final Formula parsed = FormulaReader.readPseudoBooleanFormula("src/test/resources/formulas/formula1.txt", f);
final List<Formula> formulas = new ArrayList<>();
for (final Formula formula : parsed) {
if (formula instanceof PBConstraint) {
formulas.add(formula);
} else {
formulas.add(formula.transform(new CNFFactorization()));
}
}
final Graph<Variable> constraintGraph = ConstraintGraphGenerator.generateFromCnf(formulas);
final Set<Set<Node<Variable>>> ccs = ConnectedComponentsComputation.compute(constraintGraph);
assertThat(ccs).hasSize(4);
}
use of org.logicng.formulas.PBConstraint in project LogicNG by logic-ng.
the class DnnfCompilerTest method testLargeFormula.
@Test
@LongRunningTag
public void testLargeFormula() throws IOException, ParserException {
final FormulaFactory f = new FormulaFactory();
f.putConfiguration(CCConfig.builder().amoEncoding(CCConfig.AMO_ENCODER.PURE).build());
final Formula parsed = FormulaReader.readPseudoBooleanFormula("src/test/resources/formulas/formula1.txt", f);
final DnnfFactory dnnfFactory = new DnnfFactory();
Dnnf dnnf = dnnfFactory.compile(parsed);
final BigInteger dnnfCount = dnnf.execute(DnnfModelCountFunction.get());
final List<Formula> formulas = new ArrayList<>();
final List<Formula> originalFormulas = new ArrayList<>();
for (final Formula formula : parsed) {
originalFormulas.add(formula);
if (formula instanceof PBConstraint) {
formulas.add(formula);
} else {
formulas.add(formula.transform(new CNFFactorization()));
}
}
final Graph<Variable> constraintGraph = ConstraintGraphGenerator.generateFromCnf(formulas);
final Set<Set<Node<Variable>>> ccs = ConnectedComponentsComputation.compute(constraintGraph);
final List<List<Formula>> split = ConnectedComponentsComputation.splitFormulasByComponent(originalFormulas, ccs);
BigInteger multipliedCount = BigInteger.ONE;
for (final List<Formula> component : split) {
dnnf = dnnfFactory.compile(f.and(component));
multipliedCount = multipliedCount.multiply(dnnf.execute(DnnfModelCountFunction.get()));
}
assertThat(dnnfCount).isEqualTo(multipliedCount);
}
use of org.logicng.formulas.PBConstraint in project LogicNG by logic-ng.
the class ModelCounterTest method testCornerCases.
@Test
public void testCornerCases() {
final FormulaFactory f = new FormulaFactory();
final FormulaCornerCases cornerCases = new FormulaCornerCases(f);
for (final Formula formula : cornerCases.cornerCases()) {
if (formula.type() == FType.PBC) {
final PBConstraint pbc = (PBConstraint) formula;
if (!pbc.isAmo() && !pbc.isExo()) {
assertThatThrownBy(() -> ModelCounter.count(Collections.singletonList(formula), formula.variables())).isInstanceOf(UnsupportedOperationException.class);
continue;
}
}
final BigInteger expCount = enumerationBasedModelCount(Collections.singletonList(formula), f);
final BigInteger count = ModelCounter.count(Collections.singleton(formula), formula.variables());
assertThat(count).isEqualTo(expCount);
}
}
use of org.logicng.formulas.PBConstraint in project LogicNG by logic-ng.
the class PureExpansionTransformationTest method testCornerCases.
@Test
public void testCornerCases() {
final FormulaFactory f = new FormulaFactory();
final FormulaCornerCases cornerCases = new FormulaCornerCases(f);
for (final Formula formula : cornerCases.cornerCases()) {
if (formula.type() == FType.PBC) {
final PBConstraint pbc = (PBConstraint) formula;
if (!pbc.isAmo() && !pbc.isExo()) {
assertThatThrownBy(() -> ModelCounter.count(Collections.singletonList(formula), formula.variables())).isInstanceOf(UnsupportedOperationException.class);
continue;
}
}
computeAndVerify(formula, transformation);
}
}
Aggregations