Пример #1
0
def test_evicted_gone(minimal: SystemGraph, full_example_system: SystemGraph):
    tiny = LRUCacheRepository(1)
    tiny.put(minimal)
    tiny.put(full_example_system)

    with pytest.raises(RepositoryLookupError):
        tiny.get(minimal.get_id())

    tiny.get(full_example_system.get_id())
Пример #2
0
def test_evicted_lru(minimal: SystemGraph, diamond: SystemGraph,
                     full_example_system: SystemGraph):
    tiny = LRUCacheRepository(2)
    # add two
    tiny.put(minimal)
    tiny.put(diamond)
    # use first; should now move ahead of second
    tiny.get(minimal.get_id())
    # add a third; second should be evicted
    tiny.put(full_example_system)

    with pytest.raises(RepositoryLookupError):
        tiny.get(diamond.get_id())

    tiny.get(full_example_system.get_id())
Пример #3
0
def get_birnbaum_importances_select(sg: SystemGraph,
                                    data: Dict,
                                    selector: Dict,
                                    data_src: str,
                                    prefs=None
                                    ) -> Dict[str, Dict[bool, float]]:
    prefs = apply_prefs(prefs)

    select_key = list(selector.keys())
    if len(select_key) != 1:
        return {}
    select_key = select_key[0]
    select_value = selector[select_key]
    select = []

    for node, node_data in data["nodes"].items():
        if "attributes" not in node_data:
            continue
        attrs = node_data["attributes"]

        if select_key in attrs and attrs[select_key] == select_value:
            select.append(node)

    bdd_with_root = sg.get_bdd_with_root()
    if data_src == "data":
        p = provide_p_direct_from_data(sg, data)
    else:
        p = provide_p_unknown_data(sg)
    result = birnbaum_importance(sg,
                                 p,
                                 bdd_with_root=bdd_with_root,
                                 select=select)

    return {select_key: {select_value: result["select"]}}
Пример #4
0
def get_birnbaum_structural_importances(sg: SystemGraph,
                                        data=None,
                                        prefs=None) -> Dict[str, float]:
    prefs = apply_prefs(prefs)
    bdd_with_root = sg.get_bdd_with_root()
    result = birnbaum_structural_importance(sg, bdd_with_root=bdd_with_root)
    return apply_scaling(result, prefs["SCALE_METRICS"])
Пример #5
0
def test_load_from_dict():
    d = {
        "nodes": {
            "indicator": {
                "tags": {"indicator"},
                "logic": {
                    "component": "and"
                }
            },
            "x1": {
                "tags": {"component"},
                "logic": {
                    "component": "and"
                }
            },
            "x2": {
                "tags": {"component"},
                "logic": {
                    "component": "and"
                }
            }
        },
        "edges": [{
            "src": "x1",
            "dst": "x2"
        }]
    }
    sg = SystemGraph(**d)
    assert sg is not None
Пример #6
0
def test_e2e_supplier_choice_solve(full_with_supplier_choices: SystemGraph,
                                   full_with_supplier_choices_data):
    prob = SupplierChoiceProblem(full_with_supplier_choices,
                                 full_with_supplier_choices_data)
    results = prob.solve({"alpha": 0.01, "budget": 500})
    updated = full_with_supplier_choices.with_suppliers(results[0])
    assert updated is not None
Пример #7
0
def test_e2e_supplier_choice_solve_bad_supplier(
        full_with_supplier_choices: SystemGraph,
        full_with_supplier_choices_data):
    bad = full_with_supplier_choices_data["edges"][0]
    bad["risk"] = 0.99
    prob = SupplierChoiceProblem(full_with_supplier_choices,
                                 full_with_supplier_choices_data)
    results = prob.solve({"alpha": 0.01, "budget": 500})
    updated = full_with_supplier_choices.with_suppliers(results[0])
    assert Edge(src=bad["src"], dst=bad["dst"]) not in updated.edges
Пример #8
0
def test_supplier_groups_inner_cycle():
    nodes = ["x1", "x2", "x3"]
    edges = [("x1", "x2"), ("x2", "x3"), ("x3", "x2")]

    nodes = {
        n: Node(logic=dict(), tags=frozenset(["supplier"]))
        for n in nodes
    }
    nodes.update({"indicator": Node(tags=frozenset(["indicator"]))})
    sg = SystemGraph(nodes=nodes, edges=[Edge(src=s, dst=d) for s, d in edges])

    assert len(sg.supplier_groups) == 0
Пример #9
0
def get_birnbaum_importances(sg: SystemGraph,
                             data: Dict,
                             data_src: str,
                             prefs=None) -> Dict[str, float]:
    prefs = apply_prefs(prefs)
    bdd_with_root = sg.get_bdd_with_root()
    if data_src == "data":
        p = provide_p_direct_from_data(sg, data)
    else:
        p = provide_p_unknown_data(sg)

    result = birnbaum_importance(sg, p, bdd_with_root=bdd_with_root)
    return apply_scaling(result, prefs["SCALE_METRICS"])
