Search in sources :

Example 6 with PBConstraint

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());
    }
}
Also used : Variable(org.logicng.formulas.Variable) ArrayList(java.util.ArrayList) Formula(org.logicng.formulas.Formula) FormulaFactory(org.logicng.formulas.FormulaFactory) Not(org.logicng.formulas.Not) EncodingResult(org.logicng.datastructures.EncodingResult) BinaryOperator(org.logicng.formulas.BinaryOperator) NAryOperator(org.logicng.formulas.NAryOperator) PBConstraint(org.logicng.formulas.PBConstraint)

Example 7 with PBConstraint

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);
    }
}
Also used : MiniCard(org.logicng.solvers.sat.MiniCard) EncodingResult(org.logicng.datastructures.EncodingResult) PBConstraint(org.logicng.formulas.PBConstraint)

Example 8 with PBConstraint

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);
}
Also used : Formula(org.logicng.formulas.Formula) FormulaFactory(org.logicng.formulas.FormulaFactory) Variable(org.logicng.formulas.Variable) Set(java.util.Set) HashSet(java.util.HashSet) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) PBConstraint(org.logicng.formulas.PBConstraint) CNFFactorization(org.logicng.transformations.cnf.CNFFactorization) GraphTest(org.logicng.graphs.datastructures.GraphTest) Test(org.junit.jupiter.api.Test)

Example 9 with PBConstraint

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());
    }
}
Also used : SATSolver(org.logicng.solvers.SATSolver) Formula(org.logicng.formulas.Formula) Variable(org.logicng.formulas.Variable) Literal(org.logicng.formulas.Literal) ArrayList(java.util.ArrayList) PBConstraint(org.logicng.formulas.PBConstraint) Test(org.junit.jupiter.api.Test) LogicNGTest(org.logicng.LogicNGTest)

Example 10 with PBConstraint

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"));
}
Also used : Literal(org.logicng.formulas.Literal) ArrayList(java.util.ArrayList) PBConstraint(org.logicng.formulas.PBConstraint) Test(org.junit.jupiter.api.Test) LogicNGTest(org.logicng.LogicNGTest)

Aggregations

PBConstraint (org.logicng.formulas.PBConstraint)35 Formula (org.logicng.formulas.Formula)22 Test (org.junit.jupiter.api.Test)21 Literal (org.logicng.formulas.Literal)15 Variable (org.logicng.formulas.Variable)11 ArrayList (java.util.ArrayList)9 LogicNGTest (org.logicng.LogicNGTest)9 FormulaFactory (org.logicng.formulas.FormulaFactory)9 BinaryOperator (org.logicng.formulas.BinaryOperator)8 Not (org.logicng.formulas.Not)8 SATSolver (org.logicng.solvers.SATSolver)8 NAryOperator (org.logicng.formulas.NAryOperator)6 CardinalityConstraint (org.logicng.formulas.CardinalityConstraint)5 List (java.util.List)3 Set (java.util.Set)3 CNFFactorization (org.logicng.transformations.cnf.CNFFactorization)3 BigInteger (java.math.BigInteger)2 SortedSet (java.util.SortedSet)2 TreeSet (java.util.TreeSet)2 EncodingResult (org.logicng.datastructures.EncodingResult)2