Search in sources :

Example 1 with HypergraphNode

use of org.logicng.graphs.datastructures.HypergraphNode 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 HypergraphNode

use of org.logicng.graphs.datastructures.HypergraphNode 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 HypergraphNode

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

the class HypergraphGeneratorTest method testCNF.

@Test
public void testCNF() throws ParserException {
    final FormulaFactory f = new FormulaFactory();
    final PropositionalParser p = new PropositionalParser(f);
    assertThat(HypergraphGenerator.fromCNF(p.parse("$false")).nodes()).isEmpty();
    assertThat(HypergraphGenerator.fromCNF(p.parse("$false")).edges()).isEmpty();
    assertThat(HypergraphGenerator.fromCNF(p.parse("$true")).nodes()).isEmpty();
    assertThat(HypergraphGenerator.fromCNF(p.parse("$true")).edges()).isEmpty();
    Hypergraph<Variable> hypergraph = HypergraphGenerator.fromCNF(p.parse("A"));
    HypergraphNode<Variable> nodeA = new HypergraphNode<>(hypergraph, f.variable("A"));
    assertThat(hypergraph.nodes()).containsExactly(nodeA);
    assertThat(hypergraph.edges()).containsExactly(new HypergraphEdge<>(Collections.singletonList(nodeA)));
    hypergraph = HypergraphGenerator.fromCNF(p.parse("A | B | ~C"));
    nodeA = new HypergraphNode<>(hypergraph, f.variable("A"));
    HypergraphNode<Variable> nodeB = new HypergraphNode<>(hypergraph, f.variable("B"));
    HypergraphNode<Variable> nodeC = new HypergraphNode<>(hypergraph, f.variable("C"));
    assertThat(hypergraph.nodes()).containsExactlyInAnyOrder(nodeA, nodeB, nodeC);
    assertThat(hypergraph.edges()).containsExactlyInAnyOrder(new HypergraphEdge<>(Arrays.asList(nodeA, nodeB, nodeC)));
    hypergraph = HypergraphGenerator.fromCNF(p.parse("(A | B | ~C) & (B | ~D) & (C | ~E) & (~B | ~D | E) & X & ~Y"));
    nodeA = new HypergraphNode<>(hypergraph, f.variable("A"));
    nodeB = new HypergraphNode<>(hypergraph, f.variable("B"));
    nodeC = new HypergraphNode<>(hypergraph, f.variable("C"));
    final HypergraphNode<Variable> nodeD = new HypergraphNode<>(hypergraph, f.variable("D"));
    final HypergraphNode<Variable> nodeE = new HypergraphNode<>(hypergraph, f.variable("E"));
    final HypergraphNode<Variable> nodeX = new HypergraphNode<>(hypergraph, f.variable("X"));
    final HypergraphNode<Variable> nodeY = new HypergraphNode<>(hypergraph, f.variable("Y"));
    assertThat(hypergraph.nodes()).containsExactlyInAnyOrder(nodeA, nodeB, nodeC, nodeD, nodeE, nodeX, nodeY);
    assertThat(hypergraph.edges()).containsExactlyInAnyOrder(new HypergraphEdge<>(Arrays.asList(nodeA, nodeB, nodeC)), new HypergraphEdge<>(Arrays.asList(nodeB, nodeD)), new HypergraphEdge<>(Arrays.asList(nodeC, nodeE)), new HypergraphEdge<>(Arrays.asList(nodeB, nodeD, nodeE)), new HypergraphEdge<>(Collections.singletonList(nodeX)), new HypergraphEdge<>(Collections.singletonList(nodeY)));
}
Also used : FormulaFactory(org.logicng.formulas.FormulaFactory) Variable(org.logicng.formulas.Variable) HypergraphNode(org.logicng.graphs.datastructures.HypergraphNode) PropositionalParser(org.logicng.io.parsers.PropositionalParser) Test(org.junit.jupiter.api.Test)

Example 4 with HypergraphNode

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

the class HypergraphGeneratorTest method testCNFFromList.

