/** * Rebuilds the heap from a given vector of elements. * * @param ns the vector of elements */ public void build(final LNGIntVector ns) { for (int i = 0; i < this.heap.size(); i++) this.indices.set(this.heap.get(i), -1); this.heap.clear(); for (int i = 0; i < ns.size(); i++) { this.indices.set(ns.get(i), i); this.heap.push(ns.get(i)); } for (int i = this.heap.size() / 2 - 1; i >= 0; i--) this.percolateDown(i); }
private MiniSatStyleSolver rebuildSolver() { final MiniSatStyleSolver s = newSATSolver(); for (int i = 0; i < nVars(); i++) newSATVariable(s); for (int i = 0; i < nHard(); i++) s.addClause(hardClauses.get(i).clause()); LNGIntVector clause; for (int i = 0; i < nSoft(); i++) { clause = new LNGIntVector(softClauses.get(i).clause()); for (int j = 0; j < softClauses.get(i).relaxationVars().size(); j++) clause.push(softClauses.get(i).relaxationVars().get(j)); s.addClause(clause); } return s; }
private MaxSATResult none() { nbInitialVariables = nVars(); Tristate res; this.initRelaxation(); this.solver = this.rebuildSolver(); LNGIntVector assumptions = new LNGIntVector(); LNGIntVector currentObjFunction = new LNGIntVector(); this.encoder.setIncremental(IncrementalStrategy.NONE); this.activeSoft.growTo(nSoft(), false); for (int i = 0; i < nSoft(); i++) this.coreMapping.put(softClauses.get(i).assumptionVar(), i); while (true) { res = searchSATSolver(this.solver, satHandler(), assumptions); if (res == Tristate.UNDEF) return MaxSATResult.UNDEF; else if (res == Tristate.TRUE) { nbSatisfiable++; int newCost = computeCostModel(this.solver.model(), Integer.MAX_VALUE); saveModel(this.solver.model()); if (verbosity != Verbosity.NONE) this.output.println("o " + newCost); ubCost = newCost; if (nbSatisfiable == 1) { if (!foundUpperBound(ubCost, null)) return MaxSATResult.UNDEF; for (int i = 0; i < this.objFunction.size(); i++) assumptions.push(not(this.objFunction.get(i))); } else return MaxSATResult.OPTIMUM; } else { lbCost++; nbCores++; if (verbosity != Verbosity.NONE) this.output.println("c LB : " + lbCost); if (nbSatisfiable == 0) return MaxSATResult.UNSATISFIABLE; else if (lbCost == ubCost) { assert nbSatisfiable > 0; if (verbosity != Verbosity.NONE) this.output.println("c LB = UB"); return MaxSATResult.OPTIMUM; } else if (!foundLowerBound(lbCost, null)) return MaxSATResult.UNDEF; sumSizeCores += this.solver.conflict().size(); for (int i = 0; i < this.solver.conflict().size(); 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); } currentObjFunction.clear(); assumptions.clear(); for (int i = 0; i < nSoft(); i++) { if (this.activeSoft.get(i)) currentObjFunction.push(softClauses.get(i).relaxationVars().get(0)); else assumptions.push(not(softClauses.get(i).assumptionVar())); } if (verbosity != Verbosity.NONE) this.output.println( String.format( "c Relaxed soft clauses %d / %d", currentObjFunction.size(), this.objFunction.size())); this.solver = this.rebuildSolver(); this.encoder.encodeCardinality(this.solver, currentObjFunction, lbCost); } } }
private MaxSATResult iterative() { if (this.encoder.cardEncoding() != CardinalityEncoding.TOTALIZER) throw new IllegalStateException( "Error: Currently algorithm MSU3 with iterative encoding only supports the totalizer encoding."); 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(softClauses.get(i).assumptionVar(), i); while (true) { res = searchSATSolver(this.solver, satHandler(), assumptions); if (res == Tristate.UNDEF) return MaxSATResult.UNDEF; else if (res == Tristate.TRUE) { nbSatisfiable++; int newCost = computeCostModel(this.solver.model(), Integer.MAX_VALUE); saveModel(this.solver.model()); if (verbosity != Verbosity.NONE) this.output.println("o " + newCost); ubCost = newCost; if (nbSatisfiable == 1) { if (!foundUpperBound(ubCost, null)) return MaxSATResult.UNDEF; for (int i = 0; i < this.objFunction.size(); i++) assumptions.push(not(this.objFunction.get(i))); } else { assert lbCost == newCost; return MaxSATResult.OPTIMUM; } } else { lbCost++; nbCores++; if (verbosity != Verbosity.NONE) this.output.println("c LB : " + lbCost); if (nbSatisfiable == 0) return MaxSATResult.UNSATISFIABLE; if (lbCost == ubCost) { assert nbSatisfiable > 0; if (verbosity != Verbosity.NONE) this.output.println("c LB = UB"); return MaxSATResult.OPTIMUM; } sumSizeCores += this.solver.conflict().size(); if (this.solver.conflict().size() == 0) return MaxSATResult.UNSATISFIABLE; if (!foundLowerBound(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( 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(softClauses.get(i).relaxationVars().get(0)); else assumptions.push(not(softClauses.get(i).assumptionVar())); if (verbosity != Verbosity.NONE) this.output.println( String.format( "c Relaxed soft clauses %d / %d", currentObjFunction.size(), this.objFunction.size())); if (!this.encoder.hasCardEncoding()) { if (lbCost != currentObjFunction.size()) { this.encoder.buildCardinality(this.solver, currentObjFunction, lbCost); joinObjFunction.clear(); this.encoder.incUpdateCardinality( this.solver, joinObjFunction, currentObjFunction, lbCost, encodingAssumptions); } } else this.encoder.incUpdateCardinality( this.solver, joinObjFunction, currentObjFunction, lbCost, encodingAssumptions); for (int i = 0; i < encodingAssumptions.size(); i++) assumptions.push(encodingAssumptions.get(i)); } } }