Search in sources :

Example 16 with LNGIntVector

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;
}
Also used : LNGBooleanVector(org.logicng.collections.LNGBooleanVector) LNGIntVector(org.logicng.collections.LNGIntVector) LNGHeap(org.logicng.solvers.datastructures.LNGHeap)

Example 17 with LNGIntVector

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);
    }
}
Also used : LNGIntVector(org.logicng.collections.LNGIntVector)

Example 18 with LNGIntVector

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;
}
Also used : Variable(org.logicng.formulas.Variable) ArrayList(java.util.ArrayList) LNGIntVector(org.logicng.collections.LNGIntVector) Assignment(org.logicng.datastructures.Assignment) SolverState(org.logicng.solvers.SolverState) TreeSet(java.util.TreeSet) LNGBooleanVector(org.logicng.collections.LNGBooleanVector) Map(java.util.Map)

Example 19 with LNGIntVector

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);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) DRUPTrim(org.logicng.explanations.drup.DRUPTrim) MiniCard(org.logicng.solvers.sat.MiniCard) GlucoseSyrup(org.logicng.solvers.sat.GlucoseSyrup) MiniSatStyleSolver(org.logicng.solvers.sat.MiniSatStyleSolver) HashMap(java.util.HashMap) UNSATCore(org.logicng.explanations.UNSATCore) LNGIntVector(org.logicng.collections.LNGIntVector) LNGVector(org.logicng.collections.LNGVector) StandardProposition(org.logicng.propositions.StandardProposition) Formula(org.logicng.formulas.Formula) StandardProposition(org.logicng.propositions.StandardProposition) Proposition(org.logicng.propositions.Proposition)

Example 20 with LNGIntVector

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;
}
Also used : TreeSet(java.util.TreeSet) Literal(org.logicng.formulas.Literal) LNGIntVector(org.logicng.collections.LNGIntVector)

Aggregations

LNGIntVector (org.logicng.collections.LNGIntVector)79 Tristate (org.logicng.datastructures.Tristate)10 Literal (org.logicng.formulas.Literal)8 MSClause (org.logicng.solvers.datastructures.MSClause)8 LNGBooleanVector (org.logicng.collections.LNGBooleanVector)7 SATHandler (org.logicng.handlers.SATHandler)7 MiniSatStyleSolver (org.logicng.solvers.sat.MiniSatStyleSolver)7 LNGVector (org.logicng.collections.LNGVector)6 Test (org.junit.jupiter.api.Test)5 Formula (org.logicng.formulas.Formula)4 CardinalityConstraint (org.logicng.formulas.CardinalityConstraint)3 PBConstraint (org.logicng.formulas.PBConstraint)3 MSVariable (org.logicng.solvers.datastructures.MSVariable)3 ArrayList (java.util.ArrayList)2 TreeSet (java.util.TreeSet)2 Pair (org.logicng.util.Pair)2 BitSet (java.util.BitSet)1 HashMap (java.util.HashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 Map (java.util.Map)1