@Test
public void testCNFFromList() throws ParserException {
    final FormulaFactory f = new FormulaFactory();
    final PropositionalParser p = new PropositionalParser(f);
    assertThat(HypergraphGenerator.fromCNF(Collections.singletonList(p.parse("$false"))).nodes()).isEmpty();
    assertThat(HypergraphGenerator.fromCNF(Collections.singletonList(p.parse("$false"))).edges()).isEmpty();
    assertThat(HypergraphGenerator.fromCNF(Collections.singletonList(p.parse("$true"))).nodes()).isEmpty();
    assertThat(HypergraphGenerator.fromCNF(Collections.singletonList(p.parse("$true"))).edges()).isEmpty();
    Hypergraph<Variable> hypergraph = HypergraphGenerator.fromCNF(Collections.singletonList(p.parse("A")));
    HypergraphNode<Variable> nodeA = new HypergraphNode<>(hypergraph, f.variable("A"));
    assertThat(hypergraph.nodes()).containsExactly(nodeA);
    assertThat(hypergraph.edges()).containsExactly(new HypergraphEdge<>(Collections.singletonList(nodeA)));
    hypergraph = HypergraphGenerator.fromCNF(Collections.singletonList(p.parse("A | B | ~C")));
    nodeA = new HypergraphNode<>(hypergraph, f.variable("A"));
    HypergraphNode<Variable> nodeB = new HypergraphNode<>(hypergraph, f.variable("B"));
    HypergraphNode<Variable> nodeC = new HypergraphNode<>(hypergraph, f.variable("C"));
    assertThat(hypergraph.nodes()).containsExactlyInAnyOrder(nodeA, nodeB, nodeC);
    assertThat(hypergraph.edges()).containsExactlyInAnyOrder(new HypergraphEdge<>(Arrays.asList(nodeA, nodeB, nodeC)));
    hypergraph = HypergraphGenerator.fromCNF(Arrays.asList(p.parse("(A | B | ~C)"), p.parse("(B | ~D)"), p.parse("(C | ~E)"), p.parse("(~B | ~D | E)"), p.parse("X"), p.parse("~Y")));
    nodeA = new HypergraphNode<>(hypergraph, f.variable("A"));
    nodeB = new HypergraphNode<>(hypergraph, f.variable("B"));
    nodeC = new HypergraphNode<>(hypergraph, f.variable("C"));
    HypergraphNode<Variable> nodeD = new HypergraphNode<>(hypergraph, f.variable("D"));
    HypergraphNode<Variable> nodeE = new HypergraphNode<>(hypergraph, f.variable("E"));
    HypergraphNode<Variable> nodeX = new HypergraphNode<>(hypergraph, f.variable("X"));
    HypergraphNode<Variable> nodeY = new HypergraphNode<>(hypergraph, f.variable("Y"));
    assertThat(hypergraph.nodes()).containsExactlyInAnyOrder(nodeA, nodeB, nodeC, nodeD, nodeE, nodeX, nodeY);
    assertThat(hypergraph.edges()).containsExactlyInAnyOrder(new HypergraphEdge<>(Arrays.asList(nodeA, nodeB, nodeC)), new HypergraphEdge<>(Arrays.asList(nodeB, nodeD)), new HypergraphEdge<>(Arrays.asList(nodeC, nodeE)), new HypergraphEdge<>(Arrays.asList(nodeB, nodeD, nodeE)), new HypergraphEdge<>(Collections.singletonList(nodeX)), new HypergraphEdge<>(Collections.singletonList(nodeY)));
    hypergraph = HypergraphGenerator.fromCNF(p.parse("(A | B | ~C)"), p.parse("(B | ~D)"), p.parse("(C | ~E)"), p.parse("(~B | ~D | E)"), p.parse("X"), p.parse("~Y"));
    nodeA = new HypergraphNode<>(hypergraph, f.variable("A"));
    nodeB = new HypergraphNode<>(hypergraph, f.variable("B"));
    nodeC = new HypergraphNode<>(hypergraph, f.variable("C"));
    nodeD = new HypergraphNode<>(hypergraph, f.variable("D"));
    nodeE = new HypergraphNode<>(hypergraph, f.variable("E"));
    nodeX = new HypergraphNode<>(hypergraph, f.variable("X"));
    nodeY = new HypergraphNode<>(hypergraph, f.variable("Y"));
    assertThat(hypergraph.nodes()).containsExactlyInAnyOrder(nodeA, nodeB, nodeC, nodeD, nodeE, nodeX, nodeY);
    assertThat(hypergraph.edges()).containsExactlyInAnyOrder(new HypergraphEdge<>(Arrays.asList(nodeA, nodeB, nodeC)), new HypergraphEdge<>(Arrays.asList(nodeB, nodeD)), new HypergraphEdge<>(Arrays.asList(nodeC, nodeE)), new HypergraphEdge<>(Arrays.asList(nodeB, nodeD, nodeE)), new HypergraphEdge<>(Collections.singletonList(nodeX)), new HypergraphEdge<>(Collections.singletonList(nodeY)));
}
Also used : FormulaFactory(org.logicng.formulas.FormulaFactory) Variable(org.logicng.formulas.Variable) HypergraphNode(org.logicng.graphs.datastructures.HypergraphNode) PropositionalParser(org.logicng.io.parsers.PropositionalParser) Test(org.junit.jupiter.api.Test)

Example 5 with HypergraphNode

use of org.logicng.graphs.datastructures.HypergraphNode 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

Variable (org.logicng.formulas.Variable)6 HypergraphNode (org.logicng.graphs.datastructures.HypergraphNode)6 HashMap (java.util.HashMap)3 Formula (org.logicng.formulas.Formula)3 Hypergraph (org.logicng.graphs.datastructures.Hypergraph)3 Test (org.junit.jupiter.api.Test)2 FormulaFactory (org.logicng.formulas.FormulaFactory)2 PropositionalParser (org.logicng.io.parsers.PropositionalParser)2 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Comparator (java.util.Comparator)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)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