use of org.logicng.collections.LNGIntVector in project LogicNG by logic-ng.
the class IncWBO method relaxCore.
protected void relaxCore(final LNGIntVector conflict, final int weightCore) {
assert conflict.size() > 0;
assert weightCore > 0;
final LNGIntVector lits = new LNGIntVector();
for (int i = 0; i < conflict.size(); i++) {
final int indexSoft = this.coreMapping.get(conflict.get(i));
if (this.softClauses.get(indexSoft).weight() == weightCore) {
final LNGIntVector clause = new LNGIntVector(this.softClauses.get(indexSoft).clause());
final LNGIntVector vars = new LNGIntVector(this.softClauses.get(indexSoft).relaxationVars());
final int p = newLiteral(false);
newSATVariable(this.solver);
vars.push(p);
lits.push(p);
addSoftClause(weightCore, clause, vars);
final int l = newLiteral(false);
newSATVariable(this.solver);
this.softClauses.get(nSoft() - 1).setAssumptionVar(l);
this.coreMapping.put(l, nSoft() - 1);
this.incSoft.set(indexSoft, true);
this.incSoft.push(false);
for (int j = 0; j < vars.size(); j++) {
clause.push(vars.get(j));
}
clause.push(l);
this.solver.addClause(clause, null);
clause.clear();
clause.push(this.softClauses.get(indexSoft).assumptionVar());
this.solver.addClause(clause, null);
if (this.symmetryStrategy) {
this.softMapping.push(new LNGIntVector(this.softMapping.get(indexSoft)));
this.softMapping.get(indexSoft).clear();
this.relaxationMapping.push(new LNGIntVector(this.relaxationMapping.get(indexSoft)));
this.relaxationMapping.get(indexSoft).clear();
symmetryLog(nSoft() - 1);
}
} else {
assert this.softClauses.get(indexSoft).weight() - weightCore > 0;
this.softClauses.get(indexSoft).setWeight(this.softClauses.get(indexSoft).weight() - weightCore);
LNGIntVector clause = new LNGIntVector(this.softClauses.get(indexSoft).clause());
LNGIntVector vars = new LNGIntVector(this.softClauses.get(indexSoft).relaxationVars());
addSoftClause(this.softClauses.get(indexSoft).weight(), clause, vars);
if (this.symmetryStrategy) {
this.softMapping.push(new LNGIntVector(this.softMapping.get(indexSoft)));
this.softMapping.get(indexSoft).clear();
this.relaxationMapping.push(new LNGIntVector(this.relaxationMapping.get(indexSoft)));
this.relaxationMapping.get(indexSoft).clear();
}
this.incSoft.set(indexSoft, true);
int l = newLiteral(false);
newSATVariable(this.solver);
this.softClauses.get(nSoft() - 1).setAssumptionVar(l);
this.coreMapping.put(l, nSoft() - 1);
this.incSoft.push(false);
for (int j = 0; j < vars.size(); j++) {
clause.push(vars.get(j));
}
clause.push(l);
this.solver.addClause(clause, null);
clause.clear();
vars.clear();
clause = new LNGIntVector(this.softClauses.get(indexSoft).clause());
vars = new LNGIntVector(this.softClauses.get(indexSoft).relaxationVars());
l = newLiteral(false);
newSATVariable(this.solver);
vars.push(l);
lits.push(l);
addSoftClause(weightCore, clause, vars);
l = newLiteral(false);
newSATVariable(this.solver);
this.softClauses.get(nSoft() - 1).setAssumptionVar(l);
this.coreMapping.put(l, nSoft() - 1);
this.incSoft.push(false);
for (int j = 0; j < vars.size(); j++) {
clause.push(vars.get(j));
}
clause.push(l);
this.solver.addClause(clause, null);
clause.clear();
clause.push(this.softClauses.get(indexSoft).assumptionVar());
this.solver.addClause(clause, null);
if (this.symmetryStrategy) {
this.softMapping.push(new LNGIntVector());
this.relaxationMapping.push(new LNGIntVector());
symmetryLog(nSoft() - 1);
}
}
}
this.encoder.encodeAMO(this.solver, lits);
this.nbVars = this.solver.nVars();
if (this.symmetryStrategy) {
this.symmetryBreaking();
}
this.sumSizeCores += conflict.size();
}
use of org.logicng.collections.LNGIntVector in project LogicNG by logic-ng.
the class MiniSat method sat.
@Override
public Tristate sat(final SATHandler handler, final Collection<? extends Literal> assumptions) {
final LNGIntVector assumptionVec = generateClauseVector(assumptions);
this.result = this.solver.solve(handler, assumptionVec);
this.lastComputationWithAssumptions = true;
return this.result;
}
use of org.logicng.collections.LNGIntVector in project LogicNG by logic-ng.
the class MiniSat method generateClauseVector.
/**
* Generates a clause vector of a collection of literals.
* @param literals the literals
* @return the clause vector
*/
protected LNGIntVector generateClauseVector(final Collection<? extends Literal> literals) {
final LNGIntVector clauseVec = new LNGIntVector(literals.size());
for (final Literal lit : literals) {
final int index = getOrAddIndex(lit);
final int litNum = lit.phase() ? index * 2 : (index * 2) ^ 1;
clauseVec.push(litNum);
}
return clauseVec;
}
use of org.logicng.collections.LNGIntVector in project LogicNG by logic-ng.
the class MiniSat method sat.
@Override
public Tristate sat(final SATHandler handler, final Literal literal) {
final LNGIntVector clauseVec = new LNGIntVector(1);
final int index = getOrAddIndex(literal);
final int litNum = literal.phase() ? index * 2 : (index * 2) ^ 1;
clauseVec.push(litNum);
this.result = this.solver.solve(handler, clauseVec);
this.lastComputationWithAssumptions = true;
return this.result;
}
use of org.logicng.collections.LNGIntVector in project LogicNG by logic-ng.
the class LinearSU method bmoSearch.
protected MaxSATResult bmoSearch() {
assert this.orderWeights.size() > 0;
Tristate res;
this.initRelaxation();
int currentWeight = this.orderWeights.get(0);
final int minWeight = this.orderWeights.get(this.orderWeights.size() - 1);
int posWeight = 0;
final LNGVector<LNGIntVector> functions = new LNGVector<>();
final LNGIntVector weights = new LNGIntVector();
this.solver = this.rebuildBMO(functions, weights, currentWeight);
int localCost = 0;
this.ubCost = 0;
while (true) {
final SATHandler satHandler = satHandler();
res = searchSATSolver(this.solver, satHandler);
if (aborted(satHandler)) {
return MaxSATResult.UNDEF;
}
if (res == Tristate.TRUE) {
this.nbSatisfiable++;
final int newCost = computeCostModel(this.solver.model(), currentWeight);
if (currentWeight == minWeight) {
saveModel(this.solver.model());
if (this.verbosity != Verbosity.NONE) {
this.output.println("o " + (newCost + this.lbCost));
}
this.ubCost = newCost + this.lbCost;
if (newCost > 0 && !foundUpperBound(this.ubCost, null)) {
return MaxSATResult.UNDEF;
}
} else if (this.verbosity != Verbosity.NONE) {
this.output.printf("c BMO-UB : %d (Function %d/%d)%n", newCost, posWeight + 1, this.orderWeights.size());
}
if (newCost == 0 && currentWeight == minWeight) {
return MaxSATResult.OPTIMUM;
} else {
if (newCost == 0) {
functions.push(new LNGIntVector(this.objFunction));
localCost = newCost;
weights.push(localCost / currentWeight);
posWeight++;
currentWeight = this.orderWeights.get(posWeight);
localCost = 0;
this.solver = this.rebuildBMO(functions, weights, currentWeight);
if (this.verbosity != Verbosity.NONE) {
this.output.println("c LB : " + this.lbCost);
}
} else {
if (localCost == 0) {
this.encoder.encodeCardinality(this.solver, this.objFunction, newCost / currentWeight - 1);
} else {
this.encoder.updateCardinality(this.solver, newCost / currentWeight - 1);
}
localCost = newCost;
}
}
} else {
this.nbCores++;
if (currentWeight == minWeight) {
if (this.model.size() == 0) {
assert this.nbSatisfiable == 0;
return MaxSATResult.UNSATISFIABLE;
} else {
return MaxSATResult.OPTIMUM;
}
} else {
functions.push(new LNGIntVector(this.objFunction));
weights.push(localCost / currentWeight);
this.lbCost += localCost;
posWeight++;
currentWeight = this.orderWeights.get(posWeight);
localCost = 0;
if (!foundLowerBound(this.lbCost, null)) {
return MaxSATResult.UNDEF;
}
this.solver = this.rebuildBMO(functions, weights, currentWeight);
if (this.verbosity != Verbosity.NONE) {
this.output.println("c LB : " + this.lbCost);
}
}
}
}
}
Aggregations