use of org.logicng.backbones.Backbone in project LogicNG by logic-ng.
the class MiniSatStyleSolver method computeBackbone.
/**
* Computes the backbone of the given variables with respect to the formulas added to the solver.
* @param variables variables to test
* @param type backbone type
* @param handler the handler
* @return the backbone projected to the relevant variables or {@code null} if the computation was aborted by the handler
*/
public Backbone computeBackbone(final Collection<Variable> variables, final BackboneType type, final SATHandler handler) {
final boolean sat = solve(handler) == Tristate.TRUE;
if (aborted(handler)) {
return null;
}
if (sat) {
this.computingBackbone = true;
final List<Integer> relevantVarIndices = getRelevantVarIndices(variables);
initBackboneDS(relevantVarIndices);
computeBackbone(relevantVarIndices, type, handler);
if (aborted(handler)) {
return null;
}
final Backbone backbone = buildBackbone(variables, type);
this.computingBackbone = false;
return backbone;
} else {
return Backbone.unsatBackbone();
}
}
use of org.logicng.backbones.Backbone 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.backbones.Backbone in project LogicNG by logic-ng.
the class AdvancedSimplifier method apply.
@Override
public Formula apply(final Formula formula, final boolean cache) {
start(this.handler);
final FormulaFactory f = formula.factory();
final Backbone backbone = BackboneGeneration.compute(Collections.singletonList(formula), formula.variables(), BackboneType.POSITIVE_AND_NEGATIVE, satHandler(this.handler));
if (backbone == null || aborted(this.handler)) {
return null;
}
if (!backbone.isSat()) {
return f.falsum();
}
final SortedSet<Literal> backboneLiterals = backbone.getCompleteBackbone();
final Formula restrictedFormula = formula.restrict(new Assignment(backboneLiterals));
final PrimeResult primeResult = PrimeCompiler.getWithMinimization().compute(restrictedFormula, PrimeResult.CoverageType.IMPLICANTS_COMPLETE, this.handler);
if (primeResult == null || aborted(this.handler)) {
return null;
}
final List<SortedSet<Literal>> primeImplicants = primeResult.getPrimeImplicants();
final List<Formula> minimizedPIs = SmusComputation.computeSmusForFormulas(negateAllLiterals(primeImplicants, f), Collections.singletonList(restrictedFormula), f, this.handler);
if (minimizedPIs == null || aborted(this.handler)) {
return null;
}
final Formula minDnf = f.or(negateAllLiteralsInFormulas(minimizedPIs, f).stream().map(f::and).collect(Collectors.toList()));
final Formula fullFactor = minDnf.transform(new FactorOutSimplifier(this.ratingFunction));
return f.and(f.and(backboneLiterals), fullFactor).transform(new NegationSimplifier());
}
use of org.logicng.backbones.Backbone in project LogicNG by logic-ng.
the class BackboneSimplifier method apply.
@Override
public Formula apply(final Formula formula, final boolean cache) {
final SATSolver solver = MiniSat.miniSat(formula.factory());
solver.add(formula);
final Backbone backbone = solver.execute(BackboneFunction.builder().variables(formula.variables()).type(BackboneType.POSITIVE_AND_NEGATIVE).build());
if (!backbone.isSat()) {
return formula.factory().falsum();
}
if (!backbone.getNegativeBackbone().isEmpty() || !backbone.getPositiveBackbone().isEmpty()) {
final Formula backboneFormula = backbone.toFormula(formula.factory());
final Assignment assignment = new Assignment(backbone.getCompleteBackbone());
final Formula restrictedFormula = formula.restrict(assignment);
return formula.factory().and(backboneFormula, restrictedFormula);
} else {
return formula;
}
}
use of org.logicng.backbones.Backbone in project LogicNG by logic-ng.
the class BackboneFunctionTest method testSimpleFormulas.
@ParameterizedTest
@MethodSource("solvers")
public void testSimpleFormulas(final MiniSat solver) throws ParserException {
solver.reset();
solver.add(f.parse("(a => c | d) & (b => d | ~e) & (a | b)"));
Backbone backbone = solver.backbone(v("a b c d e f"));
assertThat(backbone.isSat()).isTrue();
assertThat(backbone.getCompleteBackbone()).isEmpty();
solver.add(f.parse("a => b"));
solver.add(f.parse("b => a"));
solver.add(f.parse("~d"));
backbone = solver.backbone(v("a b c d e f g h"));
assertThat(backbone.isSat()).isTrue();
assertThat(backbone.getCompleteBackbone()).containsExactly(f.variable("a"), f.variable("b"), f.variable("c"), f.literal("d", false), f.literal("e", false));
}
Aggregations