Search in sources :

Example 1 with CardinalityConstraint

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

the class QuineMcCluskeyAlgorithm method minimize.

/**
 * Performs the minimization via incremental cardinality constraints.
 * @param satSolver the SAT solver
 * @param var2Term  a mapping from selector variable to prime implicant
 * @param f         the formula factory
 * @return a minimal set of prime implicants to cover all minterms
 */
static List<Term> minimize(final SATSolver satSolver, final LinkedHashMap<Variable, Term> var2Term, final FormulaFactory f) {
    final Assignment initialModel = satSolver.model();
    List<Variable> currentTermVars = computeCurrentTermVars(initialModel, var2Term.keySet());
    if (currentTermVars.size() == 2) {
        satSolver.add(f.amo(var2Term.keySet()));
        if (satSolver.sat() == Tristate.TRUE) {
            currentTermVars = computeCurrentTermVars(satSolver.model(), var2Term.keySet());
        }
    } else {
        final Formula cc = f.cc(CType.LE, currentTermVars.size() - 1, var2Term.keySet());
        assert cc instanceof CardinalityConstraint;
        final CCIncrementalData incData = satSolver.addIncrementalCC((CardinalityConstraint) cc);
        while (satSolver.sat() == Tristate.TRUE) {
            currentTermVars = computeCurrentTermVars(satSolver.model(), var2Term.keySet());
            incData.newUpperBoundForSolver(currentTermVars.size() - 1);
        }
    }
    return computeTerms(currentTermVars, var2Term);
}
Also used : Assignment(org.logicng.datastructures.Assignment) Formula(org.logicng.formulas.Formula) Variable(org.logicng.formulas.Variable) CCIncrementalData(org.logicng.cardinalityconstraints.CCIncrementalData) CardinalityConstraint(org.logicng.formulas.CardinalityConstraint)

Example 2 with CardinalityConstraint

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

the class CCEXOTest method testEncodingSetting.

@Test
public void testEncodingSetting() {
    final FormulaFactory f = new FormulaFactory();
    f.putConfiguration(CCConfig.builder().amoEncoding(PURE).build());
    final CardinalityConstraint exo = (CardinalityConstraint) f.exo(IntStream.range(0, 100).mapToObj(i -> f.variable("v" + i)).collect(Collectors.toList()));
    assertThat(exo.cnf().variables()).hasSize(100);
    assertThat(exo.cnf().numberOfOperands()).isEqualTo(4951);
}
Also used : IntStream(java.util.stream.IntStream) COMMANDER(org.logicng.cardinalityconstraints.CCConfig.AMO_ENCODER.COMMANDER) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) LADDER(org.logicng.cardinalityconstraints.CCConfig.AMO_ENCODER.LADDER) SATSolver(org.logicng.solvers.SATSolver) SQRT(org.logicng.cardinalityconstraints.CCConfig.BIMANDER_GROUP_SIZE.SQRT) Formula(org.logicng.formulas.Formula) BEST(org.logicng.cardinalityconstraints.CCConfig.AMO_ENCODER.BEST) NESTED(org.logicng.cardinalityconstraints.CCConfig.AMO_ENCODER.NESTED) MiniSat(org.logicng.solvers.MiniSat) Collectors(java.util.stream.Collectors) BIMANDER(org.logicng.cardinalityconstraints.CCConfig.AMO_ENCODER.BIMANDER) Test(org.junit.jupiter.api.Test) HALF(org.logicng.cardinalityconstraints.CCConfig.BIMANDER_GROUP_SIZE.HALF) LogicNGTest(org.logicng.LogicNGTest) PURE(org.logicng.cardinalityconstraints.CCConfig.AMO_ENCODER.PURE) BINARY(org.logicng.cardinalityconstraints.CCConfig.AMO_ENCODER.BINARY) Variable(org.logicng.formulas.Variable) FormulaFactory(org.logicng.formulas.FormulaFactory) FIXED(org.logicng.cardinalityconstraints.CCConfig.BIMANDER_GROUP_SIZE.FIXED) CardinalityConstraint(org.logicng.formulas.CardinalityConstraint) PRODUCT(org.logicng.cardinalityconstraints.CCConfig.AMO_ENCODER.PRODUCT) FormulaFactory(org.logicng.formulas.FormulaFactory) CardinalityConstraint(org.logicng.formulas.CardinalityConstraint) Test(org.junit.jupiter.api.Test) LogicNGTest(org.logicng.LogicNGTest)

Example 3 with CardinalityConstraint

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

the class CCEXOTest method testEXO1.

@Test
public void testEXO1() {
    final FormulaFactory f = new FormulaFactory();
    final CardinalityConstraint cc = (CardinalityConstraint) f.exo(f.variable("v0"));
    for (final CCConfig config : this.configs) {
        assertThat(new CCEncoder(f, config).encode(cc)).containsExactly(f.variable("v0"));
    }
    assertThat(f.newCCVariable().name()).endsWith("_0");
}
Also used : FormulaFactory(org.logicng.formulas.FormulaFactory) CardinalityConstraint(org.logicng.formulas.CardinalityConstraint) Test(org.junit.jupiter.api.Test) LogicNGTest(org.logicng.LogicNGTest)

Example 4 with CardinalityConstraint

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

the class CCIncrementalFormulaTest method testSimpleIncrementalAMK.

