Search in sources :

Example 1 with PBConstraint

use of org.logicng.formulas.PBConstraint in project LogicNG by logic-ng.

the class FormulaStringRepresentation method toInnerString.

/**
 * Returns the string representation of the given formula.
 * <p>
 * This method is used for recursive calls in order to format the sub-formulas.
 * @param formula the formula
 * @return the string representation of the formula
 */
protected String toInnerString(final Formula formula) {
    switch(formula.type()) {
        case FALSE:
            return this.falsum();
        case TRUE:
            return this.verum();
        case LITERAL:
            final Literal lit = (Literal) formula;
            return lit.phase() ? lit.name() : this.negation() + lit.name();
        case NOT:
            final Not not = (Not) formula;
            return this.negation() + this.bracket(not.operand());
        case IMPL:
        case EQUIV:
            final BinaryOperator binary = (BinaryOperator) formula;
            String op = formula.type() == FType.IMPL ? this.implication() : this.equivalence();
            return this.binaryOperator(binary, op);
        case AND:
        case OR:
            final NAryOperator nary = (NAryOperator) formula;
            op = formula.type() == FType.AND ? this.and() : this.or();
            return this.naryOperator(nary, String.format("%s", op));
        case PBC:
            final PBConstraint pbc = (PBConstraint) formula;
            return String.format("%s%s%d", this.pbLhs(pbc.operands(), pbc.coefficients()), this.pbComparator(pbc.comparator()), pbc.rhs());
        default:
            throw new IllegalArgumentException("Cannot print the unknown formula type " + formula.type());
    }
}
Also used : Not(org.logicng.formulas.Not) Literal(org.logicng.formulas.Literal) BinaryOperator(org.logicng.formulas.BinaryOperator) NAryOperator(org.logicng.formulas.NAryOperator) PBConstraint(org.logicng.formulas.PBConstraint)

Example 2 with PBConstraint

use of org.logicng.formulas.PBConstraint in project LogicNG by logic-ng.

the class SortedStringRepresentation method toInnerString.

/**
 * Returns the sorted string representation of the given formula.
 * @param formula the formula
 * @return the sorted string representation of the formula with regard to the variable ordering
 */
@Override
public String toInnerString(final Formula formula) {
    switch(formula.type()) {
        case FALSE:
            return falsum();
        case TRUE:
            return verum();
        case LITERAL:
            final Literal lit = (Literal) formula;
            return lit.phase() ? lit.name() : negation() + lit.name();
        case NOT:
            final Not not = (Not) formula;
            return negation() + bracket(not.operand());
        case IMPL:
            return binaryOperator((BinaryOperator) formula, implication());
        case EQUIV:
            return sortedEquivalence((Equivalence) formula);
        case AND:
        case OR:
            final NAryOperator nary = (NAryOperator) formula;
            final String op = formula.type() == FType.AND ? and() : or();
            return naryOperator(nary, String.format("%s", op));
        case PBC:
            final PBConstraint pbc = (PBConstraint) formula;
            return String.format("%s%s%d", pbLhs(pbc.operands(), pbc.coefficients()), pbComparator(pbc.comparator()), pbc.rhs());
        default:
            throw new IllegalArgumentException("Cannot print the unknown formula type " + formula.type());
    }
}
Also used : Not(org.logicng.formulas.Not) Literal(org.logicng.formulas.Literal) NAryOperator(org.logicng.formulas.NAryOperator) PBConstraint(org.logicng.formulas.PBConstraint)

Example 3 with PBConstraint

use of org.logicng.formulas.PBConstraint in project LogicNG by logic-ng.

the class VariablesFunction method apply.

@Override
public SortedSet<Variable> apply(final Formula formula, final boolean cache) {
    final Object cached = formula.functionCacheEntry(VARIABLES);
    if (cached != null) {
        return (SortedSet<Variable>) cached;
    }
    SortedSet<Variable> result = new TreeSet<>();
    switch(formula.type()) {
        case FALSE:
        case TRUE:
            result = new TreeSet<>();
            break;
        case LITERAL:
            final Literal lit = (Literal) formula;
            result.add(lit.variable());
            break;
        case NOT:
            final Not not = (Not) formula;
            result = apply(not.operand(), cache);
            break;
        case IMPL:
        case EQUIV:
            final BinaryOperator binary = (BinaryOperator) formula;
            result.addAll(apply(binary.left(), cache));
            result.addAll(apply(binary.right(), cache));
            break;
        case OR:
        case AND:
            final NAryOperator nary = (NAryOperator) formula;
            for (final Formula op : nary) {
                result.addAll(apply(op, cache));
            }
            break;
        case PBC:
            final PBConstraint pbc = (PBConstraint) formula;
            result = FormulaHelper.variables(pbc.literals());
            break;
        default:
            throw new IllegalStateException("Unknown formula type " + formula.type());
    }
    result = Collections.unmodifiableSortedSet(result);
    if (cache) {
        formula.setFunctionCacheEntry(VARIABLES, result);
    }
    return result;
}
Also used : Variable(org.logicng.formulas.Variable) SortedSet(java.util.SortedSet) Formula(org.logicng.formulas.Formula) Not(org.logicng.formulas.Not) TreeSet(java.util.TreeSet) Literal(org.logicng.formulas.Literal) BinaryOperator(org.logicng.formulas.BinaryOperator) NAryOperator(org.logicng.formulas.NAryOperator) PBConstraint(org.logicng.formulas.PBConstraint)

