Search in sources :

Example 1 with BDDKernel

use of org.logicng.knowledgecompilation.bdds.jbuddy.BDDKernel in project LogicNG by logic-ng.

the class BDDCNFFunction method apply.

@Override
public Formula apply(final BDD bdd) {
    final BDDKernel kernel = bdd.underlyingKernel();
    final List<byte[]> unsatPaths = new BDDOperations(kernel).allUnsat(bdd.index());
    final List<Formula> clauses = new ArrayList<>();
    List<Formula> literals;
    for (final byte[] path : unsatPaths) {
        literals = new ArrayList<>();
        for (int i = 0; i < path.length; i++) {
            if (path[i] == 0) {
                literals.add(kernel.getVariableForIndex(i));
            } else if (path[i] == 1) {
                literals.add(kernel.getVariableForIndex(i).negate());
            }
        }
        clauses.add(kernel.factory().or(literals));
    }
    return kernel.factory().and(clauses);
}
Also used : Formula(org.logicng.formulas.Formula) BDDKernel(org.logicng.knowledgecompilation.bdds.jbuddy.BDDKernel) BDDOperations(org.logicng.knowledgecompilation.bdds.jbuddy.BDDOperations) ArrayList(java.util.ArrayList)

Example 2 with BDDKernel

use of org.logicng.knowledgecompilation.bdds.jbuddy.BDDKernel in project LogicNG by logic-ng.

the class LngBDDFunction method apply.

@Override
public BDDNode apply(final BDD bdd) {
    final BDDKernel kernel = bdd.underlyingKernel();
    final int index = bdd.index();
    final Map<Integer, int[]> kernelNodeMap = new BDDOperations(kernel).allNodes(index).stream().collect(Collectors.toMap(node -> node[0], node -> node));
    return buildBDDNode(index, kernel, kernelNodeMap, new HashMap<>());
}
Also used : BDDNode(org.logicng.knowledgecompilation.bdds.datastructures.BDDNode) BDD(org.logicng.knowledgecompilation.bdds.BDD) BDDKernel(org.logicng.knowledgecompilation.bdds.jbuddy.BDDKernel) Variable(org.logicng.formulas.Variable) Map(java.util.Map) BDDInnerNode(org.logicng.knowledgecompilation.bdds.datastructures.BDDInnerNode) BDDConstant(org.logicng.knowledgecompilation.bdds.datastructures.BDDConstant) BDDOperations(org.logicng.knowledgecompilation.bdds.jbuddy.BDDOperations) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) BDDKernel(org.logicng.knowledgecompilation.bdds.jbuddy.BDDKernel) BDDOperations(org.logicng.knowledgecompilation.bdds.jbuddy.BDDOperations)

Example 3 with BDDKernel

use of org.logicng.knowledgecompilation.bdds.jbuddy.BDDKernel 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 4 with BDDKernel

use of org.logicng.knowledgecompilation.bdds.jbuddy.BDDKernel 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 5 with BDDKernel

use of org.logicng.knowledgecompilation.bdds.jbuddy.BDDKernel 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

BDDKernel (org.logicng.knowledgecompilation.bdds.jbuddy.BDDKernel)36 Test (org.junit.jupiter.api.Test)23 Formula (org.logicng.formulas.Formula)20 FormulaFactory (org.logicng.formulas.FormulaFactory)17 PropositionalParser (org.logicng.io.parsers.PropositionalParser)6 BDDOperations (org.logicng.knowledgecompilation.bdds.jbuddy.BDDOperations)6 Variable (org.logicng.formulas.Variable)5 BDD (org.logicng.knowledgecompilation.bdds.BDD)5 VariableOrderingProvider (org.logicng.knowledgecompilation.bdds.orderings.VariableOrderingProvider)5 Assignment (org.logicng.datastructures.Assignment)4 NQueensGenerator (org.logicng.testutils.NQueensGenerator)4 NumberOfNodesBDDHandler (org.logicng.handlers.NumberOfNodesBDDHandler)3 BDDInnerNode (org.logicng.knowledgecompilation.bdds.datastructures.BDDInnerNode)3 BigInteger (java.math.BigInteger)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 TimeoutBDDHandler (org.logicng.handlers.TimeoutBDDHandler)2 LngBDDFunction (org.logicng.knowledgecompilation.bdds.functions.LngBDDFunction)2