@Test
public void testSimpleIncrementalAMK() {
    for (final CCEncoder encoder : this.encoders) {
        final CCEncoder initialEncoder = new CCEncoder(this.f);
        final int numLits = 10;
        final Variable[] vars = new Variable[numLits];
        for (int i = 0; i < numLits; i++) {
            vars[i] = this.f.variable("v" + i);
        }
        final Pair<List<Formula>, CCIncrementalData> cc = encoder.encodeIncremental((CardinalityConstraint) this.f.cc(CType.LE, 9, vars));
        final CCIncrementalData incData = cc.second();
        final SATSolver solver = MiniSat.miniSat(this.f);
        // >= 4
        solver.add(initialEncoder.encode((CardinalityConstraint) this.f.cc(CType.GE, 4, vars)));
        // <= 7
        solver.add(initialEncoder.encode((CardinalityConstraint) this.f.cc(CType.LE, 7, vars)));
        solver.add(cc.first());
        assertSolverSat(solver);
        // <= 9
        assertSolverSat(solver);
        // <= 8
        solver.add(incData.newUpperBound(8));
        assertSolverSat(solver);
        assertThat(incData.currentRHS()).isEqualTo(8);
        // <= 7
        solver.add(incData.newUpperBound(7));
        assertSolverSat(solver);
        // <= 6
        solver.add(incData.newUpperBound(6));
        assertSolverSat(solver);
        // <= 5
        solver.add(incData.newUpperBound(5));
        assertSolverSat(solver);
        // <= 4
        solver.add(incData.newUpperBound(4));
        assertSolverSat(solver);
        final SolverState state = solver.saveState();
        // <= 3
        solver.add(incData.newUpperBound(3));
        assertSolverUnsat(solver);
        solver.loadState(state);
        assertSolverSat(solver);
        // <= 2
        solver.add(incData.newUpperBound(2));
        assertSolverUnsat(solver);
    }
}
Also used : SATSolver(org.logicng.solvers.SATSolver) Variable(org.logicng.formulas.Variable) SolverState(org.logicng.solvers.SolverState) CardinalityConstraint(org.logicng.formulas.CardinalityConstraint) List(java.util.List) CardinalityConstraint(org.logicng.formulas.CardinalityConstraint) Test(org.junit.jupiter.api.Test) LogicNGTest(org.logicng.LogicNGTest)

Example 5 with CardinalityConstraint

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

the class CCIncrementalFormulaTest method testIncrementalData.

@Test
public void testIncrementalData() {
    for (final CCEncoder encoder : this.encoders) {
        final int numLits = 10;
        final Variable[] vars = new Variable[numLits];
        for (int i = 0; i < numLits; i++) {
            vars[i] = this.f.variable("v" + i);
        }
        Pair<List<Formula>, CCIncrementalData> cc = encoder.encodeIncremental((CardinalityConstraint) this.f.cc(CType.LT, 10, vars));
        CCIncrementalData incData = cc.second();
        assertThat(incData.toString()).contains("currentRHS=9");
        cc = encoder.encodeIncremental((CardinalityConstraint) this.f.cc(CType.GT, 1, vars));
        incData = cc.second();
        assertThat(incData.toString()).contains("currentRHS=2");
        cc = encoder.encodeIncremental((CardinalityConstraint) this.f.cc(CType.LT, 1, vars));
        incData = cc.second();
        assertThat(incData).isNull();
        assertThat(cc.first()).contains(vars[0].negate());
        cc = encoder.encodeIncremental((CardinalityConstraint) this.f.cc(CType.LE, numLits + 1, vars));
        incData = cc.second();
        assertThat(incData).isNull();
        cc = encoder.encodeIncremental((CardinalityConstraint) this.f.cc(CType.GE, numLits + 1, vars));
        incData = cc.second();
        assertThat(incData).isNull();
        cc = encoder.encodeIncremental((CardinalityConstraint) this.f.cc(CType.GE, numLits, vars));
        incData = cc.second();
        assertThat(incData).isNull();
        cc = encoder.encodeIncremental((CardinalityConstraint) this.f.cc(CType.GE, 0, vars));
        incData = cc.second();
        assertThat(incData).isNull();
        cc = encoder.encodeIncremental((CardinalityConstraint) this.f.cc(CType.GE, 1, vars));
        incData = cc.second();
        assertThat(incData).isNull();
    }
}
Also used : Variable(org.logicng.formulas.Variable) CardinalityConstraint(org.logicng.formulas.CardinalityConstraint) List(java.util.List) CardinalityConstraint(org.logicng.formulas.CardinalityConstraint) Test(org.junit.jupiter.api.Test) LogicNGTest(org.logicng.LogicNGTest)

Aggregations

CardinalityConstraint (org.logicng.formulas.CardinalityConstraint)24 Test (org.junit.jupiter.api.Test)20 Variable (org.logicng.formulas.Variable)19 LogicNGTest (org.logicng.LogicNGTest)18 SATSolver (org.logicng.solvers.SATSolver)14 List (java.util.List)7 FormulaFactory (org.logicng.formulas.FormulaFactory)7 Formula (org.logicng.formulas.Formula)5 LongRunningTag (org.logicng.LongRunningTag)4 Assignment (org.logicng.datastructures.Assignment)4 SolverState (org.logicng.solvers.SolverState)4 CCIncrementalData (org.logicng.cardinalityconstraints.CCIncrementalData)2 PBConstraint (org.logicng.formulas.PBConstraint)2 TreeMap (java.util.TreeMap)1 Collectors (java.util.stream.Collectors)1 IntStream (java.util.stream.IntStream)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1 MethodSource (org.junit.jupiter.params.provider.MethodSource)1 BEST (org.logicng.cardinalityconstraints.CCConfig.AMO_ENCODER.BEST)1