use of org.logicng.formulas.PBConstraint in project LogicNG by logic-ng.
the class FormulaRandomizerTest method testExo.
@Test
public void testExo() {
final FormulaRandomizer random = new FormulaRandomizer(this.f, FormulaRandomizerConfig.builder().seed(4242).maximumCoefficientPbc(10).build());
for (int i = 0; i < 100; i++) {
final Formula formula = random.exo();
assertThat(formula).isInstanceOf(PBConstraint.class);
final PBConstraint amo = (PBConstraint) formula;
assertThat(amo.isCC()).isTrue();
assertThat(amo.rhs()).isEqualTo(1);
assertThat(amo.comparator()).isEqualTo(CType.EQ);
}
}
use of org.logicng.formulas.PBConstraint in project LogicNG by logic-ng.
the class FormulaDotFileWriter method generateDotString.
/**
* Generates the dot string for a formula
* @param formula the formula
* @param sb the current string builder
* @param ids the current ID mapping
*/
private static void generateDotString(final Formula formula, final StringBuilder sb, final Map<Formula, Integer> ids) {
switch(formula.type()) {
case FALSE:
sb.append(String.format(" false;%n"));
break;
case TRUE:
sb.append(String.format(" true;%n"));
break;
case LITERAL:
break;
case PBC:
final int id = ids.size();
ids.put(formula, id);
sb.append(" id").append(id).append(" [label=\"").append(formula).append(String.format("\"];%n"));
for (final Formula operand : ((PBConstraint) formula).operands()) {
sb.append(" id").append(id).append(" -> id").append(ids.get(operand)).append(String.format(";%n"));
}
break;
case NOT:
generateNotDotString((Not) formula, sb, ids);
break;
case IMPL:
generateBinaryDotString((BinaryOperator) formula, sb, ids, "⇒", true);
break;
case EQUIV:
generateBinaryDotString((BinaryOperator) formula, sb, ids, "⇔", true);
break;
case AND:
generateNaryDotString((NAryOperator) formula, sb, ids, "∧");
break;
case OR:
generateNaryDotString((NAryOperator) formula, sb, ids, "∨");
break;
default:
throw new IllegalArgumentException("Cannot write the formula type " + formula.type());
}
}
use of org.logicng.formulas.PBConstraint in project LogicNG by logic-ng.
the class NumberOfNodesFunction method apply.
@Override
public Long apply(final Formula formula, final boolean cache) {
final Object cached = formula.functionCacheEntry(NUMBER_OF_NODES);
if (cached != null) {
return (Long) cached;
}
long result;
switch(formula.type()) {
case FALSE:
case TRUE:
case LITERAL:
result = 1L;
break;
case NOT:
result = apply(((Not) formula).operand(), cache) + 1L;
break;
case IMPL:
case EQUIV:
final BinaryOperator binary = (BinaryOperator) formula;
result = apply(binary.left(), cache) + apply(binary.right(), cache) + 1L;
break;
case OR:
case AND:
final NAryOperator nary = (NAryOperator) formula;
result = 1L;
for (final Formula op : nary) {
result += apply(op, cache);
}
break;
case PBC:
final PBConstraint pbc = (PBConstraint) formula;
result = 1L + pbc.operands().length;
break;
default:
throw new IllegalStateException("Unknown formula type " + formula.type());
}
if (cache) {
formula.setFunctionCacheEntry(NUMBER_OF_NODES, result);
}
return result;
}
use of org.logicng.formulas.PBConstraint in project LogicNG by logic-ng.
the class LiteralsFunction method apply.
@Override
public SortedSet<Literal> apply(final Formula formula, final boolean cache) {
final Object cached = formula.functionCacheEntry(LITERALS);
if (cached != null) {
return (SortedSet<Literal>) cached;
}
SortedSet<Literal> result = new TreeSet<>();
switch(formula.type()) {
case FALSE:
case TRUE:
result = new TreeSet<>();
break;
case LITERAL:
final Literal lit = (Literal) formula;
result.add(lit);
break;
case NOT:
final Not not = (Not) formula;
result = apply(not.operand(), cache);
break;
case IMPL:
case EQUIV:
final BinaryOperator binary = (BinaryOperator) formula;
result.addAll(apply(binary.left(), cache));
result.addAll(apply(binary.right(), cache));
break;
case OR:
case AND:
final NAryOperator nary = (NAryOperator) formula;
for (final Formula op : nary) {
result.addAll(apply(op, cache));
}
break;
case PBC:
final PBConstraint pbc = (PBConstraint) formula;
result = FormulaHelper.literals(pbc.operands());
break;
default:
throw new IllegalStateException("Unknown formula type " + formula.type());
}
result = Collections.unmodifiableSortedSet(result);
if (cache) {
formula.setFunctionCacheEntry(LITERALS, result);
}
return result;
}
use of org.logicng.formulas.PBConstraint in project LogicNG by logic-ng.
the class BFSOrdering method bfs.
private LinkedHashSet<Variable> bfs(final Formula formula) {
final LinkedHashSet<Variable> variables = new LinkedHashSet<>();
final Queue<Formula> queue = new LinkedList<>();
queue.add(formula);
while (!queue.isEmpty()) {
final Formula current = queue.remove();
switch(current.type()) {
case LITERAL:
final Literal lit = (Literal) current;
if (lit.phase()) {
variables.add(lit.variable());
} else {
queue.add(lit.variable());
}
break;
case NOT:
queue.add(((Not) current).operand());
break;
case IMPL:
case EQUIV:
final BinaryOperator op = (BinaryOperator) current;
queue.add(op.left());
queue.add(op.right());
break;
case AND:
case OR:
for (final Formula operand : current) {
queue.add(operand);
}
break;
case PBC:
final PBConstraint pbc = (PBConstraint) current;
for (final Literal literal : pbc.operands()) {
variables.add(literal.variable());
}
break;
}
}
return variables;
}
Aggregations