Search in sources :

Example 1 with MaxSATSolver

use of org.logicng.solvers.MaxSATSolver in project VERDICT by ge-high-assurance.

the class VerdictSynthesis method performSynthesisMaxSat.

/**
 * Perform synthesis using LogicNG MaxSAT.
 *
 * @param tree
 * @param targetDal
 * @param dleafFactory
 * @return
 * @deprecated use the multi-requirement approach instead
 */
@Deprecated
public static Optional<Pair<Set<ComponentDefense>, Double>> performSynthesisMaxSat(DTree tree, int targetDal, DLeaf.Factory dleafFactory) {
    Collection<ComponentDefense> pairs = dleafFactory.allComponentDefensePairs();
    FormulaFactory factory = new FormulaFactory();
    MaxSATSolver solver = MaxSATSolver.wbo();
    Formula cnf = tree.toLogicNG(factory).cnf();
    int costLcd = normalizeCosts(pairs);
    for (ComponentDefense pair : pairs) {
        if (pair.dalToNormCost(targetDal) > 0) {
            solver.addSoftFormula(factory.not(pair.toLogicNG(factory)), pair.dalToNormCost(targetDal));
        }
    }
    // implicitly converts formula to CNF
    solver.addHardFormula(cnf);
    switch(solver.solve()) {
        case OPTIMUM:
            Set<ComponentDefense> output = new LinkedHashSet<>();
            int totalNormalizedCost = 0;
            Assignment model = solver.model();
            for (ComponentDefense pair : pairs) {
                if (model.evaluateLit(pair.toLogicNG(factory))) {
                    output.add(pair);
                    totalNormalizedCost += pair.dalToNormCost(targetDal);
                }
            }
            return Optional.of(new Pair<>(output, ((double) totalNormalizedCost) / costLcd));
        case UNDEF:
            System.err.println("Synthesis: SAT undefined, is input tree valid?");
            return Optional.empty();
        case UNSATISFIABLE:
            System.err.println("Synthesis: SAT not satisfiable, perhaps there are unmitigatable attacks");
            return Optional.empty();
        default:
            throw new RuntimeException("impossible");
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Assignment(org.logicng.datastructures.Assignment) Formula(org.logicng.formulas.Formula) FormulaFactory(org.logicng.formulas.FormulaFactory) ComponentDefense(com.ge.verdict.synthesis.dtree.DLeaf.ComponentDefense) MaxSATSolver(org.logicng.solvers.MaxSATSolver)

Aggregations

ComponentDefense (com.ge.verdict.synthesis.dtree.DLeaf.ComponentDefense)1 LinkedHashSet (java.util.LinkedHashSet)1 Assignment (org.logicng.datastructures.Assignment)1 Formula (org.logicng.formulas.Formula)1 FormulaFactory (org.logicng.formulas.FormulaFactory)1 MaxSATSolver (org.logicng.solvers.MaxSATSolver)1