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