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