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;
}
}
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;
}
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;
}
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;
}
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);
}
}
Aggregations