Search in sources :

Example 6 with Pair

use of com.ge.verdict.synthesis.util.Pair in project VERDICT by ge-high-assurance.

the class CostModel method cost.

/**
 * Evaluate the cost function on the given inputs.
 *
 * @param defense
 * @param component
 * @param dal
 * @return
 */
public Fraction cost(String defense, String component, int dal) {
    // If DAL is not specified, we default to using DAL to linearly scale cost
    // System.out.println("Loading cost: " + defense + ", " + component + ", " + dal);
    Fraction lookup = compDefDalModel.get(new Triple<>(component, defense, dal));
    if (lookup != null) {
        return lookup;
    }
    lookup = compDefModel.get(new Pair<>(component, defense));
    if (lookup != null) {
        return lookup.multiply(dal);
    }
    lookup = compDalModel.get(new Pair<>(component, dal));
    if (lookup != null) {
        return lookup;
    }
    lookup = defDalModel.get(new Pair<>(defense, dal));
    if (lookup != null) {
        return lookup;
    }
    lookup = compModel.get(component);
    if (lookup != null) {
        return lookup.multiply(dal);
    }
    lookup = defModel.get(defense);
    if (lookup != null) {
        return lookup.multiply(dal);
    }
    lookup = dalModel.get(dal);
    if (lookup != null) {
        return lookup;
    }
    return defaultModel.multiply(dal);
}
Also used : Fraction(org.apache.commons.math3.fraction.Fraction) Pair(com.ge.verdict.synthesis.util.Pair)

Example 7 with Pair

use of com.ge.verdict.synthesis.util.Pair in project VERDICT by ge-high-assurance.

the class VerdictSynthesis method normalizeCosts.

/**
 * Calculates (and returns) lowest common denominator of all leaf costs, and sets the
 * normalizedCost field in each leaf accordingly.
 *
 * @param pairs
 * @return
 * @deprecated use the multi-requirement approach instead
 */
@Deprecated
public static int normalizeCosts(Collection<ComponentDefense> pairs) {
    int costLcd = pairs.stream().flatMap((ComponentDefense pair) -> IntStream.range(0, 10).map(dal -> pair.dalToRawCost(dal).getDenominator()).mapToObj(// kind of dumb but need to go
    x -> x)).reduce(1, ArithmeticUtils::lcm);
    for (ComponentDefense pair : pairs) {
        int[] normCosts = new int[10];
        for (int dal = 0; dal < 10; dal++) {
            Fraction normalizedCost = pair.dalToRawCost(dal).multiply(costLcd);
            if (normalizedCost.getDenominator() != 1) {
                throw new RuntimeException();
            }
            normCosts[dal] = normalizedCost.getNumerator();
        }
        pair.normalizeCosts(normCosts);
    }
    return costLcd;
}
Also used : IntStream(java.util.stream.IntStream) Optimize(com.microsoft.z3.Optimize) Context(com.microsoft.z3.Context) Formula(org.logicng.formulas.Formula) Assignment(org.logicng.datastructures.Assignment) MaxSATSolver(org.logicng.solvers.MaxSATSolver) RatNum(com.microsoft.z3.RatNum) ArrayList(java.util.ArrayList) Map(java.util.Map) FormulaFactory(org.logicng.formulas.FormulaFactory) BoolExpr(com.microsoft.z3.BoolExpr) Status(com.microsoft.z3.Status) DTree(com.ge.verdict.synthesis.dtree.DTree) ArithExpr(com.microsoft.z3.ArithExpr) LinkedHashSet(java.util.LinkedHashSet) DLeaf(com.ge.verdict.synthesis.dtree.DLeaf) PrintWriter(java.io.PrintWriter) Collection(java.util.Collection) Set(java.util.Set) Collectors(java.util.stream.Collectors) FileNotFoundException(java.io.FileNotFoundException) ComponentDefense(com.ge.verdict.synthesis.dtree.DLeaf.ComponentDefense) Model(com.microsoft.z3.Model) List(java.util.List) Fraction(org.apache.commons.math3.fraction.Fraction) ArithmeticUtils(org.apache.commons.math3.util.ArithmeticUtils) Pair(com.ge.verdict.synthesis.util.Pair) ResultsInstance(com.ge.verdict.vdm.synthesis.ResultsInstance) Expr(com.microsoft.z3.Expr) Optional(java.util.Optional) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ComponentDefense(com.ge.verdict.synthesis.dtree.DLeaf.ComponentDefense) ArithmeticUtils(org.apache.commons.math3.util.ArithmeticUtils) Fraction(org.apache.commons.math3.fraction.Fraction)

Aggregations

Pair (com.ge.verdict.synthesis.util.Pair)7 Fraction (org.apache.commons.math3.fraction.Fraction)6 DLeaf (com.ge.verdict.synthesis.dtree.DLeaf)5 ComponentDefense (com.ge.verdict.synthesis.dtree.DLeaf.ComponentDefense)5 DTree (com.ge.verdict.synthesis.dtree.DTree)4 ADOr (com.ge.verdict.attackdefensecollector.adtree.ADOr)3 ADTree (com.ge.verdict.attackdefensecollector.adtree.ADTree)3 Optional (java.util.Optional)3 Set (java.util.Set)3 ADAnd (com.ge.verdict.attackdefensecollector.adtree.ADAnd)2 Attack (com.ge.verdict.attackdefensecollector.adtree.Attack)2 SystemModel (com.ge.verdict.attackdefensecollector.model.SystemModel)2 Approach (com.ge.verdict.synthesis.VerdictSynthesis.Approach)2 DOr (com.ge.verdict.synthesis.dtree.DOr)2 ResultsInstance (com.ge.verdict.vdm.synthesis.ResultsInstance)2 ArithExpr (com.microsoft.z3.ArithExpr)2 BoolExpr (com.microsoft.z3.BoolExpr)2 Context (com.microsoft.z3.Context)2 Expr (com.microsoft.z3.Expr)2 Model (com.microsoft.z3.Model)2