use of org.logicng.solvers.SATSolver in project LogicNG by logic-ng.
the class PBSolvingTest method testCCEQ.
@Test
public void testCCEQ() {
for (final SATSolver solver : this.solvers) {
testCCEQ(solver, 0, 1);
testCCEQ(solver, 1, 10);
testCCEQ(solver, 2, 45);
testCCEQ(solver, 3, 120);
testCCEQ(solver, 4, 210);
testCCEQ(solver, 5, 252);
testCCEQ(solver, 6, 210);
testCCEQ(solver, 7, 120);
testCCEQ(solver, 8, 45);
testCCEQ(solver, 9, 10);
testCCEQ(solver, 10, 1);
}
}
use of org.logicng.solvers.SATSolver in project LogicNG by logic-ng.
the class PBSolvingTest method testPBLess.
@Test
public void testPBLess() {
for (final PBEncoder encoder : this.encoders) {
for (final SATSolver solver : this.solvers) {
solver.reset();
final int[] coeffs10 = new int[] { 3, 2, 2, 2, 2, 2, 2, 2, 2, 2 };
solver.add(encoder.encode((PBConstraint) this.f.pbc(CType.LE, 6, this.literals10, coeffs10)));
assertSolverSat(solver);
assertThat(solver.enumerateAllModels(this.literals10)).hasSize(140).allMatch(model -> model.positiveVariables().size() <= 3);
solver.reset();
solver.add(encoder.encode((PBConstraint) this.f.pbc(CType.LT, 7, this.literals10, coeffs10)));
assertSolverSat(solver);
assertThat(solver.enumerateAllModels(this.literals10)).hasSize(140).allMatch(model -> model.positiveVariables().size() <= 3);
solver.reset();
solver.add(encoder.encode((PBConstraint) this.f.pbc(CType.LE, 0, this.literals10, coeffs10)));
assertSolverSat(solver);
assertThat(solver.enumerateAllModels(this.literals10)).hasSize(1);
solver.reset();
solver.add(encoder.encode((PBConstraint) this.f.pbc(CType.LE, 1, this.literals10, coeffs10)));
assertSolverSat(solver);
assertThat(solver.enumerateAllModels(this.literals10)).hasSize(1);
solver.reset();
solver.add(encoder.encode((PBConstraint) this.f.pbc(CType.LT, 2, this.literals10, coeffs10)));
assertSolverSat(solver);
assertThat(solver.enumerateAllModels(this.literals10)).hasSize(1);
solver.reset();
solver.add(encoder.encode((PBConstraint) this.f.pbc(CType.LT, 1, this.literals10, coeffs10)));
assertSolverSat(solver);
assertThat(solver.enumerateAllModels(this.literals10)).hasSize(1);
}
}
}
use of org.logicng.solvers.SATSolver in project LogicNG by logic-ng.
the class PBSolvingTest method testCCEXO.
@Test
public void testCCEXO() {
for (final SATSolver solver : this.solvers) {
solver.reset();
solver.add(this.f.exo(this.literals100));
final List<Assignment> models = solver.enumerateAllModels(this.literals100);
assertThat(models.size()).isEqualTo(100);
for (final Assignment model : models) {
assertThat(model.positiveVariables().size() == 1).isTrue();
}
}
}
use of org.logicng.solvers.SATSolver in project LogicNG by logic-ng.
the class PBSolvingTest method testPBGreater.
@Test
public void testPBGreater() {
for (final PBEncoder encoder : this.encoders) {
for (final SATSolver solver : this.solvers) {
solver.reset();
final int[] coeffs10 = new int[] { 3, 2, 2, 2, 2, 2, 2, 2, 2, 2 };
solver.add(encoder.encode((PBConstraint) this.f.pbc(CType.GE, 17, this.literals10, coeffs10)));
assertSolverSat(solver);
assertThat(solver.enumerateAllModels(this.literals10)).hasSize(47).allMatch(model -> model.positiveVariables().size() >= 8);
solver.reset();
solver.add(encoder.encode((PBConstraint) this.f.pbc(CType.GT, 16, this.literals10, coeffs10)));
assertSolverSat(solver);
assertThat(solver.enumerateAllModels(this.literals10)).hasSize(47).allMatch(model -> model.positiveVariables().size() >= 8);
solver.reset();
solver.add(encoder.encode((PBConstraint) this.f.pbc(CType.GE, 21, this.literals10, coeffs10)));
assertSolverSat(solver);
assertThat(solver.enumerateAllModels(this.literals10)).hasSize(1);
solver.reset();
solver.add(encoder.encode((PBConstraint) this.f.pbc(CType.GE, 22, this.literals10, coeffs10)));
assertSolverUnsat(solver);
solver.reset();
solver.add(encoder.encode((PBConstraint) this.f.pbc(CType.GT, 42, this.literals10, coeffs10)));
assertSolverUnsat(solver);
}
}
}
use of org.logicng.solvers.SATSolver in project LogicNG by logic-ng.
the class OptimizationFunctionTest method testOptimumModel.
private void testOptimumModel(final Formula formula, final Assignment optimumModel, final Collection<? extends Literal> literals, final boolean maximize) {
final FormulaFactory f = formula.factory();
final SATPredicate satPredicate = new SATPredicate(f);
if (formula.holds(satPredicate)) {
assertThat(f.and(formula, f.and(optimumModel.literals())).holds(satPredicate));
final int numSatisfiedLiterals = satisfiedLiterals(optimumModel, literals).size();
final SortedSet<Variable> selVars = new TreeSet<>();
final SATSolver solver = MiniSat.miniSat(formula.factory());
solver.add(formula);
for (final Literal lit : literals) {
final Variable selVar = f.variable("SEL_VAR_" + selVars.size());
if (maximize) {
solver.add(f.equivalence(selVar.negate(), lit));
} else {
solver.add(f.equivalence(selVar.negate(), lit.negate()));
}
}
solver.add(formula.factory().cc(CType.GT, numSatisfiedLiterals + 1, selVars));
assertSolverUnsat(solver);
} else {
assertThat(optimumModel).isNull();
}
}
Aggregations