use of org.logicng.formulas.PBConstraint in project LogicNG by logic-ng.
the class FormulaRandomizerTest method testAtom.
@Test
public void testAtom() {
final FormulaRandomizer random = new FormulaRandomizer(this.f, FormulaRandomizerConfig.builder().weightConstant(1).weightPositiveLiteral(2).weightNegativeLiteral(3).weightPbc(4).weightCc(5).weightAmo(6).weightExo(7).seed(42).build());
int numConst = 0;
int numPos = 0;
int numNeg = 0;
int numPbc = 0;
int numCc = 0;
int numAmo = 0;
int numExo = 0;
for (int i = 0; i < 1000; i++) {
final Formula formula = random.atom();
assertThat(formula.isAtomicFormula()).isTrue();
if (formula.isConstantFormula()) {
numConst++;
} else if (formula.type() == FType.LITERAL) {
if (((Literal) formula).phase()) {
numPos++;
} else {
numNeg++;
}
} else {
final PBConstraint pbc = (PBConstraint) formula;
if (!pbc.isCC()) {
numPbc++;
} else if (pbc.rhs() == 1 && pbc.comparator() == CType.LE) {
numAmo++;
} else if (pbc.rhs() == 1 && pbc.comparator() == CType.EQ) {
numExo++;
} else {
numCc++;
}
}
}
assertThat(numExo).isStrictlyBetween((int) (.8 * 7 / 6 * numAmo), (int) (1.2 * 7 / 6 * numAmo));
assertThat(numAmo).isStrictlyBetween((int) (.8 * 6 / 5 * numCc), (int) (1.2 * 6 / 5 * numCc));
assertThat(numCc).isStrictlyBetween((int) (.8 * 5 / 4 * numPbc), (int) (1.2 * 5 / 4 * numPbc));
assertThat(numPbc).isStrictlyBetween((int) (.8 * 4 / 3 * numNeg), (int) (1.2 * 4 / 3 * numNeg));
assertThat(numNeg).isStrictlyBetween((int) (.8 * 3 / 2 * numPos), (int) (1.2 * 3 / 2 * numPos));
assertThat(numPos).isStrictlyBetween((int) (.8 * 2 / 1 * numConst), (int) (1.2 * 2 / 1 * numConst));
final FormulaRandomizer randomOnlyLiterals = new FormulaRandomizer(this.f, FormulaRandomizerConfig.builder().weightConstant(0).weightPositiveLiteral(3).weightNegativeLiteral(6).seed(42).build());
for (int i = 0; i < 100; i++) {
assertThat(randomOnlyLiterals.atom().type()).isEqualTo(FType.LITERAL);
}
}
use of org.logicng.formulas.PBConstraint 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.PBConstraint in project LogicNG by logic-ng.
the class LiteralSubstitution method apply.
@Override
public Formula apply(final Formula formula, final boolean cache) {
final FormulaFactory f = formula.factory();
switch(formula.type()) {
case TRUE:
case FALSE:
return formula;
case LITERAL:
final Literal literal = (Literal) formula;
Literal lit = this.substitution.get(literal);
if (lit != null) {
return lit;
}
if (!literal.phase()) {
lit = this.substitution.get(literal.variable());
return lit != null ? lit.negate() : formula;
}
return formula;
case NOT:
return f.not(apply(((Not) formula).operand(), cache));
case EQUIV:
case IMPL:
final BinaryOperator binOp = (BinaryOperator) formula;
return f.binaryOperator(formula.type(), apply(binOp.left(), cache), apply(binOp.right(), cache));
case OR:
case AND:
final List<Formula> operands = new ArrayList<>();
for (final Formula op : formula) {
operands.add(apply(op, cache));
}
return f.naryOperator(formula.type(), operands);
case PBC:
final PBConstraint pbc = (PBConstraint) formula;
final Literal[] originalOperands = pbc.operands();
final Literal[] literals = new Literal[originalOperands.length];
for (int i = 0; i < literals.length; i++) {
literals[i] = (Literal) apply(originalOperands[i], false);
}
return f.pbc(pbc.comparator(), pbc.rhs(), literals, pbc.coefficients());
default:
throw new IllegalArgumentException("Unknown formula type: " + formula.type());
}
}
use of org.logicng.formulas.PBConstraint in project LogicNG by logic-ng.
the class PBSolvingTest method testLargePBs.
@Test
public void testLargePBs() {
for (final PBEncoder encoder : this.encoders) {
final SATSolver solver = this.solvers[0];
solver.reset();
final int numLits = 100;
final Variable[] lits = new Variable[numLits];
final int[] coeffs = new int[numLits];
for (int i = 0; i < numLits; i++) {
lits[i] = this.f.variable("v" + i);
coeffs[i] = i + 1;
}
final PBConstraint pbc = (PBConstraint) this.f.pbc(CType.GE, 5000, lits, coeffs);
solver.add(encoder.encode(pbc));
assertSolverSat(solver);
assertThat(pbc.evaluate(solver.model())).isTrue();
}
}
use of org.logicng.formulas.PBConstraint in project LogicNG by logic-ng.
the class PBSolvingTest method testPBLess.
@Test
public void testPBLess() {
for (final PBEncoder encoder : this.encoders) {
for (final SATSolver solver : this.solvers) {
solver.reset();
final int[] coeffs10 = new int[] { 3, 2, 2, 2, 2, 2, 2, 2, 2, 2 };
solver.add(encoder.encode((PBConstraint) this.f.pbc(CType.LE, 6, this.literals10, coeffs10)));
assertSolverSat(solver);
assertThat(solver.enumerateAllModels(this.literals10)).hasSize(140).allMatch(model -> model.positiveVariables().size() <= 3);
solver.reset();
solver.add(encoder.encode((PBConstraint) this.f.pbc(CType.LT, 7, this.literals10, coeffs10)));
assertSolverSat(solver);
assertThat(solver.enumerateAllModels(this.literals10)).hasSize(140).allMatch(model -> model.positiveVariables().size() <= 3);
solver.reset();
solver.add(encoder.encode((PBConstraint) this.f.pbc(CType.LE, 0, this.literals10, coeffs10)));
assertSolverSat(solver);
assertThat(solver.enumerateAllModels(this.literals10)).hasSize(1);
solver.reset();
solver.add(encoder.encode((PBConstraint) this.f.pbc(CType.LE, 1, this.literals10, coeffs10)));
assertSolverSat(solver);
assertThat(solver.enumerateAllModels(this.literals10)).hasSize(1);
solver.reset();
solver.add(encoder.encode((PBConstraint) this.f.pbc(CType.LT, 2, this.literals10, coeffs10)));
assertSolverSat(solver);
assertThat(solver.enumerateAllModels(this.literals10)).hasSize(1);
solver.reset();
solver.add(encoder.encode((PBConstraint) this.f.pbc(CType.LT, 1, this.literals10, coeffs10)));
assertSolverSat(solver);
assertThat(solver.enumerateAllModels(this.literals10)).hasSize(1);
}
}
}
Aggregations