Example 4 with PBConstraint

use of org.logicng.formulas.PBConstraint in project LogicNG by logic-ng.

the class DFSOrdering method dfs.

private void dfs(final Formula formula, final LinkedHashSet<Variable> variables) {
    switch(formula.type()) {
        case LITERAL:
            variables.add(((Literal) formula).variable());
            break;
        case NOT:
            dfs(((Not) formula).operand(), variables);
            break;
        case IMPL:
        case EQUIV:
            final BinaryOperator op = (BinaryOperator) formula;
            dfs(op.left(), variables);
            dfs(op.right(), variables);
            break;
        case AND:
        case OR:
            for (final Formula o : formula) {
                dfs(o, variables);
            }
            break;
        case PBC:
            final PBConstraint pbc = (PBConstraint) formula;
            for (final Literal lit : pbc.operands()) {
                variables.add(lit.variable());
            }
            break;
    }
}
Also used : Formula(org.logicng.formulas.Formula) Literal(org.logicng.formulas.Literal) BinaryOperator(org.logicng.formulas.BinaryOperator) PBConstraint(org.logicng.formulas.PBConstraint)

Example 5 with PBConstraint

use of org.logicng.formulas.PBConstraint in project LogicNG by logic-ng.

the class NNFTransformation method applyRec.

private Formula applyRec(final Formula formula, final boolean polarity) {
    final FormulaFactory f = formula.factory();
    Formula nnf;
    if (polarity) {
        nnf = formula.transformationCacheEntry(NNF);
        if (nnf != null) {
            return nnf;
        }
    }
    final FType type = formula.type();
    switch(type) {
        case TRUE:
        case FALSE:
        case LITERAL:
            nnf = polarity ? formula : formula.negate();
            break;
        case NOT:
            nnf = applyRec(((Not) formula).operand(), !polarity);
            break;
        case OR:
        case AND:
            nnf = applyRec(formula.iterator(), formula.type(), polarity, f);
            break;
        case EQUIV:
            final Equivalence equiv = (Equivalence) formula;
            if (polarity) {
                nnf = f.and(f.or(applyRec(equiv.left(), false), applyRec(equiv.right(), true)), f.or(applyRec(equiv.left(), true), applyRec(equiv.right(), false)));
            } else {
                nnf = f.and(f.or(applyRec(equiv.left(), false), applyRec(equiv.right(), false)), f.or(applyRec(equiv.left(), true), applyRec(equiv.right(), true)));
            }
            break;
        case IMPL:
            final Implication impl = (Implication) formula;
            if (polarity) {
                nnf = f.or(applyRec(impl.left(), false), applyRec(impl.right(), true));
            } else {
                nnf = f.and(applyRec(impl.left(), true), applyRec(impl.right(), false));
            }
            break;
        case PBC:
            final PBConstraint pbc = (PBConstraint) formula;
            if (polarity) {
                final List<Formula> encoding = pbc.getEncoding();
                nnf = applyRec(encoding.iterator(), FType.AND, true, f);
            } else {
                nnf = applyRec(pbc.negate(), true);
            }
            break;
        default:
            throw new IllegalStateException("Unknown formula type = " + type);
    }
    if (polarity) {
        formula.setTransformationCacheEntry(NNF, nnf);
    }
    return nnf;
}
Also used : Formula(org.logicng.formulas.Formula) FormulaFactory(org.logicng.formulas.FormulaFactory) FType(org.logicng.formulas.FType) Not(org.logicng.formulas.Not) Equivalence(org.logicng.formulas.Equivalence) Implication(org.logicng.formulas.Implication) PBConstraint(org.logicng.formulas.PBConstraint)

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