use of org.logicng.formulas.Literal in project LogicNG by logic-ng.
the class OptimizationFunctionTest method testOptimumModel.
private void testOptimumModel(final Formula formula, final Assignment optimumModel, final Collection<? extends Literal> literals, final boolean maximize) {
final FormulaFactory f = formula.factory();
final SATPredicate satPredicate = new SATPredicate(f);
if (formula.holds(satPredicate)) {
assertThat(f.and(formula, f.and(optimumModel.literals())).holds(satPredicate));
final int numSatisfiedLiterals = satisfiedLiterals(optimumModel, literals).size();
final SortedSet<Variable> selVars = new TreeSet<>();
final SATSolver solver = MiniSat.miniSat(formula.factory());
solver.add(formula);
for (final Literal lit : literals) {
final Variable selVar = f.variable("SEL_VAR_" + selVars.size());
if (maximize) {
solver.add(f.equivalence(selVar.negate(), lit));
} else {
solver.add(f.equivalence(selVar.negate(), lit.negate()));
}
}
solver.add(formula.factory().cc(CType.GT, numSatisfiedLiterals + 1, selVars));
assertSolverUnsat(solver);
} else {
assertThat(optimumModel).isNull();
}
}
use of org.logicng.formulas.Literal in project LogicNG by logic-ng.
the class PartialMaxSATTest method readCNF.
private void readCNF(final MaxSATSolver solver, final String fileName) throws IOException {
final BufferedReader reader = new BufferedReader(new FileReader(fileName));
int hardWeight = 0;
while (reader.ready()) {
final String line = reader.readLine();
if (line.startsWith("p wcnf")) {
final String[] header = line.split(" ", -1);
hardWeight = Integer.parseInt(header[4]);
break;
}
}
String[] tokens;
final List<Literal> literals = new ArrayList<>();
while (reader.ready()) {
tokens = reader.readLine().split(" ");
assert tokens.length >= 3;
assert "0".equals(tokens[tokens.length - 1]);
literals.clear();
final int weight = Integer.parseInt(tokens[0]);
for (int i = 1; i < tokens.length - 1; i++) {
if (!tokens[i].isEmpty()) {
final int parsedLit = Integer.parseInt(tokens[i]);
final String var = "v" + Math.abs(parsedLit);
literals.add(parsedLit > 0 ? this.f.literal(var, true) : this.f.literal(var, false));
}
}
if (weight == hardWeight) {
solver.addHardFormula(this.f.or(literals));
} else {
solver.addSoftFormula(this.f.or(literals), weight);
}
}
}
use of org.logicng.formulas.Literal in project LogicNG by logic-ng.
the class PartialWeightedMaxSATTest method readCNF.
private void readCNF(final MaxSATSolver solver, final String fileName) throws IOException {
final BufferedReader reader = new BufferedReader(new FileReader(fileName));
int hardWeight = 0;
while (reader.ready()) {
final String line = reader.readLine();
if (line.startsWith("p wcnf")) {
final String[] header = line.split(" ", -1);
hardWeight = Integer.parseInt(header[4]);
break;
}
}
String[] tokens;
final List<Literal> literals = new ArrayList<>();
while (reader.ready()) {
tokens = reader.readLine().split(" ");
assert tokens.length >= 3;
assert "0".equals(tokens[tokens.length - 1]);
literals.clear();
final int weight = Integer.parseInt(tokens[0]);
for (int i = 1; i < tokens.length - 1; i++) {
if (!tokens[i].isEmpty()) {
final int parsedLit = Integer.parseInt(tokens[i]);
final String var = "v" + Math.abs(parsedLit);
literals.add(parsedLit > 0 ? this.f.literal(var, true) : this.f.literal(var, false));
}
}
if (weight == hardWeight) {
solver.addHardFormula(this.f.or(literals));
} else {
solver.addSoftFormula(this.f.or(literals), weight);
}
}
}
use of org.logicng.formulas.Literal in project LogicNG by logic-ng.
the class AssumeTest method testAssume.
@Test
public void testAssume() throws ParserException {
final List<Literal> assumptions1 = Arrays.asList(this.f.literal("c", true), this.f.literal("d", true));
final List<Literal> assumptions2 = Arrays.asList(this.f.literal("x", false), this.f.literal("y", true), this.f.literal("d", true));
final List<Literal> assumptions3 = Arrays.asList(this.f.literal("a", false), this.f.literal("c", true), this.f.literal("a", false));
final List<Literal> assumptions4 = Arrays.asList(this.f.literal("c", false), this.f.literal("d", true));
final List<Literal> assumptions5 = Arrays.asList(this.f.literal("x", true), this.f.literal("x", false));
final List<Literal> assumptions6 = Arrays.asList(this.f.literal("a", true), this.f.literal("a", false));
for (final SATSolver s : this.solvers) {
s.add(this.parser.parse("~a"));
s.add(this.parser.parse("b"));
s.add(this.parser.parse("b => c"));
s.add(this.parser.parse("c => d"));
s.add(this.parser.parse("d => e"));
s.add(this.parser.parse("e => f"));
assertThat(s.sat(this.f.literal("a", false))).isEqualTo(TRUE);
assertThat(s.sat(this.f.variable("b"))).isEqualTo(TRUE);
assertThat(s.sat(this.f.variable("c"))).isEqualTo(TRUE);
assertThat(s.sat(this.f.variable("d"))).isEqualTo(TRUE);
assertThat(s.sat(this.f.variable("e"))).isEqualTo(TRUE);
assertThat(s.sat(this.f.variable("f"))).isEqualTo(TRUE);
assertThat(s.sat(this.f.variable("g"))).isEqualTo(TRUE);
assertThat(s.sat(this.f.variable("a"))).isEqualTo(FALSE);
assertThat(s.sat(this.f.literal("b", false))).isEqualTo(FALSE);
assertThat(s.sat(this.f.literal("c", false))).isEqualTo(FALSE);
assertThat(s.sat(this.f.literal("d", false))).isEqualTo(FALSE);
assertThat(s.sat(this.f.literal("e", false))).isEqualTo(FALSE);
assertThat(s.sat(this.f.literal("f", false))).isEqualTo(FALSE);
assertThat(s.sat(this.f.literal("g", false))).isEqualTo(TRUE);
assertThat(s.sat(assumptions1)).isEqualTo(TRUE);
assertThat(s.sat(assumptions2)).isEqualTo(TRUE);
assertThat(s.sat(assumptions3)).isEqualTo(TRUE);
assertThat(s.sat(assumptions4)).isEqualTo(FALSE);
assertThat(s.sat(assumptions5)).isEqualTo(FALSE);
assertThat(s.sat(assumptions6)).isEqualTo(FALSE);
s.reset();
}
}
use of org.logicng.formulas.Literal in project LogicNG by logic-ng.
the class BDDOperationsTest method testRestriction.
@Test
public void testRestriction() throws ParserException {
final Literal a = this.f.literal("A", true);
final List<Literal> resNotA = Collections.singletonList(this.f.literal("A", false));
final List<Literal> resAB = Arrays.asList(this.f.literal("A", true), this.f.literal("B", true));
assertThat(this.bddPosLit.construction.restrict(0, 1)).isEqualTo(0);
assertThat(this.bddPosLit.construction.restrict(1, 1)).isEqualTo(1);
assertThat(this.bddVerum.restrict(a)).isEqualTo(this.bddVerum);
assertThat(this.bddVerum.restrict(resNotA)).isEqualTo(this.bddVerum);
assertThat(this.bddVerum.restrict(resAB)).isEqualTo(this.bddVerum);
assertThat(this.bddFalsum.restrict(a)).isEqualTo(this.bddFalsum);
assertThat(this.bddFalsum.restrict(resNotA)).isEqualTo(this.bddFalsum);
assertThat(this.bddFalsum.restrict(resAB)).isEqualTo(this.bddFalsum);
assertThat(this.bddPosLit.restrict(a)).isEqualTo(this.bddVerum);
assertThat(this.bddPosLit.restrict(resNotA)).isEqualTo(this.bddFalsum);
assertThat(this.bddPosLit.restrict(resAB)).isEqualTo(this.bddVerum);
assertThat(this.bddNegLit.restrict(a)).isEqualTo(this.bddFalsum);
assertThat(this.bddNegLit.restrict(resNotA)).isEqualTo(this.bddVerum);
assertThat(this.bddNegLit.restrict(resAB)).isEqualTo(this.bddFalsum);
assertThat(this.bddImpl.restrict(a)).isEqualTo(BDDFactory.build(this.f.literal("B", false), this.kernel));
assertThat(this.bddImpl.restrict(resNotA)).isEqualTo(this.bddVerum);
assertThat(this.bddImpl.restrict(resAB)).isEqualTo(this.bddFalsum);
assertThat(this.bddEquiv.restrict(a)).isEqualTo(BDDFactory.build(this.f.literal("B", false), this.kernel));
assertThat(this.bddEquiv.restrict(resNotA)).isEqualTo(BDDFactory.build(this.f.literal("B", true), this.kernel));
assertThat(this.bddEquiv.restrict(resAB)).isEqualTo(this.bddFalsum);
assertThat(this.bddOr.restrict(a)).isEqualTo(this.bddVerum);
assertThat(this.bddOr.restrict(resNotA)).isEqualTo(BDDFactory.build(this.parser.parse("B | ~C"), this.kernel));
assertThat(this.bddOr.restrict(resAB)).isEqualTo(this.bddVerum);
assertThat(this.bddAnd.restrict(a)).isEqualTo(BDDFactory.build(this.parser.parse("B & ~C"), this.kernel));
assertThat(this.bddAnd.restrict(resNotA)).isEqualTo(this.bddFalsum);
assertThat(this.bddAnd.restrict(resAB)).isEqualTo(BDDFactory.build(this.f.literal("C", false), this.kernel));
}
Aggregations