use of org.logicng.collections.LNGIntVector in project LogicNG by logic-ng.
the class MiniSatStyleSolver method initialize.
/**
* Initializes the internal solver state.
*/
protected void initialize() {
this.initializeConfig();
this.ok = true;
this.qhead = 0;
this.clauses = new LNGVector<>();
this.learnts = new LNGVector<>();
this.watches = new LNGVector<>();
this.vars = new LNGVector<>();
this.orderHeap = new LNGHeap(this);
this.trail = new LNGIntVector();
this.trailLim = new LNGIntVector();
this.model = new LNGBooleanVector();
this.conflict = new LNGIntVector();
this.assumptions = new LNGIntVector();
this.seen = new LNGBooleanVector();
this.analyzeStack = new LNGIntVector();
this.analyzeToClear = new LNGIntVector();
this.analyzeBtLevel = 0;
this.claInc = 1;
this.simpDBAssigns = -1;
this.simpDBProps = 0;
this.clausesLiterals = 0;
this.learntsLiterals = 0;
this.name2idx = new TreeMap<>();
this.idx2name = new TreeMap<>();
this.canceledByHandler = false;
if (this.config.proofGeneration) {
this.pgOriginalClauses = new LNGVector<>();
this.pgProof = new LNGVector<>();
}
this.computingBackbone = false;
this.selectionOrder = new LNGIntVector();
this.selectionOrderIdx = 0;
}
use of org.logicng.collections.LNGIntVector in project LogicNG by logic-ng.
the class MiniSatStyleSolver method initBackboneDS.
/**
* Initializes the internal solver state for backbones.
* @param variables to test
*/
protected void initBackboneDS(final List<Integer> variables) {
this.backboneCandidates = new Stack<>();
this.backboneAssumptions = new LNGIntVector(variables.size());
this.backboneMap = new HashMap<>();
for (final Integer var : variables) {
this.backboneMap.put(var, UNDEF);
}
}
use of org.logicng.collections.LNGIntVector in project LogicNG by logic-ng.
the class ModelEnumerationFunction method apply.
@Override
public List<Assignment> apply(final MiniSat solver, final Consumer<Tristate> resultSetter) {
start(this.handler);
final List<Assignment> models = new ArrayList<>();
SolverState stateBeforeEnumeration = null;
if (solver.getStyle() == MiniSat.SolverStyle.MINISAT && solver.isIncremental()) {
stateBeforeEnumeration = solver.saveState();
}
boolean proceed = true;
final LNGIntVector relevantIndices;
if (this.variables == null) {
if (!solver.getConfig().isAuxiliaryVariablesInModels()) {
relevantIndices = new LNGIntVector();
for (final Map.Entry<String, Integer> entry : solver.underlyingSolver().getName2idx().entrySet()) {
if (solver.isRelevantVariable(entry.getKey())) {
relevantIndices.push(entry.getValue());
}
}
} else {
relevantIndices = null;
}
} else {
relevantIndices = new LNGIntVector(this.variables.size());
for (final Variable var : this.variables) {
relevantIndices.push(solver.underlyingSolver().idxForName(var.name()));
}
}
LNGIntVector relevantAllIndices = null;
final SortedSet<Variable> uniqueAdditionalVariables = new TreeSet<>(this.additionalVariables == null ? Collections.emptyList() : this.additionalVariables);
if (this.variables != null) {
uniqueAdditionalVariables.removeAll(this.variables);
}
if (relevantIndices != null) {
if (uniqueAdditionalVariables.isEmpty()) {
relevantAllIndices = relevantIndices;
} else {
relevantAllIndices = new LNGIntVector(relevantIndices.size() + uniqueAdditionalVariables.size());
for (int i = 0; i < relevantIndices.size(); ++i) {
relevantAllIndices.push(relevantIndices.get(i));
}
for (final Variable var : uniqueAdditionalVariables) {
relevantAllIndices.push(solver.underlyingSolver().idxForName(var.name()));
}
}
}
while (proceed && modelEnumerationSATCall(solver, this.handler)) {
final LNGBooleanVector modelFromSolver = solver.underlyingSolver().model();
final Assignment model = solver.createAssignment(modelFromSolver, relevantAllIndices);
models.add(model);
proceed = this.handler == null || this.handler.foundModel(model);
if (model.size() > 0) {
final LNGIntVector blockingClause = generateBlockingClause(modelFromSolver, relevantIndices);
solver.underlyingSolver().addClause(blockingClause, null);
resultSetter.accept(UNDEF);
} else {
break;
}
}
if (solver.getStyle() == MiniSat.SolverStyle.MINISAT && solver.isIncremental()) {
solver.loadState(stateBeforeEnumeration);
}
return models;
}
use of org.logicng.collections.LNGIntVector in project LogicNG by logic-ng.
the class UnsatCoreFunction method apply.
@Override
public UNSATCore<Proposition> apply(final MiniSat solver, final Consumer<Tristate> resultSetter) {
if (!solver.getConfig().proofGeneration()) {
throw new IllegalStateException("Cannot generate an unsat core if proof generation is not turned on");
}
if (solver.getResult() == TRUE) {
throw new IllegalStateException("An unsat core can only be generated if the formula is solved and is UNSAT");
}
if (solver.getResult() == Tristate.UNDEF) {
throw new IllegalStateException("Cannot generate an unsat core before the formula was solved.");
}
if (solver.underlyingSolver() instanceof MiniCard) {
throw new IllegalStateException("Cannot compute an unsat core with MiniCard.");
}
if (solver.underlyingSolver() instanceof GlucoseSyrup && solver.getConfig().incremental()) {
throw new IllegalStateException("Cannot compute an unsat core with Glucose in incremental mode.");
}
if (solver.isLastComputationWithAssumptions()) {
throw new IllegalStateException("Cannot compute an unsat core for a computation with assumptions.");
}
final DRUPTrim trimmer = new DRUPTrim();
final Map<Formula, Proposition> clause2proposition = new HashMap<>();
final LNGVector<LNGIntVector> clauses = new LNGVector<>(solver.underlyingSolver().pgOriginalClauses().size());
for (final MiniSatStyleSolver.ProofInformation pi : solver.underlyingSolver().pgOriginalClauses()) {
clauses.push(pi.clause());
final Formula clause = getFormulaForVector(solver, pi.clause());
Proposition proposition = pi.proposition();
if (proposition == null) {
proposition = new StandardProposition(clause);
}
clause2proposition.put(clause, proposition);
}
if (containsEmptyClause(clauses)) {
final Proposition emptyClause = clause2proposition.get(solver.factory().falsum());
return new UNSATCore<>(Collections.singletonList(emptyClause), true);
}
final DRUPTrim.DRUPResult result = trimmer.compute(clauses, solver.underlyingSolver().pgProof());
if (result.trivialUnsat()) {
return handleTrivialCase(solver);
}
final LinkedHashSet<Proposition> propositions = new LinkedHashSet<>();
for (final LNGIntVector vector : result.unsatCore()) {
propositions.add(clause2proposition.get(getFormulaForVector(solver, vector)));
}
return new UNSATCore<>(new ArrayList<>(propositions), false);
}
use of org.logicng.collections.LNGIntVector in project LogicNG by logic-ng.
the class UpZeroLiteralsFunction method apply.
@Override
public SortedSet<Literal> apply(final MiniSat solver, final Consumer<Tristate> resultSetter) {
if (solver.getResult() == UNDEF) {
throw new IllegalStateException("Cannot get unit propagated literals on level 0 as long as the formula is not solved. Call 'sat' first.");
}
if (solver.getResult() == FALSE) {
return null;
}
final LNGIntVector literals = solver.underlyingSolver().upZeroLiterals();
final SortedSet<Literal> upZeroLiterals = new TreeSet<>();
for (int i = 0; i < literals.size(); ++i) {
final int lit = literals.get(i);
upZeroLiterals.add(solver.factory().literal(solver.underlyingSolver().nameForIdx(MiniSatStyleSolver.var(lit)), !MiniSatStyleSolver.sign(lit)));
}
return upZeroLiterals;
}
Aggregations