Search in sources :

Example 36 with Formula

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

the class HypergraphGenerator method fromCNF.

/**
 * Generates a hypergraph from a CNF given as a list of clauses.  Each variable is represented by a node in the
 * hypergraph, each clause is represented by a hyperedge between all variables of the clause.
 * @param cnf the list of clauses of the CNF for the hypergraph
 * @return the hypergraph for the CNF formula
 */
public static Hypergraph<Variable> fromCNF(final List<Formula> cnf) {
    final Hypergraph<Variable> hypergraph = new Hypergraph<>();
    final Map<Variable, HypergraphNode<Variable>> nodes = new HashMap<>();
    for (final Formula clause : cnf) {
        switch(clause.type()) {
            case PBC:
            case EQUIV:
            case IMPL:
            case NOT:
            case AND:
                throw new IllegalStateException("Unexpected element in clause: " + clause);
            case LITERAL:
            case OR:
                addClause(clause, hypergraph, nodes);
                break;
        }
    }
    return hypergraph;
}
Also used : Hypergraph(org.logicng.graphs.datastructures.Hypergraph) Formula(org.logicng.formulas.Formula) Variable(org.logicng.formulas.Variable) HashMap(java.util.HashMap) HypergraphNode(org.logicng.graphs.datastructures.HypergraphNode)

Example 37 with Formula

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

the class AIGTransformation method transformOr.

private Formula transformOr(final Or or) {
    Formula aig = or.transformationCacheEntry(AIG);
    if (aig == null) {
        final LinkedHashSet<Formula> nops = new LinkedHashSet<>(or.numberOfOperands());
        for (final Formula op : or) {
            nops.add(f.not(apply(op, cache)));
        }
        aig = f.not(f.and(nops));
        if (cache) {
            or.setTransformationCacheEntry(AIG, aig);
            aig.setPredicateCacheEntry(PredicateCacheEntry.IS_AIG, true);
        }
    }
    return aig;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Formula(org.logicng.formulas.Formula)

Example 38 with Formula

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

the class AIGTransformation method transformAnd.

private Formula transformAnd(final And and) {
    Formula aig = and.transformationCacheEntry(AIG);
    if (aig == null) {
        final LinkedHashSet<Formula> nops = new LinkedHashSet<>(and.numberOfOperands());
        for (final Formula op : and) {
            nops.add(apply(op, cache));
        }
        aig = f.and(nops);
        if (cache) {
            and.setTransformationCacheEntry(AIG, aig);
            aig.setPredicateCacheEntry(PredicateCacheEntry.IS_AIG, true);
        }
    }
    return aig;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Formula(org.logicng.formulas.Formula)

Example 39 with Formula

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

the class AIGTransformation method transformImplication.

private Formula transformImplication(final Implication impl) {
    Formula aig = impl.transformationCacheEntry(AIG);
    if (aig == null) {
        aig = f.not(f.and(apply(impl.left(), cache), f.not(apply(impl.right(), cache))));
        if (cache) {
            impl.setTransformationCacheEntry(AIG, aig);
            aig.setPredicateCacheEntry(PredicateCacheEntry.IS_AIG, true);
        }
    }
    return aig;
}
Also used : Formula(org.logicng.formulas.Formula)

Example 40 with Formula

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

the class AIGTransformation method transformNot.

private Formula transformNot(final Not not) {
    Formula aig = not.transformationCacheEntry(AIG);
    if (aig == null) {
        aig = f.not(apply(not.operand(), cache));
        if (cache) {
            not.setTransformationCacheEntry(AIG, aig);
            aig.setPredicateCacheEntry(PredicateCacheEntry.IS_AIG, true);
        }
    }
    return aig;
}
Also used : Formula(org.logicng.formulas.Formula)

Aggregations

Formula (org.logicng.formulas.Formula)349 Test (org.junit.jupiter.api.Test)190 FormulaFactory (org.logicng.formulas.FormulaFactory)129 ArrayList (java.util.ArrayList)58 Variable (org.logicng.formulas.Variable)55 Literal (org.logicng.formulas.Literal)54 Assignment (org.logicng.datastructures.Assignment)50 PropositionalParser (org.logicng.io.parsers.PropositionalParser)50 PBConstraint (org.logicng.formulas.PBConstraint)45 SATSolver (org.logicng.solvers.SATSolver)36 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)25 CardinalityConstraint (org.logicng.formulas.CardinalityConstraint)25 MethodSource (org.junit.jupiter.params.provider.MethodSource)24 TreeSet (java.util.TreeSet)21 BDDKernel (org.logicng.knowledgecompilation.bdds.jbuddy.BDDKernel)20 TautologyPredicate (org.logicng.predicates.satisfiability.TautologyPredicate)20 HashMap (java.util.HashMap)17 LinkedHashSet (java.util.LinkedHashSet)17 List (java.util.List)17 LogicNGTest (org.logicng.LogicNGTest)16