Search in sources :

Example 41 with Literal

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

the class VariableProfileTest method testNot.

@Test
public void testNot() throws ParserException {
    final Map<Literal, Integer> expected = new HashMap<>();
    expected.put(this.f2.variable("a"), 1);
    expected.put(this.f2.variable("b"), 2);
    expected.put(this.f2.variable("c"), 3);
    final PropositionalParser p = new PropositionalParser(this.f);
    final Formula formula = p.parse("~(a & (b | c) & ((~b | ~c) => c))");
    assertThat(formula.apply(this.varProfile, true)).isEqualTo(expected);
    assertThat(formula.apply(this.varProfile, false)).isEqualTo(expected);
}
Also used : Formula(org.logicng.formulas.Formula) HashMap(java.util.HashMap) Literal(org.logicng.formulas.Literal) PropositionalParser(org.logicng.io.parsers.PropositionalParser) Test(org.junit.jupiter.api.Test)

Example 42 with Literal

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

the class VariableProfileTest method testBinaryOperator.

@Test
public void testBinaryOperator() throws ParserException {
    final Map<Literal, Integer> expected = new HashMap<>();
    expected.put(this.f2.variable("a"), 1);
    expected.put(this.f2.variable("b"), 2);
    expected.put(this.f2.variable("c"), 3);
    final PropositionalParser p = new PropositionalParser(this.f);
    final Formula impl = p.parse("(a & (b | c) & (~b | ~c)) => c");
    final Formula equiv = p.parse("(a & (b | c) & (~b | ~c)) <=> c");
    assertThat(impl.apply(this.varProfile, true)).isEqualTo(expected);
    assertThat(impl.apply(this.varProfile, false)).isEqualTo(expected);
    assertThat(equiv.apply(this.varProfile, true)).isEqualTo(expected);
    assertThat(equiv.apply(this.varProfile, false)).isEqualTo(expected);
}
Also used : Formula(org.logicng.formulas.Formula) HashMap(java.util.HashMap) Literal(org.logicng.formulas.Literal) PropositionalParser(org.logicng.io.parsers.PropositionalParser) Test(org.junit.jupiter.api.Test)

Example 43 with Literal

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

the class BackboneGenerationTest method testSimpleBackbones.

@Test
public void testSimpleBackbones() {
    final FormulaFactory f = new FormulaFactory();
    final MiniSat solver = MiniSat.miniSat(f);
    final Literal x = f.literal("x", true);
    final Literal y = f.literal("y", true);
    final Literal z = f.literal("z", true);
    final Literal u = f.literal("u", true);
    final Literal v = f.literal("v", true);
    final Collection<Variable> variables = new ArrayList<>(Arrays.asList(f.variable("x"), f.variable("y"), f.variable("z"), f.variable("u"), f.variable("v")));
    Formula formula = f.verum();
    SolverState before = solver.saveState();
    solver.add(formula);
    assertThat(solver.execute(BackboneFunction.builder().variables(Collections.emptyList()).build()).getCompleteBackbone()).isEmpty();
    solver.loadState(before);
    formula = x;
    before = solver.saveState();
    solver.add(formula);
    assertThat(solver.execute(BackboneFunction.builder().variables(variables).build()).getCompleteBackbone()).containsExactly(x);
    solver.loadState(before);
    formula = f.and(x, y);
    before = solver.saveState();
    solver.add(formula);
    assertThat(solver.execute(BackboneFunction.builder().variables(variables).build()).getCompleteBackbone()).containsExactly(x, y);
    solver.loadState(before);
    formula = f.or(x, y);
    before = solver.saveState();
    solver.add(formula);
    assertThat(solver.execute(BackboneFunction.builder().variables(variables).build()).getCompleteBackbone()).isEmpty();
    solver.loadState(before);
    formula = x.negate();
    before = solver.saveState();
    solver.add(formula);
    assertThat(solver.execute(BackboneFunction.builder().variables(variables).build()).getCompleteBackbone()).containsExactly(x.negate());
    solver.loadState(before);
    formula = f.or(f.and(x, y, z), f.and(x, y, u), f.and(x, u, z));
    before = solver.saveState();
    solver.add(formula);
    assertThat(solver.execute(BackboneFunction.builder().variables(variables).build()).getCompleteBackbone()).containsExactly(x);
    solver.loadState(before);
    formula = f.and(f.or(x, y, z), f.or(x, y, u), f.or(x, u, z));
    before = solver.saveState();
    solver.add(formula);
    assertThat(solver.execute(BackboneFunction.builder().variables(variables).build()).getCompleteBackbone()).isEmpty();
    solver.loadState(before);
    formula = f.and(f.or(x.negate(), y), x);
    before = solver.saveState();
    solver.add(formula);
    assertThat(solver.execute(BackboneFunction.builder().variables(variables).build()).getCompleteBackbone()).containsExactly(x, y);
    solver.loadState(before);
    formula = f.and(f.or(x, y), f.or(x.negate(), y));
    before = solver.saveState();
    solver.add(formula);
    assertThat(solver.execute(BackboneFunction.builder().variables(variables).build()).getCompleteBackbone()).containsExactly(y);
    solver.loadState(before);
    formula = f.and(f.and(f.or(x.negate(), y), x.negate()), f.and(z, f.or(x, y)));
    before = solver.saveState();
    solver.add(formula);
    assertThat(solver.execute(BackboneFunction.builder().variables(variables).build()).getCompleteBackbone()).containsExactly(x.negate(), y, z);
    solver.loadState(before);
    formula = f.and(f.or(x, y), f.or(u, v), z);
    solver.add(formula);
    assertThat(solver.execute(BackboneFunction.builder().variables(variables).build()).getCompleteBackbone()).containsExactly(z);
}
Also used : Formula(org.logicng.formulas.Formula) FormulaFactory(org.logicng.formulas.FormulaFactory) Variable(org.logicng.formulas.Variable) SolverState(org.logicng.solvers.SolverState) MiniSat(org.logicng.solvers.MiniSat) Literal(org.logicng.formulas.Literal) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test)

