Search in sources :

Example 31 with Formula

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

the class DFSOrdering method dfs.

private void dfs(final Formula formula, final LinkedHashSet<Variable> variables) {
    switch(formula.type()) {
        case LITERAL:
            variables.add(((Literal) formula).variable());
            break;
        case NOT:
            dfs(((Not) formula).operand(), variables);
            break;
        case IMPL:
        case EQUIV:
            final BinaryOperator op = (BinaryOperator) formula;
            dfs(op.left(), variables);
            dfs(op.right(), variables);
            break;
        case AND:
        case OR:
            for (final Formula o : formula) {
                dfs(o, variables);
            }
            break;
        case PBC:
            final PBConstraint pbc = (PBConstraint) formula;
            for (final Literal lit : pbc.operands()) {
                variables.add(lit.variable());
            }
            break;
    }
}
Also used : Formula(org.logicng.formulas.Formula) Literal(org.logicng.formulas.Literal) BinaryOperator(org.logicng.formulas.BinaryOperator) PBConstraint(org.logicng.formulas.PBConstraint)

Example 32 with Formula

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

the class ForceOrdering method getOrder.

@Override
public List<Variable> getOrder(final Formula formula) {
    final SortedSet<Variable> originalVariables = new TreeSet<>(formula.variables());
    final Formula nnf = formula.nnf();
    originalVariables.addAll(nnf.variables());
    final Formula cnf = nnf.cnf();
    final Hypergraph<Variable> hypergraph = HypergraphGenerator.fromCNF(cnf);
    final Map<Variable, HypergraphNode<Variable>> nodes = new HashMap<>();
    for (final HypergraphNode<Variable> node : hypergraph.nodes()) {
        nodes.put(node.content(), node);
    }
    final List<Variable> ordering = force(cnf, hypergraph, nodes).stream().filter(originalVariables::contains).collect(Collectors.toList());
    originalVariables.stream().filter(v -> !ordering.contains(v)).forEach(ordering::add);
    return ordering;
}
Also used : Arrays(java.util.Arrays) HypergraphGenerator(org.logicng.graphs.generators.HypergraphGenerator) SortedSet(java.util.SortedSet) Hypergraph(org.logicng.graphs.datastructures.Hypergraph) Formula(org.logicng.formulas.Formula) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) List(java.util.List) Variable(org.logicng.formulas.Variable) Map(java.util.Map) HypergraphNode(org.logicng.graphs.datastructures.HypergraphNode) Comparator(java.util.Comparator) Formula(org.logicng.formulas.Formula) Variable(org.logicng.formulas.Variable) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TreeSet(java.util.TreeSet) HypergraphNode(org.logicng.graphs.datastructures.HypergraphNode)

Example 33 with Formula

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

the class FormulaDotFileWriter method generateNaryDotString.

private static void generateNaryDotString(final NAryOperator formula, final StringBuilder sb, final Map<Formula, Integer> ids, final String op) {
    for (final Formula operand : formula) {
        if (!ids.containsKey(operand)) {
            generateDotString(operand, sb, ids);
        }
    }
    final int id = ids.size();
    ids.put(formula, id);
    sb.append("  id").append(id).append(" [label=\"").append(op).append(String.format("\"];%n"));
    for (final Formula operand : formula) {
        sb.append("  id").append(id).append(" -> id").append(ids.get(operand)).append(String.format(";%n"));
    }
}
Also used : Formula(org.logicng.formulas.Formula) PBConstraint(org.logicng.formulas.PBConstraint)

Example 34 with Formula

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

the class FormulaWriter method write.

/**
 * Writes a given formula to a file  with a given formula formatter.
 * @param file              the file
 * @param formula           the formula to write
 * @param splitAndMultiline indicates whether - if the formula is an conjunction - the single operands should be
 *                          written to different lines without a conjoining operator
 * @param formatter         the formatter for the formula
 * @throws IOException if there was a problem writing the file
 */
public static void write(final File file, final Formula formula, final boolean splitAndMultiline, final FormulaStringRepresentation formatter) throws IOException {
    final StringBuilder sb = new StringBuilder();
    if (splitAndMultiline && formula.type() == FType.AND) {
        for (final Formula f : formula) {
            sb.append(formatter.toString(f)).append(System.lineSeparator());
        }
    } else {
        sb.append(formatter.toString(formula));
    }
    try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8))) {
        writer.append(sb);
        writer.flush();
    }
}
Also used : Formula(org.logicng.formulas.Formula) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) BufferedWriter(java.io.BufferedWriter)

Example 35 with Formula

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

the class BDDCNFFunction method apply.

@Override
public Formula apply(final BDD bdd) {
    final BDDKernel kernel = bdd.underlyingKernel();
    final List<byte[]> unsatPaths = new BDDOperations(kernel).allUnsat(bdd.index());
    final List<Formula> clauses = new ArrayList<>();
    List<Formula> literals;
    for (final byte[] path : unsatPaths) {
        literals = new ArrayList<>();
        for (int i = 0; i < path.length; i++) {
            if (path[i] == 0) {
                literals.add(kernel.getVariableForIndex(i));
            } else if (path[i] == 1) {
                literals.add(kernel.getVariableForIndex(i).negate());
            }
        }
        clauses.add(kernel.factory().or(literals));
    }
    return kernel.factory().and(clauses);
}
Also used : Formula(org.logicng.formulas.Formula) BDDKernel(org.logicng.knowledgecompilation.bdds.jbuddy.BDDKernel) BDDOperations(org.logicng.knowledgecompilation.bdds.jbuddy.BDDOperations) ArrayList(java.util.ArrayList)

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