Пример #10
0
def test_supplier_groups_basic():
    nodes = ["x1", "x2", "x3", "x4", "x5", "x6"]
    edges = [("x1", "x2"), ("x2", "x3"), ("x4", "x5"), ("x4", "x6")]

    nodes = {
        n: Node(logic=dict(), tags=frozenset(["supplier"]))
        for n in nodes
    }
    nodes.update({"indicator": Node(tags=frozenset(["indicator"]))})
    sg = SystemGraph(nodes=nodes, edges=[Edge(src=s, dst=d) for s, d in edges])

    expected = {"x1": {"x1", "x2", "x3"}, "x4": {"x4", "x5", "x6"}}

    assert sg.supplier_groups == expected
Пример #11
0
def test_e2e_supplier_choice_solve_bad_supplier_risk(
        full_with_supplier_choices: SystemGraph,
        full_with_supplier_choices_data):
    bad = full_with_supplier_choices_data["edges"][0]
    bad["risk"] = 0.99
    before = risk_by_bdd(
        full_with_supplier_choices,
        provide_p_direct_from_data(full_with_supplier_choices,
                                   full_with_supplier_choices_data))
    prob = SupplierChoiceProblem(full_with_supplier_choices,
                                 full_with_supplier_choices_data)
    results = prob.solve({"alpha": 0.01, "budget": 500})
    updated = full_with_supplier_choices.with_suppliers(results[0])
    assert Edge(src=bad["src"], dst=bad["dst"]) not in updated.edges
    after = risk_by_bdd(
        updated,
        provide_p_direct_from_data(updated, full_with_supplier_choices_data))
    assert before > after
Пример #12
0
def test_with_edges_basic(full_example_system: SystemGraph):
    to_add = [("x29", "x1"), ("x28", "x2"), ("x27", "x3"), ("x26", "x4"),
              ("x25", "x5"), ("x24", "x6"), ("x23", "x7"), ("x22", "x8"),
              ("x21", "x9"), ("x20", "x10"), ("x19", "x11"), ("x18", "x12"),
              ("x17", "x13"), ("x16", "x14"), ("x18", "x15")]

    new_edges = [Edge(src=s, dst=d) for s, d in to_add]
    new_sg = full_example_system.with_suppliers(new_edges)

    updated_edges = set(new_sg.edges)
    for e in new_edges:
        assert (e in updated_edges)

    removed_edges = [
        ("x16", "x1"), ("x17", "x2"), ("x25", "x3"), ("x19", "x4"),
        ("x20", "x5"), ("x21", "x6"), ("x22", "x7"), ("x23", "x8"),
        ("x24", "x10"), ("x25", "x9"), ("x26", "x11"), ("x27", "x12"),
        ("x26", "x11"), ("x27", "x12"), ("x28", "x13"), ("x29", "x14"),
        ("x29", "x15")
    ]

    for rs, rd in removed_edges:
        assert (Edge(src=rs, dst=rd, tags=frozenset(["potential"]))
                in updated_edges)
Пример #13
0
def get_risk(sg: SystemGraph, data: Dict, prefs=None) -> Dict[str, float]:
    bdd_with_root = sg.get_bdd_with_root()
    validate_data(sg, data)
    p = provide_p_direct_from_data(sg, data)
    risk = risk_by_bdd(sg, p, bdd_with_root=bdd_with_root)
    return {"system": risk}
Пример #14
0
 def _make_key(cls, sg: SystemGraph, resource_identifier: str = None):
     if not resource_identifier:
         return sg.get_id()
     else:
         return str(hash(sg)) + "|" + str(hash(resource_identifier))
Пример #15
0
def test_basic_get(minimal: SystemGraph):
    repo = LRUCacheRepository()
    repo._storage[LRUCacheRepository._make_key(minimal)] = minimal
    retrieved = repo.get(minimal.get_id())
    assert retrieved == minimal
