Search in sources :

Example 1 with Hypergraph

use of org.logicng.graphs.datastructures.Hypergraph 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 2 with Hypergraph

use of org.logicng.graphs.datastructures.Hypergraph in project LogicNG by logic-ng.

the class HypergraphGenerator method fromCNF.

/**
 * Generates a hypergraph from a CNF given as a list of clauses.  Each variable is represented by a node in the
 * hypergraph, each clause is represented by a hyperedge between all variables of the clause.
 * @param cnf the list of clauses of the CNF for the hypergraph
 * @return the hypergraph for the CNF formula
 */
public static Hypergraph<Variable> fromCNF(final List<Formula> cnf) {
    final Hypergraph<Variable> hypergraph = new Hypergraph<>();
    final Map<Variable, HypergraphNode<Variable>> nodes = new HashMap<>();
    for (final Formula clause : cnf) {
        switch(clause.type()) {
            case PBC:
            case EQUIV:
            case IMPL:
            case NOT:
            case AND:
                throw new IllegalStateException("Unexpected element in clause: " + clause);
            case LITERAL:
            case OR:
                addClause(clause, hypergraph, nodes);
                break;
        }
    }
    return hypergraph;
}
Also used : Hypergraph(org.logicng.graphs.datastructures.Hypergraph) Formula(org.logicng.formulas.Formula) Variable(org.logicng.formulas.Variable) HashMap(java.util.HashMap) HypergraphNode(org.logicng.graphs.datastructures.HypergraphNode)

Example 3 with Hypergraph

use of org.logicng.graphs.datastructures.Hypergraph in project LogicNG by logic-ng.

the class HypergraphGenerator method fromCNF.

/**
 * Generates a hypergraph from a CNF.  Each variable is represented by a node in the hypergraph, each clause
 * is represented by a hyperedge between all variables of the clause.
 * @param cnf the CNF formula for the hypergraph
 * @return the hypergraph for the CNF formula
 */
public static Hypergraph<Variable> fromCNF(final Formula cnf) {
    if (!cnf.holds(CNFPredicate.get())) {
        throw new IllegalArgumentException("Cannot generate a hypergraph from a non-cnf formula");
    }
    final Hypergraph<Variable> hypergraph = new Hypergraph<>();
    final Map<Variable, HypergraphNode<Variable>> nodes = new HashMap<>();
    switch(cnf.type()) {
        case PBC:
        case EQUIV:
        case IMPL:
        case NOT:
            throw new IllegalStateException("Unexpected element in CNF: " + cnf);
        case LITERAL:
        case OR:
            addClause(cnf, hypergraph, nodes);
            break;
        case AND:
            for (final Formula clause : cnf) {
                addClause(clause, hypergraph, nodes);
            }
            break;
    }
    return hypergraph;
}
Also used : Hypergraph(org.logicng.graphs.datastructures.Hypergraph) Formula(org.logicng.formulas.Formula) Variable(org.logicng.formulas.Variable) HashMap(java.util.HashMap) HypergraphNode(org.logicng.graphs.datastructures.HypergraphNode)

Aggregations

HashMap (java.util.HashMap)3 Formula (org.logicng.formulas.Formula)3 Variable (org.logicng.formulas.Variable)3 Hypergraph (org.logicng.graphs.datastructures.Hypergraph)3 HypergraphNode (org.logicng.graphs.datastructures.HypergraphNode)3 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Comparator (java.util.Comparator)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 SortedSet (java.util.SortedSet)1 TreeSet (java.util.TreeSet)1 Collectors (java.util.stream.Collectors)1 HypergraphGenerator (org.logicng.graphs.generators.HypergraphGenerator)1