use of org.logicng.formulas.Literal in project LogicNG by logic-ng.
the class EncodingResult method addClause.
/**
* Adds a clause to the result
* @param literals the literals of the clause
*/
public void addClause(final Literal... literals) {
if (this.miniSat == null) {
this.result.add(this.f.clause(literals));
} else {
final LNGIntVector clauseVec = new LNGIntVector(literals.length);
for (final Literal literal : literals) {
addLiteral(clauseVec, literal);
}
this.miniSat.underlyingSolver().addClause(clauseVec, this.proposition);
this.miniSat.setSolverToUndef();
}
}
use of org.logicng.formulas.Literal in project LogicNG by logic-ng.
the class CCAMONested method encodeIntern.
/**
* Internal recursive encoding.
* @param vars the variables of the constraint
*/
private void encodeIntern(final LNGVector<Literal> vars) {
if (vars.size() <= this.groupSize) {
for (int i = 0; i + 1 < vars.size(); i++) {
for (int j = i + 1; j < vars.size(); j++) {
this.result.addClause(vars.get(i).negate(), vars.get(j).negate());
}
}
} else {
final LNGVector<Literal> l1 = new LNGVector<>(vars.size() / 2);
final LNGVector<Literal> l2 = new LNGVector<>(vars.size() / 2);
int i = 0;
for (; i < vars.size() / 2; i++) {
l1.push(vars.get(i));
}
for (; i < vars.size(); i++) {
l2.push(vars.get(i));
}
final Variable newVariable = this.result.newVariable();
l1.push(newVariable);
l2.push(newVariable.negate());
this.encodeIntern(l1);
this.encodeIntern(l2);
}
}
use of org.logicng.formulas.Literal in project LogicNG by logic-ng.
the class DnnfCompiler method cnf2Ddnnf.
protected Formula cnf2Ddnnf(final DTree tree, final int currentShannons) throws TimeoutException {
final BitSet separator = tree.dynamicSeparator();
final Formula implied = this.newlyImpliedLiterals(tree.staticVarSet());
if (separator.isEmpty()) {
if (tree instanceof DTreeLeaf) {
return this.f.and(implied, leaf2Ddnnf((DTreeLeaf) tree));
} else {
return conjoin(implied, (DTreeNode) tree, currentShannons);
}
} else {
final int var = chooseShannonVariable(tree, separator, currentShannons);
if (this.handler != null && !this.handler.shannonExpansion()) {
throw new TimeoutException();
}
/* Positive branch */
Formula positiveDnnf = this.f.falsum();
if (this.solver.decide(var, true)) {
positiveDnnf = cnf2Ddnnf(tree, currentShannons + 1);
}
this.solver.undoDecide(var);
if (positiveDnnf == this.f.falsum()) {
if (this.solver.atAssertionLevel() && this.solver.assertCdLiteral()) {
return cnf2Ddnnf(tree);
} else {
return this.f.falsum();
}
}
/* Negative branch */
Formula negativeDnnf = this.f.falsum();
if (this.solver.decide(var, false)) {
negativeDnnf = cnf2Ddnnf(tree, currentShannons + 1);
}
this.solver.undoDecide(var);
if (negativeDnnf == this.f.falsum()) {
if (this.solver.atAssertionLevel() && this.solver.assertCdLiteral()) {
return cnf2Ddnnf(tree);
} else {
return this.f.falsum();
}
}
final Literal lit = this.solver.litForIdx(var);
final Formula positiveBranch = this.f.and(lit, positiveDnnf);
final Formula negativeBranch = this.f.and(lit.negate(), negativeDnnf);
return this.f.and(implied, this.f.or(positiveBranch, negativeBranch));
}
}
use of org.logicng.formulas.Literal in project LogicNG by logic-ng.
the class DnnfCompiler method leaf2Ddnnf.
protected Formula leaf2Ddnnf(final DTreeLeaf leaf) {
final Iterator<Literal> literals = leaf.clause().literals().iterator();
this.leafResultOperands.clear();
this.leafCurrentLiterals.clear();
Literal lit;
int index = 0;
while (literals.hasNext()) {
lit = literals.next();
switch(this.solver.valueOf(MiniSatStyleSolver.mkLit(this.solver.variableIndex(lit), !lit.phase()))) {
case TRUE:
return this.f.verum();
case UNDEF:
this.leafCurrentLiterals.add(lit);
this.leafResultOperands.add(this.f.and(this.leafCurrentLiterals));
this.leafCurrentLiterals.set(index, lit.negate());
index++;
}
}
return this.f.or(this.leafResultOperands);
}
use of org.logicng.formulas.Literal in project LogicNG by logic-ng.
the class DnnfMiniSatStyleSolver method generateClauseVector.
protected LNGIntVector generateClauseVector(final Collection<Literal> literals) {
final LNGIntVector clauseVec = new LNGIntVector(literals.size());
for (final Literal lit : literals) {
int index = idxForName(lit.name());
if (index == -1) {
index = newVar(false, true);
addName(lit.name(), index);
}
final int litNum = lit.phase() ? index * 2 : (index * 2) ^ 1;
clauseVec.push(litNum);
}
return clauseVec;
}
Aggregations