Search in sources :

Example 6 with Literal

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

the class LiteralProfileFunction method nonCachingRecursion.

/**
 * Recursive function for the non-caching literal profile computation.
 * @param formula the formula
 * @param map     the literal profile
 */
private static void nonCachingRecursion(final Formula formula, final Map<Literal, Integer> map) {
    if (formula.type() == FType.LITERAL) {
        final Literal lit = (Literal) formula;
        map.merge(lit, 1, Integer::sum);
    } else if (formula.type() == FType.PBC) {
        for (final Literal l : formula.literals()) {
            nonCachingRecursion(l, map);
        }
    } else {
        for (final Formula op : formula) {
            nonCachingRecursion(op, map);
        }
    }
}
Also used : Formula(org.logicng.formulas.Formula) Literal(org.logicng.formulas.Literal)

Example 7 with Literal

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

the class MinimumPrimeImplicantFunction method apply.

@Override
public SortedSet<Literal> apply(final Formula formula, final boolean cache) {
    final Formula nnf = formula.nnf();
    final Map<Variable, Literal> newVar2oldLit = new HashMap<>();
    final LiteralSubstitution substitution = new LiteralSubstitution();
    for (final Literal literal : nnf.literals()) {
        final Variable newVar = formula.factory().variable(literal.name() + (literal.phase() ? POS : NEG));
        newVar2oldLit.put(newVar, literal);
        substitution.addSubstitution(literal, newVar);
    }
    final Formula substitued = nnf.transform(substitution);
    final SATSolver solver = MiniSat.miniSat(formula.factory(), MiniSatConfig.builder().cnfMethod(MiniSatConfig.CNFMethod.PG_ON_SOLVER).build());
    solver.add(substitued);
    for (final Literal literal : newVar2oldLit.values()) {
        if (literal.phase() && newVar2oldLit.containsValue(literal.negate())) {
            solver.add(formula.factory().amo(formula.factory().variable(literal.name() + POS), formula.factory().variable(literal.name() + NEG)));
        }
    }
    if (solver.sat() != Tristate.TRUE) {
        return null;
    }
    final Assignment minimumModel = solver.execute(OptimizationFunction.minimize(newVar2oldLit.keySet()));
    final SortedSet<Literal> primeImplicant = new TreeSet<>();
    for (final Variable variable : minimumModel.positiveVariables()) {
        final Literal literal = newVar2oldLit.get(variable);
        if (literal != null) {
            primeImplicant.add(literal);
        }
    }
    return primeImplicant;
}
Also used : SATSolver(org.logicng.solvers.SATSolver) Assignment(org.logicng.datastructures.Assignment) Formula(org.logicng.formulas.Formula) LiteralSubstitution(org.logicng.transformations.LiteralSubstitution) Variable(org.logicng.formulas.Variable) HashMap(java.util.HashMap) TreeSet(java.util.TreeSet) Literal(org.logicng.formulas.Literal)

Example 8 with Literal

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

the class VariableProfileFunction method nonCachingRecursion.

/**
 * Recursive function for the non-caching variable profile computation.
 * @param formula the formula
 * @param map     the variable profile
 */
private static void nonCachingRecursion(final Formula formula, final Map<Variable, Integer> map) {
    if (formula.type() == FType.LITERAL) {
        final Literal lit = (Literal) formula;
        map.merge(lit.variable(), 1, Integer::sum);
    } else if (formula.type() == FType.PBC) {
        for (final Literal l : formula.literals()) {
            nonCachingRecursion(l.variable(), map);
        }
    } else {
        for (final Formula op : formula) {
            nonCachingRecursion(op, map);
        }
    }
}
Also used : Formula(org.logicng.formulas.Formula) Literal(org.logicng.formulas.Literal)

Example 9 with Literal

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

the class VariablesFunction method apply.

@Override
public SortedSet<Variable> apply(final Formula formula, final boolean cache) {
    final Object cached = formula.functionCacheEntry(VARIABLES);
    if (cached != null) {
        return (SortedSet<Variable>) cached;
    }
    SortedSet<Variable> result = new TreeSet<>();
    switch(formula.type()) {
        case FALSE:
        case TRUE:
            result = new TreeSet<>();
            break;
        case LITERAL:
            final Literal lit = (Literal) formula;
            result.add(lit.variable());
            break;
        case NOT:
            final Not not = (Not) formula;
            result = apply(not.operand(), cache);
            break;
        case IMPL:
        case EQUIV:
            final BinaryOperator binary = (BinaryOperator) formula;
            result.addAll(apply(binary.left(), cache));
            result.addAll(apply(binary.right(), cache));
            break;
        case OR:
        case AND:
            final NAryOperator nary = (NAryOperator) formula;
            for (final Formula op : nary) {
                result.addAll(apply(op, cache));
            }
            break;
        case PBC:
            final PBConstraint pbc = (PBConstraint) formula;
            result = FormulaHelper.variables(pbc.literals());
            break;
        default:
            throw new IllegalStateException("Unknown formula type " + formula.type());
    }
    result = Collections.unmodifiableSortedSet(result);
    if (cache) {
        formula.setFunctionCacheEntry(VARIABLES, result);
    }
    return result;
}
Also used : Variable(org.logicng.formulas.Variable) SortedSet(java.util.SortedSet) Formula(org.logicng.formulas.Formula) Not(org.logicng.formulas.Not) TreeSet(java.util.TreeSet) Literal(org.logicng.formulas.Literal) BinaryOperator(org.logicng.formulas.BinaryOperator) NAryOperator(org.logicng.formulas.NAryOperator) PBConstraint(org.logicng.formulas.PBConstraint)

Example 10 with Literal

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

the class CCCardinalityNetworks method buildALK.

void buildALK(final EncodingResult result, final Variable[] vars, final int rhs) {
    result.reset();
    final LNGVector<Literal> input = new LNGVector<>();
    final LNGVector<Literal> output = new LNGVector<>();
    final int newRHS = vars.length - rhs;
    if (newRHS > vars.length / 2) {
        final int geq = vars.length - newRHS;
        for (final Variable v : vars) {
            input.push(v);
        }
        this.sorting.sort(geq, input, result, output, OUTPUT_TO_INPUT);
        for (int i = 0; i < geq; i++) {
            result.addClause(output.get(i));
        }
    } else {
        for (final Variable v : vars) {
            input.push(v.negate());
        }
        this.sorting.sort(newRHS + 1, input, result, output, INPUT_TO_OUTPUT);
        assert output.size() > newRHS;
        result.addClause(output.get(newRHS).negate());
    }
}
Also used : Variable(org.logicng.formulas.Variable) Literal(org.logicng.formulas.Literal) LNGVector(org.logicng.collections.LNGVector)

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