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;
}
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;
}
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)));
}
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)));
}
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;
}
Aggregations