Example 44 with Literal

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

the class NegationSimplifier method minimize.

private MinimizationResult minimize(final Formula formula, final boolean topLevel) {
    final FormulaFactory f = formula.factory();
    switch(formula.type()) {
        case LITERAL:
            final Literal lit = (Literal) formula;
            return new MinimizationResult(lit, lit.negate());
        case OR:
        case AND:
            final NAryOperator nary = (NAryOperator) formula;
            final List<MinimizationResult> opResults = new ArrayList<>(nary.numberOfOperands());
            for (final Formula op : formula) {
                opResults.add(minimize(op, false));
            }
            final List<Formula> positiveOpResults = new ArrayList<>(opResults.size());
            final List<Formula> negativeOpResults = new ArrayList<>(opResults.size());
            for (final MinimizationResult result : opResults) {
                positiveOpResults.add(result.getPositiveResult());
                negativeOpResults.add(result.getNegativeResult());
            }
            final Formula smallestPositive = findSmallestPositive(formula.type(), positiveOpResults, negativeOpResults, topLevel, f);
            final Formula smallestNegative = findSmallestNegative(formula.type(), negativeOpResults, smallestPositive, topLevel, f);
            return new MinimizationResult(smallestPositive, smallestNegative);
        case FALSE:
        case TRUE:
        case NOT:
        case EQUIV:
        case IMPL:
        case PBC:
            throw new IllegalStateException("Unexpected LogicNG formula type: " + formula.type());
        default:
            throw new IllegalArgumentException("Unknown LogicNG formula type: " + formula.type());
    }
}
Also used : Formula(org.logicng.formulas.Formula) FormulaFactory(org.logicng.formulas.FormulaFactory) Literal(org.logicng.formulas.Literal) ArrayList(java.util.ArrayList) NAryOperator(org.logicng.formulas.NAryOperator)

Example 45 with Literal

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

the class FormulaRandomizer method pbc.

/**
 * Returns a random pseudo boolean constraint.
 * @return the random pseudo boolean constraint
 */
public Formula pbc() {
    final int numOps = this.random.nextInt(this.config.maximumOperandsPbc);
    final Literal[] literals = new Literal[numOps];
    final int[] coefficients = new int[numOps];
    // (positive) sum of all negative coefficients
    int minSum = 0;
    // sum of all positive coefficients
    int maxSum = 0;
    for (int i = 0; i < numOps; i++) {
        literals[i] = literal();
        coefficients[i] = this.random.nextInt(this.config.maximumCoefficientPbc) + 1;
        if (this.random.nextDouble() < this.coefficientNegativeProbability) {
            minSum += coefficients[i];
            coefficients[i] = -coefficients[i];
        } else {
            maxSum += coefficients[i];
        }
    }
    final CType type = cType();
    final int rhs = this.random.nextInt(maxSum + minSum + 1) - minSum;
    final Formula pbc = this.f.pbc(type, rhs, literals, coefficients);
    if (pbc.isConstantFormula()) {
        return pbc();
    }
    return pbc;
}
Also used : Formula(org.logicng.formulas.Formula) CType(org.logicng.formulas.CType) Literal(org.logicng.formulas.Literal)

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