use of org.logicng.formulas.PBConstraint in project LogicNG by logic-ng.
the class PureExpansionTransformation method apply.
@Override
public Formula apply(final Formula formula, final boolean cache) {
final FormulaFactory f = formula.factory();
switch(formula.type()) {
case FALSE:
case TRUE:
case LITERAL:
return formula;
case NOT:
final Not not = (Not) formula;
return f.not(apply(not.operand(), cache));
case OR:
case AND:
final NAryOperator nary = (NAryOperator) formula;
final List<Formula> newOps = new ArrayList<>(nary.numberOfOperands());
for (final Formula op : nary) {
newOps.add(apply(op, cache));
}
return f.naryOperator(formula.type(), newOps);
case IMPL:
case EQUIV:
final BinaryOperator binary = (BinaryOperator) formula;
final Formula newLeft = apply(binary.left(), cache);
final Formula newRight = apply(binary.right(), cache);
return f.binaryOperator(formula.type(), newLeft, newRight);
case PBC:
final PBConstraint pbc = (PBConstraint) formula;
if (pbc.isAmo() || pbc.isExo()) {
final EncodingResult encodingResult = EncodingResult.resultForFormula(f);
final Variable[] vars = literalsAsVariables(pbc.operands());
this.amoEncoder.build(encodingResult, vars);
final List<Formula> encoding = encodingResult.result();
if (pbc.isExo()) {
encoding.add(f.or(vars));
}
return f.and(encoding);
} else {
throw new UnsupportedOperationException("Pure encoding for a PBC of type other than AMO or EXO is currently not supported.");
}
default:
throw new IllegalStateException("Unknown formula type: " + formula.type());
}
}
use of org.logicng.formulas.PBConstraint in project LogicNG by logic-ng.
the class MiniSat method add.
@Override
public void add(final Formula formula, final Proposition proposition) {
this.result = UNDEF;
if (formula.type() == FType.PBC) {
final PBConstraint constraint = (PBConstraint) formula;
if (constraint.isCC()) {
if (this.style == SolverStyle.MINICARD) {
if (constraint.comparator() == CType.LE) {
((MiniCard) this.solver).addAtMost(generateClauseVector(Arrays.asList(constraint.operands())), constraint.rhs());
} else if (constraint.comparator() == CType.LT && constraint.rhs() > 3) {
((MiniCard) this.solver).addAtMost(generateClauseVector(Arrays.asList(constraint.operands())), constraint.rhs() - 1);
} else if (constraint.comparator() == CType.EQ && constraint.rhs() == 1) {
((MiniCard) this.solver).addAtMost(generateClauseVector(Arrays.asList(constraint.operands())), constraint.rhs());
this.solver.addClause(generateClauseVector(Arrays.asList(constraint.operands())), proposition);
} else {
addFormulaAsCNF(constraint, proposition);
}
} else {
final EncodingResult result = EncodingResult.resultForMiniSat(this.f, this, proposition);
this.ccEncoder.encode((CardinalityConstraint) constraint, result);
}
} else {
addFormulaAsCNF(constraint, proposition);
}
} else {
addFormulaAsCNF(formula, proposition);
}
}
use of org.logicng.formulas.PBConstraint in project LogicNG by logic-ng.
the class ConnectedComponentsComputerTest method testFormulaSplit.
@Test
public void testFormulaSplit() 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<>();
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);
assertThat(split).hasSize(4);
assertThat(split.get(0)).hasSize(1899);
assertThat(split.get(1)).hasSize(3);
assertThat(split.get(2)).hasSize(3);
assertThat(split.get(3)).hasSize(3);
}
use of org.logicng.formulas.PBConstraint in project LogicNG by logic-ng.
the class PBEncoderTest method testCC0.
@Test
public void testCC0() {
for (final PBEncoder encoder : this.encoders) {
final int numLits = 100;
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, 0, lits, coeffs));
final SATSolver solver = MiniSat.miniSat(this.f);
solver.add(clauses);
assertSolverSat(solver);
assertThat(solver.enumerateAllModels(problemLits)).hasSize(1).allMatch(m -> m.positiveVariables().isEmpty());
}
}
use of org.logicng.formulas.PBConstraint in project LogicNG by logic-ng.
the class PBEncoderTest method testCCNormalized.
@Test
public void testCCNormalized() throws ParserException {
final List<Literal> lits = new ArrayList<>();
lits.add(this.f.literal("m", true));
lits.add(this.f.literal("n", true));
final List<Integer> coeffs2 = new ArrayList<>();
coeffs2.add(2);
coeffs2.add(2);
final PBConstraint normCC = (PBConstraint) this.f.pbc(CType.LE, 2, lits, coeffs2);
assertThat(this.encoders[0].encode(normCC)).containsExactly(this.f.parse("~m | ~n"));
}
Aggregations