Search in sources :

Example 31 with PBConstraint

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

the class FormulaRandomizerTest method testExo.

@Test
public void testExo() {
    final FormulaRandomizer random = new FormulaRandomizer(this.f, FormulaRandomizerConfig.builder().seed(4242).maximumCoefficientPbc(10).build());
    for (int i = 0; i < 100; i++) {
        final Formula formula = random.exo();
        assertThat(formula).isInstanceOf(PBConstraint.class);
        final PBConstraint amo = (PBConstraint) formula;
        assertThat(amo.isCC()).isTrue();
        assertThat(amo.rhs()).isEqualTo(1);
        assertThat(amo.comparator()).isEqualTo(CType.EQ);
    }
}
Also used : Formula(org.logicng.formulas.Formula) PBConstraint(org.logicng.formulas.PBConstraint) PBConstraint(org.logicng.formulas.PBConstraint) CardinalityConstraint(org.logicng.formulas.CardinalityConstraint) Test(org.junit.jupiter.api.Test)

Example 32 with PBConstraint

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

the class FormulaDotFileWriter method generateDotString.

/**
 * Generates the dot string for a formula
 * @param formula the formula
 * @param sb      the current string builder
 * @param ids     the current ID mapping
 */
private static void generateDotString(final Formula formula, final StringBuilder sb, final Map<Formula, Integer> ids) {
    switch(formula.type()) {
        case FALSE:
            sb.append(String.format("  false;%n"));
            break;
        case TRUE:
            sb.append(String.format("  true;%n"));
            break;
        case LITERAL:
            break;
        case PBC:
            final int id = ids.size();
            ids.put(formula, id);
            sb.append("  id").append(id).append(" [label=\"").append(formula).append(String.format("\"];%n"));
            for (final Formula operand : ((PBConstraint) formula).operands()) {
                sb.append("  id").append(id).append(" -> id").append(ids.get(operand)).append(String.format(";%n"));
            }
            break;
        case NOT:
            generateNotDotString((Not) formula, sb, ids);
            break;
        case IMPL:
            generateBinaryDotString((BinaryOperator) formula, sb, ids, "⇒", true);
            break;
        case EQUIV:
            generateBinaryDotString((BinaryOperator) formula, sb, ids, "⇔", true);
            break;
        case AND:
            generateNaryDotString((NAryOperator) formula, sb, ids, "∧");
            break;
        case OR:
            generateNaryDotString((NAryOperator) formula, sb, ids, "∨");
            break;
        default:
            throw new IllegalArgumentException("Cannot write the formula type " + formula.type());
    }
}
Also used : Formula(org.logicng.formulas.Formula) PBConstraint(org.logicng.formulas.PBConstraint) PBConstraint(org.logicng.formulas.PBConstraint)

Example 33 with PBConstraint

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

the class NumberOfNodesFunction method apply.

@Override
public Long apply(final Formula formula, final boolean cache) {
    final Object cached = formula.functionCacheEntry(NUMBER_OF_NODES);
    if (cached != null) {
        return (Long) cached;
    }
    long result;
    switch(formula.type()) {
        case FALSE:
        case TRUE:
        case LITERAL:
            result = 1L;
            break;
        case NOT:
            result = apply(((Not) formula).operand(), cache) + 1L;
            break;
        case IMPL:
        case EQUIV:
            final BinaryOperator binary = (BinaryOperator) formula;
            result = apply(binary.left(), cache) + apply(binary.right(), cache) + 1L;
            break;
        case OR:
        case AND:
            final NAryOperator nary = (NAryOperator) formula;
            result = 1L;
            for (final Formula op : nary) {
                result += apply(op, cache);
            }
            break;
        case PBC:
            final PBConstraint pbc = (PBConstraint) formula;
            result = 1L + pbc.operands().length;
            break;
        default:
            throw new IllegalStateException("Unknown formula type " + formula.type());
    }
    if (cache) {
        formula.setFunctionCacheEntry(NUMBER_OF_NODES, result);
    }
    return result;
}
Also used : Formula(org.logicng.formulas.Formula) BinaryOperator(org.logicng.formulas.BinaryOperator) NAryOperator(org.logicng.formulas.NAryOperator) PBConstraint(org.logicng.formulas.PBConstraint)

Example 34 with PBConstraint

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

the class LiteralsFunction method apply.

@Override
public SortedSet<Literal> apply(final Formula formula, final boolean cache) {
    final Object cached = formula.functionCacheEntry(LITERALS);
    if (cached != null) {
        return (SortedSet<Literal>) cached;
    }
    SortedSet<Literal> result = new TreeSet<>();
    switch(formula.type()) {
        case FALSE:
        case TRUE:
            result = new TreeSet<>();
            break;
        case LITERAL:
            final Literal lit = (Literal) formula;
            result.add(lit);
            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.literals(pbc.operands());
            break;
        default:
            throw new IllegalStateException("Unknown formula type " + formula.type());
    }
    result = Collections.unmodifiableSortedSet(result);
    if (cache) {
        formula.setFunctionCacheEntry(LITERALS, result);
    }
    return result;
}
Also used : Formula(org.logicng.formulas.Formula) Not(org.logicng.formulas.Not) TreeSet(java.util.TreeSet) Literal(org.logicng.formulas.Literal) BinaryOperator(org.logicng.formulas.BinaryOperator) SortedSet(java.util.SortedSet) NAryOperator(org.logicng.formulas.NAryOperator) PBConstraint(org.logicng.formulas.PBConstraint)

Example 35 with PBConstraint

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

the class BFSOrdering method bfs.

private LinkedHashSet<Variable> bfs(final Formula formula) {
    final LinkedHashSet<Variable> variables = new LinkedHashSet<>();
    final Queue<Formula> queue = new LinkedList<>();
    queue.add(formula);
    while (!queue.isEmpty()) {
        final Formula current = queue.remove();
        switch(current.type()) {
            case LITERAL:
                final Literal lit = (Literal) current;
                if (lit.phase()) {
                    variables.add(lit.variable());
                } else {
                    queue.add(lit.variable());
                }
                break;
            case NOT:
                queue.add(((Not) current).operand());
                break;
            case IMPL:
            case EQUIV:
                final BinaryOperator op = (BinaryOperator) current;
                queue.add(op.left());
                queue.add(op.right());
                break;
            case AND:
            case OR:
                for (final Formula operand : current) {
                    queue.add(operand);
                }
                break;
            case PBC:
                final PBConstraint pbc = (PBConstraint) current;
                for (final Literal literal : pbc.operands()) {
                    variables.add(literal.variable());
                }
                break;
        }
    }
    return variables;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Formula(org.logicng.formulas.Formula) Variable(org.logicng.formulas.Variable) Literal(org.logicng.formulas.Literal) BinaryOperator(org.logicng.formulas.BinaryOperator) PBConstraint(org.logicng.formulas.PBConstraint) LinkedList(java.util.LinkedList)

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