Search in sources :

Example 1 with SolverState

use of org.logicng.solvers.SolverState in project LogicNG by logic-ng.

the class SmusComputation method grow.

private static SortedSet<Variable> grow(final SATSolver growSolver, final SortedSet<Variable> h, final Set<Variable> variables, final OptimizationHandler handler) {
    final SolverState solverState = growSolver.saveState();
    growSolver.add(h);
    final Assignment maxModel = growSolver.execute(OptimizationFunction.builder().handler(handler).literals(variables).maximize().build());
    if (maxModel == null || aborted(handler)) {
        return null;
    } else {
        final List<Variable> maximumSatisfiableSet = maxModel.positiveVariables();
        growSolver.loadState(solverState);
        final SortedSet<Variable> minimumCorrectionSet = new TreeSet<>(variables);
        maximumSatisfiableSet.forEach(minimumCorrectionSet::remove);
        return minimumCorrectionSet;
    }
}
Also used : Assignment(org.logicng.datastructures.Assignment) SolverState(org.logicng.solvers.SolverState) Variable(org.logicng.formulas.Variable) TreeSet(java.util.TreeSet)

Example 2 with SolverState

use of org.logicng.solvers.SolverState in project LogicNG by logic-ng.

the class BackboneFunction method apply.

@Override
public Backbone apply(final MiniSat solver, final Consumer<Tristate> resultSetter) {
    start(handler);
    SolverState stateBeforeBackbone = null;
    if (solver.getStyle() == MiniSat.SolverStyle.MINISAT && solver.isIncremental()) {
        stateBeforeBackbone = solver.saveState();
    }
    final Backbone backbone = solver.underlyingSolver().computeBackbone(this.variables, this.type, handler);
    if (solver.getStyle() == MiniSat.SolverStyle.MINISAT && solver.isIncremental()) {
        solver.loadState(stateBeforeBackbone);
    }
    return backbone;
}
Also used : Backbone(org.logicng.backbones.Backbone) SolverState(org.logicng.solvers.SolverState)

Example 3 with SolverState

use of org.logicng.solvers.SolverState 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 4 with SolverState

use of org.logicng.solvers.SolverState in project LogicNG by logic-ng.

the class OptimizationFunction method apply.

@Override
public Assignment apply(final MiniSat solver, final Consumer<Tristate> resultSetter) {
    SolverState initialState = null;
    if (solver.getStyle() == MiniSat.SolverStyle.MINISAT && solver.isIncremental()) {
        initialState = solver.saveState();
    }
    final Assignment model = maximize(solver);
    if (solver.getStyle() == MiniSat.SolverStyle.MINISAT && solver.isIncremental()) {
        solver.loadState(initialState);
    }
    return model;
}
Also used : Assignment(org.logicng.datastructures.Assignment) SolverState(org.logicng.solvers.SolverState)

Example 5 with SolverState

use of org.logicng.solvers.SolverState in project LogicNG by logic-ng.

the class CCIncrementalFormulaTest method testSimpleIncrementalAMK.

@Test
public void testSimpleIncrementalAMK() {
    for (final CCEncoder encoder : this.encoders) {
        final CCEncoder initialEncoder = new CCEncoder(this.f);
        final int numLits = 10;
        final Variable[] vars = new Variable[numLits];
        for (int i = 0; i < numLits; i++) {
            vars[i] = this.f.variable("v" + i);
        }
        final Pair<List<Formula>, CCIncrementalData> cc = encoder.encodeIncremental((CardinalityConstraint) this.f.cc(CType.LE, 9, vars));
        final CCIncrementalData incData = cc.second();
        final SATSolver solver = MiniSat.miniSat(this.f);
        // >= 4
        solver.add(initialEncoder.encode((CardinalityConstraint) this.f.cc(CType.GE, 4, vars)));
        // <= 7
        solver.add(initialEncoder.encode((CardinalityConstraint) this.f.cc(CType.LE, 7, vars)));
        solver.add(cc.first());
        assertSolverSat(solver);
        // <= 9
        assertSolverSat(solver);
        // <= 8
        solver.add(incData.newUpperBound(8));
        assertSolverSat(solver);
        assertThat(incData.currentRHS()).isEqualTo(8);
        // <= 7
        solver.add(incData.newUpperBound(7));
        assertSolverSat(solver);
        // <= 6
        solver.add(incData.newUpperBound(6));
        assertSolverSat(solver);
        // <= 5
        solver.add(incData.newUpperBound(5));
        assertSolverSat(solver);
        // <= 4
        solver.add(incData.newUpperBound(4));
        assertSolverSat(solver);
        final SolverState state = solver.saveState();
        // <= 3
        solver.add(incData.newUpperBound(3));
        assertSolverUnsat(solver);
        solver.loadState(state);
        assertSolverSat(solver);
        // <= 2
        solver.add(incData.newUpperBound(2));
        assertSolverUnsat(solver);
    }
}
Also used : SATSolver(org.logicng.solvers.SATSolver) Variable(org.logicng.formulas.Variable) SolverState(org.logicng.solvers.SolverState) CardinalityConstraint(org.logicng.formulas.CardinalityConstraint) List(java.util.List) CardinalityConstraint(org.logicng.formulas.CardinalityConstraint) Test(org.junit.jupiter.api.Test) LogicNGTest(org.logicng.LogicNGTest)

Aggregations

SolverState (org.logicng.solvers.SolverState)22 Test (org.junit.jupiter.api.Test)12 Variable (org.logicng.formulas.Variable)10 SATSolver (org.logicng.solvers.SATSolver)10 LogicNGTest (org.logicng.LogicNGTest)9 Formula (org.logicng.formulas.Formula)8 Backbone (org.logicng.backbones.Backbone)6 ArrayList (java.util.ArrayList)5 TreeSet (java.util.TreeSet)5 MiniSat (org.logicng.solvers.MiniSat)5 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 MethodSource (org.junit.jupiter.params.provider.MethodSource)4 Assignment (org.logicng.datastructures.Assignment)4 CardinalityConstraint (org.logicng.formulas.CardinalityConstraint)4 FormulaFactory (org.logicng.formulas.FormulaFactory)4 MiniSat2Solver (org.logicng.solvers.sat.MiniSat2Solver)4 BufferedReader (java.io.BufferedReader)2 FileReader (java.io.FileReader)2 List (java.util.List)2 Literal (org.logicng.formulas.Literal)2