Search in sources :

Example 86 with Formula

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

the class NegationSimplifier method findSmallestNegative.

private Formula findSmallestNegative(final FType type, final List<Formula> negativeOpResults, final Formula smallestPositive, final boolean topLevel, final FormulaFactory f) {
    final Formula negation = f.not(smallestPositive);
    final Formula flipped = f.naryOperator(dual(type), negativeOpResults);
    return getSmallestFormula(Arrays.asList(negation, flipped), topLevel);
}
Also used : Formula(org.logicng.formulas.Formula)

Example 87 with Formula

use of org.logicng.formulas.Formula 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 88 with Formula

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

the class FormulaRandomizer method or.

/**
 * Returns a random disjunction with a given maximal depth.
 * @param maxDepth the maximal depth
 * @return the random disjunction
 */
public Formula or(final int maxDepth) {
    if (maxDepth == 0) {
        return atom();
    }
    final Formula[] operands = new Formula[2 + this.random.nextInt(this.config.maximumOperandsOr - 2)];
    for (int i = 0; i < operands.length; i++) {
        operands[i] = formula(maxDepth - 1);
    }
    final Formula formula = this.f.or(operands);
    if (formula.type() != FType.OR) {
        return or(maxDepth);
    }
    return formula;
}
Also used : Formula(org.logicng.formulas.Formula)

Example 89 with Formula

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

Example 90 with Formula

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

the class SATTest method testSelectionOrderSimple01.

@Test
public void testSelectionOrderSimple01() throws ParserException {
    for (final SATSolver solver : this.solvers) {
        final Formula formula = this.parser.parse("~(x <=> y)");
        solver.add(formula);
        List<Literal> selectionOrder = Arrays.asList(this.X, this.Y);
        assertThat(solver.satWithSelectionOrder(selectionOrder)).isEqualTo(Tristate.TRUE);
        Assignment assignment = solver.model();
        assertThat(assignment.literals()).containsExactlyInAnyOrder(this.X, this.NY);
        testLocalMinimum(solver, assignment, selectionOrder);
        testHighestLexicographicalAssignment(solver, assignment, selectionOrder);
        solver.setSolverToUndef();
        selectionOrder = Arrays.asList(this.Y, this.X);
        assertThat(solver.satWithSelectionOrder(selectionOrder)).isEqualTo(Tristate.TRUE);
        assignment = solver.model();
        assertThat(assignment.literals()).containsExactlyInAnyOrder(this.Y, this.NX);
        testLocalMinimum(solver, assignment, selectionOrder);
        testHighestLexicographicalAssignment(solver, assignment, selectionOrder);
        solver.setSolverToUndef();
        selectionOrder = Collections.singletonList(this.NX);
        assertThat(solver.sat(selectionOrder)).isEqualTo(Tristate.TRUE);
        assignment = solver.model();
        assertThat(assignment.literals()).containsExactlyInAnyOrder(this.Y, this.NX);
        testLocalMinimum(solver, assignment, selectionOrder);
        testHighestLexicographicalAssignment(solver, assignment, selectionOrder);
        solver.setSolverToUndef();
        selectionOrder = Arrays.asList(this.NY, this.NX);
        assertThat(solver.satWithSelectionOrder(selectionOrder)).isEqualTo(Tristate.TRUE);
        assignment = solver.model();
        assertThat(assignment.literals()).containsExactlyInAnyOrder(this.X, this.NY);
        testLocalMinimum(solver, assignment, selectionOrder);
        testHighestLexicographicalAssignment(solver, assignment, selectionOrder);
        solver.reset();
    }
}
Also used : SATSolver(org.logicng.solvers.SATSolver) Assignment(org.logicng.datastructures.Assignment) Formula(org.logicng.formulas.Formula) Literal(org.logicng.formulas.Literal) LogicNGTest(org.logicng.LogicNGTest) Test(org.junit.jupiter.api.Test)

Aggregations

Formula (org.logicng.formulas.Formula)349 Test (org.junit.jupiter.api.Test)190 FormulaFactory (org.logicng.formulas.FormulaFactory)129 ArrayList (java.util.ArrayList)58 Variable (org.logicng.formulas.Variable)55 Literal (org.logicng.formulas.Literal)54 Assignment (org.logicng.datastructures.Assignment)50 PropositionalParser (org.logicng.io.parsers.PropositionalParser)50 PBConstraint (org.logicng.formulas.PBConstraint)45 SATSolver (org.logicng.solvers.SATSolver)36 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)25 CardinalityConstraint (org.logicng.formulas.CardinalityConstraint)25 MethodSource (org.junit.jupiter.params.provider.MethodSource)24 TreeSet (java.util.TreeSet)21 BDDKernel (org.logicng.knowledgecompilation.bdds.jbuddy.BDDKernel)20 TautologyPredicate (org.logicng.predicates.satisfiability.TautologyPredicate)20 HashMap (java.util.HashMap)17 LinkedHashSet (java.util.LinkedHashSet)17 List (java.util.List)17 LogicNGTest (org.logicng.LogicNGTest)16