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);
}
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);
}
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");
}
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);
}
}
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();
}
}
Aggregations