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