use of org.logicng.formulas.Formula in project LogicNG by logic-ng.
the class NegationSimplifier method findSmallestNegative.
private Formula findSmallestNegative(final FType type, final List<Formula> negativeOpResults, final Formula smallestPositive, final boolean topLevel, final FormulaFactory f) {
final Formula negation = f.not(smallestPositive);
final Formula flipped = f.naryOperator(dual(type), negativeOpResults);
return getSmallestFormula(Arrays.asList(negation, flipped), topLevel);
}
use of org.logicng.formulas.Formula in project LogicNG by logic-ng.
the class NegationSimplifier method minimize.
private MinimizationResult minimize(final Formula formula, final boolean topLevel) {
final FormulaFactory f = formula.factory();
switch(formula.type()) {
case LITERAL:
final Literal lit = (Literal) formula;
return new MinimizationResult(lit, lit.negate());
case OR:
case AND:
final NAryOperator nary = (NAryOperator) formula;
final List<MinimizationResult> opResults = new ArrayList<>(nary.numberOfOperands());
for (final Formula op : formula) {
opResults.add(minimize(op, false));
}
final List<Formula> positiveOpResults = new ArrayList<>(opResults.size());
final List<Formula> negativeOpResults = new ArrayList<>(opResults.size());
for (final MinimizationResult result : opResults) {
positiveOpResults.add(result.getPositiveResult());
negativeOpResults.add(result.getNegativeResult());
}
final Formula smallestPositive = findSmallestPositive(formula.type(), positiveOpResults, negativeOpResults, topLevel, f);
final Formula smallestNegative = findSmallestNegative(formula.type(), negativeOpResults, smallestPositive, topLevel, f);
return new MinimizationResult(smallestPositive, smallestNegative);
case FALSE:
case TRUE:
case NOT:
case EQUIV:
case IMPL:
case PBC:
throw new IllegalStateException("Unexpected LogicNG formula type: " + formula.type());
default:
throw new IllegalArgumentException("Unknown LogicNG formula type: " + formula.type());
}
}
use of org.logicng.formulas.Formula in project LogicNG by logic-ng.
the class FormulaRandomizer method or.
/**
* Returns a random disjunction with a given maximal depth.
* @param maxDepth the maximal depth
* @return the random disjunction
*/
public Formula or(final int maxDepth) {
if (maxDepth == 0) {
return atom();
}
final Formula[] operands = new Formula[2 + this.random.nextInt(this.config.maximumOperandsOr - 2)];
for (int i = 0; i < operands.length; i++) {
operands[i] = formula(maxDepth - 1);
}
final Formula formula = this.f.or(operands);
if (formula.type() != FType.OR) {
return or(maxDepth);
}
return formula;
}
use of org.logicng.formulas.Formula in project LogicNG by logic-ng.
the class FormulaRandomizer method pbc.
/**
* Returns a random pseudo boolean constraint.
* @return the random pseudo boolean constraint
*/
public Formula pbc() {
final int numOps = this.random.nextInt(this.config.maximumOperandsPbc);
final Literal[] literals = new Literal[numOps];
final int[] coefficients = new int[numOps];
// (positive) sum of all negative coefficients
int minSum = 0;
// sum of all positive coefficients
int maxSum = 0;
for (int i = 0; i < numOps; i++) {
literals[i] = literal();
coefficients[i] = this.random.nextInt(this.config.maximumCoefficientPbc) + 1;
if (this.random.nextDouble() < this.coefficientNegativeProbability) {
minSum += coefficients[i];
coefficients[i] = -coefficients[i];
} else {
maxSum += coefficients[i];
}
}
final CType type = cType();
final int rhs = this.random.nextInt(maxSum + minSum + 1) - minSum;
final Formula pbc = this.f.pbc(type, rhs, literals, coefficients);
if (pbc.isConstantFormula()) {
return pbc();
}
return pbc;
}
use of org.logicng.formulas.Formula in project LogicNG by logic-ng.
the class SATTest method testSelectionOrderSimple01.
@Test
public void testSelectionOrderSimple01() throws ParserException {
for (final SATSolver solver : this.solvers) {
final Formula formula = this.parser.parse("~(x <=> y)");
solver.add(formula);
List<Literal> selectionOrder = Arrays.asList(this.X, this.Y);
assertThat(solver.satWithSelectionOrder(selectionOrder)).isEqualTo(Tristate.TRUE);
Assignment assignment = solver.model();
assertThat(assignment.literals()).containsExactlyInAnyOrder(this.X, this.NY);
testLocalMinimum(solver, assignment, selectionOrder);
testHighestLexicographicalAssignment(solver, assignment, selectionOrder);
solver.setSolverToUndef();
selectionOrder = Arrays.asList(this.Y, this.X);
assertThat(solver.satWithSelectionOrder(selectionOrder)).isEqualTo(Tristate.TRUE);
assignment = solver.model();
assertThat(assignment.literals()).containsExactlyInAnyOrder(this.Y, this.NX);
testLocalMinimum(solver, assignment, selectionOrder);
testHighestLexicographicalAssignment(solver, assignment, selectionOrder);
solver.setSolverToUndef();
selectionOrder = Collections.singletonList(this.NX);
assertThat(solver.sat(selectionOrder)).isEqualTo(Tristate.TRUE);
assignment = solver.model();
assertThat(assignment.literals()).containsExactlyInAnyOrder(this.Y, this.NX);
testLocalMinimum(solver, assignment, selectionOrder);
testHighestLexicographicalAssignment(solver, assignment, selectionOrder);
solver.setSolverToUndef();
selectionOrder = Arrays.asList(this.NY, this.NX);
assertThat(solver.satWithSelectionOrder(selectionOrder)).isEqualTo(Tristate.TRUE);
assignment = solver.model();
assertThat(assignment.literals()).containsExactlyInAnyOrder(this.X, this.NY);
testLocalMinimum(solver, assignment, selectionOrder);
testHighestLexicographicalAssignment(solver, assignment, selectionOrder);
solver.reset();
}
}
Aggregations