Search in sources :

Example 21 with CardinalityConstraint

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

the class CCIncrementalSolverTest method testLargeTotalizerUpperBoundAMK.

@Test
public void testLargeTotalizerUpperBoundAMK() {
    this.f.putConfiguration(this.configs[2]);
    final int numLits = 100;
    int currentBound = numLits - 1;
    final Variable[] vars = new Variable[numLits];
    for (int i = 0; i < numLits; i++) {
        vars[i] = this.f.variable("v" + i);
    }
    final SATSolver solver = this.solvers[3];
    solver.reset();
    // >= 42
    solver.add(this.f.cc(CType.GE, 42, vars));
    this.f.putConfiguration(this.configs[0]);
    final CCIncrementalData incData = solver.addIncrementalCC((CardinalityConstraint) this.f.cc(CType.LE, currentBound, vars));
    // search the lower bound
    while (solver.sat() == Tristate.TRUE) {
        // <= currentBound - 1
        incData.newUpperBoundForSolver(--currentBound);
    }
    assertThat(currentBound).isEqualTo(41);
}
Also used : SATSolver(org.logicng.solvers.SATSolver) Variable(org.logicng.formulas.Variable) CardinalityConstraint(org.logicng.formulas.CardinalityConstraint) Test(org.junit.jupiter.api.Test) LogicNGTest(org.logicng.LogicNGTest)

Example 22 with CardinalityConstraint

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

the class CCALKTest method testIllegalCC1.

@Test
public void testIllegalCC1() {
    final FormulaFactory f = new FormulaFactory();
    final CCEncoder encoder = new CCEncoder(f);
    final int numLits = 100;
    final Variable[] problemLits = new Variable[numLits];
    for (int i = 0; i < numLits; i++) {
        problemLits[i] = f.variable("v" + i);
    }
    assertThatThrownBy(() -> encoder.encode((CardinalityConstraint) f.cc(CType.GE, -1, problemLits))).isInstanceOf(IllegalArgumentException.class);
}
Also used : FormulaFactory(org.logicng.formulas.FormulaFactory) Variable(org.logicng.formulas.Variable) CardinalityConstraint(org.logicng.formulas.CardinalityConstraint) Test(org.junit.jupiter.api.Test) LogicNGTest(org.logicng.LogicNGTest)

Example 23 with CardinalityConstraint

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

the class CCAMKTest method testIllegalCC1.

@Test
public void testIllegalCC1() {
    final FormulaFactory f = new FormulaFactory();
    final CCEncoder encoder = new CCEncoder(f);
    final int numLits = 100;
    final Variable[] problemLits = new Variable[numLits];
    for (int i = 0; i < numLits; i++) {
        problemLits[i] = f.variable("v" + i);
    }
    assertThatThrownBy(() -> encoder.encode((CardinalityConstraint) f.cc(CType.LE, -1, problemLits))).isInstanceOf(IllegalArgumentException.class);
}
Also used : FormulaFactory(org.logicng.formulas.FormulaFactory) Variable(org.logicng.formulas.Variable) CardinalityConstraint(org.logicng.formulas.CardinalityConstraint) Test(org.junit.jupiter.api.Test) LogicNGTest(org.logicng.LogicNGTest)

Example 24 with CardinalityConstraint

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

the class OptimizationFunction method maximize.

private Assignment maximize(final MiniSat solver) {
    start(this.handler);
    final FormulaFactory f = solver.factory();
    LNGBooleanVector internalModel;
    final Map<Variable, Literal> selectorMap = new TreeMap<>();
    for (final Literal lit : this.literals) {
        final Variable selVar = f.variable(SEL_PREFIX + selectorMap.size());
        selectorMap.put(selVar, lit);
    }
    final Set<Variable> selectors = selectorMap.keySet();
    if (this.maximize) {
        selectorMap.forEach((selVar, lit) -> solver.add(f.or(selVar.negate(), lit)));
        selectorMap.forEach((selVar, lit) -> solver.add(f.or(lit.negate(), selVar)));
    } else {
        selectorMap.forEach((selVar, lit) -> solver.add(f.or(selVar.negate(), lit.negate())));
        selectorMap.forEach((selVar, lit) -> solver.add(f.or(lit, selVar)));
    }
    Tristate sat = solver.sat(satHandler(handler));
    if (sat != Tristate.TRUE || aborted(handler)) {
        return null;
    }
    internalModel = solver.underlyingSolver().model();
    Assignment currentModel = solver.model(selectors);
    int currentBound = currentModel.positiveVariables().size();
    if (currentBound == 0) {
        solver.add(f.cc(CType.GE, 1, selectors));
        sat = solver.sat(satHandler(handler));
        if (aborted(handler)) {
            return null;
        } else if (sat == Tristate.FALSE) {
            return mkResultModel(solver, internalModel);
        } else {
            internalModel = solver.underlyingSolver().model();
            currentModel = solver.model(selectors);
            currentBound = currentModel.positiveVariables().size();
        }
    } else if (currentBound == selectors.size()) {
        return mkResultModel(solver, internalModel);
    }
    final Formula cc = f.cc(CType.GE, currentBound + 1, selectors);
    assert cc instanceof CardinalityConstraint;
    final CCIncrementalData incrementalData = solver.addIncrementalCC((CardinalityConstraint) cc);
    sat = solver.sat(satHandler(handler));
    if (aborted(handler)) {
        return null;
    }
    while (sat == Tristate.TRUE) {
        final LNGBooleanVector modelCopy = new LNGBooleanVector(solver.underlyingSolver().model());
        if (this.handler != null && !this.handler.foundBetterBound(() -> mkResultModel(solver, modelCopy))) {
            return null;
        }
        internalModel = modelCopy;
        currentModel = solver.model(selectors);
        currentBound = currentModel.positiveVariables().size();
        if (currentBound == selectors.size()) {
            return mkResultModel(solver, internalModel);
        }
        incrementalData.newLowerBoundForSolver(currentBound + 1);
        sat = solver.sat(satHandler(handler));
        if (aborted(handler)) {
            return null;
        }
    }
    return mkResultModel(solver, internalModel);
}
Also used : Variable(org.logicng.formulas.Variable) Tristate(org.logicng.datastructures.Tristate) TreeMap(java.util.TreeMap) CardinalityConstraint(org.logicng.formulas.CardinalityConstraint) Assignment(org.logicng.datastructures.Assignment) Formula(org.logicng.formulas.Formula) FormulaFactory(org.logicng.formulas.FormulaFactory) Literal(org.logicng.formulas.Literal) CCIncrementalData(org.logicng.cardinalityconstraints.CCIncrementalData) CardinalityConstraint(org.logicng.formulas.CardinalityConstraint) LNGBooleanVector(org.logicng.collections.LNGBooleanVector)

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