Пример #16
0
    def __init__(self, sg: SystemGraph, data: Dict):
        """ Parameters extracted/computed from SystemGraph and data:
                   - N int: number of components in system
                   - M int: number of suppliers in system
                   - NxM matrix of floats: component_risks
                   - NxM matrix of floats: component_costs
                   - Nx1 vector of floats: component_importances
                   - NxM matrix of booleans: valid_choices pairings of supplier/component
                   - Mx1 vector of floats: supplier risks
                   - K int: number of supplier groups in system
                   - KxM matrix of booleans: membership of suppliers in groups
                   - Kx1 vector of float: group risks
               """

        all_suppliers = set(sg.suppliers)
        supplier_groups_raw = sg.supplier_groups

        if len(all_suppliers) == 0 or len(supplier_groups_raw) == 0:
            raise OptimizationError("No suppliers found.")

        self.N = len(sg.components)
        self.M = len(all_suppliers)
        self.K = len(supplier_groups_raw)
        self.component_risks = [[0 for _ in range(self.M)]
                                for _ in range(self.N)]
        self.component_costs = [[0 for _ in range(self.M)]
                                for _ in range(self.N)]
        self.potential_suppliers = [[0 for _ in range(self.M)]
                                    for _ in range(self.N)]
        self.component_importances = [False for _ in range(self.N)]
        self.supplier_risks = [0 for _ in range(self.M)]
        self.supplier_groups = [[False for _ in range(self.M)]
                                for _ in range(self.K)]
        self.group_risks = [0 for _ in range(self.K)]
        component_index_map = {
            cname: idx
            for idx, cname in enumerate(sorted(sg.components))
        }
        supplier_index_map = {
            sname: idx
            for idx, sname in enumerate(sorted(all_suppliers))
        }
        group_index_map = {
            gname: idx
            for idx, gname in enumerate(sorted(supplier_groups_raw.keys()))
        }
        pre_existing_suppliers = {}  # map component name to supplier name

        # begin with any supplier/component information in the system graph already
        for edge in sg.edges:
            if edge.src in all_suppliers and edge.dst in sg.components:
                pre_existing_suppliers[edge.dst] = edge.src
                i = component_index_map[edge.dst]
                j = supplier_index_map[edge.src]
                self.potential_suppliers[i][j] = True

        # extract risk values which are given in node data.
        for nodeId, nodeData in data.get("nodes", {}).items():
            if nodeId in sg.components:
                i = component_index_map[nodeId]
                supplier_of_i = pre_existing_suppliers.get(nodeId, None)
                save = nodeData.get("risk", 0)
                if supplier_of_i is not None:
                    j = supplier_index_map[supplier_of_i]
                    self.component_risks[i][j] = save
                else:
                    # This should not generally be used, but if a node has no supplier it may still contribute risk.
                    self.component_risks[i] = [save] * self.M
            elif nodeId in all_suppliers:
                self.supplier_risks[supplier_index_map[nodeId]] = nodeData.get(
                    "risk", 0.0)
                if group_index_map.get(nodeId):
                    self.group_risks[group_index_map.get(
                        nodeId)] = nodeData.get("risk", 0.0)

        # extract risk and cost values if given in data-edges for valid pairings
        for edgeDict in data.get("edges", []):
            if edgeDict["src"] in all_suppliers and edgeDict[
                    "dst"] in sg.components:
                i = component_index_map[edgeDict["dst"]]
                j = supplier_index_map[edgeDict["src"]]
                self.component_risks[i][j] = edgeDict.get("risk", 0.0)
                self.component_costs[i][j] = edgeDict.get("cost", 0.0)
                self.potential_suppliers[i][j] = True

        # save indices for use after optimization
        self.map_index_component = invert_dict(component_index_map)
        self.map_index_supplier = invert_dict(supplier_index_map)
        self.map_index_group = invert_dict(group_index_map)

        # load supplier groups into matrix
        for root, group in supplier_groups_raw.items():
            k = group_index_map[root]
            self.supplier_groups[k] = [
                True if self.map_index_supplier[m] in group else False
                for m in range(0, self.M)
            ]

        # load component importances
        all_importances = birnbaum_structural_importance(
            sg, bdd_with_root=sg.get_bdd_with_root())
        for i in range(0, self.N):
            self.component_importances[i] = all_importances[
                self.map_index_component[i]]

        self.model = self.make_pyomo_model()
Пример #17
0
def test_dict_nested_success(canonical: SystemGraph):
    d = canonical.dict()
    assert "nodes" in d and "edges" in d
    assert "x1" in d["nodes"] and d["nodes"]["x1"]["tags"] is not None
Пример #18
0
def test_basic_put_get(minimal: SystemGraph):
    repo = LRUCacheRepository()
    repo.put(minimal)
    retrieved = repo.get(minimal.get_id())
    assert retrieved == minimal
Пример #19
0
def get_system_graph_optimized_suppliers(sg: SystemGraph, data: Dict,
                                         params: Dict) -> SystemGraph:
    problem = SupplierChoiceProblem(sg, data)
    chosen_suppliers, metadata = problem.solve(params)
    return sg.with_suppliers(chosen_suppliers)
Пример #20
0
def get_sg_from_file(filename: str) -> SystemGraph:
    file_data = read_text("iscram.tests.system_graph_test_data", filename)
    sg_dict = json.loads(file_data)
    return SystemGraph(**sg_dict)