Search in sources :

Example 96 with SATSolver

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

Example 97 with SATSolver

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

Example 98 with SATSolver

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

Example 99 with SATSolver

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

Example 100 with SATSolver

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();
    }
}
Also used : SATSolver(org.logicng.solvers.SATSolver) MaxSATSolver(org.logicng.solvers.MaxSATSolver) FormulaFactory(org.logicng.formulas.FormulaFactory) Variable(org.logicng.formulas.Variable) TreeSet(java.util.TreeSet) Literal(org.logicng.formulas.Literal) SATPredicate(org.logicng.predicates.satisfiability.SATPredicate) CardinalityConstraint(org.logicng.formulas.CardinalityConstraint)

Aggregations

SATSolver (org.logicng.solvers.SATSolver)130 Test (org.junit.jupiter.api.Test)104 LogicNGTest (org.logicng.LogicNGTest)83 Variable (org.logicng.formulas.Variable)44 Assignment (org.logicng.datastructures.Assignment)41 Formula (org.logicng.formulas.Formula)36 CardinalityConstraint (org.logicng.formulas.CardinalityConstraint)19 FormulaFactory (org.logicng.formulas.FormulaFactory)19 ArrayList (java.util.ArrayList)16 Literal (org.logicng.formulas.Literal)14 StandardProposition (org.logicng.propositions.StandardProposition)12 SolverState (org.logicng.solvers.SolverState)10 List (java.util.List)9 TreeSet (java.util.TreeSet)8 PBConstraint (org.logicng.formulas.PBConstraint)8 ExtendedProposition (org.logicng.propositions.ExtendedProposition)8 Proposition (org.logicng.propositions.Proposition)7 LongRunningTag (org.logicng.LongRunningTag)6 Tristate (org.logicng.datastructures.Tristate)6 File (java.io.File)5