Search in sources :

Example 56 with Formula

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

the class CNFFactorization method applyRec.

private Formula applyRec(final Formula formula, final boolean cache) {
    if (!this.proceed) {
        return null;
    }
    if (formula.type().precedence() >= LITERAL.precedence()) {
        return formula;
    }
    Formula cached = formula.transformationCacheEntry(FACTORIZED_CNF);
    if (cached != null) {
        return cached;
    }
    switch(formula.type()) {
        case NOT:
        case IMPL:
        case EQUIV:
            cached = this.applyRec(formula.nnf(), cache);
            break;
        case OR:
            LinkedHashSet<Formula> nops = new LinkedHashSet<>();
            for (final Formula op : formula) {
                if (!this.proceed) {
                    return null;
                }
                nops.add(this.applyRec(op, cache));
            }
            final Iterator<Formula> it = nops.iterator();
            cached = it.next();
            while (it.hasNext()) {
                if (!this.proceed) {
                    return null;
                }
                cached = this.distribute(cached, it.next());
            }
            break;
        case AND:
            nops = new LinkedHashSet<>();
            for (final Formula op : formula) {
                final Formula apply = this.applyRec(op, cache);
                if (!this.proceed) {
                    return null;
                }
                nops.add(apply);
            }
            cached = formula.factory().and(nops);
            break;
        case PBC:
            cached = formula.nnf();
            break;
        default:
            throw new IllegalArgumentException("Could not process the formula type " + formula.type());
    }
    if (this.proceed) {
        if (cache) {
            formula.setTransformationCacheEntry(FACTORIZED_CNF, cached);
        }
        return cached;
    }
    return null;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Formula(org.logicng.formulas.Formula)

Example 57 with Formula

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

the class CNFFactorization method distribute.

/**
 * Computes the distribution (factorization) of two formulas.
 * @param f1 the first formula
 * @param f2 the second formula
 * @return the distribution of the two formulas
 */
private Formula distribute(final Formula f1, final Formula f2) {
    if (this.handler != null) {
        this.proceed = this.handler.performedDistribution();
    }
    if (this.proceed) {
        final FormulaFactory f = f1.factory();
        if (f1.type() == AND || f2.type() == AND) {
            final LinkedHashSet<Formula> nops = new LinkedHashSet<>();
            for (final Formula op : f1.type() == AND ? f1 : f2) {
                final Formula distribute = this.distribute(op, f1.type() == AND ? f2 : f1);
                if (!this.proceed) {
                    return null;
                }
                nops.add(distribute);
            }
            return f.and(nops);
        }
        final Formula clause = f.or(f1, f2);
        if (this.handler != null) {
            this.proceed = this.handler.createdClause(clause);
        }
        return clause;
    }
    return null;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Formula(org.logicng.formulas.Formula) FormulaFactory(org.logicng.formulas.FormulaFactory)

Example 58 with Formula

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

the class TimeoutBDDHandlerTest method testTimeoutHandlerSingleTimeout.

@Test
public void testTimeoutHandlerSingleTimeout() {
    final Formula formula = pg.generate(10);
    final VariableOrderingProvider provider = VariableOrdering.BFS.provider();
    final BDDKernel kernel = new BDDKernel(this.f, provider.getOrder(formula), 100, 100);
    final TimeoutBDDHandler handler = new TimeoutBDDHandler(100L);
    final BDD result = BDDFactory.build(formula, kernel, handler);
    assertThat(handler.aborted).isTrue();
    assertThat(result).isEqualTo(new BDD(BDDKernel.BDD_ABORT, kernel));
}
Also used : Formula(org.logicng.formulas.Formula) VariableOrderingProvider(org.logicng.knowledgecompilation.bdds.orderings.VariableOrderingProvider) BDDKernel(org.logicng.knowledgecompilation.bdds.jbuddy.BDDKernel) BDD(org.logicng.knowledgecompilation.bdds.BDD) Test(org.junit.jupiter.api.Test)

Example 59 with Formula

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

the class TimeoutBDDHandlerTest method testThatMethodsAreCalled.

@Test
public void testThatMethodsAreCalled() throws ParserException {
    final Formula formula = f.parse("(A => ~B) & ((A & C) | (D & ~C)) & (A | Y | X)");
    final VariableOrderingProvider provider = VariableOrdering.BFS.provider();
    final BDDKernel kernel = new BDDKernel(this.f, provider.getOrder(formula), 100, 100);
    final TimeoutBDDHandler handler = Mockito.mock(TimeoutBDDHandler.class);
    BDDFactory.build(formula, kernel, handler);
    verify(handler, times(1)).started();
    verify(handler, atLeast(1)).newRefAdded();
}
Also used : Formula(org.logicng.formulas.Formula) VariableOrderingProvider(org.logicng.knowledgecompilation.bdds.orderings.VariableOrderingProvider) BDDKernel(org.logicng.knowledgecompilation.bdds.jbuddy.BDDKernel) Test(org.junit.jupiter.api.Test)

Example 60 with Formula

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

the class TimeoutBDDHandlerTest method testThatNewRefAddedHandledProperly.

@Test
public void testThatNewRefAddedHandledProperly() throws ParserException {
    final Formula formula = f.parse("(A => ~B) & ((A & C) | ~(D & ~C)) & (A | Y | X)");
    final VariableOrderingProvider provider = VariableOrdering.BFS.provider();
    final BDDKernel kernel = new BDDKernel(this.f, provider.getOrder(formula), 100, 100);
    final TimeoutBDDHandler handler = Mockito.mock(TimeoutBDDHandler.class);
    final AtomicInteger count = new AtomicInteger(0);
    when(handler.newRefAdded()).then(invocationOnMock -> count.addAndGet(1) < 5);
    final BDD result = BDDFactory.build(formula, kernel, handler);
    assertThat(result).isEqualTo(new BDD(BDDKernel.BDD_ABORT, kernel));
    verify(handler, times(1)).started();
    verify(handler, times(5)).newRefAdded();
}
Also used : Formula(org.logicng.formulas.Formula) VariableOrderingProvider(org.logicng.knowledgecompilation.bdds.orderings.VariableOrderingProvider) BDDKernel(org.logicng.knowledgecompilation.bdds.jbuddy.BDDKernel) BDD(org.logicng.knowledgecompilation.bdds.BDD) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.jupiter.api.Test)

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