use of org.logicng.formulas.And in project LogicNG by logic-ng.
the class FormulaRandomizerTest method testMaximumOperandsAnd.
@Test
public void testMaximumOperandsAnd() {
final FormulaRandomizer random = new FormulaRandomizer(this.f, FormulaRandomizerConfig.builder().maximumOperandsAnd(10).seed(42).build());
for (int i = 0; i < 100; i++) {
final Formula formula = random.and(1);
assertThat(formula.type()).isEqualTo(FType.AND);
final And and = (And) formula;
assertThat(and.numberOfOperands()).isLessThanOrEqualTo(10);
}
}
use of org.logicng.formulas.And in project LogicNG by logic-ng.
the class FormulaFactoryImporter method apply.
@Override
public Formula apply(final Formula formula, final boolean cache) {
if (formula.factory() == this.newFormulaFactory) {
return formula;
}
switch(formula.type()) {
case TRUE:
return this.newFormulaFactory.verum();
case FALSE:
return this.newFormulaFactory.falsum();
case LITERAL:
final Literal literal = (Literal) formula;
return this.newFormulaFactory.literal(literal.name(), literal.phase());
case NOT:
final Not not = (Not) formula;
return this.newFormulaFactory.not(apply(not.operand(), cache));
case IMPL:
final Implication implication = (Implication) formula;
return this.newFormulaFactory.implication(apply(implication.left(), cache), apply(implication.right(), cache));
case EQUIV:
final Equivalence equivalence = (Equivalence) formula;
return this.newFormulaFactory.equivalence(apply(equivalence.left(), cache), apply(equivalence.right(), cache));
case OR:
final Or or = (Or) formula;
return this.newFormulaFactory.or(gatherAppliedOperands(or));
case AND:
final And and = (And) formula;
return this.newFormulaFactory.and(gatherAppliedOperands(and));
case PBC:
final PBConstraint pbc = (PBConstraint) formula;
final Literal[] literals = new Literal[pbc.operands().length];
for (int i = 0; i < pbc.operands().length; i++) {
literals[i] = (Literal) apply(pbc.operands()[i], cache);
}
return this.newFormulaFactory.pbc(pbc.comparator(), pbc.rhs(), literals, pbc.coefficients());
default:
throw new IllegalArgumentException("Unknown LogicNG formula type: " + formula.type());
}
}
use of org.logicng.formulas.And in project LogicNG by logic-ng.
the class TseitinTransformation method computeTseitin.
/**
* Computes the Tseitin transformation for a given formula and stores it in the formula cache.
* @param formula the formula
*/
private void computeTseitin(final Formula formula) {
if (formula.transformationCacheEntry(TSEITIN) != null) {
return;
}
final FormulaFactory f = formula.factory();
switch(formula.type()) {
case LITERAL:
formula.setTransformationCacheEntry(TSEITIN, formula);
formula.setTransformationCacheEntry(TSEITIN_VARIABLE, formula);
break;
case AND:
case OR:
final boolean isConjunction = formula instanceof And;
final Literal tsLiteral = f.newCNFVariable();
final List<Formula> nops = new ArrayList<>();
final List<Formula> operands = new ArrayList<>(formula.numberOfOperands());
final List<Formula> negOperands = new ArrayList<>(formula.numberOfOperands());
if (isConjunction) {
negOperands.add(tsLiteral);
handleNary(formula, nops, operands, negOperands);
for (final Formula operand : operands) {
nops.add(f.or(tsLiteral.negate(), operand));
}
nops.add(f.or(negOperands));
} else {
operands.add(tsLiteral.negate());
handleNary(formula, nops, operands, negOperands);
for (final Formula operand : negOperands) {
nops.add(f.or(tsLiteral, operand));
}
nops.add(f.or(operands));
}
formula.setTransformationCacheEntry(TSEITIN_VARIABLE, tsLiteral);
formula.setTransformationCacheEntry(TSEITIN, f.and(nops));
break;
default:
throw new IllegalArgumentException("Could not process the formula type " + formula.type());
}
}
use of org.logicng.formulas.And in project LogicNG by logic-ng.
the class BDDFactory method buildRec.
/**
* Recursive build procedure for the BDD.
* <p>
* If a BDD handler is given and the BDD generation is aborted due to the handler, the method will return
* {@link BDDKernel#BDD_ABORT} as result. If {@code null} is passed as handler, the generation will continue without
* interruption.
* @param formula the formula
* @param kernel the BDD kernel
* @param construction the BDD construction instance
* @param handler the BDD handler
* @return the BDD index or {@link BDDKernel#BDD_ABORT} if the computation was aborted
*/
private static int buildRec(final Formula formula, final BDDKernel kernel, final BDDConstruction construction, final BDDHandler handler) {
switch(formula.type()) {
case FALSE:
return BDDKernel.BDD_FALSE;
case TRUE:
return BDDKernel.BDD_TRUE;
case LITERAL:
final Literal lit = (Literal) formula;
final int idx = kernel.getOrAddVarIndex(lit.variable());
return lit.phase() ? construction.ithVar(idx) : construction.nithVar(idx);
case NOT:
{
final Not not = (Not) formula;
final int operand = buildRec(not.operand(), kernel, construction, handler);
if (operand == BDDKernel.BDD_ABORT) {
return BDDKernel.BDD_ABORT;
}
final int res = kernel.addRef(construction.not(operand), handler);
kernel.delRef(operand);
return res;
}
case IMPL:
case EQUIV:
final BinaryOperator binary = (BinaryOperator) formula;
final int left = buildRec(binary.left(), kernel, construction, handler);
if (left == BDDKernel.BDD_ABORT) {
return BDDKernel.BDD_ABORT;
}
final int right = buildRec(binary.right(), kernel, construction, handler);
if (right == BDDKernel.BDD_ABORT) {
return BDDKernel.BDD_ABORT;
}
int res = kernel.addRef(binary instanceof Implication ? construction.implication(left, right) : construction.equivalence(left, right), handler);
kernel.delRef(left);
kernel.delRef(right);
return res;
case AND:
case OR:
{
final Iterator<Formula> it = formula.iterator();
res = buildRec(it.next(), kernel, construction, handler);
if (res == BDDKernel.BDD_ABORT) {
return BDDKernel.BDD_ABORT;
}
while (it.hasNext()) {
final int operand = buildRec(it.next(), kernel, construction, handler);
if (operand == BDDKernel.BDD_ABORT) {
return BDDKernel.BDD_ABORT;
}
final int previous = res;
res = formula instanceof And ? kernel.addRef(construction.and(res, operand), handler) : kernel.addRef(construction.or(res, operand), handler);
kernel.delRef(previous);
kernel.delRef(operand);
}
return res;
}
case PBC:
return buildRec(formula.nnf(), kernel, construction, handler);
default:
throw new IllegalArgumentException("Unsupported operator for BDD generation: " + formula.type());
}
}
Aggregations