Search in sources :

Example 11 with PBConstraint

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();
    }
}
Also used : Literal(org.logicng.formulas.Literal) ArrayList(java.util.ArrayList) PBConstraint(org.logicng.formulas.PBConstraint) Test(org.junit.jupiter.api.Test) LogicNGTest(org.logicng.LogicNGTest)

Example 12 with PBConstraint

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);
}
Also used : SATSolver(org.logicng.solvers.SATSolver) Formula(org.logicng.formulas.Formula) Variable(org.logicng.formulas.Variable) PBConstraint(org.logicng.formulas.PBConstraint)

Example 13 with PBConstraint

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);
    }
}
Also used : SATSolver(org.logicng.solvers.SATSolver) Formula(org.logicng.formulas.Formula) Variable(org.logicng.formulas.Variable) Literal(org.logicng.formulas.Literal) ArrayList(java.util.ArrayList) PBConstraint(org.logicng.formulas.PBConstraint) Test(org.junit.jupiter.api.Test) LogicNGTest(org.logicng.LogicNGTest)

Example 14 with PBConstraint

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();
        }
    }
}
Also used : SATSolver(org.logicng.solvers.SATSolver) PBConstraint(org.logicng.formulas.PBConstraint) Test(org.junit.jupiter.api.Test) LogicNGTest(org.logicng.LogicNGTest)

Example 15 with PBConstraint

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);
        }
    }
}
Also used : SATSolver(org.logicng.solvers.SATSolver) Test(org.junit.jupiter.api.Test) LogicNGTest(org.logicng.LogicNGTest) List(java.util.List) PBConstraint(org.logicng.formulas.PBConstraint) MiniSatConfig(org.logicng.solvers.sat.MiniSatConfig) CType(org.logicng.formulas.CType) Variable(org.logicng.formulas.Variable) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) FormulaFactory(org.logicng.formulas.FormulaFactory) SATSolver(org.logicng.solvers.SATSolver) Assignment(org.logicng.datastructures.Assignment) MiniSat(org.logicng.solvers.MiniSat) PBConstraint(org.logicng.formulas.PBConstraint) Test(org.junit.jupiter.api.Test) LogicNGTest(org.logicng.LogicNGTest)

Aggregations

PBConstraint (org.logicng.formulas.PBConstraint)35 Formula (org.logicng.formulas.Formula)22 Test (org.junit.jupiter.api.Test)21 Literal (org.logicng.formulas.Literal)15 Variable (org.logicng.formulas.Variable)11 ArrayList (java.util.ArrayList)9 LogicNGTest (org.logicng.LogicNGTest)9 FormulaFactory (org.logicng.formulas.FormulaFactory)9 BinaryOperator (org.logicng.formulas.BinaryOperator)8 Not (org.logicng.formulas.Not)8 SATSolver (org.logicng.solvers.SATSolver)8 NAryOperator (org.logicng.formulas.NAryOperator)6 CardinalityConstraint (org.logicng.formulas.CardinalityConstraint)5 List (java.util.List)3 Set (java.util.Set)3 CNFFactorization (org.logicng.transformations.cnf.CNFFactorization)3 BigInteger (java.math.BigInteger)2 SortedSet (java.util.SortedSet)2 TreeSet (java.util.TreeSet)2 EncodingResult (org.logicng.datastructures.EncodingResult)2