Search in sources :

Example 21 with LNGIntVector

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

Example 22 with LNGIntVector

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

Example 23 with LNGIntVector

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

Example 24 with LNGIntVector

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

Example 25 with LNGIntVector

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

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