use of org.logicng.collections.LNGIntVector in project LogicNG by logic-ng.
the class PBConstraint method normalize.
/**
* Normalizes this constraint s.t. it can be converted to CNF.
* @return the normalized constraint
*/
public Formula normalize() {
final LNGVector<Literal> normPs = new LNGVector<>(this.literals.length);
final LNGIntVector normCs = new LNGIntVector(this.literals.length);
int normRhs;
switch(this.comparator) {
case EQ:
for (int i = 0; i < this.literals.length; i++) {
normPs.push(this.literals[i]);
normCs.push(this.coefficients[i]);
}
normRhs = this.rhs;
final Formula f1 = this.normalize(normPs, normCs, normRhs);
normPs.clear();
normCs.clear();
for (int i = 0; i < this.literals.length; i++) {
normPs.push(this.literals[i]);
normCs.push(-this.coefficients[i]);
}
normRhs = -this.rhs;
final Formula f2 = this.normalize(normPs, normCs, normRhs);
return this.f.and(f1, f2);
case LT:
case LE:
for (int i = 0; i < this.literals.length; i++) {
normPs.push(this.literals[i]);
normCs.push(this.coefficients[i]);
}
normRhs = this.comparator == CType.LE ? this.rhs : this.rhs - 1;
return this.normalize(normPs, normCs, normRhs);
case GT:
case GE:
for (int i = 0; i < this.literals.length; i++) {
normPs.push(this.literals[i]);
normCs.push(-this.coefficients[i]);
}
normRhs = this.comparator == CType.GE ? -this.rhs : -this.rhs - 1;
return this.normalize(normPs, normCs, normRhs);
default:
throw new IllegalStateException("Unknown pseudo-Boolean comparator: " + this.comparator);
}
}
use of org.logicng.collections.LNGIntVector 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 LNGVector<Literal> literals) {
if (this.miniSat == null) {
this.result.add(this.vec2clause(literals));
} else {
final LNGIntVector clauseVec = new LNGIntVector(literals.size());
for (final Literal l : literals) {
addLiteral(clauseVec, l);
}
this.miniSat.underlyingSolver().addClause(clauseVec, this.proposition);
this.miniSat.setSolverToUndef();
}
}
use of org.logicng.collections.LNGIntVector 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.collections.LNGIntVector in project LogicNG by logic-ng.
the class DnnfMiniSatStyleSolver method handleConflict.
protected void handleConflict(final MSClause conflict) {
if (decisionLevel() > 0) {
this.lastLearnt = new LNGIntVector();
analyze(conflict, this.lastLearnt);
this.assertionLevel = this.analyzeBtLevel;
} else {
// solver unsat
cancelUntil(0);
this.lastLearnt = null;
this.assertionLevel = -1;
}
}
use of org.logicng.collections.LNGIntVector 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