Search in sources :

Example 1 with Backbone

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

Example 2 with Backbone

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;
}
Also used : Backbone(org.logicng.backbones.Backbone) SolverState(org.logicng.solvers.SolverState)

Example 3 with 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());
}
Also used : Backbone(org.logicng.backbones.Backbone) SortedSet(java.util.SortedSet) Assignment(org.logicng.datastructures.Assignment) Formula(org.logicng.formulas.Formula) FormulaFactory(org.logicng.formulas.FormulaFactory) Literal(org.logicng.formulas.Literal) PrimeResult(org.logicng.primecomputation.PrimeResult)

Example 4 with Backbone

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;
    }
}
Also used : SATSolver(org.logicng.solvers.SATSolver) Backbone(org.logicng.backbones.Backbone) Assignment(org.logicng.datastructures.Assignment) Formula(org.logicng.formulas.Formula)

Example 5 with Backbone

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));
}
Also used : Backbone(org.logicng.backbones.Backbone) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

Backbone (org.logicng.backbones.Backbone)14 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)9 MethodSource (org.junit.jupiter.params.provider.MethodSource)9 Formula (org.logicng.formulas.Formula)7 SolverState (org.logicng.solvers.SolverState)6 BufferedReader (java.io.BufferedReader)4 FileReader (java.io.FileReader)4 ArrayList (java.util.ArrayList)4 MiniSat2Solver (org.logicng.solvers.sat.MiniSat2Solver)4 LongRunningTag (org.logicng.LongRunningTag)3 Assignment (org.logicng.datastructures.Assignment)2 FormulaFactory (org.logicng.formulas.FormulaFactory)2 SATSolver (org.logicng.solvers.SATSolver)2 SortedSet (java.util.SortedSet)1 TreeMap (java.util.TreeMap)1 Literal (org.logicng.formulas.Literal)1 Variable (org.logicng.formulas.Variable)1 PrimeResult (org.logicng.primecomputation.PrimeResult)1