use of org.logicng.collections.LNGIntVector in project LogicNG by logic-ng.
the class MSU3 method iterative.
protected MaxSATResult iterative() {
if (this.encoder.cardEncoding() != CardinalityEncoding.TOTALIZER) {
throw new IllegalStateException("Error: Currently algorithm MSU3 with iterative encoding only supports the totalizer encoding.");
}
this.nbInitialVariables = nVars();
Tristate res;
this.initRelaxation();
this.solver = this.rebuildSolver();
final LNGIntVector assumptions = new LNGIntVector();
final LNGIntVector joinObjFunction = new LNGIntVector();
final LNGIntVector currentObjFunction = new LNGIntVector();
final LNGIntVector encodingAssumptions = new LNGIntVector();
this.encoder.setIncremental(IncrementalStrategy.ITERATIVE);
this.activeSoft.growTo(nSoft(), false);
for (int i = 0; i < nSoft(); i++) {
this.coreMapping.put(this.softClauses.get(i).assumptionVar(), i);
}
while (true) {
final SATHandler satHandler = satHandler();
res = searchSATSolver(this.solver, satHandler, assumptions);
if (aborted(satHandler)) {
return MaxSATResult.UNDEF;
} else if (res == Tristate.TRUE) {
this.nbSatisfiable++;
final int newCost = computeCostModel(this.solver.model(), Integer.MAX_VALUE);
saveModel(this.solver.model());
if (this.verbosity != Verbosity.NONE) {
this.output.println("o " + newCost);
}
this.ubCost = newCost;
if (this.nbSatisfiable == 1) {
if (!foundUpperBound(this.ubCost, null)) {
return MaxSATResult.UNDEF;
}
for (int i = 0; i < this.objFunction.size(); i++) {
assumptions.push(not(this.objFunction.get(i)));
}
} else {
assert this.lbCost == newCost;
return MaxSATResult.OPTIMUM;
}
} else {
this.lbCost++;
this.nbCores++;
if (this.verbosity != Verbosity.NONE) {
this.output.println("c LB : " + this.lbCost);
}
if (this.nbSatisfiable == 0) {
return MaxSATResult.UNSATISFIABLE;
}
if (this.lbCost == this.ubCost) {
assert this.nbSatisfiable > 0;
if (this.verbosity != Verbosity.NONE) {
this.output.println("c LB = UB");
}
return MaxSATResult.OPTIMUM;
}
this.sumSizeCores += this.solver.conflict().size();
if (this.solver.conflict().size() == 0) {
return MaxSATResult.UNSATISFIABLE;
}
if (!foundLowerBound(this.lbCost, null)) {
return MaxSATResult.UNDEF;
}
joinObjFunction.clear();
for (int i = 0; i < this.solver.conflict().size(); i++) {
if (this.coreMapping.containsKey(this.solver.conflict().get(i))) {
assert !this.activeSoft.get(this.coreMapping.get(this.solver.conflict().get(i)));
this.activeSoft.set(this.coreMapping.get(this.solver.conflict().get(i)), true);
joinObjFunction.push(this.softClauses.get(this.coreMapping.get(this.solver.conflict().get(i))).relaxationVars().get(0));
}
}
currentObjFunction.clear();
assumptions.clear();
for (int i = 0; i < nSoft(); i++) {
if (this.activeSoft.get(i)) {
currentObjFunction.push(this.softClauses.get(i).relaxationVars().get(0));
} else {
assumptions.push(not(this.softClauses.get(i).assumptionVar()));
}
}
if (this.verbosity != Verbosity.NONE) {
this.output.printf("c Relaxed soft clauses %d / %d%n", currentObjFunction.size(), this.objFunction.size());
}
if (!this.encoder.hasCardEncoding()) {
if (this.lbCost != currentObjFunction.size()) {
this.encoder.buildCardinality(this.solver, currentObjFunction, this.lbCost);
joinObjFunction.clear();
this.encoder.incUpdateCardinality(this.solver, joinObjFunction, currentObjFunction, this.lbCost, encodingAssumptions);
}
} else {
this.encoder.incUpdateCardinality(this.solver, joinObjFunction, currentObjFunction, this.lbCost, encodingAssumptions);
}
for (int i = 0; i < encodingAssumptions.size(); i++) {
assumptions.push(encodingAssumptions.get(i));
}
}
}
}
use of org.logicng.collections.LNGIntVector in project LogicNG by logic-ng.
the class PBEncoder method encode.
/**
* Builds a pseudo Boolean constraint of the form {@code c_1 * lit_1 + c_2 * lit_2 + ... + c_n * lit_n >= k}.
* @param lits the literals {@code lit_1 ... lit_n}
* @param coeffs the coefficients {@code c_1 ... c_n}
* @param rhs the right hand side {@code k} of the constraint
* @return the CNF encoding of the pseudo Boolean constraint
* @throws IllegalArgumentException if the right hand side of the cardinality constraint is negative or
* larger than the number of literals
*/
protected List<Formula> encode(final Literal[] lits, final int[] coeffs, final int rhs) {
if (rhs == Integer.MAX_VALUE) {
throw new IllegalArgumentException("Overflow in the Encoding");
}
if (rhs < 0) {
return Collections.singletonList(this.f.falsum());
}
final LNGVector<Literal> simplifiedLits = new LNGVector<>();
final LNGIntVector simplifiedCoeffs = new LNGIntVector();
final List<Formula> result = new ArrayList<>();
if (rhs == 0) {
for (final Literal lit : lits) {
result.add(lit.negate());
}
return result;
}
for (int i = 0; i < lits.length; i++) {
if (coeffs[i] <= rhs) {
simplifiedLits.push(lits[i]);
simplifiedCoeffs.push(coeffs[i]);
} else {
result.add(lits[i].negate());
}
}
if (simplifiedLits.size() <= 1) {
return result;
}
switch(this.config().pbEncoder) {
case SWC:
case BEST:
if (this.swc == null) {
this.swc = new PBSWC(this.f);
}
return this.swc.encode(simplifiedLits, simplifiedCoeffs, rhs, result);
case BINARY_MERGE:
return new PBBinaryMerge(this.f, this.config()).encode(simplifiedLits, simplifiedCoeffs, rhs, result);
case ADDER_NETWORKS:
if (this.adderNetworks == null) {
this.adderNetworks = new PBAdderNetworks(this.f);
}
return this.adderNetworks.encode(simplifiedLits, simplifiedCoeffs, rhs, result);
default:
throw new IllegalStateException("Unknown pseudo-Boolean encoder: " + this.config().pbEncoder);
}
}
use of org.logicng.collections.LNGIntVector in project LogicNG by logic-ng.
the class MaxSATSolver method addClause.
/**
* Adds a clause to the solver.
* @param formula the clause
* @param weight the weight of the clause (or -1 for a hard clause)
*/
protected void addClause(final Formula formula, final int weight) {
this.result = UNDEF;
final LNGIntVector clauseVec = new LNGIntVector((int) formula.numberOfAtoms());
for (final Literal lit : formula.literals()) {
Integer index = this.var2index.get(lit.variable());
if (index == null) {
index = this.solver.newLiteral(false) >> 1;
this.var2index.put(lit.variable(), index);
this.index2var.put(index, lit.variable());
}
final int litNum = lit.phase() ? index * 2 : (index * 2) ^ 1;
clauseVec.push(litNum);
}
if (weight == -1) {
this.solver.addHardClause(clauseVec);
} else {
this.solver.setCurrentWeight(weight);
this.solver.updateSumWeights(weight);
this.solver.addSoftClause(weight, clauseVec);
}
}
use of org.logicng.collections.LNGIntVector in project LogicNG by logic-ng.
the class MiniSat method addClause.
@Override
protected void addClause(final Formula formula, final Proposition proposition) {
this.result = UNDEF;
final LNGIntVector ps = generateClauseVector(formula.literals());
this.solver.addClause(ps, proposition);
}
Aggregations