use of org.logicng.formulas.PBConstraint in project LogicNG by logic-ng.
the class PBEncoderTest method testSpecialCases.
@Test
public void testSpecialCases() {
final List<Literal> lits = new ArrayList<>();
lits.add(this.f.literal("m", true));
lits.add(this.f.literal("n", true));
final List<Integer> coeffs = new ArrayList<>();
coeffs.add(2);
coeffs.add(1);
final PBConstraint truePBC = (PBConstraint) this.f.pbc(CType.GE, 0, lits, coeffs);
for (final PBEncoder encoder : this.encoders) {
assertThat(encoder.encode(truePBC)).isEmpty();
}
}
use of org.logicng.formulas.PBConstraint in project LogicNG by logic-ng.
the class PBEncoderTest method testCC.
private void testCC(final int numLits, final int rhs, final int expected, final PBEncoder encoder) {
final Variable[] problemLits = new Variable[numLits];
final int[] coeffs = new int[numLits];
for (int i = 0; i < numLits; i++) {
problemLits[i] = this.f.variable("v" + i);
coeffs[i] = 1;
}
final List<Formula> clauses = encoder.encode((PBConstraint) this.f.pbc(CType.LE, rhs, problemLits, coeffs));
final SATSolver solver = MiniSat.miniSat(this.f);
solver.add(clauses);
assertSolverSat(solver);
assertThat(solver.enumerateAllModels(problemLits)).hasSize(expected).allMatch(m -> m.positiveVariables().size() <= rhs);
}
use of org.logicng.formulas.PBConstraint in project LogicNG by logic-ng.
the class PBEncoderTest method testCC1.
@Test
public void testCC1() {
for (final PBEncoder encoder : this.encoders) {
final int numLits = 100;
final int rhs = 1;
final List<Literal> lits = new ArrayList<>();
final List<Integer> coeffs = new ArrayList<>();
final Variable[] problemLits = new Variable[numLits];
for (int i = 0; i < numLits; i++) {
final Variable var = this.f.variable("v" + i);
lits.add(var);
problemLits[i] = var;
coeffs.add(1);
}
final List<Formula> clauses = encoder.encode((PBConstraint) this.f.pbc(CType.LE, rhs, lits, coeffs));
final SATSolver solver = MiniSat.miniSat(this.f);
solver.add(clauses);
assertSolverSat(solver);
assertThat(solver.enumerateAllModels(problemLits)).hasSize(numLits + 1).allMatch(m -> m.positiveVariables().size() <= rhs);
}
}
use of org.logicng.formulas.PBConstraint in project LogicNG by logic-ng.
the class PBSolvingTest method testPBNegative.
@Test
public void testPBNegative() {
for (final PBEncoder encoder : this.encoders) {
for (final SATSolver solver : this.solvers) {
solver.reset();
int[] coeffs10 = new int[] { 2, 2, 2, 2, 2, 2, 2, 2, 2, -2 };
final PBConstraint pbc = (PBConstraint) this.f.pbc(CType.EQ, 2, this.literals10, coeffs10);
solver.add(encoder.encode(pbc));
assertSolverSat(solver);
assertThat(solver.enumerateAllModels(this.literals10)).hasSize(45).allMatch(pbc::evaluate);
solver.reset();
coeffs10 = new int[] { 2, 2, 2, 2, 2, 2, 2, 2, 2, -2 };
final PBConstraint pbc2 = (PBConstraint) this.f.pbc(CType.EQ, 4, this.literals10, coeffs10);
solver.add(encoder.encode(pbc2));
assertSolverSat(solver);
assertThat(solver.enumerateAllModels(this.literals10)).hasSize(120).allMatch(pbc2::evaluate);
solver.reset();
coeffs10 = new int[] { 2, 2, -3, 2, -7, 2, 2, 2, 2, -2 };
final PBConstraint pbc3 = (PBConstraint) this.f.pbc(CType.EQ, 4, this.literals10, coeffs10);
solver.add(encoder.encode(pbc3));
assertSolverSat(solver);
assertThat(solver.enumerateAllModels(this.literals10)).hasSize(57).allMatch(pbc3::evaluate);
solver.reset();
coeffs10 = new int[] { 2, 2, -3, 2, -7, 2, 2, 2, 2, -2 };
final PBConstraint pbc4 = (PBConstraint) this.f.pbc(CType.EQ, -10, this.literals10, coeffs10);
solver.add(encoder.encode(pbc4));
assertSolverSat(solver);
assertThat(solver.enumerateAllModels(this.literals10)).hasSize(8).allMatch(pbc4::evaluate);
solver.reset();
coeffs10 = new int[] { 2, 2, -4, 2, -6, 2, 2, 2, 2, -2 };
final PBConstraint pbc5 = (PBConstraint) this.f.pbc(CType.EQ, -12, this.literals10, coeffs10);
solver.add(encoder.encode(pbc5));
assertSolverSat(solver);
assertThat(solver.enumerateAllModels(this.literals10)).hasSize(1).allMatch(pbc5::evaluate);
solver.reset();
}
}
}
use of org.logicng.formulas.PBConstraint in project LogicNG by logic-ng.
the class PBSolvingTest method testPBEQ.
@Test
public void testPBEQ() {
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.EQ, 5, this.literals10, coeffs10)));
assertSolverSat(solver);
assertThat(solver.enumerateAllModels(this.literals10)).hasSize(9).allMatch(model -> model.positiveVariables().size() == 2).allMatch(model -> model.positiveVariables().contains(this.f.variable("v" + 0)));
solver.reset();
solver.add(encoder.encode((PBConstraint) this.f.pbc(CType.EQ, 7, this.literals10, coeffs10)));
assertSolverSat(solver);
assertThat(solver.enumerateAllModels(this.literals10)).hasSize(36).allMatch(model -> model.positiveVariables().size() == 3).allMatch(model -> model.positiveVariables().contains(this.f.variable("v" + 0)));
solver.reset();
solver.add(encoder.encode((PBConstraint) this.f.pbc(CType.EQ, 0, this.literals10, coeffs10)));
assertSolverSat(solver);
assertThat(solver.enumerateAllModels(this.literals10)).hasSize(1);
solver.reset();
solver.add(encoder.encode((PBConstraint) this.f.pbc(CType.EQ, 1, this.literals10, coeffs10)));
assertSolverUnsat(solver);
solver.reset();
solver.add(encoder.encode((PBConstraint) this.f.pbc(CType.EQ, 22, this.literals10, coeffs10)));
assertSolverUnsat(solver);
}
}
}
Aggregations