use of org.logicng.formulas.CardinalityConstraint in project LogicNG by logic-ng.
the class CCIncrementalSolverTest method testSimpleIncrementalALK.
@Test
public void testSimpleIncrementalALK() {
for (final CCConfig config : this.configs) {
this.f.putConfiguration(this.configs[2]);
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 SATSolver solver = this.solvers[2];
solver.reset();
// >= 4
solver.add(this.f.cc(CType.GE, 4, vars));
// <= 7
solver.add(this.f.cc(CType.LE, 7, vars));
this.f.putConfiguration(config);
final CCIncrementalData incData = solver.addIncrementalCC((CardinalityConstraint) this.f.cc(CType.GE, 2, vars));
// >=2
assertSolverSat(solver);
// >= 3
incData.newLowerBoundForSolver(3);
assertSolverSat(solver);
// >= 4
incData.newLowerBoundForSolver(4);
assertSolverSat(solver);
// >= 5
incData.newLowerBoundForSolver(5);
assertSolverSat(solver);
// >= 6
incData.newLowerBoundForSolver(6);
assertSolverSat(solver);
// >= 7
incData.newLowerBoundForSolver(7);
assertSolverSat(solver);
final SolverState state = solver.saveState();
// >= 8
incData.newLowerBoundForSolver(8);
assertSolverUnsat(solver);
solver.loadState(state);
assertSolverSat(solver);
// <= 9
incData.newLowerBoundForSolver(9);
assertSolverUnsat(solver);
}
}
use of org.logicng.formulas.CardinalityConstraint in project LogicNG by logic-ng.
the class CCPerformanceTest method buildAMK.
private void buildAMK(final int numLits, final FormulaFactory f, final boolean miniCard) {
final Variable[] problemLits = new Variable[numLits];
for (int i = 0; i < numLits; i++) {
problemLits[i] = f.variable("v" + i);
}
final SATSolver solver = miniCard ? MiniSat.miniCard(f) : MiniSat.miniSat(f);
for (int i = 10; i < 100; i = i + 10) {
final CardinalityConstraint cc = (CardinalityConstraint) f.cc(CType.LE, i, problemLits);
solver.reset();
solver.add(cc);
assertSolverSat(solver);
final Assignment model = solver.model();
assertThat(cc.evaluate(model)).isTrue();
}
}
use of org.logicng.formulas.CardinalityConstraint in project LogicNG by logic-ng.
the class OptimizationFunctionTest method testExoModel.
@ParameterizedTest
@MethodSource("solvers")
public void testExoModel(final SATSolver solver) {
final FormulaFactory f = solver.factory();
final CardinalityConstraint exo = (CardinalityConstraint) f.exo(f.variable("a"), f.variable("b"), f.variable("c"));
final Assignment minimumModel = optimize(Collections.singleton(exo), exo.variables(), Collections.emptyList(), false, solver, null);
testMinimumModel(exo, minimumModel, exo.variables());
final Assignment maximumModel = optimize(Collections.singleton(exo), exo.variables(), Collections.emptyList(), true, solver, null);
testMaximumModel(exo, maximumModel, exo.variables());
}
use of org.logicng.formulas.CardinalityConstraint in project LogicNG by logic-ng.
the class FormulaRandomizerTest method testCc.
@Test
public void testCc() {
final FormulaRandomizer random = new FormulaRandomizer(this.f, FormulaRandomizerConfig.builder().seed(4242).weightPbcTypeLe(5).weightPbcTypeLt(4).weightPbcTypeGe(3).weightPbcTypeGt(2).weightPbcTypeEq(1).maximumCoefficientPbc(10).build());
int le = 0;
int lt = 0;
int ge = 0;
int gt = 0;
int eq = 0;
for (int i = 0; i < 500; i++) {
final Formula formula = random.cc();
assertThat(formula).isInstanceOf(CardinalityConstraint.class);
final CardinalityConstraint cc = (CardinalityConstraint) formula;
assertThat(cc.isCC()).isTrue();
assertThat(cc.numberOfOperands()).isLessThanOrEqualTo(10);
if (cc.comparator() == CType.GT) {
assertThat(cc.rhs()).isStrictlyBetween(-2, 11);
} else if (cc.comparator() == CType.LT) {
assertThat(cc.rhs()).isStrictlyBetween(0, 11);
} else {
assertThat(cc.rhs()).isStrictlyBetween(-1, 11);
}
switch(cc.comparator()) {
case LE:
le++;
break;
case LT:
lt++;
break;
case GE:
ge++;
break;
case GT:
gt++;
break;
case EQ:
eq++;
break;
}
}
assertThat(le).isStrictlyBetween((int) (.7 * 5 / 4 * lt), (int) (1.3 * 5 / 4 * lt));
assertThat(lt).isStrictlyBetween((int) (.7 * 4 / 3 * ge), (int) (1.3 * 4 / 3 * ge));
assertThat(ge).isStrictlyBetween((int) (.7 * 3 / 2 * gt), (int) (1.3 * 3 / 2 * gt));
assertThat(gt).isStrictlyBetween((int) (.7 * 2 / 1 * eq), (int) (1.3 * 2 / 1 * eq));
}
use of org.logicng.formulas.CardinalityConstraint in project LogicNG by logic-ng.
the class FormulaRandomizerTest method testMaximumOperandsCc.
@Test
public void testMaximumOperandsCc() {
final FormulaRandomizer random = new FormulaRandomizer(this.f, FormulaRandomizerConfig.builder().maximumOperandsOr(10).seed(42).build());
for (int i = 0; i < 100; i++) {
final Formula formula = random.cc();
assertThat(formula.type()).isEqualTo(FType.PBC);
final CardinalityConstraint cc = (CardinalityConstraint) formula;
assertThat(cc.literals().size()).isLessThanOrEqualTo(10);
}
}
Aggregations