use of org.logicng.formulas.Literal in project LogicNG by logic-ng.
the class QuineMcCluskeyAlgorithm method transformModels2Terms.
/**
* Transforms a given list of models to a term list as used in the QMC implementation.
* @param models the models
* @param varOrder the variable ordering
* @param f the formula factory
* @return the list of terms
*/
static List<Term> transformModels2Terms(final List<Assignment> models, final List<Variable> varOrder, final FormulaFactory f) {
final List<Term> terms = new ArrayList<>(models.size());
for (final Assignment model : models) {
final List<Literal> minterm = new ArrayList<>();
for (final Variable variable : varOrder) {
minterm.add(model.evaluateLit(variable) ? variable : variable.negate());
}
terms.add(convertToTerm(minterm, f));
}
return terms;
}
use of org.logicng.formulas.Literal in project LogicNG by logic-ng.
the class QuineMcCluskeyAlgorithm method initializeSolver.
/**
* Initializes the SAT solver for the SET COVER problem formulation.
* @param table the prime implicant table
* @param f the formula factory
* @param var2Term a mapping from selector variable to prime implicant
* @param formula2VarMapping a mapping form minterm to variable
* @return the initialized SAT solver
*/
static SATSolver initializeSolver(final TermTable table, final FormulaFactory f, final LinkedHashMap<Variable, Term> var2Term, final LinkedHashMap<Formula, Variable> formula2VarMapping) {
final LinkedHashMap<Variable, List<Variable>> minterm2Variants = new LinkedHashMap<>();
int count = 0;
String prefix = "@MINTERM_SEL_";
for (final Formula formula : table.columnHeaders()) {
final Variable selector = f.variable(prefix + count++);
formula2VarMapping.put(formula, selector);
minterm2Variants.put(selector, new ArrayList<>());
}
count = 0;
prefix = "@TERM_SEL_";
final SATSolver solver = MiniSat.miniSat(f);
for (final Term term : table.lineHeaders()) {
final Variable termSelector = f.variable(prefix + count);
var2Term.put(termSelector, term);
final List<Variable> mintermSelectors = new ArrayList<>();
for (final Formula formula : term.minterms()) {
final Variable mintermSelector = formula2VarMapping.get(formula);
if (mintermSelector != null) {
final Variable selectorVariant = f.variable(mintermSelector.name() + "_" + count);
minterm2Variants.get(mintermSelector).add(selectorVariant);
mintermSelectors.add(selectorVariant);
}
}
final List<Literal> operands = new ArrayList<>();
for (int i = 0; i < mintermSelectors.size(); i++) {
final Variable mintermSelector = mintermSelectors.get(i);
solver.add(f.clause(termSelector.negate(), mintermSelector));
operands.add(mintermSelector.negate());
for (int j = i + 1; j < mintermSelectors.size(); j++) {
final Variable mintermSelector2 = mintermSelectors.get(j);
solver.add(f.or(mintermSelector.negate(), mintermSelector2));
solver.add(f.or(mintermSelector2.negate(), mintermSelector));
}
}
operands.add(termSelector);
solver.add(f.clause(operands));
count++;
}
for (final List<Variable> variables : minterm2Variants.values()) {
solver.add(f.clause(variables));
}
return solver;
}
use of org.logicng.formulas.Literal in project LogicNG by logic-ng.
the class BackboneGenerationTest method testBackboneType.
@Test
public void testBackboneType() {
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);
Formula formula = f.not(x);
SolverState before = solver.saveState();
solver.add(formula);
Backbone backbone = solver.execute(BackboneFunction.builder().variables(formula.variables()).type(BackboneType.POSITIVE_AND_NEGATIVE).build());
Backbone backbonePositive = solver.execute(BackboneFunction.builder().variables(formula.variables()).type(BackboneType.ONLY_POSITIVE).build());
Backbone backboneNegative = solver.execute(BackboneFunction.builder().variables(formula.variables()).type(BackboneType.ONLY_NEGATIVE).build());
assertThat(backbone.getCompleteBackbone()).containsExactly(x.negate());
assertThat(backbonePositive.getCompleteBackbone()).isEmpty();
assertThat(backboneNegative.getCompleteBackbone()).containsExactly(x.negate());
SortedSet<Literal> combinedPosNegBackbone = new TreeSet<>(backbonePositive.getCompleteBackbone());
combinedPosNegBackbone.addAll(backboneNegative.getCompleteBackbone());
assertThat(backbone.getCompleteBackbone()).isEqualTo(combinedPosNegBackbone);
solver.loadState(before);
formula = f.and(f.or(x, y.negate()), x.negate());
before = solver.saveState();
solver.add(formula);
backbone = solver.execute(BackboneFunction.builder().variables(formula.variables()).type(BackboneType.POSITIVE_AND_NEGATIVE).build());
backbonePositive = solver.execute(BackboneFunction.builder().variables(formula.variables()).type(BackboneType.ONLY_POSITIVE).build());
backboneNegative = solver.execute(BackboneFunction.builder().variables(formula.variables()).type(BackboneType.ONLY_NEGATIVE).build());
assertThat(backbone.getCompleteBackbone()).containsExactly(x.negate(), y.negate());
assertThat(backbonePositive.getCompleteBackbone()).isEmpty();
assertThat(backboneNegative.getCompleteBackbone()).containsExactly(x.negate(), y.negate());
combinedPosNegBackbone = new TreeSet<>(backbonePositive.getCompleteBackbone());
combinedPosNegBackbone.addAll(backboneNegative.getCompleteBackbone());
assertThat(backbone.getCompleteBackbone()).isEqualTo(combinedPosNegBackbone);
solver.loadState(before);
formula = f.and(f.or(x, y), f.or(x.negate(), y));
before = solver.saveState();
solver.add(formula);
backbone = solver.execute(BackboneFunction.builder().variables(formula.variables()).type(BackboneType.POSITIVE_AND_NEGATIVE).build());
backbonePositive = solver.execute(BackboneFunction.builder().variables(formula.variables()).type(BackboneType.ONLY_POSITIVE).build());
backboneNegative = solver.execute(BackboneFunction.builder().variables(formula.variables()).type(BackboneType.ONLY_NEGATIVE).build());
assertThat(backbone.getCompleteBackbone()).containsExactly(y);
assertThat(backbonePositive.getCompleteBackbone()).containsExactly(y);
assertThat(backboneNegative.getCompleteBackbone()).isEmpty();
combinedPosNegBackbone = new TreeSet<>(backbonePositive.getCompleteBackbone());
combinedPosNegBackbone.addAll(backboneNegative.getCompleteBackbone());
assertThat(backbone.getCompleteBackbone()).isEqualTo(combinedPosNegBackbone);
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);
backbone = solver.execute(BackboneFunction.builder().variables(formula.variables()).type(BackboneType.POSITIVE_AND_NEGATIVE).build());
backbonePositive = solver.execute(BackboneFunction.builder().variables(formula.variables()).type(BackboneType.ONLY_POSITIVE).build());
backboneNegative = solver.execute(BackboneFunction.builder().variables(formula.variables()).type(BackboneType.ONLY_NEGATIVE).build());
assertThat(backbone.getCompleteBackbone()).containsExactly(x.negate(), y, z);
assertThat(backbone.getOptionalVariables()).containsExactly();
assertThat(backbonePositive.getCompleteBackbone()).containsExactly(y, z);
assertThat(backboneNegative.getCompleteBackbone()).containsExactly(x.negate());
combinedPosNegBackbone = new TreeSet<>(backbonePositive.getCompleteBackbone());
combinedPosNegBackbone.addAll(backboneNegative.getCompleteBackbone());
assertThat(backbone.getCompleteBackbone()).isEqualTo(combinedPosNegBackbone);
solver.loadState(before);
}
use of org.logicng.formulas.Literal in project LogicNG by logic-ng.
the class AssignmentTest method testBlockingClause.
@Test
public void testBlockingClause() throws ParserException {
final Assignment ass = new Assignment();
ass.addLiteral(this.A);
ass.addLiteral(this.B);
ass.addLiteral(this.NX);
ass.addLiteral(this.NY);
final Formula bc01 = ass.blockingClause(this.f);
assertThat(bc01.containsVariable(this.C)).isFalse();
assertThat(bc01).isEqualTo(this.f.parse("~a | ~b | x | y"));
final Formula bc02 = ass.blockingClause(this.f, null);
assertThat(bc02.containsVariable(this.C)).isFalse();
assertThat(bc02).isEqualTo(this.f.parse("~a | ~b | x | y"));
final List<Literal> lits = Arrays.asList(this.A, this.X, this.C);
final Formula bcProjected = ass.blockingClause(this.f, lits);
assertThat(bcProjected.containsVariable(this.C)).isFalse();
assertThat(bcProjected).isEqualTo(this.f.parse("~a | x"));
}
use of org.logicng.formulas.Literal in project LogicNG by logic-ng.
the class OptimizationFunctionTest method testAdditionalVariables.
@ParameterizedTest
@MethodSource("solvers")
public void testAdditionalVariables(final SATSolver solver) throws ParserException {
final FormulaFactory f = solver.factory();
final Variable a = f.variable("a");
final Literal na = f.literal("a", false);
final Variable b = f.variable("b");
final Literal nb = f.literal("b", false);
final Variable c = f.variable("c");
final Variable x = f.variable("x");
final Literal nx = f.literal("x", false);
final Variable y = f.variable("y");
solver.reset();
final Formula formula = f.parse("(a|b) & (~a => c) & (x|y)");
final List<Literal> literalsANBX = Arrays.asList(a, nb, x);
final Assignment minimumModel = optimize(Collections.singleton(formula), literalsANBX, Collections.emptyList(), false, solver, null);
assertThat(minimumModel.literals()).containsExactlyInAnyOrder(na, b, nx);
final Assignment minimumModelWithY = optimize(Collections.singleton(formula), literalsANBX, Collections.singleton(y), false, solver, null);
assertThat(minimumModelWithY.literals()).containsExactlyInAnyOrder(na, b, nx, y);
final Assignment minimumModelWithCY = optimize(Collections.singleton(formula), literalsANBX, Arrays.asList(c, y), false, solver, null);
assertThat(minimumModelWithCY.literals()).containsExactlyInAnyOrder(na, b, c, nx, y);
final List<Literal> literalsNBNX = Arrays.asList(na, nx);
final Assignment maximumModel = optimize(Collections.singleton(formula), literalsNBNX, Collections.emptyList(), true, solver, null);
assertThat(maximumModel.literals()).containsExactlyInAnyOrder(na, nx);
final Assignment maximumModelWithC = optimize(Collections.singleton(formula), literalsNBNX, Collections.singleton(c), true, solver, null);
assertThat(maximumModelWithC.literals()).containsExactlyInAnyOrder(na, c, nx);
final Assignment maximumModelWithACY = optimize(Collections.singleton(formula), literalsNBNX, Arrays.asList(a, c, y), true, solver, null);
assertThat(maximumModelWithACY.literals()).containsExactlyInAnyOrder(na, c, nx, y);
}
Aggregations