use of org.logicng.formulas.Literal in project LogicNG by logic-ng.
the class VariableProfileTest method testNot.
@Test
public void testNot() throws ParserException {
final Map<Literal, Integer> expected = new HashMap<>();
expected.put(this.f2.variable("a"), 1);
expected.put(this.f2.variable("b"), 2);
expected.put(this.f2.variable("c"), 3);
final PropositionalParser p = new PropositionalParser(this.f);
final Formula formula = p.parse("~(a & (b | c) & ((~b | ~c) => c))");
assertThat(formula.apply(this.varProfile, true)).isEqualTo(expected);
assertThat(formula.apply(this.varProfile, false)).isEqualTo(expected);
}
use of org.logicng.formulas.Literal in project LogicNG by logic-ng.
the class VariableProfileTest method testBinaryOperator.
@Test
public void testBinaryOperator() throws ParserException {
final Map<Literal, Integer> expected = new HashMap<>();
expected.put(this.f2.variable("a"), 1);
expected.put(this.f2.variable("b"), 2);
expected.put(this.f2.variable("c"), 3);
final PropositionalParser p = new PropositionalParser(this.f);
final Formula impl = p.parse("(a & (b | c) & (~b | ~c)) => c");
final Formula equiv = p.parse("(a & (b | c) & (~b | ~c)) <=> c");
assertThat(impl.apply(this.varProfile, true)).isEqualTo(expected);
assertThat(impl.apply(this.varProfile, false)).isEqualTo(expected);
assertThat(equiv.apply(this.varProfile, true)).isEqualTo(expected);
assertThat(equiv.apply(this.varProfile, false)).isEqualTo(expected);
}
use of org.logicng.formulas.Literal in project LogicNG by logic-ng.
the class BackboneGenerationTest method testSimpleBackbones.
@Test
public void testSimpleBackbones() {
final FormulaFactory f = new FormulaFactory();
final MiniSat solver = MiniSat.miniSat(f);
final Literal x = f.literal("x", true);
final Literal y = f.literal("y", true);
final Literal z = f.literal("z", true);
final Literal u = f.literal("u", true);
final Literal v = f.literal("v", true);
final Collection<Variable> variables = new ArrayList<>(Arrays.asList(f.variable("x"), f.variable("y"), f.variable("z"), f.variable("u"), f.variable("v")));
Formula formula = f.verum();
SolverState before = solver.saveState();
solver.add(formula);
assertThat(solver.execute(BackboneFunction.builder().variables(Collections.emptyList()).build()).getCompleteBackbone()).isEmpty();
solver.loadState(before);
formula = x;
before = solver.saveState();
solver.add(formula);
assertThat(solver.execute(BackboneFunction.builder().variables(variables).build()).getCompleteBackbone()).containsExactly(x);
solver.loadState(before);
formula = f.and(x, y);
before = solver.saveState();
solver.add(formula);
assertThat(solver.execute(BackboneFunction.builder().variables(variables).build()).getCompleteBackbone()).containsExactly(x, y);
solver.loadState(before);
formula = f.or(x, y);
before = solver.saveState();
solver.add(formula);
assertThat(solver.execute(BackboneFunction.builder().variables(variables).build()).getCompleteBackbone()).isEmpty();
solver.loadState(before);
formula = x.negate();
before = solver.saveState();
solver.add(formula);
assertThat(solver.execute(BackboneFunction.builder().variables(variables).build()).getCompleteBackbone()).containsExactly(x.negate());
solver.loadState(before);
formula = f.or(f.and(x, y, z), f.and(x, y, u), f.and(x, u, z));
before = solver.saveState();
solver.add(formula);
assertThat(solver.execute(BackboneFunction.builder().variables(variables).build()).getCompleteBackbone()).containsExactly(x);
solver.loadState(before);
formula = f.and(f.or(x, y, z), f.or(x, y, u), f.or(x, u, z));
before = solver.saveState();
solver.add(formula);
assertThat(solver.execute(BackboneFunction.builder().variables(variables).build()).getCompleteBackbone()).isEmpty();
solver.loadState(before);
formula = f.and(f.or(x.negate(), y), x);
before = solver.saveState();
solver.add(formula);
assertThat(solver.execute(BackboneFunction.builder().variables(variables).build()).getCompleteBackbone()).containsExactly(x, y);
solver.loadState(before);
formula = f.and(f.or(x, y), f.or(x.negate(), y));
before = solver.saveState();
solver.add(formula);
assertThat(solver.execute(BackboneFunction.builder().variables(variables).build()).getCompleteBackbone()).containsExactly(y);
solver.loadState(before);
formula = f.and(f.and(f.or(x.negate(), y), x.negate()), f.and(z, f.or(x, y)));
before = solver.saveState();
solver.add(formula);
assertThat(solver.execute(BackboneFunction.builder().variables(variables).build()).getCompleteBackbone()).containsExactly(x.negate(), y, z);
solver.loadState(before);
formula = f.and(f.or(x, y), f.or(u, v), z);
solver.add(formula);
assertThat(solver.execute(BackboneFunction.builder().variables(variables).build()).getCompleteBackbone()).containsExactly(z);
}
use of org.logicng.formulas.Literal 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.Literal 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;
}
Aggregations