Search in sources :

Example 51 with Literal

use of org.logicng.formulas.Literal in project LogicNG by logic-ng.

the class SATTest method readCNF.

private void readCNF(final SATSolver solver, final File file) throws IOException {
    solver.reset();
    final BufferedReader reader = new BufferedReader(new FileReader(file));
    while (reader.ready()) {
        final String line = reader.readLine();
        if (line.startsWith("p cnf")) {
            break;
        }
    }
    String[] tokens;
    final List<Literal> literals = new ArrayList<>();
    while (reader.ready()) {
        tokens = reader.readLine().split("\\s+");
        if (tokens.length >= 2) {
            assert "0".equals(tokens[tokens.length - 1]);
            literals.clear();
            for (int i = 0; i < tokens.length - 1; i++) {
                if (!tokens[i].isEmpty()) {
                    final int parsedLit = Integer.parseInt(tokens[i]);
                    final String var = "v" + Math.abs(parsedLit);
                    literals.add(parsedLit > 0 ? this.f.literal(var, true) : this.f.literal(var, false));
                }
            }
            if (!literals.isEmpty()) {
                solver.add(this.f.or(literals));
            }
        }
    }
}
Also used : Literal(org.logicng.formulas.Literal) BufferedReader(java.io.BufferedReader) ArrayList(java.util.ArrayList) FileReader(java.io.FileReader)

Example 52 with Literal

use of org.logicng.formulas.Literal in project LogicNG by logic-ng.

the class SATTest method testPBC.

@Test
public void testPBC() {
    for (final SATSolver s : this.solvers) {
        final List<Literal> lits = new ArrayList<>();
        final List<Integer> coeffs = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            lits.add(this.f.literal("x" + i, i % 2 == 0));
            coeffs.add(i + 1);
        }
        s.add(this.f.pbc(CType.GE, 10, lits, coeffs));
        assertSolverSat(s);
        s.reset();
    }
}
Also used : SATSolver(org.logicng.solvers.SATSolver) Literal(org.logicng.formulas.Literal) ArrayList(java.util.ArrayList) LogicNGTest(org.logicng.LogicNGTest) Test(org.junit.jupiter.api.Test)

Example 53 with Literal

use of org.logicng.formulas.Literal in project LogicNG by logic-ng.

the class PBEncoderTest method testCC0.

@Test
public void testCC0() {
    for (final PBEncoder encoder : this.encoders) {
        final int numLits = 100;
        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, 0, lits, coeffs));
        final SATSolver solver = MiniSat.miniSat(this.f);
        solver.add(clauses);
        assertSolverSat(solver);
        assertThat(solver.enumerateAllModels(problemLits)).hasSize(1).allMatch(m -> m.positiveVariables().isEmpty());
    }
}
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 54 with Literal

use of org.logicng.formulas.Literal in project LogicNG by logic-ng.

the class PBEncoderTest method testCCNormalized.

@Test
public void testCCNormalized() throws ParserException {
    final List<Literal> lits = new ArrayList<>();
    lits.add(this.f.literal("m", true));
    lits.add(this.f.literal("n", true));
    final List<Integer> coeffs2 = new ArrayList<>();
    coeffs2.add(2);
    coeffs2.add(2);
    final PBConstraint normCC = (PBConstraint) this.f.pbc(CType.LE, 2, lits, coeffs2);
    assertThat(this.encoders[0].encode(normCC)).containsExactly(this.f.parse("~m | ~n"));
}
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 55 with Literal

use of org.logicng.formulas.Literal 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)

Aggregations

Literal (org.logicng.formulas.Literal)115 Formula (org.logicng.formulas.Formula)51 ArrayList (java.util.ArrayList)38 Test (org.junit.jupiter.api.Test)32 Variable (org.logicng.formulas.Variable)29 FormulaFactory (org.logicng.formulas.FormulaFactory)25 PBConstraint (org.logicng.formulas.PBConstraint)21 TreeSet (java.util.TreeSet)17 SATSolver (org.logicng.solvers.SATSolver)14 Assignment (org.logicng.datastructures.Assignment)13 LNGVector (org.logicng.collections.LNGVector)12 HashMap (java.util.HashMap)11 LogicNGTest (org.logicng.LogicNGTest)11 LNGIntVector (org.logicng.collections.LNGIntVector)8 CardinalityConstraint (org.logicng.formulas.CardinalityConstraint)8 PropositionalParser (org.logicng.io.parsers.PropositionalParser)8 BufferedReader (java.io.BufferedReader)7 FileReader (java.io.FileReader)7 BinaryOperator (org.logicng.formulas.BinaryOperator)7 Not (org.logicng.formulas.Not)7